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