b87965055bdca6b6869ae9853f244b2f914e80be
[anneal.git] / src / edu / berkeley / qfat / Viewer.java
1 package edu.berkeley.qfat;
2 import java.io.*;
3 import java.nio.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
10 import java.util.*;
11 import edu.berkeley.qfat.geom.*;
12 import edu.berkeley.qfat.geom.Point;
13
14 public abstract class Viewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener  {
15
16     Mesh.Vertex closest = null;
17     Matrix projection = null;
18
19     public Mesh tile = new Mesh(false);
20     public Mesh goal = new Mesh(false);
21
22     JFrame f;
23     GLCanvas glcanvas;
24     Point clickPoint = null;
25     Point clickClosest = null;
26
27     int mousex;
28     int mousey;
29     float tx = 0;
30     float ty = 0;
31     float tz = 0;
32     float anglex = 0;
33     float angley = 0;
34
35     boolean alt = false;
36     boolean shift = false;
37     boolean control = false;
38
39     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
40     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
41
42     public void init(GLAutoDrawable gld) {
43         GL gl = gld.getGL();
44         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
45         gl.glViewport(0, 0, 500, 300);
46         gl.glEnable(GL.GL_DEPTH_TEST);
47         gl.glClearDepth(1.0);
48         gl.glDepthFunc(GL.GL_LEQUAL);
49         gl.glMatrixMode(GL.GL_PROJECTION);
50         gl.glLoadIdentity();
51         gl.glMatrixMode(GL.GL_MODELVIEW);
52
53         float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
54         float mat_shininess[] = { 50.0f };
55         gl.glShadeModel(GL.GL_SMOOTH);
56         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
57         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);  
58         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);  
59         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
60         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f,    4.0f,  -10.0f, 0.0f }, 0);
61         gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f,   10.0f, 0.0f }, 0);
62         gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f,   10.0f, 0.0f }, 0);
63         gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f,  10.0f,  -10.0f, 0.0f }, 0);
64         gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f,  -10.0f, 0.0f }, 0);
65         gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f,  -10.0f, 0.0f }, 0);
66         gl.glEnable(GL.GL_LIGHTING);
67         gl.glEnable(GL.GL_LIGHT0);
68
69         gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
70         gl.glEnable(GL.GL_COLOR_MATERIAL);
71
72         display(gld);
73     }
74
75     public abstract void _display(GLAutoDrawable drawable, GL gl);
76     public final void display(GLAutoDrawable drawable) {
77         GL gl = drawable.getGL();
78         _display(drawable, gl);
79         this.projection = Matrix.getProjectionMatrix(gl);
80         updateVisibility(gl, tile);
81     }
82
83     private void updateVisibility(GL gl, Mesh mesh) {
84         IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
85         gl.glColor3f(0,0,0);
86         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
87         double dist = Double.MAX_VALUE;
88         if (clickPoint==null) closest = null;
89         synchronized(this) {
90             for(Mesh.T t : mesh)
91                 t.glTriangle(gl, null);
92             for(Mesh.Vertex v : mesh.vertices()) {
93                 Point p = v.getPoint();
94                 gl.glColor3f(1,1,1);
95                 gl.glBegin(gl.GL_POINTS);
96                 p.glVertex(gl);
97                 gl.glEnd();
98                 gl.glFlush();
99
100                 Point projected = projection.times(p);
101                 int x = (int)projected.x;
102                 int y = (int)projected.y;
103                 gl.glReadPixels(x-1, y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
104
105                 boolean vis = false;
106                 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
107                 v.visible = vis;
108                 if (vis) {
109                     gl.glColor3f(0,0,0);
110                     gl.glBegin(gl.GL_POINTS);
111                     p.glVertex(gl);
112                     gl.glEnd();
113                     y = glcanvas.getHeight()-y;
114                     if (clickPoint==null) {
115                         if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
116                             dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
117                             closest = v;
118                         }
119                     }
120                 }
121             }
122         }
123         gl.glShadeModel(GL.GL_SMOOTH);
124         gl.glEnable(GL.GL_LIGHTING);
125         gl.glDrawBuffer(GL.GL_FRONT);
126     }
127
128
129     /** return the position of the mouse as a point in window-space */
130     public Point getMouse() {
131         return new Point(mousex, glcanvas.getHeight()-mousey, 0);
132     }
133
134     public void mouseWheelMoved(MouseWheelEvent e) {
135         tz -= e.getWheelRotation();
136     }
137
138     public void keyTyped(KeyEvent e)  { }
139     public void keyPressed(KeyEvent e)  {
140         switch(e.getKeyCode()) {
141             case KeyEvent.VK_CONTROL: control = true; break;
142             case KeyEvent.VK_ALT: alt = true; break;
143             case KeyEvent.VK_SHIFT: shift = true; break;
144         }
145     }
146
147     public void keyReleased(KeyEvent e) {
148         switch(e.getKeyCode()) {
149             case KeyEvent.VK_CONTROL: control = false; break;
150             case KeyEvent.VK_ALT: alt = false; break;
151             case KeyEvent.VK_SHIFT: shift = false; break;
152         }
153     }
154
155     public void mouseClicked(MouseEvent e) { }
156     public void mouseEntered(MouseEvent e) { }
157     public void mouseExited(MouseEvent e) { }
158     public void mousePressed(MouseEvent e) {
159         clickPoint = getMouse();
160         clickClosest = closest == null ? null : closest.getPoint();
161     }
162
163     public void mouseReleased(MouseEvent e) {
164         clickPoint = null;
165         clickClosest = null;
166     }
167
168     public void mouseMoved(MouseEvent e) {
169         mousex = e.getX();
170         mousey = e.getY();
171     }
172
173     public void mouseDragged(MouseEvent e) {
174         if (shift) {
175             /*
176             tx += (mousex - e.getX())/(float)20;
177             ty += (mousey - e.getY())/(float)20;
178             */
179             if (closest != null && projection != null && clickClosest != null) {
180                 synchronized(this) {
181                     Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
182                     Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
183                     //System.out.println(delta + " " + closest.getPoint());
184                     System.out.println(getMouse().minus(clickPoint));
185                     closest.move(Matrix.translate(delta), true);
186                 }
187             }
188         } else {
189             anglex -= mousex - e.getX();
190             angley += mousey - e.getY();
191         }
192         mousex = e.getX();
193         mousey = e.getY();
194     }
195
196     public Viewer(JFrame f) {
197         this.f = f;
198         glcanvas = new GLCanvas();
199         glcanvas.addGLEventListener(this);
200         f.add(glcanvas, BorderLayout.CENTER);
201         glcanvas.addMouseListener(this);
202         glcanvas.addMouseMotionListener(this);
203         glcanvas.addMouseWheelListener(this);
204         glcanvas.addKeyListener(this);
205     }
206
207     public void repaint() {
208         glcanvas.repaint();
209     }
210 }