1 package edu.berkeley.qfat;
5 import java.awt.event.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
11 import edu.berkeley.qfat.geom.*;
12 import edu.berkeley.qfat.geom.Point;
14 public class MeshViewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
16 Mesh.Vertex closest = null;
18 private HashSet<Mesh> meshes = new HashSet<Mesh>();
19 public synchronized void addMesh(Mesh m) { meshes.add(m); }
20 public synchronized void removeMesh(Mesh m) { meshes.remove(m); }
24 private Matrix projection = null;
25 private Point clickPoint = null;
29 Point clickClosest = null;
36 boolean shift = false;
37 boolean control = false;
39 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
40 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
42 public synchronized void init(GLAutoDrawable gld) {
44 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
45 gl.glViewport(0, 0, 500, 300);
46 gl.glEnable(GL.GL_DEPTH_TEST);
48 gl.glDepthFunc(GL.GL_LEQUAL);
49 gl.glMatrixMode(GL.GL_PROJECTION);
51 gl.glMatrixMode(GL.GL_MODELVIEW);
53 float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
54 float mat_shininess[] = { 50.0f };
55 gl.glShadeModel(GL.GL_SMOOTH);
56 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
57 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
58 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);
59 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
60 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f, 4.0f, -10.0f, 0.0f }, 0);
61 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f, 10.0f, 0.0f }, 0);
62 gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f, 10.0f, 0.0f }, 0);
63 gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f, 10.0f, -10.0f, 0.0f }, 0);
64 gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f, -10.0f, 0.0f }, 0);
65 gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f, -10.0f, 0.0f }, 0);
66 gl.glEnable(GL.GL_LIGHTING);
67 gl.glEnable(GL.GL_LIGHT0);
69 gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
70 gl.glEnable(GL.GL_COLOR_MATERIAL);
75 public synchronized final void display(GLAutoDrawable drawable) {
76 GL gl = drawable.getGL();
77 glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight() - 100);
80 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
83 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
85 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
86 gl.glRotatef(anglex/3, 0, 1, 0);
87 gl.glRotatef(-(angley/3), 1, 0, 0);
89 for(Mesh mesh : meshes)
90 mesh.render(gl, Matrix.ONE);
92 // highlight the point closest to the mouse; we do this here to avoid flicker
93 gl.glDisable(GL.GL_LIGHTING);
94 gl.glShadeModel(GL.GL_FLAT);
95 if (closest != null) {
97 gl.glBegin(gl.GL_POINTS);
98 closest.getPoint().glVertex(gl);
102 // update vertex visibilities
103 // FIXME: only do this when we switch into vertex-finding mode
104 updateVisibility(gl);
106 Matrix projection = Matrix.getProjectionMatrix(gl);
107 double dist = Double.MAX_VALUE;
108 if (getMouseClick() != null) return;
110 for(Mesh mesh : meshes)
111 if (mesh.option_selectable)
112 for(Mesh.Vertex v : mesh.vertices()) {
113 if (!v.visible) continue;
114 Point p = projection.times(v.getPoint());
117 int mousex = (int)getMouse().x;
118 int mousey = (int)getMouse().y;
119 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
120 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
125 projection = Matrix.getProjectionMatrix(gl);
128 protected synchronized void updateVisibility(GL gl) {
129 Matrix projection = Matrix.getProjectionMatrix(gl);
130 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
132 gl.glDrawBuffer(GL.GL_BACK);
133 gl.glReadBuffer(GL.GL_BACK);
134 gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
136 gl.glDisable(GL.GL_LIGHTING);
137 gl.glShadeModel(GL.GL_FLAT);
139 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
140 for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
141 for(Mesh mesh : meshes)
142 if (mesh.option_selectable)
143 for(Mesh.Vertex v : mesh.vertices()) {
144 Point p = v.getPoint();
146 gl.glBegin(gl.GL_POINTS);
151 Point projected = projection.times(p);
152 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
155 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
159 gl.glBegin(gl.GL_POINTS);
164 gl.glShadeModel(GL.GL_SMOOTH);
165 gl.glEnable(GL.GL_LIGHTING);
166 gl.glDrawBuffer(GL.GL_FRONT);
169 /** return the position of the mouse as a point in window-space */
170 public Point getMouse() {
171 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
174 /** return the position where the mouse button was pressed, or null if it is not currently pressed */
175 public Point getMouseClick() {
179 public void mouseWheelMoved(MouseWheelEvent e) {
180 tz -= e.getWheelRotation();
183 public void keyTyped(KeyEvent e) { }
184 public void keyPressed(KeyEvent e) {
185 switch(e.getKeyCode()) {
186 case KeyEvent.VK_CONTROL: control = true; break;
187 case KeyEvent.VK_ALT: alt = true; break;
188 case KeyEvent.VK_SHIFT: shift = true; break;
192 public void keyReleased(KeyEvent e) {
193 switch(e.getKeyCode()) {
194 case KeyEvent.VK_CONTROL: control = false; break;
195 case KeyEvent.VK_ALT: alt = false; break;
196 case KeyEvent.VK_SHIFT: shift = false; break;
200 public void mouseClicked(MouseEvent e) { }
201 public void mouseEntered(MouseEvent e) { }
202 public void mouseExited(MouseEvent e) { }
203 public void mousePressed(MouseEvent e) {
204 clickPoint = getMouse();
205 clickClosest = closest == null ? null : closest.getPoint();
208 public void mouseReleased(MouseEvent e) {
213 public void mouseMoved(MouseEvent e) {
218 public void mouseDragged(MouseEvent e) {
220 if (closest != null && projection != null && clickClosest != null) {
222 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
223 Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
224 closest.move(delta, false);
228 anglex -= mousex - e.getX();
229 angley += mousey - e.getY();
235 public MeshViewer(JFrame f) {
236 glcanvas = new GLCanvas();
237 glcanvas.addGLEventListener(this);
238 f.add(glcanvas, BorderLayout.CENTER);
239 glcanvas.addMouseListener(this);
240 glcanvas.addMouseMotionListener(this);
241 glcanvas.addMouseWheelListener(this);
242 glcanvas.addKeyListener(this);
245 public void repaint() {