checkpoint
[anneal.git] / src / edu / berkeley / qfat / MeshViewer.java
index f7fa9c1..d7481e5 100644 (file)
@@ -8,16 +8,24 @@ import javax.media.opengl.*;
 import javax.media.opengl.glu.*;
 import com.sun.opengl.util.*;
 import java.util.*;
+import edu.berkeley.qfat.bind.*;
 import edu.berkeley.qfat.geom.*;
 import edu.berkeley.qfat.geom.Point;
 
+/**
+ *  A basic MeshViewer displays zero or more meshes to the user, in
+ *  wireframe or shaded panels.
+ */
 public class MeshViewer extends JPanel implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
 
+    Main main;
+
     private float tz = 0;
     private float anglex = 0;
     private float angley = 0;
 
     private Mesh.Vertex closest = null;
+    private Point       closestOriginallyAt = null;
     private int         mousex;
     private int         mousey;
     private Matrix      projection = null;
@@ -34,6 +42,8 @@ public class MeshViewer extends JPanel implements GLEventListener, MouseListener
     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
 
+    public void addKeyListener(KeyListener kl) { glcanvas.addKeyListener(kl); }
+
     public synchronized void init(GLAutoDrawable gld) {
         GL gl = glcanvas.getGL();//gld.getGL();
         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@@ -48,10 +58,6 @@ public class MeshViewer extends JPanel implements GLEventListener, MouseListener
         float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
         float mat_shininess[] = { 50.0f };
         gl.glShadeModel(GL.GL_SMOOTH);
-        //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
-        //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);  
-        //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);  
-        //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f,    4.0f,  -10.0f, 0.0f }, 0);
         gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f,   10.0f, 0.0f }, 0);
         gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f,   10.0f, 0.0f }, 0);
@@ -91,8 +97,14 @@ public class MeshViewer extends JPanel implements GLEventListener, MouseListener
 
         gl.glEnable(GL.GL_LIGHTING);
         gl.glShadeModel(GL.GL_SMOOTH);
-        for(Mesh mesh : meshes)
+        for(Mesh mesh : meshes) {
             mesh.render(gl, Matrix.ONE);
+            if (main != null && main.whichNeighbor>0 && main.transforms!=null && !mesh.option_wireframe) {
+                mesh.option_wireframe = true;
+                mesh.render(gl, main.transforms[main.whichNeighbor-1]);
+                mesh.option_wireframe = false;
+            }
+        }
 
         // highlight the point closest to the mouse; we do this here to avoid flicker
         if (closest != null) {
@@ -114,6 +126,7 @@ public class MeshViewer extends JPanel implements GLEventListener, MouseListener
             //Matrix projection = Matrix.getProjectionMatrix(gl);
             double dist = Double.MAX_VALUE;
             closest = null;
+            closestOriginallyAt = null;
             for(Mesh mesh : meshes)
                 if (mesh.option_selectable)
                     for(Mesh.Vertex v : mesh.vertices()) {
@@ -211,9 +224,9 @@ public class MeshViewer extends JPanel implements GLEventListener, MouseListener
         if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
             if (closest != null && projection != null) {
                 synchronized(this) {
-                    Point clickClosest = closest == null ? null : closest.getPoint();
+                    if (closestOriginallyAt==null) closestOriginallyAt = closest.getPoint();
                     Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
-                    Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
+                    Vec delta = d1.plus(closestOriginallyAt).minus(closest.getPoint());
                     closest.move(delta, false);
                 }
             }