Sign in to follow this  

Framebuffer

This topic is 3596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have trouble displaying a texture onto the screen. I attached two outbound textures to the framebuffer (GLSL). Both the textures are populated with color (1,0,0,1) in the fragment shader. I read both the buffers through a call to glReadPixels and both the buffers have the correct values. But when I display any texture onto the screen, I see wrong images. I see a very dark image, seems like color (0.01, 0.0, 0.0). The following is my code
        glGenTextures(1, &txLightAndNormals1);
	glGenTextures(1, &txLightAndNormals2);

	glGenFramebuffersEXT(1, &fbLightAndNormals);


        glActiveTexture(GL_TEXTURE0);

	glBindTexture(GL_TEXTURE_2D, txLightAndNormals1);				
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 
		fbWidth, fbHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

	glBindTexture(GL_TEXTURE_2D, txLightAndNormals2);				
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 
		fbWidth, fbHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);


	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbLightAndNormals);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, 
		GL_TEXTURE_2D, txLightAndNormals1, 0);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, 
		GL_TEXTURE_2D, txLightAndNormals2, 0);
	glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);
	GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT,GL_COLOR_ATTACHMENT1_EXT};
	glDrawBuffers(2, buffers);
	glPopAttrib();

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);


// in the display func

        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbLightAndNormals);
	glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);
	GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT,GL_COLOR_ATTACHMENT1_EXT};
	glDrawBuffers(2, buffers);

	glClearColor(1, 1, 1, 1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glUseProgram(meshLight);
	
	for( unsigned int i=0; i< stlMesh.size(); i++)
	{
		dummy = stlMesh[i]->getNormal();

		stlMesh[i]->getVertex( 1, dummy1 );
		stlMesh[i]->getVertex( 2, dummy2 );
		stlMesh[i]->getVertex( 3, dummy3 );
		
		glBegin( GL_TRIANGLES );	
			glNormal3f( dummy.x, dummy.y, dummy.z );

			glVertex3f( dummy1.x, dummy1.y, dummy1.z );
			glVertex3f( dummy2.x, dummy2.y, dummy2.z );
			glVertex3f( dummy3.x, dummy3.y, dummy3.z );
		glEnd();
	}
	glPopAttrib();


	// render image to screen
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glViewport(0, 0, fbWidth, fbHeight);

	glUseProgram(0);

	glClearColor(1, 1, 1, 1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, txLightAndNormals1);
	glEnable(GL_TEXTURE_2D);

	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex2f(-1, -1);
	glTexCoord2f(0, 1); glVertex2f(-1,  1);
	glTexCoord2f(1, 1); glVertex2f( 1,  1);
	glTexCoord2f(1, 0); glVertex2f( 1, -1);
	glEnd();
	
	glFlush();



If someone can point me to error in my program best wishes

Share this post


Link to post
Share on other sites
Well the code looks ok to me, one minor thing is that you draw the quad as clockwise (OpenGL default is counter-clockwise) so you're displaying it's back face. Be sure to disable lighting (and any other state affecting the texturing) before you draw the quad, otherwise i see no problem.

Share this post


Link to post
Share on other sites
glBegin/End is slow, but the way you're doing it is going to be really slow. If you pull the glBegin and glEnd outside the loop so you only call them once per mesh (rather than once per triangle) you'll likely get much better performance. (Although obviously vertex arrays or vbos are likely to be better still).

Also, check that you've not got an old glColor*() call lying around or you'll end up tinting your polys with that.

Share this post


Link to post
Share on other sites
thanks for the help.

The default functionality is over written and the application works clock wise, so that does not seem to be a problem.

I have moved glbegin and glend outside the loop. thanks for the tip.

But the actual problem persists :(. No old glcolor statements lying around either

regards

Share this post


Link to post
Share on other sites
If you read the textures with glGetTexImage will you also get correct values ? Otherwise asides from lighting and glcolor, you may have left other texture unit active, or blending may be in effect etc. ..

Share this post


Link to post
Share on other sites

This topic is 3596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this