1 package edu.berkeley.qfat;
3 import java.awt.event.*;
5 import javax.media.opengl.*;
6 import javax.media.opengl.glu.*;
8 import edu.berkeley.qfat.geom.*;
9 import edu.berkeley.qfat.geom.Point;
11 public class MeshViewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
13 public Mesh tile = new Mesh();
14 public Mesh goal = new Mesh();
15 public Matrix[] translations;
16 public Mesh.Vert[] points;
20 boolean shift = false;
21 boolean control = false;
23 public void mouseWheelMoved(MouseWheelEvent e) {
24 tz -= e.getWheelRotation();
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;
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;
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) { }
51 public void mouseMoved(MouseEvent e) {
61 public void mouseDragged(MouseEvent e) {
63 tx += (mousex - e.getX())/(float)20;
64 ty += (mousey - e.getY())/(float)20;
66 anglex -= mousex - e.getX();
67 angley += mousey - e.getY();
74 * Take care of initialization here.
76 public void init(GLAutoDrawable gld) {
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);
82 gl.glDepthFunc(GL.GL_LEQUAL);
83 gl.glMatrixMode(GL.GL_PROJECTION);
85 gl.glMatrixMode(GL.GL_MODELVIEW);
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();
94 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
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);
103 gl.glBegin(GL.GL_TRIANGLES);
104 draw(gl, true, tile);
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);
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;
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);
127 //gl.glEnable(GL.GL_DEPTH_TEST);
130 private synchronized void draw(GL gl, boolean triangles, Mesh mesh) {
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;
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;
148 //gl.glBegin(GL.GL_LINES);
151 gl.glBegin(GL.GL_TRIANGLES);
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);
165 Point centroid = t.centroid();
166 gl.glBegin(GL.GL_LINES);
167 gl.glColor3f(1, 1, 1);
169 centroid.glVertex(gl);
170 centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
174 for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) {
176 //p.plus(p.norm().times(p.score()*10)).glVertex(gl);
177 //p.partner().p.glVertex(gl);
178 //tile.nearest(p).centroid().glVertex(gl);
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);
196 glcanvas.addMouseListener(this);
197 glcanvas.addMouseMotionListener(this);
198 glcanvas.addMouseWheelListener(this);
199 glcanvas.addKeyListener(this);
201 public void repaint() {