boolean offScreen) {
if (VERBOSE) System.err.println("JoglPipeline.createNewContext()");
checkAppContext();
GLDrawable glDrawable = null;
GLContext glContext = null;
if (offScreen) {
glDrawable = drawable(cv.drawable); // cv.drawable != null, set in 'createOffScreenBuffer'
glContext = glDrawable.createContext(context(shareCtx));
}
else {
// determined in 'getBestConfiguration'
GraphicsConfigInfo gcInf0 = Canvas3D.graphicsConfigTable.get(cv.graphicsConfiguration);
AWTGraphicsConfiguration awtConfig = (AWTGraphicsConfiguration)gcInf0.getPrivateData();
// JAWTWindow
JAWTWindow nativeWindow = (JAWTWindow)NativeWindowFactory.getNativeWindow(cv, awtConfig);
nativeWindow.lockSurface();
try {
glDrawable = GLDrawableFactory.getFactory(profile).createGLDrawable(nativeWindow);
glContext = glDrawable.createContext(context(shareCtx));
}
finally {
nativeWindow.unlockSurface();
}
cv.drawable = new JoglDrawable(glDrawable, nativeWindow);
}
// assuming that this only gets called after addNotify has been called
glDrawable.setRealized(true);
// Apparently we are supposed to make the context current at this point
// and set up a bunch of properties
// Work around for some low end graphics driver bug, such as Intel Chipset.
// Issue 324 : Lockup J3D program and throw exception using JOGL renderer
boolean failed = false;
int failCount = 0;
int MAX_FAIL_COUNT = 5;
do {
failed = false;
int res = glContext.makeCurrent();
if (res == GLContext.CONTEXT_NOT_CURRENT) {
// System.err.println("makeCurrent fail : " + failCount);
failed = true;
++failCount;
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
}
}
} while (failed && (failCount < MAX_FAIL_COUNT));
if (failCount == MAX_FAIL_COUNT) {
throw new IllegalRenderingStateException("Unable to make new context current after " + failCount + "tries");
}
GL2 gl = glContext.getGL().getGL2();
JoglContext ctx = new JoglContext(glContext);
try {
if (!getPropertiesFromCurrentContext(ctx, gl)) {
throw new IllegalRenderingStateException("Unable to fetch properties from current OpenGL context");
}
if(!isSharedCtx){
// Set up fields in Canvas3D
setupCanvasProperties(cv, ctx, gl);
}
// Enable rescale normal
gl.glEnable(GL2.GL_RESCALE_NORMAL);
gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glEnable(GL2.GL_COLOR_MATERIAL);
/*
OpenGL specs:
glReadBuffer specifies a color buffer as the source for subsequent glReadPixels.
This source mode is initially GL_FRONT in single-buffered and GL_BACK in double-buffered configurations.
We leave this mode unchanged in on-screen rendering and adjust it in off-screen rendering. See below.
*/
// gl.glReadBuffer(GL_FRONT); // off window, default for single-buffered non-stereo window
// Issue 417: JOGL: Mip-mapped NPOT textures rendered incorrectly
// J3D images are aligned to 1 byte
gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
// Workaround for issue 400: Enable separate specular by default
gl.glLightModeli(GL2.GL_LIGHT_MODEL_COLOR_CONTROL, GL2.GL_SEPARATE_SPECULAR_COLOR);
// Mac OS X / JRE 7 : onscreen rendering = offscreen rendering
// bind FBO
if (!offScreen && glDrawable instanceof GLFBODrawable) {
GLFBODrawable fboDrawable = (GLFBODrawable)glDrawable;
// bind GLFBODrawable's drawing FBObject
// GL_BACK returns the correct FBOObject for single/double buffering, incl. multisampling
fboDrawable.getFBObject( GL.GL_BACK ).bind(gl);
}
// FBO or pbuffer
if (offScreen) {
// Final caps
GLCapabilitiesImmutable chosenCaps = glDrawable.getChosenGLCapabilities();
// FBO
if (glDrawable instanceof GLFBODrawable) {
GLFBODrawable fboDrawable = (GLFBODrawable)glDrawable;
// bind GLFBODrawable's drawing FBObject
// GL_BACK returns the correct FBOObject for single/double buffering, incl. multisampling
fboDrawable.getFBObject( GL.GL_BACK ).bind(gl);
}
// pbuffer
else {
// Double buffering: read from back buffer, as we don't swap
// Even this setting is identical to the initially mode it is set explicitly
if (chosenCaps.getDoubleBuffered()) {
gl.glReadBuffer(GL.GL_BACK);
}
else {
gl.glReadBuffer(GL.GL_FRONT);
}
}
}
}
finally {
glContext.release();
}
return ctx;
}