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 Viewer {
15
16     public int whichNeighbor = 1;
17     public double temp;
18     public boolean tileon = true;
19     public boolean tilemeshon = false;
20     public boolean goalon = true;
21     public boolean anneal = false;
22     public boolean hillclimb = false;
23     public boolean neighbors = false;
24     public boolean neighborsWire = false;
25     public boolean neighborsWireOne = false;
26     public boolean errorNormals = false;
27
28     public boolean force = false;
29     public Matrix[] transforms;
30     public Mesh.Vertex[] points;
31     public int breaks = 0;
32
33     public int temps;
34     public int accepts;
35     public    int vertss;
36
37     public MeshViewer(JFrame f) { super(f); }
38
39     public void _display(GLAutoDrawable drawable, GL gl) {
40
41         if (transforms==null) return;
42         glcanvas.setSize(f.getWidth(), f.getHeight() - 100);
43
44         GLU glu = new GLU();
45         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
46         gl.glPointSize(5.0f);
47         gl.glLoadIdentity();
48         glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
49
50         glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
51         gl.glRotatef(anglex/3, 0, 1, 0);
52         gl.glRotatef(-(angley/3), 1, 0, 0);
53
54         tile.render(gl, Matrix.ONE);
55         goal.render(gl, Matrix.ONE);
56
57         // highlight the point closest to the mouse
58         gl.glDisable(GL.GL_LIGHTING);
59         gl.glShadeModel(GL.GL_FLAT);
60         if (closest != null) {
61             gl.glColor3f(1,1,1);
62             gl.glBegin(gl.GL_POINTS);
63             closest.getPoint().glVertex(gl);
64             gl.glEnd();
65         }
66
67         // update vertex visibilities
68         // FIXME: only do this when we switch into vertex-finding mode
69         updateVisibility(gl, tile);
70
71         Matrix projection = Matrix.getProjectionMatrix(gl);
72         double dist = Double.MAX_VALUE;
73         if (getMouseClick() != null) return;
74         closest = null;
75         for(Mesh.Vertex v : tile.vertices()) {
76             if (!v.visible) continue;
77             Point p = projection.times(v.getPoint());
78             int x = (int)p.x;
79             int y = (int)p.y;
80             //y = glcanvas.getHeight()-y;
81             int mousex = (int)getMouse().x;
82             int mousey = (int)getMouse().y;
83             if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
84                 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
85                 closest = v;
86             }
87         }
88     }
89
90 }