Last active
April 29, 2016 11:11
-
-
Save davidbilge/d19f5fbcc5650ba4432ecf30467c5047 to your computer and use it in GitHub Desktop.
Modifications to JoglOffscreenBuffer to remedy some problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.atomic.AtomicBoolean; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import com.jme3.input.JoyInput; | |
import com.jme3.input.KeyInput; | |
import com.jme3.input.MouseInput; | |
import com.jme3.input.TouchInput; | |
import com.jme3.input.dummy.DummyKeyInput; | |
import com.jme3.input.dummy.DummyMouseInput; | |
import com.jme3.system.AppSettings; | |
import com.jme3.system.jogl.JoglContext; | |
import com.jogamp.opengl.GLCapabilities; | |
import com.jogamp.opengl.GLContext; | |
import com.jogamp.opengl.GLDrawableFactory; | |
import com.jogamp.opengl.GLOffscreenAutoDrawable; | |
import com.jogamp.opengl.GLProfile; | |
import com.jogamp.opengl.JoglVersion; | |
public class CustomJoglOffscreenBuffer extends JoglContext implements Runnable { | |
private static final Logger logger = Logger.getLogger(CustomJoglOffscreenBuffer.class.getName()); | |
private GLOffscreenAutoDrawable offscreenDrawable; | |
protected AtomicBoolean needClose = new AtomicBoolean(false); | |
private GLCapabilities caps; | |
protected void initInThread() { | |
int samples = getNumSamplesToUse(); | |
caps = new GLCapabilities(GLProfile.getMaxFixedFunc(true)); | |
caps.setHardwareAccelerated(true); | |
caps.setDoubleBuffered(true); | |
caps.setStencilBits(settings.getStencilBits()); | |
caps.setDepthBits(settings.getDepthBits()); | |
caps.setOnscreen(false); | |
caps.setSampleBuffers(true); | |
caps.setNumSamples(samples); | |
offscreenDrawable = GLDrawableFactory.getFactory(GLProfile.getMaxFixedFunc(true)).createOffscreenAutoDrawable( | |
null, caps, null, settings.getWidth(), settings.getHeight()); | |
offscreenDrawable.display(); | |
GLContext context = offscreenDrawable.getContext(); | |
context.makeCurrent(); | |
System.out.println("OpenGL version is " + GLContext.getCurrent().getGLVersionNumber().toString()); | |
// GL gl = GLContext.getCurrentGL(); | |
// if (!gl.hasFullFBOSupport()) { | |
// logger.severe("Offscreen surfaces are not supported."); | |
// return; | |
// } | |
renderable.set(true); | |
logger.fine("Offscreen buffer created."); | |
settings.setRenderer(AppSettings.JOGL_OPENGL_BACKWARD_COMPATIBLE); | |
super.internalCreate(); | |
listener.initialize(); | |
} | |
protected boolean checkGLError() { | |
// FIXME | |
// NOTE: Always return true since this is used in an "assert" statement | |
return true; | |
} | |
protected void runLoop() { | |
if (!created.get()) { | |
throw new IllegalStateException(); | |
} | |
listener.update(); | |
checkGLError(); | |
renderer.postFrame(); | |
int frameRate = settings.getFrameRate(); | |
if (frameRate >= 1) { | |
// FIXME | |
} | |
} | |
protected void deinitInThread() { | |
renderable.set(false); | |
listener.destroy(); | |
renderer.cleanup(); | |
offscreenDrawable.getNativeSurface().unlockSurface(); | |
offscreenDrawable.destroy(); | |
logger.fine("Offscreen buffer destroyed."); | |
super.internalDestroy(); | |
} | |
@Override | |
public void run() { | |
logger.log(Level.FINE, "Using JOGL {0}", JoglVersion.getInstance().getImplementationVersion()); | |
initInThread(); | |
while (!needClose.get()) { | |
runLoop(); | |
} | |
deinitInThread(); | |
} | |
@Override | |
public void destroy(boolean waitFor) { | |
needClose.set(true); | |
if (waitFor) { | |
waitFor(false); | |
} | |
} | |
@Override | |
public void create(boolean waitFor) { | |
if (created.get()) { | |
logger.warning("create() called when pbuffer is already created!"); | |
return; | |
} | |
new Thread(this, THREAD_NAME).start(); | |
if (waitFor) { | |
waitFor(true); | |
} | |
} | |
@Override | |
public void restart() { | |
} | |
@Override | |
public void setAutoFlushFrames(boolean enabled) { | |
} | |
@Override | |
public Type getType() { | |
return Type.OffscreenSurface; | |
} | |
@Override | |
public MouseInput getMouseInput() { | |
return new DummyMouseInput(); | |
} | |
@Override | |
public KeyInput getKeyInput() { | |
return new DummyKeyInput(); | |
} | |
@Override | |
public JoyInput getJoyInput() { | |
return null; | |
} | |
@Override | |
public TouchInput getTouchInput() { | |
return null; | |
} | |
@Override | |
public void setTitle(String title) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment