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