checkpoint
[anneal.git] / src / edu / berkeley / qfat / MeshViewer.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 class MeshViewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
15
16     Mesh.Vertex closest = null;
17
18     private HashSet<Mesh> meshes = new HashSet<Mesh>();
19     public synchronized void addMesh(Mesh m) { meshes.add(m); }
20     public synchronized void removeMesh(Mesh m) { meshes.remove(m); }
21
22     private int mousex;
23     private int mousey;
24     private Matrix projection = null;
25     private Point clickPoint = null;
26
27     GLCanvas glcanvas;
28
29     Point clickClosest = null;
30
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 synchronized 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 synchronized final void display(GLAutoDrawable drawable) {
76         GL gl = drawable.getGL();
77         glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight() - 100);
78
79         GLU glu = new GLU();
80         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
81         gl.glPointSize(5.0f);
82         gl.glLoadIdentity();
83         glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
84
85         glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
86         gl.glRotatef(anglex/3, 0, 1, 0);
87         gl.glRotatef(-(angley/3), 1, 0, 0);
88
89         for(Mesh mesh : meshes)
90             mesh.render(gl, Matrix.ONE);
91
92         // highlight the point closest to the mouse; we do this here to avoid flicker
93         gl.glDisable(GL.GL_LIGHTING);
94         gl.glShadeModel(GL.GL_FLAT);
95         if (closest != null) {
96             gl.glColor3f(1,1,1);
97             gl.glBegin(gl.GL_POINTS);
98             closest.getPoint().glVertex(gl);
99             gl.glEnd();
100         }
101
102         // update vertex visibilities
103         // FIXME: only do this when we switch into vertex-finding mode
104         updateVisibility(gl);
105
106         Matrix projection = Matrix.getProjectionMatrix(gl);
107         double dist = Double.MAX_VALUE;
108         if (getMouseClick() != null) return;
109         closest = null;
110         for(Mesh mesh : meshes)
111             if (mesh.option_selectable)
112                 for(Mesh.Vertex v : mesh.vertices()) {
113                     if (!v.visible) continue;
114                     Point p = projection.times(v.getPoint());
115                     int x = (int)p.x;
116                     int y = (int)p.y;
117                     int mousex = (int)getMouse().x;
118                     int mousey = (int)getMouse().y;
119                     if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
120                         dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
121                         closest = v;
122                     }
123                 }
124
125         projection = Matrix.getProjectionMatrix(gl);
126     }
127
128     protected synchronized void updateVisibility(GL gl) {
129         Matrix projection = Matrix.getProjectionMatrix(gl);
130         IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
131         gl.glFlush();
132         gl.glDrawBuffer(GL.GL_BACK);
133         gl.glReadBuffer(GL.GL_BACK);
134         gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
135         gl.glFlush();
136         gl.glDisable(GL.GL_LIGHTING);
137         gl.glShadeModel(GL.GL_FLAT);
138         gl.glColor3f(0,0,0);
139         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
140         for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
141         for(Mesh mesh : meshes)
142             if (mesh.option_selectable)
143                 for(Mesh.Vertex v : mesh.vertices()) {
144                     Point p = v.getPoint();
145                     gl.glColor3f(1,1,1);
146                     gl.glBegin(gl.GL_POINTS);
147                     p.glVertex(gl);
148                     gl.glEnd();
149                     gl.glFlush();
150                     
151                     Point projected = projection.times(p);
152                     gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
153                     
154                     boolean vis = false;
155                     for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
156                     v.visible = vis;
157                     if (vis) {
158                         gl.glColor3f(0,0,0);
159                         gl.glBegin(gl.GL_POINTS);
160                         p.glVertex(gl);
161                         gl.glEnd();
162                     }
163                 }
164         gl.glShadeModel(GL.GL_SMOOTH);
165         gl.glEnable(GL.GL_LIGHTING);
166         gl.glDrawBuffer(GL.GL_FRONT);
167     }
168
169     /** return the position of the mouse as a point in window-space */
170     public Point getMouse() {
171         return new Point(mousex, glcanvas.getHeight()-mousey, 0);
172     }
173
174     /** return the position where the mouse button was pressed, or null if it is not currently pressed */
175     public Point getMouseClick() {
176         return clickPoint;
177     }
178
179     public void mouseWheelMoved(MouseWheelEvent e) {
180         tz -= e.getWheelRotation();
181     }
182
183     public void keyTyped(KeyEvent e)  { }
184     public void keyPressed(KeyEvent e)  {
185         switch(e.getKeyCode()) {
186             case KeyEvent.VK_CONTROL: control = true; break;
187             case KeyEvent.VK_ALT: alt = true; break;
188             case KeyEvent.VK_SHIFT: shift = true; break;
189         }
190     }
191
192     public void keyReleased(KeyEvent e) {
193         switch(e.getKeyCode()) {
194             case KeyEvent.VK_CONTROL: control = false; break;
195             case KeyEvent.VK_ALT: alt = false; break;
196             case KeyEvent.VK_SHIFT: shift = false; break;
197         }
198     }
199
200     public void mouseClicked(MouseEvent e) { }
201     public void mouseEntered(MouseEvent e) { }
202     public void mouseExited(MouseEvent e) { }
203     public void mousePressed(MouseEvent e) {
204         clickPoint = getMouse();
205         clickClosest = closest == null ? null : closest.getPoint();
206     }
207
208     public void mouseReleased(MouseEvent e) {
209         clickPoint = null;
210         clickClosest = null;
211     }
212
213     public void mouseMoved(MouseEvent e) {
214         mousex = e.getX();
215         mousey = e.getY();
216     }
217
218     public void mouseDragged(MouseEvent e) {
219         if (shift) {
220             if (closest != null && projection != null && clickClosest != null) {
221                 synchronized(this) {
222                     Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
223                     Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
224                     closest.move(delta, false);
225                 }
226             }
227         } else {
228             anglex -= mousex - e.getX();
229             angley += mousey - e.getY();
230         }
231         mousex = e.getX();
232         mousey = e.getY();
233     }
234
235     public MeshViewer(JFrame f) {
236         glcanvas = new GLCanvas();
237         glcanvas.addGLEventListener(this);
238         f.add(glcanvas, BorderLayout.CENTER);
239         glcanvas.addMouseListener(this);
240         glcanvas.addMouseMotionListener(this);
241         glcanvas.addMouseWheelListener(this);
242         glcanvas.addKeyListener(this);
243     }
244
245     public void repaint() {
246         glcanvas.repaint();
247     }
248
249 }