PDA

View Full Version : Getting FBOs and shaders to work together...



bbliksted
03-18-2006, 01:20 AM
Hey...

I'm simply trying to do a fbo render-to-texture and using a shader in the same program (on separate objects). In seperate programs the fbo and shader works fine, but in the same program I'm having problems with the shader outputing only black pixels...


void setShaders(void)
{
// create, compile and link shader (works fine)

textureLocation0 = glGetUniformLocationARB(shaderProgram, "texture0"); // get link

glGenTextures(1, &texture[1]); // Creates the texture
glBindTexture(GL_TEXTURE_2D, texture[1]); // Binds the texture
glActiveTextureARB(GL_TEXTURE0_ARB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, pTexture); // inits the texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0); // Binds the texture
}

void InitRenderTexture(void)
{
glGenFramebuffersEXT(1, &frameBuffer); // Create 2 FBOs
glGenTextures(1, &texture[0]); // Create dynamic texture

// Setup bindings (fbo texture)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer); // Bind to FBO i
glBindTexture(GL_TEXTURE_2D, texture[0]); // Bind texture i
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture[0], 0); // Attach to buffer
}

void RenderTexture(void)
{
float color;

ViewOrtho(); // Set orthogonal projection mode
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
glViewport(0,0,TEXTURE_WIDTH, TEXTURE_HEIGHT); // Viewport
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); // render attachment 0

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);
color = sin(rot)/2+0.5f;
glBegin(GL_QUADS);
glColor3f(color, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, -0.0f);
glColor3f(color, 0.0f, 0.0f); glVertex3f(0.0f, TEXTURE_HEIGHT, -0.0f);
glColor3f(0.0f, 0.0f, 1.0f-color); glVertex3f(TEXTURE_WIDTH, TEXTURE_HEIGHT, -0.0f);
glColor3f(0.0f, 0.0f, 1.0f-color); glVertex3f(TEXTURE_WIDTH, 0.0f, -0.0f);
glEnd();
glFlush();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // Unbind fbo 0

// Set Perspective projection mode
ViewPerspective();
rot += 0.05f;
}

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
RenderTexture(); // Renders dynamic texture

glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glViewport(0,0,640,480); // Reset The Current Viewport

glTranslatef(0.0f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glColor3f(1.0f,1.0f,1.0f); // Set The Color To Violet

// shader applied on object 1 (doesn't work, object remains black)
glPushMatrix();
enableShader();
glUniform1iARB(textureLocation0, 0);

glTranslatef(1.2f,0.0f,0.0f); // Move to the right
glBegin(GL_QUADS); // Draw A Quad
glTexCoord2f( 0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord2f( 0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f( 1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glTexCoord2f( 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glEnd();
disableShaders();
glPopMatrix();

// fbo rendered texture applied on object 2 (works fine)
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(-1.2f,0.0f,0.0f); // Move to the left
glBindTexture(GL_TEXTURE_2D, texture[0]); // Bind dynamic texture 0
glBegin(GL_QUADS); // Draw A Quad
glTexCoord2f( 0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord2f( 0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f( 1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glTexCoord2f( 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}

// VS
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}

// FS
uniform sampler2D texture0;
void main()
{
vec4 texelColor0 = texture2D(texture0, gl_TexCoord[0].xy); // Get current color of texel
gl_FragColor = texelColor0;
}Any suggestions?

Komat
03-18-2006, 04:39 AM
Why do you not bind any texture during the "// shader applied on object 1 (doesn't work, object remains black)" part? If sampler without bound texture is sampled, color (0, 0, 0, 1) is returned.

bbliksted
03-18-2006, 05:43 AM
I see my problem now! Thank u...