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;
20 private Matrix projection = null;
21 private Point clickPoint = null;
22 private GLCanvas glcanvas;
23 private boolean updateVisibilities = false;
25 private HashSet<Mesh> meshes = new HashSet<Mesh>();
27 public synchronized void addMesh(Mesh m) { meshes.add(m); }
28 public synchronized void removeMesh(Mesh m) { meshes.remove(m); }
34 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
35 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
37 public synchronized void init(GLAutoDrawable gld) {
38 GL gl = glcanvas.getGL();//gld.getGL();
39 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
40 gl.glViewport(0, 0, 500, 300);
41 gl.glEnable(GL.GL_DEPTH_TEST);
43 gl.glDepthFunc(GL.GL_LEQUAL);
44 gl.glMatrixMode(GL.GL_PROJECTION);
46 gl.glMatrixMode(GL.GL_MODELVIEW);
48 float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
49 float mat_shininess[] = { 50.0f };
50 gl.glShadeModel(GL.GL_SMOOTH);
51 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
52 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
53 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);
54 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
55 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f, 4.0f, -10.0f, 0.0f }, 0);
56 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f, 10.0f, 0.0f }, 0);
57 gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f, 10.0f, 0.0f }, 0);
58 gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f, 10.0f, -10.0f, 0.0f }, 0);
59 gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f, -10.0f, 0.0f }, 0);
60 gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f, -10.0f, 0.0f }, 0);
61 gl.glEnable(GL.GL_LIGHTING);
62 gl.glEnable(GL.GL_LIGHT0);
64 gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
65 gl.glEnable(GL.GL_COLOR_MATERIAL);
69 // hack to get around Mac OS bug
70 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
71 gl.glReadPixels(0,0, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
74 public synchronized final void display(GLAutoDrawable drawable) {
75 glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight() - 100);
77 GL gl = glcanvas.getGL();//drawable.getGL();
79 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
82 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
84 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
85 gl.glRotatef(anglex/3, 0, 1, 0);
86 gl.glRotatef(-(angley/3), 1, 0, 0);
88 gl.glEnable(GL.GL_LIGHTING);
89 gl.glShadeModel(GL.GL_SMOOTH);
90 for(Mesh mesh : meshes)
91 mesh.render(gl, Matrix.ONE);
93 // highlight the point closest to the mouse; we do this here to avoid flicker
94 if (closest != null) {
95 gl.glDisable(GL.GL_LIGHTING);
96 gl.glShadeModel(GL.GL_FLAT);
98 gl.glBegin(gl.GL_POINTS);
99 closest.getPoint().glVertex(gl);
103 projection = Matrix.getProjectionMatrix(gl);
105 if (updateVisibilities) {
106 updateVisibilities = false;
107 // update vertex visibilities
108 updateVisibility(gl);
110 //Matrix projection = Matrix.getProjectionMatrix(gl);
111 double dist = Double.MAX_VALUE;
113 for(Mesh mesh : meshes)
114 if (mesh.option_selectable)
115 for(Mesh.Vertex v : mesh.vertices()) {
116 if (!v.visible) continue;
117 Point p = projection.times(v.getPoint());
120 int mousex = (int)getMouse().x;
121 int mousey = (int)getMouse().y;
122 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
123 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
130 protected synchronized void updateVisibility(GL gl) {
131 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
133 gl.glDrawBuffer(GL.GL_BACK);
134 gl.glReadBuffer(GL.GL_BACK);
135 gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
137 gl.glDisable(GL.GL_LIGHTING);
138 gl.glShadeModel(GL.GL_FLAT);
140 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
141 for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
142 for(Mesh mesh : meshes)
143 if (mesh.option_selectable)
144 for(Mesh.Vertex v : mesh.vertices()) {
145 Point p = v.getPoint();
147 gl.glBegin(gl.GL_POINTS);
152 Point projected = projection.times(p);
153 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
156 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
160 gl.glBegin(gl.GL_POINTS);
165 gl.glShadeModel(GL.GL_SMOOTH);
166 gl.glEnable(GL.GL_LIGHTING);
167 gl.glDrawBuffer(GL.GL_FRONT);
170 /** return the position of the mouse as a point in window-space */
171 public Point getMouse() {
172 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
175 /** return the position where the mouse button was pressed, or null if it is not currently pressed */
176 public Point getMouseClick() {
180 public void mouseWheelMoved(MouseWheelEvent e) {
181 tz -= e.getWheelRotation();
184 public void keyTyped(KeyEvent e) { }
185 public void keyPressed(KeyEvent e) { }
186 public void keyReleased(KeyEvent e) { }
188 public void mouseClicked(MouseEvent e) { }
189 public void mouseEntered(MouseEvent e) { }
190 public void mouseExited(MouseEvent e) { }
191 public void mousePressed(MouseEvent e) {
192 clickPoint = getMouse();
195 public void mouseReleased(MouseEvent e) {
199 public void mouseMoved(MouseEvent e) {
202 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0)
203 updateVisibilities = true;
206 public void mouseDragged(MouseEvent e) {
207 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
208 if (closest != null && projection != null) {
210 Point clickClosest = closest == null ? null : closest.getPoint();
211 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
212 Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
213 closest.move(delta, false);
217 anglex -= mousex - e.getX();
218 angley += mousey - e.getY();
224 public MeshViewer(JFrame f) {
225 glcanvas = new GLCanvas();
226 glcanvas.addGLEventListener(this);
227 f.add(glcanvas, BorderLayout.CENTER);
228 glcanvas.addMouseListener(this);
229 glcanvas.addMouseMotionListener(this);
230 glcanvas.addMouseWheelListener(this);
231 glcanvas.addKeyListener(this);
234 public void repaint() {