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