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