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;
23 private Matrix projection = null;
24 private Point clickPoint = null;
25 private GLCanvas glcanvas;
26 private boolean updateVisibilities = false;
27 private boolean mouseInside = false;
29 private HashSet<Mesh> meshes = new HashSet<Mesh>();
31 public synchronized void addMesh(Mesh m) { meshes.add(m); }
32 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 void addKeyListener(KeyListener kl) { glcanvas.addKeyListener(kl); }
39 public synchronized void init(GLAutoDrawable gld) {
40 GL gl = glcanvas.getGL();//gld.getGL();
41 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
42 gl.glViewport(0, 0, 500, 300);
43 gl.glEnable(GL.GL_DEPTH_TEST);
45 gl.glDepthFunc(GL.GL_LEQUAL);
46 gl.glMatrixMode(GL.GL_PROJECTION);
48 gl.glMatrixMode(GL.GL_MODELVIEW);
50 float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
51 float mat_shininess[] = { 50.0f };
52 gl.glShadeModel(GL.GL_SMOOTH);
53 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
54 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
55 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);
56 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
57 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f, 4.0f, -10.0f, 0.0f }, 0);
58 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f, 10.0f, 0.0f }, 0);
59 gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f, 10.0f, 0.0f }, 0);
60 gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f, 10.0f, -10.0f, 0.0f }, 0);
61 gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f, -10.0f, 0.0f }, 0);
62 gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f, -10.0f, 0.0f }, 0);
63 gl.glEnable(GL.GL_LIGHTING);
64 gl.glEnable(GL.GL_LIGHT0);
66 gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
67 gl.glEnable(GL.GL_COLOR_MATERIAL);
71 // hack to get around Mac OS bug
72 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
73 gl.glReadPixels(0,0, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
76 public synchronized final void display(GLAutoDrawable drawable) {
77 glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight());
79 GL gl = glcanvas.getGL();//drawable.getGL();
82 if (!mouseInside) gl.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
83 else gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
85 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
88 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
90 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
91 gl.glRotatef(anglex/3, 0, 1, 0);
92 gl.glRotatef(-(angley/3), 1, 0, 0);
94 gl.glEnable(GL.GL_LIGHTING);
95 gl.glShadeModel(GL.GL_SMOOTH);
96 for(Mesh mesh : meshes)
97 mesh.render(gl, Matrix.ONE);
99 // highlight the point closest to the mouse; we do this here to avoid flicker
100 if (closest != null) {
101 gl.glDisable(GL.GL_LIGHTING);
102 gl.glShadeModel(GL.GL_FLAT);
104 gl.glBegin(gl.GL_POINTS);
105 closest.getPoint().glVertex(gl);
109 projection = Matrix.getProjectionMatrix(gl);
111 if (updateVisibilities) {
112 updateVisibilities = false;
113 // update vertex visibilities
114 updateVisibility(gl);
116 //Matrix projection = Matrix.getProjectionMatrix(gl);
117 double dist = Double.MAX_VALUE;
119 for(Mesh mesh : meshes)
120 if (mesh.option_selectable)
121 for(Mesh.Vertex v : mesh.vertices()) {
122 if (!v.visible) continue;
123 Point p = projection.times(v.getPoint());
126 int mousex = (int)getMouse().x;
127 int mousey = (int)getMouse().y;
128 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
129 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
136 protected synchronized void updateVisibility(GL gl) {
137 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
139 gl.glDrawBuffer(GL.GL_BACK);
140 gl.glReadBuffer(GL.GL_BACK);
141 gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
143 gl.glDisable(GL.GL_LIGHTING);
144 gl.glShadeModel(GL.GL_FLAT);
146 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
147 for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
148 for(Mesh mesh : meshes)
149 if (mesh.option_selectable)
150 for(Mesh.Vertex v : mesh.vertices()) {
151 Point p = v.getPoint();
153 gl.glBegin(gl.GL_POINTS);
158 Point projected = projection.times(p);
159 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
162 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
166 gl.glBegin(gl.GL_POINTS);
171 gl.glShadeModel(GL.GL_SMOOTH);
172 gl.glEnable(GL.GL_LIGHTING);
173 gl.glDrawBuffer(GL.GL_FRONT);
176 /** return the position of the mouse as a point in window-space */
177 public Point getMouse() {
178 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
181 /** return the position where the mouse button was pressed, or null if it is not currently pressed */
182 public Point getMouseClick() {
186 public void mouseWheelMoved(MouseWheelEvent e) {
187 tz -= e.getWheelRotation();
190 public void keyTyped(KeyEvent e) { }
191 public void keyPressed(KeyEvent e) { }
192 public void keyReleased(KeyEvent e) { }
194 public void mouseClicked(MouseEvent e) { }
195 public void mouseEntered(MouseEvent e) { mouseInside = true; }
196 public void mouseExited(MouseEvent e) { mouseInside = false; }
197 public void mousePressed(MouseEvent e) {
198 clickPoint = getMouse();
201 public void mouseReleased(MouseEvent e) {
205 public void mouseMoved(MouseEvent e) {
208 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0)
209 updateVisibilities = true;
212 public void mouseDragged(MouseEvent e) {
213 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
214 if (closest != null && projection != null) {
216 Point clickClosest = closest == null ? null : closest.getPoint();
217 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
218 Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
219 closest.move(delta, false);
223 anglex -= mousex - e.getX();
224 angley += mousey - e.getY();
230 public MeshViewer() {
231 glcanvas = new GLCanvas();
232 glcanvas.addGLEventListener(this);
233 setLayout(new BorderLayout());
234 this.add(glcanvas, BorderLayout.CENTER);
235 glcanvas.addMouseListener(this);
236 glcanvas.addMouseMotionListener(this);
237 glcanvas.addMouseWheelListener(this);
238 glcanvas.addKeyListener(this);
241 public void repaint() {
242 if (glcanvas != null) glcanvas.repaint();