1 package edu.berkeley.qfat;
5 import java.awt.event.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
11 import edu.berkeley.qfat.bind.*;
12 import edu.berkeley.qfat.geom.*;
13 import edu.berkeley.qfat.geom.Point;
16 * A basic MeshViewer displays zero or more meshes to the user, in
17 * wireframe or shaded panels.
19 public class MeshViewer extends JPanel implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
24 private float anglex = 0;
25 private float angley = 0;
27 boolean drawEdge = false;
29 private Mesh.Vertex closest = null;
30 private Mesh.E closestEdge = null;
31 private Point closestOriginallyAt = null;
34 private Matrix projection = null;
35 private Point clickPoint = null;
36 private GLCanvas glcanvas;
37 private boolean updateVisibilities = false;
38 private boolean updateClosest = false;
39 private boolean mouseInside = false;
41 private HashSet<Mesh> meshes = new HashSet<Mesh>();
43 public synchronized void addMesh(Mesh m) { meshes.add(m); }
44 public synchronized void removeMesh(Mesh m) { meshes.remove(m); }
46 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
47 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
49 public void addKeyListener(KeyListener kl) { glcanvas.addKeyListener(kl); }
51 public synchronized void init(GLAutoDrawable gld) {
52 GL gl = glcanvas.getGL();//gld.getGL();
54 gl.glEnable(gl.GL_DOUBLEBUFFER);
56 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
57 gl.glViewport(0, 0, 500, 300);
58 gl.glEnable(GL.GL_DEPTH_TEST);
60 gl.glDepthFunc(GL.GL_LEQUAL);
61 gl.glMatrixMode(GL.GL_PROJECTION);
63 gl.glMatrixMode(GL.GL_MODELVIEW);
65 float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
66 float mat_shininess[] = { 50.0f };
67 gl.glShadeModel(GL.GL_SMOOTH);
68 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f, 4.0f, -10.0f, 0.0f }, 0);
69 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f, 10.0f, 0.0f }, 0);
70 gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f, 10.0f, 0.0f }, 0);
71 gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f, 10.0f, -10.0f, 0.0f }, 0);
72 gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f, -10.0f, 0.0f }, 0);
73 gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f, -10.0f, 0.0f }, 0);
74 gl.glEnable(GL.GL_LIGHTING);
75 gl.glEnable(GL.GL_LIGHT0);
77 gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
78 gl.glEnable(GL.GL_COLOR_MATERIAL);
82 // hack to get around Mac OS bug
83 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
84 gl.glReadPixels(0,0, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
87 public synchronized final void display(GLAutoDrawable drawable) {
88 glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight());
90 GL gl = glcanvas.getGL();//drawable.getGL();
92 gl.glDrawBuffer(GL.GL_BACK);
94 if (updateVisibilities) {
95 updateVisibilities = false;
96 // update vertex visibilities
99 gl.glDrawBuffer(GL.GL_BACK);
101 if (!mouseInside) gl.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
102 else gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
104 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
105 gl.glPointSize(5.0f);
107 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
109 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
110 gl.glRotatef(anglex/3, 0, 1, 0);
111 gl.glRotatef(-(angley/3), 1, 0, 0);
113 gl.glEnable(GL.GL_LIGHTING);
114 gl.glShadeModel(GL.GL_SMOOTH);
115 for(Mesh mesh : meshes) {
116 mesh.render(gl, Matrix.ONE);
117 if (main != null && main.whichNeighbor>0 && main.transforms!=null && !mesh.option_wireframe) {
118 mesh.option_wireframe = true;
119 mesh.render(gl, main.transforms[main.whichNeighbor-1]);
120 mesh.option_wireframe = false;
124 // highlight the point closest to the mouse; we do this here to avoid flicker
125 if (closest != null) {
126 gl.glDisable(GL.GL_LIGHTING);
127 gl.glShadeModel(GL.GL_FLAT);
130 for(Mesh.E e : (Iterable<Mesh.E>)closestEdge.getBoundPeers()) {
132 gl.glBegin(gl.GL_LINES);
136 gl.glBegin(gl.GL_LINES);
137 e.pair.glVertices(gl);
141 if (closestEdge != null) {
143 gl.glBegin(gl.GL_TRIANGLES);
144 Mesh.T t = closestEdge.t;
149 main.transforms[main.whichNeighbor-1].times(closestEdge.t.p1()).glVertex(gl);
150 main.transforms[main.whichNeighbor-1].times(closestEdge.t.p2()).glVertex(gl);
151 main.transforms[main.whichNeighbor-1].times(closestEdge.t.p3()).glVertex(gl);
152 System.out.println("t="
157 System.out.println("x="
158 +main.transforms[main.whichNeighbor-1].times(t.p1())+"\n "
159 +main.transforms[main.whichNeighbor-1].times(t.p2())+"\n "
160 +main.transforms[main.whichNeighbor-1].times(t.p3())+"\n "
167 if (closest.visible) {
168 gl.glDisable(GL.GL_DEPTH_TEST);
170 gl.glBegin(gl.GL_POINTS);
171 closest.getPoint().glVertex(gl);
172 gl.glColor3f(0.5f,0.5f,0);
173 for(Mesh.Vertex v : (Iterable<Mesh.Vertex>)closest.getBoundPeers())
175 v.getPoint().glVertex(gl);
178 if (closest.getBindingConstraint() instanceof Plane) {
179 Plane p = (Plane)closest.getBindingConstraint();
181 gl.glEnable(gl.GL_BLEND);
182 gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);
183 gl.glColor4f(1,1,0,0.3f);
184 gl.glBegin(gl.GL_LINES);
185 closest.getPoint().glVertex(gl);
186 v.plus(closest.getPoint()).glVertex(gl);
187 closest.getPoint().glVertex(gl);
188 v.times(-1).plus(closest.getPoint()).glVertex(gl);
190 gl.glDisable(gl.GL_BLEND);
193 gl.glEnable(GL.GL_DEPTH_TEST);
197 projection = Matrix.getProjectionMatrix(gl);
200 updateClosest = false;
201 double dist = Double.MAX_VALUE;
202 double distE = Double.MAX_VALUE;
204 closestOriginallyAt = null;
206 for(Mesh mesh : meshes)
207 if (mesh.option_selectable)
208 for(Mesh.Vertex v : mesh.vertices()) {
209 if (!v.visible) continue;
210 Point p = projection.times(v.getPoint());
213 int mousex = (int)getMouse().x;
214 int mousey = (int)getMouse().y;
215 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
216 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
219 for(Mesh.E e = v.e; e!=null; e=e.pair.next==v.e?null:e.pair.next) {
220 if (!e.v2.visible) continue;
222 new Segment(projection.times(e.v1.getPoint()),
223 projection.times(e.v2.getPoint()));
224 double dist2 = s.distance(getMouse());
233 glcanvas.setAutoSwapBufferMode(false);
234 glcanvas.swapBuffers();
237 protected synchronized void updateVisibility(GL gl) {
238 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
240 gl.glDrawBuffer(GL.GL_BACK);
241 gl.glReadBuffer(GL.GL_BACK);
242 gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
244 gl.glDisable(GL.GL_LIGHTING);
245 gl.glShadeModel(GL.GL_FLAT);
246 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
248 for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE, true);
249 for(Mesh mesh : meshes)
250 if (mesh.option_selectable)
251 for(Mesh.Vertex v : mesh.vertices()) {
252 Point p = v.getPoint();
253 Point projected = projection.times(p);
257 gl.glBegin(gl.GL_POINTS);
263 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
264 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
269 gl.glBegin(gl.GL_POINTS);
274 gl.glShadeModel(GL.GL_SMOOTH);
275 gl.glEnable(GL.GL_LIGHTING);
276 gl.glDrawBuffer(GL.GL_FRONT);
279 /** return the position of the mouse as a point in window-space */
280 public Point getMouse() {
281 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
284 /** return the position where the mouse button was pressed, or null if it is not currently pressed */
285 public Point getMouseClick() {
289 public void mouseWheelMoved(MouseWheelEvent e) {
290 tz -= e.getWheelRotation();
293 public void keyTyped(KeyEvent e) { }
294 public void keyPressed(KeyEvent e) { }
295 public void keyReleased(KeyEvent e) { }
297 public void mouseClicked(MouseEvent e) { }
298 public void mouseEntered(MouseEvent e) { mouseInside = true; }
299 public void mouseExited(MouseEvent e) { mouseInside = false; }
300 public void mousePressed(MouseEvent e) {
301 clickPoint = getMouse();
304 public void mouseReleased(MouseEvent e) {
308 public void mouseMoved(MouseEvent e) {
312 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
313 updateVisibilities = true;
315 updateClosest = true;
319 public void mouseDragged(MouseEvent e) {
320 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
321 updateVisibilities = true;
323 if (closest != null && projection != null) {
325 if (closestOriginallyAt==null) closestOriginallyAt = closest.getPoint();
326 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
327 Vec delta = d1.plus(closestOriginallyAt).minus(closest.getPoint());
328 closest.move(delta, false);
331 } else if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0) {
334 updateVisibilities = true;
335 anglex -= mousex - e.getX();
336 angley += mousey - e.getY();
342 public MeshViewer() {
343 glcanvas = new GLCanvas();
344 glcanvas.addGLEventListener(this);
345 setLayout(new BorderLayout());
346 this.add(glcanvas, BorderLayout.CENTER);
347 glcanvas.addMouseListener(this);
348 glcanvas.addMouseMotionListener(this);
349 glcanvas.addMouseWheelListener(this);
350 glcanvas.addKeyListener(this);
353 public void repaint() {
354 if (glcanvas != null) glcanvas.repaint();