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 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());
77 GL gl = glcanvas.getGL();//drawable.getGL();
80 if (!mouseInside) gl.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
81 else gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
83 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
86 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
88 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
89 gl.glRotatef(anglex/3, 0, 1, 0);
90 gl.glRotatef(-(angley/3), 1, 0, 0);
92 gl.glEnable(GL.GL_LIGHTING);
93 gl.glShadeModel(GL.GL_SMOOTH);
94 for(Mesh mesh : meshes)
95 mesh.render(gl, Matrix.ONE);
97 // highlight the point closest to the mouse; we do this here to avoid flicker
98 if (closest != null) {
99 gl.glDisable(GL.GL_LIGHTING);
100 gl.glShadeModel(GL.GL_FLAT);
102 gl.glBegin(gl.GL_POINTS);
103 closest.getPoint().glVertex(gl);
107 projection = Matrix.getProjectionMatrix(gl);
109 if (updateVisibilities) {
110 updateVisibilities = false;
111 // update vertex visibilities
112 updateVisibility(gl);
114 //Matrix projection = Matrix.getProjectionMatrix(gl);
115 double dist = Double.MAX_VALUE;
117 for(Mesh mesh : meshes)
118 if (mesh.option_selectable)
119 for(Mesh.Vertex v : mesh.vertices()) {
120 if (!v.visible) continue;
121 Point p = projection.times(v.getPoint());
124 int mousex = (int)getMouse().x;
125 int mousey = (int)getMouse().y;
126 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
127 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
134 protected synchronized void updateVisibility(GL gl) {
135 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
137 gl.glDrawBuffer(GL.GL_BACK);
138 gl.glReadBuffer(GL.GL_BACK);
139 gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
141 gl.glDisable(GL.GL_LIGHTING);
142 gl.glShadeModel(GL.GL_FLAT);
144 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
145 for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
146 for(Mesh mesh : meshes)
147 if (mesh.option_selectable)
148 for(Mesh.Vertex v : mesh.vertices()) {
149 Point p = v.getPoint();
151 gl.glBegin(gl.GL_POINTS);
156 Point projected = projection.times(p);
157 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
160 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
164 gl.glBegin(gl.GL_POINTS);
169 gl.glShadeModel(GL.GL_SMOOTH);
170 gl.glEnable(GL.GL_LIGHTING);
171 gl.glDrawBuffer(GL.GL_FRONT);
174 /** return the position of the mouse as a point in window-space */
175 public Point getMouse() {
176 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
179 /** return the position where the mouse button was pressed, or null if it is not currently pressed */
180 public Point getMouseClick() {
184 public void mouseWheelMoved(MouseWheelEvent e) {
185 tz -= e.getWheelRotation();
188 public void keyTyped(KeyEvent e) { }
189 public void keyPressed(KeyEvent e) { }
190 public void keyReleased(KeyEvent e) { }
192 public void mouseClicked(MouseEvent e) { }
193 public void mouseEntered(MouseEvent e) { mouseInside = true; }
194 public void mouseExited(MouseEvent e) { mouseInside = false; }
195 public void mousePressed(MouseEvent e) {
196 clickPoint = getMouse();
199 public void mouseReleased(MouseEvent e) {
203 public void mouseMoved(MouseEvent e) {
206 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0)
207 updateVisibilities = true;
210 public void mouseDragged(MouseEvent e) {
211 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
212 if (closest != null && projection != null) {
214 Point clickClosest = closest == null ? null : closest.getPoint();
215 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
216 Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
217 closest.move(delta, false);
221 anglex -= mousex - e.getX();
222 angley += mousey - e.getY();
228 public MeshViewer() {
229 glcanvas = new GLCanvas();
230 glcanvas.addGLEventListener(this);
231 setLayout(new BorderLayout());
232 this.add(glcanvas, BorderLayout.CENTER);
233 glcanvas.addMouseListener(this);
234 glcanvas.addMouseMotionListener(this);
235 glcanvas.addMouseWheelListener(this);
236 glcanvas.addKeyListener(this);
239 public void repaint() {
240 if (glcanvas != null) glcanvas.repaint();