3f57d77e35b62a418785036e4b0b1833df73c035
[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     protected HashSet<Mesh.T> safeTriangles = new HashSet<Mesh.T>();
37
38     public MeshViewer(JFrame f) { super(f); }
39
40     public void _display(GLAutoDrawable drawable, GL gl) {
41
42         if (transforms==null) return;
43         glcanvas.setSize(f.getWidth(), f.getHeight() - 100);
44
45         GLU glu = new GLU();
46         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
47         gl.glPointSize(5.0f);
48         gl.glLoadIdentity();
49         glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
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         int i = 0;
58         gl.glColor4f(1,1,1,1);
59         for(Matrix m : transforms) {
60             i++;
61             if (neighborsWireOne && i!=whichNeighbor) continue;
62             if (neighbors) tile.render(gl, m);
63             else if (neighborsWire || neighborsWireOne) tile.render(gl, m);
64         }
65
66         gl.glDisable(GL.GL_LIGHTING);
67         gl.glShadeModel(GL.GL_FLAT);
68         if (closest != null) {
69             gl.glColor3f(1,1,1);
70             gl.glBegin(gl.GL_POINTS);
71             closest.getPoint().glVertex(gl);
72             gl.glEnd();
73         }
74
75         updateVisibility(gl, tile);
76
77         Matrix projection = Matrix.getProjectionMatrix(gl);
78         double dist = Double.MAX_VALUE;
79         if (clickPoint != null) return;
80         closest = null;
81         for(Mesh.Vertex v : tile.vertices()) {
82             if (!v.visible) continue;
83             Point p = projection.times(v.getPoint());
84             int x = (int)p.x;
85             int y = (int)p.y;
86             y = glcanvas.getHeight()-y;
87             if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
88                 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
89                 closest = v;
90             }
91         }
92     }
93
94 }