00001 package gui.OpenGL;
00002
00003
00004
00005 import java.awt.Dimension;
00006 import java.lang.reflect.InvocationTargetException;
00007 import java.util.ArrayList;
00008
00009 import javax.media.opengl.DebugGL;
00010 import javax.media.opengl.GL;
00011 import javax.media.opengl.GLAutoDrawable;
00012 import javax.media.opengl.GLCanvas;
00013 import javax.media.opengl.GLEventListener;
00014 import javax.media.opengl.glu.GLU;
00015 import javax.swing.SwingUtilities;
00016
00017 import commandline.Encoder.Iterators.RefinementEvent;
00018 import commandline.Encoder.Iterators.RefinementListener;
00019 import commandline.ImageStuff.LeavesCollector;
00020 import commandline.ImageStuff.GeometricObjects.GeometricObject;
00021 import commandline.ImageStuff.GeometricObjects.Triangle;
00022 import commandline.ImageStuff.Topology.AbstractTesselation;
00023 import commandline.NormalMeshAlgorithm.Mesh;
00024 import commandline.debuggingCode.Debug;
00025
00026
00032 public class TesselationViewer extends GLCanvas implements GLEventListener, RefinementListener{
00033
00034
00035 protected volatile Camera camera;
00036 protected volatile Dimension imageDimension;
00037 private volatile AbstractTesselation tesselation;
00038 private volatile Thread selfrunning;
00039 private volatile Runnable workingRunner;
00040 private volatile Mesh m;
00041 private volatile Object mutex=new Object();
00045 public TesselationViewer(Dimension imageDimension) {
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 this.setSize(imageDimension);
00056 this.addGLEventListener(this);
00057
00058
00059
00060
00061
00062
00063 this.imageDimension=imageDimension;
00064 Debug.print("new TesselationViewer created");
00065 this.setVisible(true);
00066
00067
00068 workingRunner=new Runnable(){
00069 public void run(){
00070 synchronized (mutex) {
00071 workmethod();
00072 }
00073
00074 }
00075 };
00076
00077
00078 Runnable innerRunnable=new Runnable(){
00079 public void run(){
00080 while(true){
00081 rotate();
00082 }
00083 }
00084 };
00085 selfrunning=new Thread(innerRunnable);
00086
00087
00088
00089
00090 }
00091
00092 public void performRefinementEvent(RefinementEvent event){
00093 Debug.print("performing the drawing of the refinements");
00094 tesselation=event.getAbstractTesselations();
00095 m=event.getM();
00096
00097
00098 try {
00099
00100 SwingUtilities.invokeAndWait(workingRunner);
00101 } catch (InterruptedException e) {
00102 Thread.currentThread().interrupt();
00103 } catch (InvocationTargetException e) {
00104 Thread.currentThread().interrupt();
00105 }
00106
00107 }
00108
00109
00110 public void init(GLAutoDrawable drawable)
00111 {
00112
00113 GL gl = drawable.getGL();
00114 GLU glu = new GLU();
00115 this.camera=new Camera(gl,glu,new Dimension((int)imageDimension.getWidth(),(int)imageDimension.getHeight()));
00116
00117
00118 gl.glEnable(GL.GL_DEPTH_TEST);
00119 gl.glDepthFunc(GL.GL_LESS);
00120
00121
00122 gl.glDisable(GL.GL_CULL_FACE);
00123 gl.glClearColor(0.1f, 0.2f, 0.4f, 0f);
00124 gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
00125
00126 camera.setEye(300,300,500);
00127 camera.setUpward(0,0,1);
00128 camera.aim();
00129 drawable.setGL( new DebugGL(drawable.getGL() ));
00130 }
00131
00135 public void display(GLAutoDrawable drawable)
00136 {
00137 ensureEventThread();
00138 synchronized (mutex) {
00139 GL gl = drawable.getGL();
00140
00141
00142
00143 gl.glMatrixMode(GL.GL_MODELVIEW);
00144
00145 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
00146
00147
00148
00149
00150
00151
00152
00153 gl.glLoadIdentity();
00154 gl.glPushMatrix();
00155 gl.glTranslatef(-imageDimension.width/2f,-imageDimension.height/2,0f);
00156 showAxis(drawable);
00157 moveScene();
00158 if(m!=null){
00159 drawMesh(drawable,m);}
00160 if(tesselation!=null){
00161 drawRefinement(drawable,tesselation);
00162 }
00163 gl.glPopMatrix();
00164 if(gui.debugging.Debug.DEBUG_GUI) Debug.print("OPENGL-thread:= "+ Thread.currentThread().getName());
00165 }
00166 }
00167
00168 protected void moveScene(){
00169
00170
00171 }
00172
00173 protected void rotate(){
00174 Debug.print("rotating");
00175 try {
00176 selfrunning.sleep(3000);
00177 } catch (InterruptedException e) {
00178 Thread.currentThread().interrupt();
00179 }
00180 }
00181
00182
00183 private void workmethod(){
00184 repaint();
00185 Debug.print("after workmethod");
00186 }
00187
00192 private void drawMesh(GLAutoDrawable drawable,Mesh m){
00193 for(Triangle t: m.getRootTriangles()){
00194 for(GeometricObject go: LeavesCollector.getLeaves(t)){
00195 if(go.isATriangle()) drawGeometricObject(drawable,go,null,new float[]{0,0,1},1);
00196 }
00197
00198 }
00199
00200 }
00201
00206 private void drawRefinement(GLAutoDrawable drawable,AbstractTesselation tesselation){
00207 ArrayList<GeometricObject> parts= tesselation.getParts();
00208 for(GeometricObject go: parts){
00209 drawGeometricObject(drawable,go,new float[]{1,1,0},new float[]{1,0,0},2);
00210 }
00211 }
00212
00219 private void drawGeometricObject(GLAutoDrawable drawable,GeometricObject go,float[] facecolor,float[] edgecolor,int lineWidth){
00220 ensureEventThread();
00221 GL gl = drawable.getGL();
00222 if(go.isATriangle()){
00223 if(facecolor!=null){
00224 gl.glColor3f(facecolor[0],facecolor[1],facecolor[2]);
00225 gl.glBegin(gl.GL_POLYGON);
00226 gl.glVertex3d(go.getVertex(0).getX(),go.getVertex(0).getY(),go.getVertex(0).getZ());
00227 gl.glVertex3d(go.getVertex(1).getX(),go.getVertex(1).getY(),go.getVertex(1).getZ());
00228 gl.glVertex3d(go.getVertex(2).getX(),go.getVertex(2).getY(),go.getVertex(2).getZ());
00229 gl.glEnd();
00230 }
00231 if(edgecolor!=null){
00232 gl.glColor3f(edgecolor[0],edgecolor[1],edgecolor[2]);
00233 gl.glLineWidth(lineWidth);
00234 gl.glBegin(gl.GL_LINE_LOOP);
00235 gl.glVertex3d(go.getVertex(0).getX(),go.getVertex(0).getY(),go.getVertex(0).getZ());
00236 gl.glVertex3d(go.getVertex(1).getX(),go.getVertex(1).getY(),go.getVertex(1).getZ());
00237 gl.glVertex3d(go.getVertex(2).getX(),go.getVertex(2).getY(),go.getVertex(2).getZ());
00238 gl.glEnd();
00239 gl.glLineWidth(1);
00240 }
00241 }else{
00242 if(edgecolor!=null){
00243 gl.glColor3f(edgecolor[0],edgecolor[1],edgecolor[2]);
00244 gl.glBegin(gl.GL_LINE_STRIP);
00245 gl.glVertex3d(go.getVertex(0).getX(),go.getVertex(0).getY(),go.getVertex(0).getZ());
00246 gl.glVertex3d(go.getVertex(1).getX(),go.getVertex(1).getY(),go.getVertex(1).getZ());
00247 gl.glEnd();
00248 gl.glColor3f(edgecolor[0],edgecolor[1],edgecolor[2]);
00249 }
00250 }
00251
00252 }
00253
00254 private void showAxis(GLAutoDrawable drawable){
00255 ensureEventThread();
00256 GL gl = drawable.getGL();
00257 gl.glBegin(gl.GL_LINE_STRIP);
00258 gl.glColor3f(1,0,0);
00259 gl.glVertex3f(0,0,0);
00260 gl.glColor3f(1,0,0);
00261 gl.glVertex3f((float)imageDimension.getWidth(),0,0);
00262 gl.glEnd();
00263
00264
00265 gl.glBegin(gl.GL_LINE_STRIP);
00266 gl.glColor3f(0,1,0);
00267 gl.glVertex3f(0,0,0);
00268 gl.glColor3f(0,1,0);
00269 gl.glVertex3f(0,(float)imageDimension.getHeight(),0);
00270 gl.glEnd();
00271
00272
00273 gl.glBegin(gl.GL_LINE_STRIP);
00274 gl.glColor3f(0,0,1);
00275 gl.glVertex3f(0,0,0);
00276 gl.glColor3f(0,0,1);
00277 gl.glVertex3f(0,0,255);
00278 gl.glEnd();
00279
00280
00281 }
00282
00283 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
00284 {
00285 }
00286
00287 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
00288 {
00289 }
00290
00291 public Camera getCamera(){
00292 return this.camera;
00293 }
00294
00295
00296 private void ensureEventThread(){
00297 if(SwingUtilities.isEventDispatchThread()){
00298 return;
00299 }
00300 throw new RuntimeException("only the event-thread should invoke this method. Current thread = "+ Thread.currentThread().getName());
00301 }
00302 }
00303