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;
28 private HashSet<Mesh> meshes = new HashSet<Mesh>();
30 public synchronized void addMesh(Mesh m) { meshes.add(m); }
31 public synchronized void removeMesh(Mesh m) { meshes.remove(m); }
33 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
34 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
36 public synchronized void init(GLAutoDrawable gld) {
37 GL gl = glcanvas.getGL();//gld.getGL();
38 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
39 gl.glViewport(0, 0, 500, 300);
40 gl.glEnable(GL.GL_DEPTH_TEST);
42 gl.glDepthFunc(GL.GL_LEQUAL);
43 gl.glMatrixMode(GL.GL_PROJECTION);
45 gl.glMatrixMode(GL.GL_MODELVIEW);
47 float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
48 float mat_shininess[] = { 50.0f };
49 gl.glShadeModel(GL.GL_SMOOTH);
50 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
51 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
52 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);
53 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
54 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f, 4.0f, -10.0f, 0.0f }, 0);
55 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f, 10.0f, 0.0f }, 0);
56 gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f, 10.0f, 0.0f }, 0);
57 gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f, 10.0f, -10.0f, 0.0f }, 0);
58 gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f, -10.0f, 0.0f }, 0);
59 gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f, -10.0f, 0.0f }, 0);
60 gl.glEnable(GL.GL_LIGHTING);
61 gl.glEnable(GL.GL_LIGHT0);
63 gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
64 gl.glEnable(GL.GL_COLOR_MATERIAL);
68 // hack to get around Mac OS bug
69 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
70 gl.glReadPixels(0,0, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
73 public synchronized final void display(GLAutoDrawable drawable) {
74 glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight());
76 GL gl = glcanvas.getGL();//drawable.getGL();
78 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
81 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
83 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
84 gl.glRotatef(anglex/3, 0, 1, 0);
85 gl.glRotatef(-(angley/3), 1, 0, 0);
87 gl.glEnable(GL.GL_LIGHTING);
88 gl.glShadeModel(GL.GL_SMOOTH);
89 for(Mesh mesh : meshes)
90 mesh.render(gl, Matrix.ONE);
92 // highlight the point closest to the mouse; we do this here to avoid flicker
93 if (closest != null) {
94 gl.glDisable(GL.GL_LIGHTING);
95 gl.glShadeModel(GL.GL_FLAT);
97 gl.glBegin(gl.GL_POINTS);
98 closest.getPoint().glVertex(gl);
102 projection = Matrix.getProjectionMatrix(gl);
104 if (updateVisibilities) {
105 updateVisibilities = false;
106 // update vertex visibilities
107 updateVisibility(gl);
109 //Matrix projection = Matrix.getProjectionMatrix(gl);
110 double dist = Double.MAX_VALUE;
112 for(Mesh mesh : meshes)
113 if (mesh.option_selectable)
114 for(Mesh.Vertex v : mesh.vertices()) {
115 if (!v.visible) continue;
116 Point p = projection.times(v.getPoint());
119 int mousex = (int)getMouse().x;
120 int mousey = (int)getMouse().y;
121 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
122 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
129 protected synchronized void updateVisibility(GL gl) {
130 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
132 gl.glDrawBuffer(GL.GL_BACK);
133 gl.glReadBuffer(GL.GL_BACK);
134 gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
136 gl.glDisable(GL.GL_LIGHTING);
137 gl.glShadeModel(GL.GL_FLAT);
139 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
140 for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
141 for(Mesh mesh : meshes)
142 if (mesh.option_selectable)
143 for(Mesh.Vertex v : mesh.vertices()) {
144 Point p = v.getPoint();
146 gl.glBegin(gl.GL_POINTS);
151 Point projected = projection.times(p);
152 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
155 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
159 gl.glBegin(gl.GL_POINTS);
164 gl.glShadeModel(GL.GL_SMOOTH);
165 gl.glEnable(GL.GL_LIGHTING);
166 gl.glDrawBuffer(GL.GL_FRONT);
169 /** return the position of the mouse as a point in window-space */
170 public Point getMouse() {
171 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
174 /** return the position where the mouse button was pressed, or null if it is not currently pressed */
175 public Point getMouseClick() {
179 public void mouseWheelMoved(MouseWheelEvent e) {
180 tz -= e.getWheelRotation();
183 public void keyTyped(KeyEvent e) { }
184 public void keyPressed(KeyEvent e) { }
185 public void keyReleased(KeyEvent e) { }
187 public void mouseClicked(MouseEvent e) { }
188 public void mouseEntered(MouseEvent e) { }
189 public void mouseExited(MouseEvent e) { }
190 public void mousePressed(MouseEvent e) {
191 clickPoint = getMouse();
194 public void mouseReleased(MouseEvent e) {
198 public void mouseMoved(MouseEvent e) {
201 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0)
202 updateVisibilities = true;
205 public void mouseDragged(MouseEvent e) {
206 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
207 if (closest != null && projection != null) {
209 Point clickClosest = closest == null ? null : closest.getPoint();
210 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
211 Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
212 closest.move(delta, false);
216 anglex -= mousex - e.getX();
217 angley += mousey - e.getY();
223 public MeshViewer() {
224 glcanvas = new GLCanvas();
225 glcanvas.addGLEventListener(this);
226 setLayout(new BorderLayout());
227 this.add(glcanvas, BorderLayout.CENTER);
228 glcanvas.addMouseListener(this);
229 glcanvas.addMouseMotionListener(this);
230 glcanvas.addMouseWheelListener(this);
231 glcanvas.addKeyListener(this);
234 public void repaint() {
235 if (glcanvas != null) glcanvas.repaint();