So, I'm writing a convenience class for rendering to textures, and I've got a bit of a problem. Basically, I think that I start rendering to texture with the method I have in place, but I never stop, so my subsequent render operations are all to the texture and not to the screen. I've been looking over it for a while and I'm tired, so I figure I'll just post the lot and ask what I'm doing wrong.
Rendering to a texture is set up to actually be "recursive," you can begin rendering to another texture while drawing to another, and pick up where you left off. I haven't been able to do enough testing yet to see how well that's working, so I might be missing something there as well.
Here are the pertinent methods:
private static Stack<Integer> frameBuffersActive = new Stack<Integer>();
/** Begins drawing to the specified texture. */
public static boolean beginDrawingTo(Texture texture)
{
if(renderToTextureEnabled())
{
IntBuffer buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
EXTFramebufferObject.glGenFramebuffersEXT(buffer);
frameBuffersActive.push(buffer.get());
EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
frameBuffersActive.peek());
EXTFramebufferObject.glFramebufferTexture2DEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, texture.getTextureID(), 0);
checkFBO(frameBuffersActive.peek());
GL11.glPushMatrix();
GL11.glOrtho(0, texture.getImageWidth(), 0, -texture.getImageHeight(), -1, 1);
GL11.glLoadIdentity();
GL11.glTranslated(0, WindowManager.getScreenHeight() - texture.getImageHeight(), 0);
GL11.glTranslated(texture.getImageWidth() / 2, texture.getImageHeight() / 2, 0);
GL11.glScalef(1f, -1f, 1f);
return true;
}
return false;
}
public static void finishDrawing()
{
frameBuffersActive.pop();
if(!renderingToTexture())
{
EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
}
else
{
EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
frameBuffersActive.peek());
}
GL11.glPopMatrix();
}
public static void checkFBO(int FBOID)
{
int framebuffer = EXTFramebufferObject.glCheckFramebufferStatusEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT );
switch ( framebuffer )
{
case EXTFramebufferObject.GL_FRAMEBUFFER_COMPLETE_EXT:
break;
case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
throw new RuntimeException( "FrameBuffer: " + FBOID
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT exception" );
case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
throw new RuntimeException( "FrameBuffer: " + FBOID
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT exception" );
case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
throw new RuntimeException( "FrameBuffer: " + FBOID
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT exception" );
case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
throw new RuntimeException( "FrameBuffer: " + FBOID
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT exception" );
case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
throw new RuntimeException( "FrameBuffer: " + FBOID
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT exception" );
case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
throw new RuntimeException( "FrameBuffer: " + FBOID
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT exception" );
default:
throw new RuntimeException( "Unexpected reply from glCheckFramebufferStatusEXT: " + framebuffer );
}
}
[\code]
So, in short: what do I have to do to make this work like intended?