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