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 int      mousex;
19     private int      mousey;
20     private Matrix   projection = null;
21     private Point    clickPoint = null;
22     private GLCanvas glcanvas;
23     private boolean  updateVisibilities = false;
24
25     private HashSet<Mesh> meshes = new HashSet<Mesh>();
26
27     public synchronized void addMesh(Mesh m) { meshes.add(m); }
28     public synchronized void removeMesh(Mesh m) { meshes.remove(m); }
29
30     float tz = 0;
31     float anglex = 0;
32     float angley = 0;
33
34     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
35     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
36
37     public synchronized void init(GLAutoDrawable gld) {
38         GL gl = glcanvas.getGL();//gld.getGL();
39         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
40         gl.glViewport(0, 0, 500, 300);
41         gl.glEnable(GL.GL_DEPTH_TEST);
42         gl.glClearDepth(1.0);
43         gl.glDepthFunc(GL.GL_LEQUAL);
44         gl.glMatrixMode(GL.GL_PROJECTION);
45         gl.glLoadIdentity();
46         gl.glMatrixMode(GL.GL_MODELVIEW);
47
48         float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
49         float mat_shininess[] = { 50.0f };
50         gl.glShadeModel(GL.GL_SMOOTH);
51         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
52         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);  
53         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);  
54         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
55         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f,    4.0f,  -10.0f, 0.0f }, 0);
56         gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f,   10.0f, 0.0f }, 0);
57         gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f,   10.0f, 0.0f }, 0);
58         gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f,  10.0f,  -10.0f, 0.0f }, 0);
59         gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f,  -10.0f, 0.0f }, 0);
60         gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f,  -10.0f, 0.0f }, 0);
61         gl.glEnable(GL.GL_LIGHTING);
62         gl.glEnable(GL.GL_LIGHT0);
63
64         gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
65         gl.glEnable(GL.GL_COLOR_MATERIAL);
66
67         display(gld);
68
69         // hack to get around Mac OS bug
70         IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
71         gl.glReadPixels(0,0, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
72     }
73
74     public synchronized final void display(GLAutoDrawable drawable) {
75         glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight() - 100);
76
77         GL gl = glcanvas.getGL();//drawable.getGL();
78         GLU glu = new GLU();
79         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
80         gl.glPointSize(5.0f);
81         gl.glLoadIdentity();
82         glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
83
84         glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
85         gl.glRotatef(anglex/3, 0, 1, 0);
86         gl.glRotatef(-(angley/3), 1, 0, 0);
87
88         gl.glEnable(GL.GL_LIGHTING);
89         gl.glShadeModel(GL.GL_SMOOTH);
90         for(Mesh mesh : meshes)
91             mesh.render(gl, Matrix.ONE);
92
93         // highlight the point closest to the mouse; we do this here to avoid flicker
94         if (closest != null) {
95             gl.glDisable(GL.GL_LIGHTING);
96             gl.glShadeModel(GL.GL_FLAT);
97             gl.glColor3f(1,1,1);
98             gl.glBegin(gl.GL_POINTS);
99             closest.getPoint().glVertex(gl);
100             gl.glEnd();
101         }
102
103         projection = Matrix.getProjectionMatrix(gl);
104
105         if (updateVisibilities) {
106             updateVisibilities = false;
107             // update vertex visibilities
108             updateVisibility(gl);
109             
110             //Matrix projection = Matrix.getProjectionMatrix(gl);
111             double dist = Double.MAX_VALUE;
112             closest = null;
113             for(Mesh mesh : meshes)
114                 if (mesh.option_selectable)
115                     for(Mesh.Vertex v : mesh.vertices()) {
116                         if (!v.visible) continue;
117                         Point p = projection.times(v.getPoint());
118                         int x = (int)p.x;
119                         int y = (int)p.y;
120                         int mousex = (int)getMouse().x;
121                         int mousey = (int)getMouse().y;
122                         if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
123                             dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
124                             closest = v;
125                         }
126                     }
127         }
128     }
129
130     protected synchronized void updateVisibility(GL gl) {
131         IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
132         gl.glFlush();
133         gl.glDrawBuffer(GL.GL_BACK);
134         gl.glReadBuffer(GL.GL_BACK);
135         gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
136         gl.glFlush();
137         gl.glDisable(GL.GL_LIGHTING);
138         gl.glShadeModel(GL.GL_FLAT);
139         gl.glColor3f(0,0,0);
140         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
141         for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
142         for(Mesh mesh : meshes)
143             if (mesh.option_selectable)
144                 for(Mesh.Vertex v : mesh.vertices()) {
145                     Point p = v.getPoint();
146                     gl.glColor3f(1,1,1);
147                     gl.glBegin(gl.GL_POINTS);
148                     p.glVertex(gl);
149                     gl.glEnd();
150                     gl.glFlush();
151                     
152                     Point projected = projection.times(p);
153                     gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
154                     
155                     boolean vis = false;
156                     for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
157                     v.visible = vis;
158                     if (vis) {
159                         gl.glColor3f(0,0,0);
160                         gl.glBegin(gl.GL_POINTS);
161                         p.glVertex(gl);
162                         gl.glEnd();
163                     }
164                 }
165         gl.glShadeModel(GL.GL_SMOOTH);
166         gl.glEnable(GL.GL_LIGHTING);
167         gl.glDrawBuffer(GL.GL_FRONT);
168     }
169
170     /** return the position of the mouse as a point in window-space */
171     public Point getMouse() {
172         return new Point(mousex, glcanvas.getHeight()-mousey, 0);
173     }
174
175     /** return the position where the mouse button was pressed, or null if it is not currently pressed */
176     public Point getMouseClick() {
177         return clickPoint;
178     }
179
180     public void mouseWheelMoved(MouseWheelEvent e) {
181         tz -= e.getWheelRotation();
182     }
183
184     public void keyTyped(KeyEvent e)  { }
185     public void keyPressed(KeyEvent e)  { }
186     public void keyReleased(KeyEvent e) { }
187
188     public void mouseClicked(MouseEvent e) { }
189     public void mouseEntered(MouseEvent e) { }
190     public void mouseExited(MouseEvent e) { }
191     public void mousePressed(MouseEvent e) {
192         clickPoint = getMouse();
193     }
194
195     public void mouseReleased(MouseEvent e) {
196         clickPoint = null;
197     }
198
199     public void mouseMoved(MouseEvent e) {
200         mousex = e.getX();
201         mousey = e.getY();
202         if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0)
203             updateVisibilities = true;
204     }
205
206     public void mouseDragged(MouseEvent e) {
207         if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
208             if (closest != null && projection != null) {
209                 synchronized(this) {
210                     Point clickClosest = closest == null ? null : closest.getPoint();
211                     Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
212                     Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
213                     closest.move(delta, false);
214                 }
215             }
216         } else {
217             anglex -= mousex - e.getX();
218             angley += mousey - e.getY();
219         }
220         mousex = e.getX();
221         mousey = e.getY();
222     }
223
224     public MeshViewer(JFrame f) {
225         glcanvas = new GLCanvas();
226         glcanvas.addGLEventListener(this);
227         f.add(glcanvas, BorderLayout.CENTER);
228         glcanvas.addMouseListener(this);
229         glcanvas.addMouseMotionListener(this);
230         glcanvas.addMouseWheelListener(this);
231         glcanvas.addKeyListener(this);
232     }
233
234     public void repaint() {
235         glcanvas.repaint();
236     }
237
238 }