PDA

View Full Version : Problem Texturing Quad with two textures



CelticDaddio
05-04-2006, 11:31 AM
I am trying to render to two RGB textures, Texture A and Texture B, using an FBO. I then want to take texture A and display it on the left half of a rectangular quad and display Texture B on the right half of the quad.

I initialize my FBO:


glGenFramebuffersEXT( 1, &g_FrameBuffer );
glGenRenderbuffersEXT( 1, &g_DepthRenderBuffer );

glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, g_DepthRenderBuffer );
glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, RENDERBUFFER_WIDTH, RENDERBUFFER_HEIGHT );

glGenTextures( 1, &g_ColorTextureA );
glBindTexture( GL_TEXTURE_2D, g_ColorTextureA );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, RENDERBUFFER_WIDTH, RENDERBUFFER_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, 0 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

glGenTextures( 1, &g_ColorTextureB );
glBindTexture( GL_TEXTURE_2D, g_ColorTextureB );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, RENDERBUFFER_WIDTH, RENDERBUFFER_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, 0 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );I render the scene to the FBO:


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_FrameBuffer);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_ColorTextureA, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, g_ColorTextureB, 0);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, g_DepthRenderBuffer);

GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT};
glDrawBuffers(2, drawBuffers);

glUseProgram(g_GenericShaderProgram);

RenderScene();

glUseProgram(0);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_ColorTextureA);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, g_ColorTextureB);My frag shader is simple:


void main( void )
{
gl_FragData[1] = vec4(1.0, 0.0, 0.0, 1.0);
gl_FragData[0] = vec4(0.0, 0.0, 1.0, 1.0);
}I use imdebug to view the textures after I render to the scene, and they appear just as I would expect, which is one is a blue torus, and one is a red torus. But when I texture the quad with them, I only see the blue torus. I know I must be missing something really simple.... What is it?

This is my code that textures the quad

// viewport for 1:1 pixel=texture mapping
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, g_nWindowWidth, 0.0, g_nWindowHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glViewport( 0, 0, g_nWindowWidth, g_nWindowHeight );
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Render Texture A On Left Half of Quad
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_ColorTextureB);
imdebugTexImage(GL_TEXTURE_2D, g_ColorTextureB);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(0.0, 0.0);
glTexCoord2f(1.0, 0.0);
glVertex2f((g_nWindowWidth/2.0), 0.0);
glTexCoord2f(1.0, 1.0);
glVertex2f((g_nWindowWidth/2.0), g_nWindowHeight);
glTexCoord2f(0.0, 1.0);
glVertex2f(0.0, g_nWindowHeight);
glEnd();

// Render Texture B On Right Half of Quad
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, g_ColorTextureB);

imdebugTexImage(GL_TEXTURE_2D, g_ColorTextureB);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f((g_nWindowWidth/2.0), 0.0);
glTexCoord2f(1.0, 0.0);
glVertex2f(g_nWindowWidth, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex2f(g_nWindowWidth, g_nWindowHeight);
glTexCoord2f(0.0, 1.0);
glVertex2f((g_nWindowWidth/2.0), g_nWindowHeight);
glEnd();

k_szczech
05-04-2006, 11:52 AM
First of all I don't see any glEnable(GL_TEXTURE_2D) in your code.
Next thing - you bind first texture to GL_TEXTURE0, and then second texture to GL_TEXTURE1. Do you want first texture to be visible on left quad and second texture on the right one? If yes, then why use multitexturing?
And one more - if you apply both textures to second quad, then multiplying blue torus by red torus always gives black colour (unless you have some white specular highlights on the torus). Either use only one texture on the second quad, or don't us GL_MODULATE as your texture mode for the second texture unit.

CelticDaddio
05-04-2006, 11:53 AM
Oops... The code that I use to texture the quad should have been:


// viewport for 1:1 pixel=texture mapping
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, g_nWindowWidth, 0.0, g_nWindowHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glViewport( 0, 0, g_nWindowWidth, g_nWindowHeight );
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Render Texture A On Left Half of Quad
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_ColorTextureA);
imdebugTexImage(GL_TEXTURE_2D, g_ColorTextureA);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(0.0, 0.0);
glTexCoord2f(1.0, 0.0);
glVertex2f((g_nWindowWidth/2.0), 0.0);
glTexCoord2f(1.0, 1.0);
glVertex2f((g_nWindowWidth/2.0), g_nWindowHeight);
glTexCoord2f(0.0, 1.0);
glVertex2f(0.0, g_nWindowHeight);
glEnd();

// Render Texture B On Right Half of Quad
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, g_ColorTextureB);

imdebugTexImage(GL_TEXTURE_2D, g_ColorTextureB);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f((g_nWindowWidth/2.0), 0.0);
glTexCoord2f(1.0, 0.0);
glVertex2f(g_nWindowWidth, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex2f(g_nWindowWidth, g_nWindowHeight);
glTexCoord2f(0.0, 1.0);
glVertex2f((g_nWindowWidth/2.0), g_nWindowHeight);
glEnd();

charliejay
05-05-2006, 02:03 AM
My best guess is this:

You're drawing a quad over the entire area in question, and it has a red torus on the left hand side. Then you draw a quad over the entire area again, and it has a blue torus on the right hand side, and black on the left hand side, which overwrites the red torus.

Try either drawing two half screen quads (even if the images are supposed to overlap), or just one quad specifiying texture coordinates using glMultiTexCoord.

Otherwise, something really odd may be happening, and the below are just random guesses which may produce results that *might* help to locate what the actual problem is.

From a very brief reading of the framebuffers spec, your fragment shader doesn't declare "ARB_draw_buffers", which seems to be needed to use gl_fragData[n].

Try:
Not using framebuffers.
Declaring ARB_draw_buffers in the fragment shader.
Confirming that the fragment shader is loading properly.
Not using the fragment shader.
Enabling both texture units.
Using only one (enabled) texture unit.

charliejay
05-05-2006, 02:15 AM
Oops, i'm stupid, scratch the idea about one fullscreen quad overwriting the other, your quads don't overlap.

The single quad with glMultiTexCoord still looks like a good idea, though. I think your first texture unit is enabled by default, and your second isn't, which will be proven or disproven by that...