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