checkpoint
[anneal.git] / src / edu / berkeley / qfat / Viewer.java
1 package edu.berkeley.qfat;
2 import java.io.*;
3 import java.nio.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
10 import java.util.*;
11 import edu.berkeley.qfat.geom.*;
12 import edu.berkeley.qfat.geom.Point;
13
14 public abstract class Viewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener  {
15
16     Mesh.Vertex closest = null;
17     Matrix projection = null;
18
19     public Mesh tile = new Mesh(false);
20     public Mesh goal = new Mesh(false);
21
22     JFrame f;
23     GLCanvas glcanvas;
24     Point clickPoint = null;
25     Point clickClosest = null;
26
27     int mousex;
28     int mousey;
29     float tx = 0;
30     float ty = 0;
31     float tz = 0;
32     float anglex = 0;
33     float angley = 0;
34
35     boolean alt = false;
36     boolean shift = false;
37     boolean control = false;
38
39     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
40     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
41
42     public void init(GLAutoDrawable gld) {
43         GL gl = gld.getGL();
44         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
45         gl.glViewport(0, 0, 500, 300);
46         gl.glEnable(GL.GL_DEPTH_TEST);
47         gl.glClearDepth(1.0);
48         gl.glDepthFunc(GL.GL_LEQUAL);
49         gl.glMatrixMode(GL.GL_PROJECTION);
50         gl.glLoadIdentity();
51         gl.glMatrixMode(GL.GL_MODELVIEW);
52
53         float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
54         float mat_shininess[] = { 50.0f };
55         gl.glShadeModel(GL.GL_SMOOTH);
56         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
57         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);  
58         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);  
59         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
60         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f,    4.0f,  -10.0f, 0.0f }, 0);
61         gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f,   10.0f, 0.0f }, 0);
62         gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f,   10.0f, 0.0f }, 0);
63         gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f,  10.0f,  -10.0f, 0.0f }, 0);
64         gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f,  -10.0f, 0.0f }, 0);
65         gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f,  -10.0f, 0.0f }, 0);
66         gl.glEnable(GL.GL_LIGHTING);
67         gl.glEnable(GL.GL_LIGHT0);
68
69         gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
70         gl.glEnable(GL.GL_COLOR_MATERIAL);
71
72         display(gld);
73     }
74
75     public abstract void _display(GLAutoDrawable drawable, GL gl);
76     public void display(GLAutoDrawable drawable) {
77         GL gl = drawable.getGL();
78         _display(drawable, gl);
79         this.projection = Matrix.getProjectionMatrix(gl);
80     }
81
82     /** return the position of the mouse as a point in window-space */
83     public Point getMouse() {
84         return new Point(mousex, glcanvas.getHeight()-mousey, 0);
85     }
86
87     public void mouseWheelMoved(MouseWheelEvent e) {
88         tz -= e.getWheelRotation();
89     }
90
91     public void keyTyped(KeyEvent e)  { }
92     public void keyPressed(KeyEvent e)  {
93         switch(e.getKeyCode()) {
94             case KeyEvent.VK_CONTROL: control = true; break;
95             case KeyEvent.VK_ALT: alt = true; break;
96             case KeyEvent.VK_SHIFT: shift = true; break;
97         }
98     }
99
100     public void keyReleased(KeyEvent e) {
101         switch(e.getKeyCode()) {
102             case KeyEvent.VK_CONTROL: control = false; break;
103             case KeyEvent.VK_ALT: alt = false; break;
104             case KeyEvent.VK_SHIFT: shift = false; break;
105         }
106     }
107
108     public void mouseClicked(MouseEvent e) { }
109     public void mouseEntered(MouseEvent e) { }
110     public void mouseExited(MouseEvent e) { }
111     public void mousePressed(MouseEvent e) {
112         clickPoint = getMouse();
113         clickClosest = closest == null ? null : closest.getPoint();
114     }
115
116     public void mouseReleased(MouseEvent e) {
117         clickPoint = null;
118         clickClosest = null;
119     }
120
121     public void mouseMoved(MouseEvent e) {
122         mousex = e.getX();
123         mousey = e.getY();
124     }
125
126     public void mouseDragged(MouseEvent e) {
127         if (shift) {
128             /*
129             tx += (mousex - e.getX())/(float)20;
130             ty += (mousey - e.getY())/(float)20;
131             */
132             if (closest != null && projection != null && clickClosest != null) {
133                 synchronized(this) {
134                     Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
135                     Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
136                     //System.out.println(delta + " " + closest.getPoint());
137                     System.out.println(getMouse().minus(clickPoint));
138                     closest.move(Matrix.translate(delta), true);
139                 }
140             }
141         } else {
142             anglex -= mousex - e.getX();
143             angley += mousey - e.getY();
144         }
145         mousex = e.getX();
146         mousey = e.getY();
147     }
148
149     public Viewer(JFrame f) {
150         this.f = f;
151         glcanvas = new GLCanvas();
152         glcanvas.addGLEventListener(this);
153         f.add(glcanvas, BorderLayout.CENTER);
154         glcanvas.addMouseListener(this);
155         glcanvas.addMouseMotionListener(this);
156         glcanvas.addMouseWheelListener(this);
157         glcanvas.addKeyListener(this);
158     }
159
160     public void repaint() {
161         glcanvas.repaint();
162     }
163 }