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 if (translations==null) return;
93 GL gl = drawable.getGL();
95 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
98 glu.gluPerspective(50-tz, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
99 glu.gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0);
100 gl.glTranslatef(tx/(float)20, ty/(float)20, 0);
101 gl.glRotatef(anglex/3, 0, 1, 0);
102 gl.glRotatef(angley/3, 1, 0, 0);
104 gl.glBegin(GL.GL_TRIANGLES);
105 draw(gl, true, tile);
108 gl.glBegin(GL.GL_TRIANGLES);
109 gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
110 //draw(gl, false, goal);
115 //gl.glDisable(GL.GL_DEPTH_TEST);
116 gl.glColor4f(1,1,1,1);
117 for(Matrix m : translations) {
118 //if (v1.z==0 && v1.y==0) continue;
120 if (i != 1 /*&& i!=4*/) continue;
121 Point p = new Point(0, 0, 0).times(m);
122 Vec v = new Vec(p.x, p.y, p.z);
123 v = v.times((float)1.04);
124 gl.glTranslatef(v.x, v.y, v.z);
125 draw(gl, false, tile);
126 gl.glTranslatef(-v.x, -v.y, -v.z);
128 //gl.glEnable(GL.GL_DEPTH_TEST);
131 private synchronized void draw(GL gl, boolean triangles, Mesh mesh) {
135 for(Mesh.T t : mesh) {
136 if (red < 0.15) red = 1.0f;
137 if (green < 0.15) green = 1.0f;
138 if (blue < 0.15) blue = 1.0f;
143 if (triangles) switch(t.color) {
144 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
145 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
146 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
147 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
149 //gl.glBegin(GL.GL_LINES);
152 gl.glBegin(GL.GL_TRIANGLES);
156 gl.glBegin(GL.GL_LINES);
157 t.e1().p1.p.glVertex(gl);
158 t.e1().p2.p.glVertex(gl);
159 t.e2().p1.p.glVertex(gl);
160 t.e2().p2.p.glVertex(gl);
161 t.e3().p1.p.glVertex(gl);
162 t.e3().p2.p.glVertex(gl);
166 Point centroid = t.centroid();
167 gl.glBegin(GL.GL_LINES);
168 gl.glColor3f(1, 1, 1);
170 centroid.glVertex(gl);
171 centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
175 for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) {
177 //p.plus(p.norm().times(p.score()*10)).glVertex(gl);
178 //p.partner().p.glVertex(gl);
179 //tile.nearest(p).centroid().glVertex(gl);
187 private GLCanvas glcanvas;
188 public MeshViewer(Frame f) {
189 GLCapabilities glcaps = new GLCapabilities();
190 glcanvas = new GLCanvas();
191 glcanvas.addGLEventListener(this);
192 f.add(glcanvas, BorderLayout.CENTER);
197 glcanvas.addMouseListener(this);
198 glcanvas.addMouseMotionListener(this);
199 glcanvas.addMouseWheelListener(this);
200 glcanvas.addKeyListener(this);
202 public void repaint() {