From 917325a616f2bd89589bc3b6a4bdce0679bb99cf Mon Sep 17 00:00:00 2001 From: Kenneth Russel Date: Tue, 4 Nov 2003 02:29:09 +0000 Subject: Implemented a per-thread GLContext stack, which gives a thread knowledge of the OpenGL contexts it has made current and allows a GLDrawable to make its context current recursively as well as allowing a GLEventListener to call another GLDrawable's display() method from within its display(). This mechanism can be used fairly easily to expose swapBuffers in the public API, as has been requested. Updated the demos which had to explicitly call display() on more than one drawable to use the Animator class and to call GLDrawable.display() from within their GLEventListeners' display() methods. Updated documentation. Fixed bugs in gleem's CameraParameters class. git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/../svn-server-sync/jogl-demos/trunk@27 3298f667-5e0e-4b4a-8ed4-a3559d26a5f4 --- .../hwShadowmapsSimple/HWShadowmapsSimple.java | 82 ++++++++++--------- .../ProceduralTexturePhysics.java | 91 +++++++++------------- src/gleem/CameraParameters.java | 17 ++++ 3 files changed, 92 insertions(+), 98 deletions(-) diff --git a/src/demos/hwShadowmapsSimple/HWShadowmapsSimple.java b/src/demos/hwShadowmapsSimple/HWShadowmapsSimple.java index 74ecbdd..e600dbd 100644 --- a/src/demos/hwShadowmapsSimple/HWShadowmapsSimple.java +++ b/src/demos/hwShadowmapsSimple/HWShadowmapsSimple.java @@ -62,6 +62,7 @@ public class HWShadowmapsSimple { private GLCanvas canvas; private GLPbuffer pbuffer; + private Animator animator; private GLUT glut; @@ -145,6 +146,8 @@ public class HWShadowmapsSimple { canvas.addGLEventListener(new Listener()); canvas.setNoAutoRedrawMode(true); + animator = new Animator(canvas); + Frame frame = new Frame("ARB_shadow Shadows"); frame.setLayout(new BorderLayout()); canvas.setSize(512, 512); @@ -155,40 +158,11 @@ public class HWShadowmapsSimple { frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { - quit = true; + runExit(); } }); - try { - while (!quit) { - if (viewer != null) { - viewer.update(); - - // Grab these values once per render to avoid multithreading - // issues with their values being changed by manipulation from - // the AWT thread during the render - CameraParameters params = viewer.getCameraParameters(); - - cameraPerspective.set(params.getProjectionMatrix()); - cameraInverseTransform.set(params.getModelviewMatrix()); - cameraTransform.set(cameraInverseTransform); - cameraTransform.invertRigid(); - spotlightTransform.set(spotlight.getTransform()); - spotlightInverseTransform.set(spotlightTransform); - spotlightInverseTransform.invertRigid(); - objectTransform.set(object.getTransform()); - } - - if (displayMode == RENDER_SCENE_FROM_CAMERA_VIEW_SHADOWED || !fullyInitialized) { - if (pbuffer != null) { - pbuffer.display(); - } - } - canvas.display(); - } - } finally { - System.exit(0); - } + animator.start(); } //---------------------------------------------------------------------- @@ -212,7 +186,6 @@ public class HWShadowmapsSimple { checkExtension(gl, "GL_ARB_pixel_format"); } catch (GLException e) { e.printStackTrace(); - quit = true; throw(e); } @@ -319,8 +292,26 @@ public class HWShadowmapsSimple { } public void display(GLDrawable drawable) { - if (quit) { - return; + viewer.update(); + + // Grab these values once per render to avoid multithreading + // issues with their values being changed by manipulation from + // the AWT thread during the render + CameraParameters params = viewer.getCameraParameters(); + + cameraPerspective.set(params.getProjectionMatrix()); + cameraInverseTransform.set(params.getModelviewMatrix()); + cameraTransform.set(cameraInverseTransform); + cameraTransform.invertRigid(); + spotlightTransform.set(spotlight.getTransform()); + spotlightInverseTransform.set(spotlightTransform); + spotlightInverseTransform.invertRigid(); + objectTransform.set(object.getTransform()); + + if (displayMode == RENDER_SCENE_FROM_CAMERA_VIEW_SHADOWED || !fullyInitialized) { + if (pbuffer != null) { + pbuffer.display(); + } } if (!fullyInitialized) { @@ -355,8 +346,6 @@ public class HWShadowmapsSimple { gl.glLoadIdentity(); } - CameraParameters params = viewer.getCameraParameters(); - switch (displayMode) { case RENDER_SCENE_FROM_CAMERA_VIEW: render_scene_from_camera_view(gl, glu, drawable, params); break; case RENDER_SCENE_FROM_CAMERA_VIEW_SHADOWED: render_scene_from_camera_view_shadowed(gl, glu, drawable, params); break; @@ -382,15 +371,10 @@ public class HWShadowmapsSimple { } private void dispatchKey(char k) { - if ((k == (char) 27) || (k == 'q')) { - quit = true; - return; - } - switch (k) { case 27: case 'q': - quit = true; + runExit(); break; case 'v': @@ -847,4 +831,18 @@ public class HWShadowmapsSimple { return m; } + + private void runExit() { + // Note: calling System.exit() synchronously inside the draw, + // reshape or init callbacks can lead to deadlocks on certain + // platforms (in particular, X11) because the JAWT's locking + // routines cause a global AWT lock to be grabbed. Run the + // exit routine in another thread. + new Thread(new Runnable() { + public void run() { + animator.stop(); + System.exit(0); + } + }).start(); + } } diff --git a/src/demos/proceduralTexturePhysics/ProceduralTexturePhysics.java b/src/demos/proceduralTexturePhysics/ProceduralTexturePhysics.java index f34940c..03ef552 100644 --- a/src/demos/proceduralTexturePhysics/ProceduralTexturePhysics.java +++ b/src/demos/proceduralTexturePhysics/ProceduralTexturePhysics.java @@ -57,8 +57,6 @@ import gleem.linalg.*; */ public class ProceduralTexturePhysics { - private static volatile boolean quit; - private volatile boolean drawing; private volatile int mousePosX; private volatile int mousePosY; @@ -66,6 +64,7 @@ public class ProceduralTexturePhysics { private Dimension dim = new Dimension(); private GLCanvas canvas; private Water water; + private Animator animator; private volatile ExaminerViewer viewer; private boolean[] b = new boolean[256]; private boolean doViewAll = true; @@ -85,6 +84,8 @@ public class ProceduralTexturePhysics { canvas.addGLEventListener(new Listener()); canvas.setNoAutoRedrawMode(true); + animator = new Animator(canvas); + Frame frame = new Frame("Procedural Texture Waves"); frame.setLayout(new BorderLayout()); canvas.setSize(512, 512); @@ -95,47 +96,17 @@ public class ProceduralTexturePhysics { frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { - quit = true; + runExit(); } }); - try { - water = new Water(); - water.initialize("demos/data/images/nvfixed.tga", - "demos/data/images/nvspin.tga", - "demos/data/images/droplet.tga", - "demos/data/cubemaps/CloudyHills_{0}.tga", - canvas); - } catch (GLException e) { - JOptionPane.showMessageDialog(null, e.toString(), "Unavailable extension", JOptionPane.ERROR_MESSAGE); - } - - try { - while (!quit) { - if (viewer != null) { - try { - if (drawing) { - canvas.getSize(dim); - water.addDroplet(new Water.Droplet( 2 * (mousePosX / (float) dim.width - 0.5f), - -2 * (mousePosY / (float) dim.height - 0.5f), - 0.08f)); - } - water.tick(); - canvas.display(); - } catch (GLException e) { - // Have seen spurious exceptions getting thrown during SwapBuffers. - // Not sure why at this time; disabling of repaint() should prevent - // AWT thread from getting involved. Continue animating anyway. - e.printStackTrace(); - } - } else { - // Make the pbuffer get created - canvas.display(); - } - } - } finally { - System.exit(0); - } + water = new Water(); + water.initialize("demos/data/images/nvfixed.tga", + "demos/data/images/nvspin.tga", + "demos/data/images/droplet.tga", + "demos/data/cubemaps/CloudyHills_{0}.tga", + canvas); + animator.start(); } //---------------------------------------------------------------------- @@ -167,7 +138,6 @@ public class ProceduralTexturePhysics { checkExtension(gl, "GL_ARB_pixel_format"); } catch (GLException e) { e.printStackTrace(); - quit = true; throw(e); } @@ -220,14 +190,6 @@ public class ProceduralTexturePhysics { } public void display(GLDrawable drawable) { - if (water == null) { - return; - } - - if (quit) { - return; - } - if (!firstRender) { if (++frameCount == 30) { timer.stop(); @@ -255,6 +217,14 @@ public class ProceduralTexturePhysics { ManipManager.getManipManager().updateCameraParameters(drawable, viewer.getCameraParameters()); ManipManager.getManipManager().render(drawable, gl); + if (drawing) { + canvas.getSize(dim); + water.addDroplet(new Water.Droplet( 2 * (mousePosX / (float) dim.width - 0.5f), + -2 * (mousePosY / (float) dim.height - 0.5f), + 0.08f)); + } + water.tick(); + CameraParameters params = viewer.getCameraParameters(); water.draw(gl, params.getOrientation().inverse()); } @@ -272,22 +242,17 @@ public class ProceduralTexturePhysics { if (!gl.isExtensionAvailable(extensionName)) { String message = "Unable to initialize " + extensionName + " OpenGL extension"; JOptionPane.showMessageDialog(null, message, "Unavailable extension", JOptionPane.ERROR_MESSAGE); - throw new GLException(message); + runExit(); } } private void dispatchKey(char k) { setFlag(k, !getFlag(k)); - if ((k == (char) 27) || (k == 'q')) { - quit = true; - return; - } - switch (k) { case 27: case 'q': - quit = true; + runExit(); break; case 'w': water.enableWireframe(getFlag('w')); @@ -389,4 +354,18 @@ public class ProceduralTexturePhysics { } } } + + private void runExit() { + // Note: calling System.exit() synchronously inside the draw, + // reshape or init callbacks can lead to deadlocks on certain + // platforms (in particular, X11) because the JAWT's locking + // routines cause a global AWT lock to be grabbed. Run the + // exit routine in another thread. + new Thread(new Runnable() { + public void run() { + animator.stop(); + System.exit(0); + } + }).start(); + } } diff --git a/src/gleem/CameraParameters.java b/src/gleem/CameraParameters.java index 658820d..c406cda 100644 --- a/src/gleem/CameraParameters.java +++ b/src/gleem/CameraParameters.java @@ -61,6 +61,8 @@ public class CameraParameters { Vec3f forwardDirection, Vec3f upDirection, Rotf orientation, + Mat4f modelviewMatrix, + Mat4f projectionMatrix, float vertFOV, float imagePlaneAspectRatio, int xSize, @@ -69,6 +71,8 @@ public class CameraParameters { setForwardDirection(forwardDirection); setUpDirection(upDirection); setOrientation(orientation); + setModelviewMatrix(modelviewMatrix); + setProjectionMatrix(projectionMatrix); setVertFOV(vertFOV); setImagePlaneAspectRatio(imagePlaneAspectRatio); setXSize(xSize); @@ -79,12 +83,25 @@ public class CameraParameters { setPosition(params.getPosition()); setForwardDirection(params.getForwardDirection()); setUpDirection(params.getUpDirection()); + setOrientation(params.getOrientation()); + setModelviewMatrix(params.getModelviewMatrix()); + setProjectionMatrix(params.getProjectionMatrix()); setVertFOV(params.getVertFOV()); setImagePlaneAspectRatio(params.getImagePlaneAspectRatio()); setXSize(params.getXSize()); setYSize(params.getYSize()); } + public Object clone() { + CameraParameters params = new CameraParameters(); + params.set(this); + return params; + } + + public CameraParameters copy() { + return (CameraParameters) clone(); + } + /** Sets 3-space origin of camera */ public void setPosition(Vec3f position) { this.position.set(position); } /** Gets 3-space origin of camera */ -- cgit v1.2.3