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.bind.*;
12 import edu.berkeley.qfat.geom.*;
13 import edu.berkeley.qfat.geom.Point;
15 public class MeshViewer extends JPanel implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
21 private float anglex = 0;
22 private float angley = 0;
24 private Mesh.Vertex closest = null;
25 private Point closestOriginallyAt = null;
28 private Matrix projection = null;
29 private Point clickPoint = null;
30 private GLCanvas glcanvas;
31 private boolean updateVisibilities = false;
32 private boolean mouseInside = false;
34 private HashSet<Mesh> meshes = new HashSet<Mesh>();
36 public synchronized void addMesh(Mesh m) { meshes.add(m); }
37 public synchronized void removeMesh(Mesh m) { meshes.remove(m); }
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 void addKeyListener(KeyListener kl) { glcanvas.addKeyListener(kl); }
44 public synchronized void init(GLAutoDrawable gld) {
45 GL gl = glcanvas.getGL();//gld.getGL();
46 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
47 gl.glViewport(0, 0, 500, 300);
48 gl.glEnable(GL.GL_DEPTH_TEST);
50 gl.glDepthFunc(GL.GL_LEQUAL);
51 gl.glMatrixMode(GL.GL_PROJECTION);
53 gl.glMatrixMode(GL.GL_MODELVIEW);
55 float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
56 float mat_shininess[] = { 50.0f };
57 gl.glShadeModel(GL.GL_SMOOTH);
58 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
59 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
60 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);
61 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
62 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f, 4.0f, -10.0f, 0.0f }, 0);
63 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f, 10.0f, 0.0f }, 0);
64 gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f, 10.0f, 0.0f }, 0);
65 gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f, 10.0f, -10.0f, 0.0f }, 0);
66 gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f, -10.0f, 0.0f }, 0);
67 gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f, -10.0f, 0.0f }, 0);
68 gl.glEnable(GL.GL_LIGHTING);
69 gl.glEnable(GL.GL_LIGHT0);
71 gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
72 gl.glEnable(GL.GL_COLOR_MATERIAL);
76 // hack to get around Mac OS bug
77 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
78 gl.glReadPixels(0,0, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
81 public synchronized final void display(GLAutoDrawable drawable) {
82 glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight());
84 GL gl = glcanvas.getGL();//drawable.getGL();
87 if (!mouseInside) gl.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
88 else gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
90 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
93 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
95 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
96 gl.glRotatef(anglex/3, 0, 1, 0);
97 gl.glRotatef(-(angley/3), 1, 0, 0);
99 gl.glEnable(GL.GL_LIGHTING);
100 gl.glShadeModel(GL.GL_SMOOTH);
101 for(Mesh mesh : meshes) {
102 mesh.render(gl, Matrix.ONE);
103 if (main != null && main.whichNeighbor>0 && main.transforms!=null && !mesh.option_wireframe) {
104 mesh.option_wireframe = true;
105 mesh.render(gl, main.transforms[main.whichNeighbor-1]);
106 mesh.option_wireframe = false;
110 // highlight the point closest to the mouse; we do this here to avoid flicker
111 if (closest != null) {
112 gl.glDisable(GL.GL_LIGHTING);
113 gl.glShadeModel(GL.GL_FLAT);
115 gl.glBegin(gl.GL_POINTS);
116 closest.getPoint().glVertex(gl);
120 projection = Matrix.getProjectionMatrix(gl);
122 if (updateVisibilities) {
123 updateVisibilities = false;
124 // update vertex visibilities
125 updateVisibility(gl);
127 //Matrix projection = Matrix.getProjectionMatrix(gl);
128 double dist = Double.MAX_VALUE;
130 closestOriginallyAt = null;
131 for(Mesh mesh : meshes)
132 if (mesh.option_selectable)
133 for(Mesh.Vertex v : mesh.vertices()) {
134 if (!v.visible) continue;
135 Point p = projection.times(v.getPoint());
138 int mousex = (int)getMouse().x;
139 int mousey = (int)getMouse().y;
140 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
141 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
148 protected synchronized void updateVisibility(GL gl) {
149 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
151 gl.glDrawBuffer(GL.GL_BACK);
152 gl.glReadBuffer(GL.GL_BACK);
153 gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
155 gl.glDisable(GL.GL_LIGHTING);
156 gl.glShadeModel(GL.GL_FLAT);
158 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
159 for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
160 for(Mesh mesh : meshes)
161 if (mesh.option_selectable)
162 for(Mesh.Vertex v : mesh.vertices()) {
163 Point p = v.getPoint();
165 gl.glBegin(gl.GL_POINTS);
170 Point projected = projection.times(p);
171 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
174 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
178 gl.glBegin(gl.GL_POINTS);
183 gl.glShadeModel(GL.GL_SMOOTH);
184 gl.glEnable(GL.GL_LIGHTING);
185 gl.glDrawBuffer(GL.GL_FRONT);
188 /** return the position of the mouse as a point in window-space */
189 public Point getMouse() {
190 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
193 /** return the position where the mouse button was pressed, or null if it is not currently pressed */
194 public Point getMouseClick() {
198 public void mouseWheelMoved(MouseWheelEvent e) {
199 tz -= e.getWheelRotation();
202 public void keyTyped(KeyEvent e) { }
203 public void keyPressed(KeyEvent e) { }
204 public void keyReleased(KeyEvent e) { }
206 public void mouseClicked(MouseEvent e) { }
207 public void mouseEntered(MouseEvent e) { mouseInside = true; }
208 public void mouseExited(MouseEvent e) { mouseInside = false; }
209 public void mousePressed(MouseEvent e) {
210 clickPoint = getMouse();
213 public void mouseReleased(MouseEvent e) {
217 public void mouseMoved(MouseEvent e) {
220 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0)
221 updateVisibilities = true;
224 public void mouseDragged(MouseEvent e) {
225 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
226 if (closest != null && projection != null) {
228 if (closestOriginallyAt==null) closestOriginallyAt = closest.getPoint();
229 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
230 Vec delta = d1.plus(closestOriginallyAt).minus(closest.getPoint());
231 closest.move(delta, false);
235 anglex -= mousex - e.getX();
236 angley += mousey - e.getY();
242 public MeshViewer() {
243 glcanvas = new GLCanvas();
244 glcanvas.addGLEventListener(this);
245 setLayout(new BorderLayout());
246 this.add(glcanvas, BorderLayout.CENTER);
247 glcanvas.addMouseListener(this);
248 glcanvas.addMouseMotionListener(this);
249 glcanvas.addMouseWheelListener(this);
250 glcanvas.addKeyListener(this);
253 public void repaint() {
254 if (glcanvas != null) glcanvas.repaint();