3192c83137f2a85cb2b8902b5faa14ce05f3f40b
[anneal.git] / src / edu / berkeley / qfat / MeshViewer.java
1 package edu.berkeley.qfat;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.swing.*;
5 import javax.media.opengl.*;
6 import javax.media.opengl.glu.*;
7 import java.util.*;
8 import edu.berkeley.qfat.geom.*;
9 import edu.berkeley.qfat.geom.Point;
10
11 public class MeshViewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener  {
12
13     public Mesh tile = new Mesh();
14     public Mesh goal = new Mesh();
15     public Matrix[] translations;
16     public Mesh.Vert[] points;
17
18
19     boolean alt = false;
20     boolean shift = false;
21     boolean control = false;
22
23     public void mouseWheelMoved(MouseWheelEvent e) {
24         tz -= e.getWheelRotation();
25     }
26
27     public void keyTyped(KeyEvent e)  { }
28     public void keyPressed(KeyEvent e)  {
29         switch(e.getKeyCode()) {
30             case KeyEvent.VK_CONTROL: control = true; break;
31             case KeyEvent.VK_ALT: alt = true; break;
32             case KeyEvent.VK_SHIFT: shift = true; break;
33         }
34     }
35     public void keyReleased(KeyEvent e) {
36         switch(e.getKeyCode()) {
37             case KeyEvent.VK_CONTROL: control = false; break;
38             case KeyEvent.VK_ALT: alt = false; break;
39             case KeyEvent.VK_SHIFT: shift = false; break;
40         }
41     }
42
43     public void mouseClicked(MouseEvent e) { }
44     public void mouseEntered(MouseEvent e) { }
45     public void mouseExited(MouseEvent e) { }
46     public void mousePressed(MouseEvent e) { }
47     public void mouseReleased(MouseEvent e) { }
48
49     int mousex;
50     int mousey;
51     public void mouseMoved(MouseEvent e) {
52         mousex = e.getX();
53         mousey = e.getY();
54     }
55
56     float tx = 0;
57     float ty = 0;
58     float tz = 0;
59     float anglex = 0;
60     float angley = 0;
61     public void mouseDragged(MouseEvent e) {
62         if (shift) {
63             tx += (mousex - e.getX())/(float)20;
64             ty += (mousey - e.getY())/(float)20;
65         } else {
66             anglex -= mousex - e.getX();
67             angley += mousey - e.getY();
68         }
69         mousex = e.getX();
70         mousey = e.getY();
71     }
72
73     /**
74      * Take care of initialization here.
75      */
76     public void init(GLAutoDrawable gld) {
77         GL gl = gld.getGL();
78         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
79         gl.glViewport(0, 0, 500, 300);
80         gl.glEnable(GL.GL_DEPTH_TEST);
81         gl.glClearDepth(1.0);
82         gl.glDepthFunc(GL.GL_LEQUAL);
83         gl.glMatrixMode(GL.GL_PROJECTION);
84         gl.glLoadIdentity();
85         gl.glMatrixMode(GL.GL_MODELVIEW);
86         display(gld);
87     }
88
89     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
90     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
91     public synchronized void display(GLAutoDrawable drawable) {
92         GL gl = drawable.getGL();
93         GLU glu = new GLU();
94         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
95         gl.glPointSize(5.0f);
96         gl.glLoadIdentity();
97         glu.gluPerspective(50-tz, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
98         glu.gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0);
99         gl.glTranslatef(tx/(float)20, ty/(float)20, 0);
100         gl.glRotatef(anglex/3, 0, 1, 0);
101         gl.glRotatef(angley/3, 1, 0, 0);
102
103         gl.glBegin(GL.GL_TRIANGLES);
104         draw(gl, true, tile);
105         gl.glEnd();
106
107         gl.glBegin(GL.GL_TRIANGLES);
108         gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
109         //draw(gl, false, goal);
110         gl.glEnd();
111
112
113         int i = 0;
114         //gl.glDisable(GL.GL_DEPTH_TEST);
115         gl.glColor4f(1,1,1,1);
116         for(Matrix m : translations) {
117             //if (v1.z==0 && v1.y==0) continue;
118             i++;
119             if (i != 1 /*&& i!=4*/) continue;
120             Point p = new Point(0, 0, 0).times(m);
121             Vec v = new Vec(p.x, p.y, p.z);
122             v = v.times((float)1.04);
123             gl.glTranslatef(v.x, v.y, v.z);
124             draw(gl, false, tile);
125             gl.glTranslatef(-v.x, -v.y, -v.z);
126         }
127         //gl.glEnable(GL.GL_DEPTH_TEST);
128     }
129
130     private synchronized void draw(GL gl, boolean triangles, Mesh mesh) {
131         float red = 0.0f;
132         float green = 0.0f;
133         float blue = 0.0f;
134         for(Mesh.T t : mesh) {
135             if (red < 0.15) red = 1.0f;
136             if (green < 0.15) green = 1.0f;
137             if (blue < 0.15) blue = 1.0f;
138             red -= .09f;
139             green -= .12f;
140             blue -= .15f;
141
142             if (triangles) switch(t.color) {
143                 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
144                 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
145                 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
146                 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
147             }
148             //gl.glBegin(GL.GL_LINES);
149
150             if (triangles) {
151                 gl.glBegin(GL.GL_TRIANGLES);
152                 t.glVertices(gl);
153                 gl.glEnd();
154             } else {
155                 gl.glBegin(GL.GL_LINES);
156                 t.e1().p1.p.glVertex(gl);
157                 t.e1().p2.p.glVertex(gl);
158                 t.e2().p1.p.glVertex(gl);
159                 t.e2().p2.p.glVertex(gl);
160                 t.e3().p1.p.glVertex(gl);
161                 t.e3().p2.p.glVertex(gl);
162                 gl.glEnd();
163             }
164
165             Point centroid = t.centroid();
166             gl.glBegin(GL.GL_LINES);
167             gl.glColor3f(1, 1, 1);
168             /*
169             centroid.glVertex(gl);
170             centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
171             */
172
173             if (mesh==goal)
174                 for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) {
175                     p.p.glVertex(gl);
176                     //p.plus(p.norm().times(p.score()*10)).glVertex(gl);
177                     //p.partner().p.glVertex(gl);
178                     //tile.nearest(p).centroid().glVertex(gl);
179                 }
180
181             gl.glEnd();
182
183         }
184     }
185
186     private GLCanvas glcanvas;
187     public MeshViewer(Frame f) {
188         GLCapabilities glcaps = new GLCapabilities();
189         glcanvas = new GLCanvas();
190         glcanvas.addGLEventListener(this);
191         f.add(glcanvas, BorderLayout.CENTER);
192         f.pack();
193         f.show();
194         f.setSize(900, 900);
195         f.doLayout();
196         glcanvas.addMouseListener(this);
197         glcanvas.addMouseMotionListener(this);
198         glcanvas.addMouseWheelListener(this);
199         glcanvas.addKeyListener(this);
200     }
201     public void repaint() {
202         glcanvas.repaint();
203     }
204 }