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 implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
16 Mesh.Vertex closest = null;
18 private HashSet<Mesh> meshes = new HashSet<Mesh>();
19 public synchronized void addMesh(Mesh m) { meshes.add(m); }
20 public synchronized void removeMesh(Mesh m) { meshes.remove(m); }
24 private Matrix projection = null;
25 private Point clickPoint = null;
29 Point clickClosest = null;
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 synchronized void init(GLAutoDrawable gld) {
40 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
41 gl.glViewport(0, 0, 500, 300);
42 gl.glEnable(GL.GL_DEPTH_TEST);
44 gl.glDepthFunc(GL.GL_LEQUAL);
45 gl.glMatrixMode(GL.GL_PROJECTION);
47 gl.glMatrixMode(GL.GL_MODELVIEW);
49 float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
50 float mat_shininess[] = { 50.0f };
51 gl.glShadeModel(GL.GL_SMOOTH);
52 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
53 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
54 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);
55 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
56 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f, 4.0f, -10.0f, 0.0f }, 0);
57 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f, 10.0f, 0.0f }, 0);
58 gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f, 10.0f, 0.0f }, 0);
59 gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f, 10.0f, -10.0f, 0.0f }, 0);
60 gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f, -10.0f, 0.0f }, 0);
61 gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f, -10.0f, 0.0f }, 0);
62 gl.glEnable(GL.GL_LIGHTING);
63 gl.glEnable(GL.GL_LIGHT0);
65 gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
66 gl.glEnable(GL.GL_COLOR_MATERIAL);
71 public synchronized final void display(GLAutoDrawable drawable) {
72 GL gl = drawable.getGL();
73 glcanvas.setSize(glcanvas.getParent().getWidth(), glcanvas.getParent().getHeight() - 100);
76 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
79 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
81 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
82 gl.glRotatef(anglex/3, 0, 1, 0);
83 gl.glRotatef(-(angley/3), 1, 0, 0);
85 for(Mesh mesh : meshes)
86 mesh.render(gl, Matrix.ONE);
88 // highlight the point closest to the mouse; we do this here to avoid flicker
89 gl.glDisable(GL.GL_LIGHTING);
90 gl.glShadeModel(GL.GL_FLAT);
91 if (closest != null) {
93 gl.glBegin(gl.GL_POINTS);
94 closest.getPoint().glVertex(gl);
98 // update vertex visibilities
99 // FIXME: only do this when we switch into vertex-finding mode
100 updateVisibility(gl);
102 Matrix projection = Matrix.getProjectionMatrix(gl);
103 double dist = Double.MAX_VALUE;
104 if (getMouseClick() != null) return;
106 for(Mesh mesh : meshes)
107 if (mesh.option_selectable)
108 for(Mesh.Vertex v : mesh.vertices()) {
109 if (!v.visible) continue;
110 Point p = projection.times(v.getPoint());
113 int mousex = (int)getMouse().x;
114 int mousey = (int)getMouse().y;
115 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
116 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
121 projection = Matrix.getProjectionMatrix(gl);
124 protected synchronized void updateVisibility(GL gl) {
125 Matrix projection = Matrix.getProjectionMatrix(gl);
126 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
128 gl.glDrawBuffer(GL.GL_BACK);
129 gl.glReadBuffer(GL.GL_BACK);
130 gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
132 gl.glDisable(GL.GL_LIGHTING);
133 gl.glShadeModel(GL.GL_FLAT);
135 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
136 for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE);
137 for(Mesh mesh : meshes)
138 if (mesh.option_selectable)
139 for(Mesh.Vertex v : mesh.vertices()) {
140 Point p = v.getPoint();
142 gl.glBegin(gl.GL_POINTS);
147 Point projected = projection.times(p);
148 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
151 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
155 gl.glBegin(gl.GL_POINTS);
160 gl.glShadeModel(GL.GL_SMOOTH);
161 gl.glEnable(GL.GL_LIGHTING);
162 gl.glDrawBuffer(GL.GL_FRONT);
165 /** return the position of the mouse as a point in window-space */
166 public Point getMouse() {
167 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
170 /** return the position where the mouse button was pressed, or null if it is not currently pressed */
171 public Point getMouseClick() {
175 public void mouseWheelMoved(MouseWheelEvent e) {
176 tz -= e.getWheelRotation();
179 public void keyTyped(KeyEvent e) { }
180 public void keyPressed(KeyEvent e) { }
181 public void keyReleased(KeyEvent e) { }
183 public void mouseClicked(MouseEvent e) { }
184 public void mouseEntered(MouseEvent e) { }
185 public void mouseExited(MouseEvent e) { }
186 public void mousePressed(MouseEvent e) {
187 clickPoint = getMouse();
188 clickClosest = closest == null ? null : closest.getPoint();
191 public void mouseReleased(MouseEvent e) {
196 public void mouseMoved(MouseEvent e) {
201 public void mouseDragged(MouseEvent e) {
202 if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
203 if (closest != null && projection != null && clickClosest != null) {
205 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
206 Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
207 closest.move(delta, false);
211 anglex -= mousex - e.getX();
212 angley += mousey - e.getY();
218 public MeshViewer(JFrame f) {
219 glcanvas = new GLCanvas();
220 glcanvas.addGLEventListener(this);
221 f.add(glcanvas, BorderLayout.CENTER);
222 glcanvas.addMouseListener(this);
223 glcanvas.addMouseMotionListener(this);
224 glcanvas.addMouseWheelListener(this);
225 glcanvas.addKeyListener(this);
228 public void repaint() {