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