diff options
Diffstat (limited to 'ardor3d-jogl/src/main')
-rw-r--r-- | ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java | 506 | ||||
-rw-r--r-- | ardor3d-jogl/src/main/java/com/ardor3d/renderer/jogl/JoglTextureRendererProvider.java | 98 |
2 files changed, 302 insertions, 302 deletions
diff --git a/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java b/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java index 70aacb4..d77edb0 100644 --- a/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java +++ b/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java @@ -1,253 +1,253 @@ -/**
- * Copyright (c) 2008-2010 Ardor Labs, Inc.
- *
- * This file is part of Ardor3D.
- *
- * Ardor3D is free software: you can redistribute it and/or modify it
- * under the terms of its license which may be found in the accompanying
- * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
- */
-
-package com.ardor3d.framework.jogl;
-
-import java.util.logging.Logger;
-
-import javax.media.opengl.DebugGL2;
-import javax.media.opengl.GL;
-import javax.media.opengl.GLContext;
-import javax.media.opengl.GLDrawableFactory;
-import javax.media.opengl.GLException;
-import javax.media.opengl.GLProfile;
-
-import com.ardor3d.annotation.MainThread;
-import com.ardor3d.framework.CanvasRenderer;
-import com.ardor3d.framework.DisplaySettings;
-import com.ardor3d.framework.Scene;
-import com.ardor3d.math.ColorRGBA;
-import com.ardor3d.math.Vector3;
-import com.ardor3d.renderer.Camera;
-import com.ardor3d.renderer.Camera.ProjectionMode;
-import com.ardor3d.renderer.ContextCapabilities;
-import com.ardor3d.renderer.ContextManager;
-import com.ardor3d.renderer.RenderContext;
-import com.ardor3d.renderer.Renderer;
-import com.ardor3d.renderer.jogl.JoglContextCapabilities;
-import com.ardor3d.renderer.jogl.JoglRenderer;
-import com.ardor3d.util.Ardor3dException;
-
-public class JoglCanvasRenderer implements CanvasRenderer {
-
- private static final Logger LOGGER = Logger.getLogger(JoglCanvasRenderer.class.getName());
-
- protected Scene _scene;
- protected Camera _camera;
- protected boolean _doSwap;
- protected GLContext _context;
- protected JoglRenderer _renderer;
- protected int _frameClear = Renderer.BUFFER_COLOR_AND_DEPTH;
-
- private RenderContext _currentContext;
-
- /**
- * <code>true</code> if debugging (checking for error codes on each GL call) is desired.
- */
- private final boolean _useDebug;
-
- /**
- * <code>true</code> if debugging is currently enabled for this GLContext.
- */
- private boolean _debugEnabled = false;
-
- public JoglCanvasRenderer(final Scene scene) {
- this(scene, false);
- }
-
- public JoglCanvasRenderer(final Scene scene, final boolean useDebug) {
- _scene = scene;
- _useDebug = useDebug;
- }
-
- public void makeCurrentContext() throws Ardor3dException {
- int value = GLContext.CONTEXT_NOT_CURRENT;
- int attempt = 0;
- do {
- try {
- value = _context.makeCurrent();
- } catch (final GLException gle) {
- gle.printStackTrace();
- } finally {
- attempt++;
- if (attempt == MAX_CONTEXT_GRAB_ATTEMPTS) {
- // failed, throw exception
- throw new Ardor3dException("Failed to claim OpenGL context.");
- }
- }
- try {
- Thread.sleep(5);
- } catch (final InterruptedException e1) {
- e1.printStackTrace();
- }
- } while (value == GLContext.CONTEXT_NOT_CURRENT);
- if (ContextManager.getCurrentContext() != null) {
- if (value == GLContext.CONTEXT_CURRENT_NEW) {
- ContextManager.getCurrentContext().contextLost();
-
- // Whenever the context is created or replaced, the GL chain
- // is lost. Debug will have to be added if desired.
- _debugEnabled = false;
- }
-
- if (ContextManager.getContextForKey(_context) != null) {
- ContextManager.switchContext(_context);
- }
- }
- }
-
- public void releaseCurrentContext() {
- if (_context.equals(GLContext.getCurrent())) {
- try {
- _context.release();
- } catch (final GLException gle) {
- gle.printStackTrace();
- }
- }
- }
-
- @MainThread
- protected ContextCapabilities createContextCapabilities() {
- return new JoglContextCapabilities(_context.getGL());
- }
-
- @MainThread
- public void init(final DisplaySettings settings, final boolean doSwap) {
- _doSwap = doSwap;
- if (_context == null) {
- _context = GLDrawableFactory.getFactory(GLProfile.getMaxFixedFunc(true)).createExternalGLContext();
- }
-
- makeCurrentContext();
-
- try {
-
- // Look up a shared context, if a shared JoglCanvasRenderer is given.
- RenderContext sharedContext = null;
- if (settings.getShareContext() != null) {
- sharedContext = ContextManager.getContextForKey(settings.getShareContext().getRenderContext()
- .getContextKey());
- }
-
- final ContextCapabilities caps = createContextCapabilities();
- _currentContext = new RenderContext(_context, caps, sharedContext);
-
- ContextManager.addContext(_context, _currentContext);
- ContextManager.switchContext(_context);
-
- _renderer = new JoglRenderer();
-
- if (settings.getSamples() != 0 && caps.isMultisampleSupported()) {
- final GL gl = GLContext.getCurrentGL();
- gl.glEnable(GL.GL_MULTISAMPLE);
- }
-
- _renderer.setBackgroundColor(ColorRGBA.BLACK);
-
- if (_camera == null) {
- /** Set up how our camera sees. */
- _camera = new Camera(settings.getWidth(), settings.getHeight());
- _camera.setFrustumPerspective(45.0f, (float) settings.getWidth() / (float) settings.getHeight(), 1,
- 1000);
- _camera.setProjectionMode(ProjectionMode.Perspective);
-
- final Vector3 loc = new Vector3(0.0f, 0.0f, 10.0f);
- final Vector3 left = new Vector3(-1.0f, 0.0f, 0.0f);
- final Vector3 up = new Vector3(0.0f, 1.0f, 0.0f);
- final Vector3 dir = new Vector3(0.0f, 0f, -1.0f);
- /** Move our camera to a correct place and orientation. */
- _camera.setFrame(loc, left, up, dir);
- } else {
- // use new width and height to set ratio.
- _camera.setFrustumPerspective(_camera.getFovY(),
- (float) settings.getWidth() / (float) settings.getHeight(), _camera.getFrustumNear(),
- _camera.getFrustumFar());
- }
- } finally {
- releaseCurrentContext();
- }
- }
-
- public GLContext getContext() {
- return _context;
- }
-
- public void setContext(final GLContext context) {
- _context = context;
- }
-
- public int MAX_CONTEXT_GRAB_ATTEMPTS = 10;
-
- @MainThread
- public boolean draw() {
-
- // set up context for rendering this canvas
- makeCurrentContext();
-
- // Enable Debugging if requested.
- if (_useDebug != _debugEnabled) {
- _context.setGL(new DebugGL2(_context.getGL().getGL2()));
- _debugEnabled = true;
-
- LOGGER.info("DebugGL Enabled");
- }
-
- // render stuff, first apply our camera if we have one
- if (_camera != null) {
- if (Camera.getCurrentCamera() != _camera) {
- _camera.update();
- }
- _camera.apply(_renderer);
- }
- _renderer.clearBuffers(_frameClear);
-
- final boolean drew = _scene.renderUnto(_renderer);
- _renderer.flushFrame(drew && _doSwap);
-
- // release the context if we're done (swapped and all)
- if (_doSwap) {
- releaseCurrentContext();
- }
-
- return drew;
- }
-
- public Camera getCamera() {
- return _camera;
- }
-
- public Scene getScene() {
- return _scene;
- }
-
- public void setScene(final Scene scene) {
- _scene = scene;
- }
-
- public Renderer getRenderer() {
- return _renderer;
- }
-
- public void setCamera(final Camera camera) {
- _camera = camera;
- }
-
- public RenderContext getRenderContext() {
- return _currentContext;
- }
-
- public int getFrameClear() {
- return _frameClear;
- }
-
- public void setFrameClear(final int buffers) {
- _frameClear = buffers;
- }
-}
+/** + * Copyright (c) 2008-2010 Ardor Labs, Inc. + * + * This file is part of Ardor3D. + * + * Ardor3D is free software: you can redistribute it and/or modify it + * under the terms of its license which may be found in the accompanying + * LICENSE file or at <http://www.ardor3d.com/LICENSE>. + */ + +package com.ardor3d.framework.jogl; + +import java.util.logging.Logger; + +import javax.media.opengl.DebugGL2; +import javax.media.opengl.GL; +import javax.media.opengl.GLContext; +import javax.media.opengl.GLDrawableFactory; +import javax.media.opengl.GLException; +import javax.media.opengl.GLProfile; + +import com.ardor3d.annotation.MainThread; +import com.ardor3d.framework.CanvasRenderer; +import com.ardor3d.framework.DisplaySettings; +import com.ardor3d.framework.Scene; +import com.ardor3d.math.ColorRGBA; +import com.ardor3d.math.Vector3; +import com.ardor3d.renderer.Camera; +import com.ardor3d.renderer.Camera.ProjectionMode; +import com.ardor3d.renderer.ContextCapabilities; +import com.ardor3d.renderer.ContextManager; +import com.ardor3d.renderer.RenderContext; +import com.ardor3d.renderer.Renderer; +import com.ardor3d.renderer.jogl.JoglContextCapabilities; +import com.ardor3d.renderer.jogl.JoglRenderer; +import com.ardor3d.util.Ardor3dException; + +public class JoglCanvasRenderer implements CanvasRenderer { + + private static final Logger LOGGER = Logger.getLogger(JoglCanvasRenderer.class.getName()); + + protected Scene _scene; + protected Camera _camera; + protected boolean _doSwap; + protected GLContext _context; + protected JoglRenderer _renderer; + protected int _frameClear = Renderer.BUFFER_COLOR_AND_DEPTH; + + private RenderContext _currentContext; + + /** + * <code>true</code> if debugging (checking for error codes on each GL call) is desired. + */ + private final boolean _useDebug; + + /** + * <code>true</code> if debugging is currently enabled for this GLContext. + */ + private boolean _debugEnabled = false; + + public JoglCanvasRenderer(final Scene scene) { + this(scene, false); + } + + public JoglCanvasRenderer(final Scene scene, final boolean useDebug) { + _scene = scene; + _useDebug = useDebug; + } + + public void makeCurrentContext() throws Ardor3dException { + int value = GLContext.CONTEXT_NOT_CURRENT; + int attempt = 0; + do { + try { + value = _context.makeCurrent(); + } catch (final GLException gle) { + gle.printStackTrace(); + } finally { + attempt++; + if (attempt == MAX_CONTEXT_GRAB_ATTEMPTS) { + // failed, throw exception + throw new Ardor3dException("Failed to claim OpenGL context."); + } + } + try { + Thread.sleep(5); + } catch (final InterruptedException e1) { + e1.printStackTrace(); + } + } while (value == GLContext.CONTEXT_NOT_CURRENT); + if (ContextManager.getCurrentContext() != null) { + if (value == GLContext.CONTEXT_CURRENT_NEW) { + ContextManager.getCurrentContext().contextLost(); + + // Whenever the context is created or replaced, the GL chain + // is lost. Debug will have to be added if desired. + _debugEnabled = false; + } + + if (ContextManager.getContextForKey(_context) != null) { + ContextManager.switchContext(_context); + } + } + } + + public void releaseCurrentContext() { + if (_context.equals(GLContext.getCurrent())) { + try { + _context.release(); + } catch (final GLException gle) { + gle.printStackTrace(); + } + } + } + + @MainThread + protected ContextCapabilities createContextCapabilities() { + return new JoglContextCapabilities(_context.getGL()); + } + + @MainThread + public void init(final DisplaySettings settings, final boolean doSwap) { + _doSwap = doSwap; + if (_context == null) { + _context = GLDrawableFactory.getFactory(GLProfile.getMaxFixedFunc(true)).createExternalGLContext(); + } + + makeCurrentContext(); + + try { + + // Look up a shared context, if a shared JoglCanvasRenderer is given. + RenderContext sharedContext = null; + if (settings.getShareContext() != null) { + sharedContext = ContextManager.getContextForKey(settings.getShareContext().getRenderContext() + .getContextKey()); + } + + final ContextCapabilities caps = createContextCapabilities(); + _currentContext = new RenderContext(_context, caps, sharedContext); + + ContextManager.addContext(_context, _currentContext); + ContextManager.switchContext(_context); + + _renderer = new JoglRenderer(); + + if (settings.getSamples() != 0 && caps.isMultisampleSupported()) { + final GL gl = GLContext.getCurrentGL(); + gl.glEnable(GL.GL_MULTISAMPLE); + } + + _renderer.setBackgroundColor(ColorRGBA.BLACK); + + if (_camera == null) { + /** Set up how our camera sees. */ + _camera = new Camera(settings.getWidth(), settings.getHeight()); + _camera.setFrustumPerspective(45.0f, (float) settings.getWidth() / (float) settings.getHeight(), 1, + 1000); + _camera.setProjectionMode(ProjectionMode.Perspective); + + final Vector3 loc = new Vector3(0.0f, 0.0f, 10.0f); + final Vector3 left = new Vector3(-1.0f, 0.0f, 0.0f); + final Vector3 up = new Vector3(0.0f, 1.0f, 0.0f); + final Vector3 dir = new Vector3(0.0f, 0f, -1.0f); + /** Move our camera to a correct place and orientation. */ + _camera.setFrame(loc, left, up, dir); + } else { + // use new width and height to set ratio. + _camera.setFrustumPerspective(_camera.getFovY(), + (float) settings.getWidth() / (float) settings.getHeight(), _camera.getFrustumNear(), + _camera.getFrustumFar()); + } + } finally { + releaseCurrentContext(); + } + } + + public GLContext getContext() { + return _context; + } + + public void setContext(final GLContext context) { + _context = context; + } + + public int MAX_CONTEXT_GRAB_ATTEMPTS = 10; + + @MainThread + public boolean draw() { + + // set up context for rendering this canvas + makeCurrentContext(); + + // Enable Debugging if requested. + if (_useDebug != _debugEnabled) { + _context.setGL(new DebugGL2(_context.getGL().getGL2())); + _debugEnabled = true; + + LOGGER.info("DebugGL Enabled"); + } + + // render stuff, first apply our camera if we have one + if (_camera != null) { + if (Camera.getCurrentCamera() != _camera) { + _camera.update(); + } + _camera.apply(_renderer); + } + _renderer.clearBuffers(_frameClear); + + final boolean drew = _scene.renderUnto(_renderer); + _renderer.flushFrame(drew && _doSwap); + + // release the context if we're done (swapped and all) + if (_doSwap) { + releaseCurrentContext(); + } + + return drew; + } + + public Camera getCamera() { + return _camera; + } + + public Scene getScene() { + return _scene; + } + + public void setScene(final Scene scene) { + _scene = scene; + } + + public Renderer getRenderer() { + return _renderer; + } + + public void setCamera(final Camera camera) { + _camera = camera; + } + + public RenderContext getRenderContext() { + return _currentContext; + } + + public int getFrameClear() { + return _frameClear; + } + + public void setFrameClear(final int buffers) { + _frameClear = buffers; + } +} diff --git a/ardor3d-jogl/src/main/java/com/ardor3d/renderer/jogl/JoglTextureRendererProvider.java b/ardor3d-jogl/src/main/java/com/ardor3d/renderer/jogl/JoglTextureRendererProvider.java index b2d433c..a93e0cf 100644 --- a/ardor3d-jogl/src/main/java/com/ardor3d/renderer/jogl/JoglTextureRendererProvider.java +++ b/ardor3d-jogl/src/main/java/com/ardor3d/renderer/jogl/JoglTextureRendererProvider.java @@ -1,49 +1,49 @@ -/**
- * Copyright (c) 2008-2010 Ardor Labs, Inc.
- *
- * This file is part of Ardor3D.
- *
- * Ardor3D is free software: you can redistribute it and/or modify it
- * under the terms of its license which may be found in the accompanying
- * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
- */
-
-package com.ardor3d.renderer.jogl;
-
-import java.util.logging.Logger;
-
-import com.ardor3d.framework.DisplaySettings;
-import com.ardor3d.renderer.ContextCapabilities;
-import com.ardor3d.renderer.Renderer;
-import com.ardor3d.renderer.TextureRenderer;
-import com.ardor3d.renderer.TextureRendererProvider;
-
-public class JoglTextureRendererProvider implements TextureRendererProvider {
-
- private static final Logger logger = Logger.getLogger(JoglTextureRendererProvider.class.getName());
-
- public TextureRenderer createTextureRenderer(final int width, final int height, final Renderer renderer,
- final ContextCapabilities caps) {
- return createTextureRenderer(width, height, 0, 0, renderer, caps);
- }
-
- public TextureRenderer createTextureRenderer(final int width, final int height, final int depthBits,
- final int samples, final Renderer renderer, final ContextCapabilities caps) {
- return createTextureRenderer(new DisplaySettings(width, height, depthBits, samples), false, renderer, caps);
- }
-
- public TextureRenderer createTextureRenderer(final DisplaySettings settings, final boolean forcePbuffer,
- final Renderer renderer, final ContextCapabilities caps) {
- if (!forcePbuffer && caps.isFBOSupported()) {
- return new JoglTextureRenderer(settings.getWidth(), settings.getHeight(), settings.getDepthBits(), settings
- .getSamples(), renderer, caps);
- } else if (caps.isPbufferSupported()) {
- return new JoglPbufferTextureRenderer(settings, renderer, caps);
- } else {
- logger.severe("No texture renderer support (FBO or Pbuffer).");
- return null;
- }
-
- }
-
-}
+/** + * Copyright (c) 2008-2010 Ardor Labs, Inc. + * + * This file is part of Ardor3D. + * + * Ardor3D is free software: you can redistribute it and/or modify it + * under the terms of its license which may be found in the accompanying + * LICENSE file or at <http://www.ardor3d.com/LICENSE>. + */ + +package com.ardor3d.renderer.jogl; + +import java.util.logging.Logger; + +import com.ardor3d.framework.DisplaySettings; +import com.ardor3d.renderer.ContextCapabilities; +import com.ardor3d.renderer.Renderer; +import com.ardor3d.renderer.TextureRenderer; +import com.ardor3d.renderer.TextureRendererProvider; + +public class JoglTextureRendererProvider implements TextureRendererProvider { + + private static final Logger logger = Logger.getLogger(JoglTextureRendererProvider.class.getName()); + + public TextureRenderer createTextureRenderer(final int width, final int height, final Renderer renderer, + final ContextCapabilities caps) { + return createTextureRenderer(width, height, 0, 0, renderer, caps); + } + + public TextureRenderer createTextureRenderer(final int width, final int height, final int depthBits, + final int samples, final Renderer renderer, final ContextCapabilities caps) { + return createTextureRenderer(new DisplaySettings(width, height, depthBits, samples), false, renderer, caps); + } + + public TextureRenderer createTextureRenderer(final DisplaySettings settings, final boolean forcePbuffer, + final Renderer renderer, final ContextCapabilities caps) { + if (!forcePbuffer && caps.isFBOSupported()) { + return new JoglTextureRenderer(settings.getWidth(), settings.getHeight(), settings.getDepthBits(), settings + .getSamples(), renderer, caps); + } else if (caps.isPbufferSupported()) { + return new JoglPbufferTextureRenderer(settings, renderer, caps); + } else { + logger.severe("No texture renderer support (FBO or Pbuffer)."); + return null; + } + + } + +} |