LWJGL Forum

Programming => OpenGL => Topic started by: Dramentiaras on June 15, 2013, 21:06:16

Title: Frame buffer texture problem
Post by: Dramentiaras on June 15, 2013, 21:06:16
Hi!

I'm making a 2D game and i realized i need to draw to a texture for one of the features. I searched the web for a bit and found that the best way to do this is using framebuffers. I followed the lwjgl wiki tutorial and it works all fine except everything i draw is scaled over the entire picture!
I got this rendering code:

Code (java) Select
glClearColor(0,0.5f,0.5f,0);
glClear(GL_COLOR_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, 0);

EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, frameBufferID);
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);
{
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glVertex2f(400, 0);
glVertex2f(400, 600);
glVertex2f(0, 600);

glColor3f(0, 1, 0);
glVertex2f(400, 0);
glVertex2f(800, 0);
glVertex2f(800, 600);
glVertex2f(400, 600);
}
glEnd();

EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, colorTextureID);

glBegin(GL_QUADS);
{

glVertex2f(0, 0); glTexCoord2f(0, 0);
glVertex2f(800, 0); glTexCoord2f(1, 0);
glVertex2f(800, 600); glTexCoord2f(1, 1);
glVertex2f(0, 600); glTexCoord2f(0, 1);

}
glEnd();

glDisable(GL_TEXTURE_2D);

Display.update();


The texture should consist of a red quad on the left and a green quad on the right. Instead the whole texture becomes green!

This is the code i use to initialize the texture and FBO:

Code (java) Select

                        IntBuffer buffer = ByteBuffer.allocateDirect(1 * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
EXTFramebufferObject.glGenFramebuffersEXT(buffer);
frameBufferID = buffer.get();

colorTextureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, colorTextureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);

EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, frameBufferID);
EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, colorTextureID, 0);
Title: Re: Frame buffer texture problem
Post by: quew8 on June 16, 2013, 09:38:10
Just because it is a framebuffer, doesn't mean you don't have to call glViewport, or set the correct projection matrix, or an identity modelview.

Also I don't think you understand your calls to glPushAttrib and glPopAttrib. Pushing the viewport bit stores things like the current viewport. Popping it again retrieves the viewport. So there is no need to do it if you are just going to call glViewport right after popping it.
Title: Re: Frame buffer texture problem
Post by: Dramentiaras on June 16, 2013, 10:09:34
Quote from: quew8 on June 16, 2013, 09:38:10
Just because it is a framebuffer, doesn't mean you don't have to call glViewport, or set the correct projection matrix, or an identity modelview.

Also I don't think you understand your calls to glPushAttrib and glPopAttrib. Pushing the viewport bit stores things like the current viewport. Popping it again retrieves the viewport. So there is no need to do it if you are just going to call glViewport right after popping it.

Gotcha! I was just following along with the lwjgl tutorial to get more of an understanding with framebuffers. According to the tutorial you should set the viewport as well as pushing and popping it.
I have now removed the glViewport() and glPushAttrib as well as glPopAttrib, however the problem persists. I'll edit the first post with the new code.
Title: Re: Frame buffer texture problem
Post by: quew8 on June 19, 2013, 10:55:22
What I meant for you to do was call glViewport after binding the framebuffer with the framebuffer's dimensions (ie glViewport(0, 0, framebufferWidth, framebufferHeight)). Then call it again after binding the default framebuffer with the values you had before ( or push the viewport bit after binding framebuffer, pop it after unbinding ).
Title: Re: Frame buffer texture problem
Post by: Dramentiaras on June 20, 2013, 17:39:14
Okey!

However my display and framebuffer are the same size is that still required?
Title: Re: Frame buffer texture problem
Post by: quew8 on June 21, 2013, 19:23:29
As long as you are talking about size in pixels then no it is not needed.

However you do still need the correct projection and modelview matrices, which looking at your code is not the identity matrix.
Title: Re: Frame buffer texture problem
Post by: Dramentiaras on June 29, 2013, 08:56:35
I'm really confused right now  ???. I have tried setting the viewport, pushing it then popping it, loading the identity matrix both before and after binding the framebuffer and nothing makes a visual difference. I am clueless right now.
Title: Re: Frame buffer texture problem
Post by: quew8 on June 29, 2013, 10:15:53
What you are doing wrong is your projection matrix. The quads you draw goes from 0 - 800 in both axes. The id matrix maps the viewport to a -1 - 1 range. You're going to need to call:


glOrtho(0, 800, 0, 800, -1, 1);


in the framebuffer pre-render code.