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 extends JPanel implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
17 private float anglex = 0;
18 private float angley = 0;
20 private Mesh.Vertex closest = null;
21 private Point closestOriginallyAt = null;
24 private Matrix projection = null;
25 private Point clickPoint = null;
26 private GLCanvas glcanvas;
27 private boolean updateVisibilities = false;
28 private boolean mouseInside = false;
30 private HashSet<Mesh> meshes = new HashSet<Mesh>();
32 public synchronized void addMesh(Mesh m) { meshes.add(m); }
33 public synchronized void removeMesh(Mesh m) { meshes.remove(m); }
35 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
36 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
38 public void addKeyListener(KeyListener kl) { glcanvas.addKeyListener(kl); }
40 public synchronized void init(GLAutoDrawable gld) {
41 GL gl = glcanvas.getGL();//gld.getGL();
42 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
43 gl.glViewport(0, 0, 500, 300);
44 gl.glEnable(GL.GL_DEPTH_TEST);
46 gl.glDepthFunc(GL.GL_LEQUAL);
47 gl.glMatrixMode(GL.GL_PROJECTION);
49 gl.glMatrixMode(GL.GL_MODELVIEW);
51 float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
52 float mat_shininess[] = { 50.0f };
53 gl.glShadeModel(GL.GL_SMOOTH);
54 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
55 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
56 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);
57 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
58 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f, 4.0f, -10.0f, 0.0f }, 0);
59 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f, 10.0f, 0.0f }, 0);
60 gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f, 10.0f, 0.0f }, 0);
61 gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f, 10.0f, -10.0f, 0.0f }, 0);
62 gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f, -10.0f, 0.0f }, 0);
63 gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f, -10.0f, 0.0f }, 0);
64 gl.glEnable(GL.GL_LIGHTING);
65 gl.glEnable(GL.GL_LIGHT0);
67 gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
68 gl.glEnable(GL.GL_COLOR_MATERIAL);
72 // hack to get around Mac OS bug
73 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
74 gl.glReadPixels(0,0, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
77 public synchronized final void display(GLAutoDrawable drawable) {
78 glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight());
80 GL gl = glcanvas.getGL();//drawable.getGL();
83 if (!mouseInside) gl.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
84 else gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
86 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
89 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
91 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
92 gl.glRotatef(anglex/3, 0, 1, 0);
93 gl.glRotatef(-(angley/3), 1, 0, 0);
95 gl.glEnable(GL.GL_LIGHTING);
96 gl.glShadeModel(GL.GL_SMOOTH);
97 for(Mesh mesh : meshes)
98 mesh.render(gl, Matrix.ONE);
100 // highlight the point closest to the mouse; we do this here to avoid flicker
101 if (closest != null) {
102 gl.glDisable(GL.GL_LIGHTING);
103 gl.glShadeModel(GL.GL_FLAT);
105 gl.glBegin(gl.GL_POINTS);
106 closest.getPoint().glVertex(gl);
110 projection = Matrix.getProjectionMatrix(gl);
112 if (updateVisibilities) {
113 updateVisibilities = false;
114 // update vertex visibilities
115 updateVisibility(gl);
117 //Matrix projection = Matrix.getProjectionMatrix(gl);
118 double dist = Double.MAX_VALUE;
120 closestOriginallyAt = null;
121 for(Mesh mesh : meshes)
122 if (mesh.option_selectable)
123 for(Mesh.Vertex v : mesh.vertices()) {
124 if (!v.visible) continue;
125 Point p = projection.times(v.getPoint());
128 int mousex = (int)getMouse().x;
129 int mousey = (int)getMouse().y;
130 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
131 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
138 protected synchronized void updateVisibility(GL gl) {
139 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
141 gl.glDrawBuffer(GL.GL_BACK);
142 gl.glReadBuffer(GL.GL_BACK);
143 gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
145 gl.glDisable(GL.GL_LIGHTING);
146 gl.glShadeModel(GL.GL_FLAT);
148 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
149 for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
150 for(Mesh mesh : meshes)
151 if (mesh.option_selectable)
152 for(Mesh.Vertex v : mesh.vertices()) {
153 Point p = v.getPoint();
155 gl.glBegin(gl.GL_POINTS);
160 Point projected = projection.times(p);
161 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
164 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
168 gl.glBegin(gl.GL_POINTS);
173 gl.glShadeModel(GL.GL_SMOOTH);
174 gl.glEnable(GL.GL_LIGHTING);
175 gl.glDrawBuffer(GL.GL_FRONT);
178 /** return the position of the mouse as a point in window-space */
179 public Point getMouse() {
180 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
183 /** return the position where the mouse button was pressed, or null if it is not currently pressed */
184 public Point getMouseClick() {
188 public void mouseWheelMoved(MouseWheelEvent e) {
189 tz -= e.getWheelRotation();
192 public void keyTyped(KeyEvent e) { }
193 public void keyPressed(KeyEvent e) { }
194 public void keyReleased(KeyEvent e) { }
196 public void mouseClicked(MouseEvent e) { }
197 public void mouseEntered(MouseEvent e) { mouseInside = true; }
198 public void mouseExited(MouseEvent e) { mouseInside = false; }
199 public void mousePressed(MouseEvent e) {
200 clickPoint = getMouse();
203 public void mouseReleased(MouseEvent e) {
207 public void mouseMoved(MouseEvent e) {
210 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0)
211 updateVisibilities = true;
214 public void mouseDragged(MouseEvent e) {
215 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
216 if (closest != null && projection != null) {
218 if (closestOriginallyAt==null) closestOriginallyAt = closest.getPoint();
219 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
220 Vec delta = d1.plus(closestOriginallyAt).minus(closest.getPoint());
221 closest.move(delta, false);
225 anglex -= mousex - e.getX();
226 angley += mousey - e.getY();
232 public MeshViewer() {
233 glcanvas = new GLCanvas();
234 glcanvas.addGLEventListener(this);
235 setLayout(new BorderLayout());
236 this.add(glcanvas, BorderLayout.CENTER);
237 glcanvas.addMouseListener(this);
238 glcanvas.addMouseMotionListener(this);
239 glcanvas.addMouseWheelListener(this);
240 glcanvas.addKeyListener(this);
243 public void repaint() {
244 if (glcanvas != null) glcanvas.repaint();