Jump to content
  • Advertisement
Sign in to follow this  
carmellose

OpenGL FBO and colors problem

This topic is 3492 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 guys, i managed to do a render to texture, but unfortunably i have problems with colors. I try to render three teapots into the FBO, but one of them is black whereas it should be red. I really don't know what i messed up. here's my code for the relevant parts: the main function where i initialize states:

//////////////////////////////////////////////////
//		MAIN  				//	
//////////////////////////////////////////////////
int main(int argc, char *argv[]) {

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA);
	glutInitWindowSize(SX, SY);
	glutCreateWindow("test FBO");

	/* GLEW INIT */
	glewInit();
	if (glewGetExtension("GL_EXT_framebuffer_object") != GL_TRUE)
	{
		printf("[!] FBO IS NOT SUPPORTED!\n"); 
		return EXIT_FAILURE;
	}

	GLInit();
	GLInitCam();

	//GLUT callbacks 
	glutDisplayFunc( DrawGLScene );

	glutMainLoop();
	return 0;
}



the opengl function where i try to render to texture:
//////////////////////////////////////////////////
//		GL FUNCTIONS			//	
//////////////////////////////////////////////////
void DrawObject(void) {
	/*
	 * The texture defined in a FBO carries its own
	 * viewport. So here we define the viewport and
	 * initilizations that needs to come with it.
	 */
	
//	glPushAttrib(GL_VIEWPORT_BIT);

	glViewport(0,0,SX, SY);
	/*
	 * draw objects here
	 */ 
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(-8,8,-8,8,-10,1000);  
	//	glFrustum(-10,10,-10,10,0.1f,1000);

	glMatrixMode(GL_MODELVIEW); 
	glPushMatrix();
	glLoadIdentity();

	glColor3f(1,0,0);  //====> Should be red ... it's black. How come ?
	glutSolidTeapot(1.0);

	glColor3f(0,1,0);
	glTranslatef(2,0,-2);
	glutSolidTeapot(1.0);

	glColor3f(0,1,1);
	glTranslatef(2,0,-2);
	glutSolidTeapot(1.0);

	glPopMatrix();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);

	//glPopAttrib();
}

void RenderToTexture (void) {

	static int i=0;
	/***************************************
	 * 		Deallocate previous buffers 
	 ***************************************/
	//Dettach, textureID = 0
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, 0/*textureID*/, 0);
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);

	glBindTexture(GL_TEXTURE_2D, 0);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
	
	glDeleteTextures(1,&TEX_SCENE_SOURCE_IMAGE);

	/***************************************
	 * 				Texture 
	 ***************************************/
	//texture initDrawGL
	glGenTextures(1, &TEX_SCENE_SOURCE_IMAGE);
	//initialize the texture to render to 
	glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE );
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 
			SX, SY, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

	/***************************************
	 * 			Frame Buffer Object 
	 ***************************************/
	glGenFramebuffersEXT(1, &myFBO);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
			GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE, 0);
	/***************************************
	 * 			Depth Buffer Object 
	 ***************************************/
	glGenRenderbuffersEXT(1, &myDBO);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, myDBO);
	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, SX, SY);
	//Attach depth buffer to the FBO
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, myDBO);
	/***************************************
	 * 		Check FBO Completeness 
	 ***************************************/
	if (i==0) {
		GLenum status = CHECK_FRAMEBUFFER_STATUS();
		switch(status) { 
			case GL_FRAMEBUFFER_COMPLETE_EXT:
				printf("framebuffer OK\n");
				break; 
			case GL_FRAMEBUFFER_UNSUPPORTED_EXT: 
				fprintf(stderr,"[!] FBO NOT SUPPORTED.\n"); 
				exit(EXIT_FAILURE);
				break; 
			default: 
				fprintf(stderr,"[!] FBO FAILED (programming error).\n"); 
				exit(EXIT_FAILURE);
		}
		++i;
	}
	/***************************************
	 * 			Render To Texture 
	 ***************************************/
	// (draw something here, rendering to texture)
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, myDBO);
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	DrawObject(); 
	//Bind 0, which means render to back buffer
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
	
	//Delete resources
	glDeleteFramebuffersEXT(1, &myFBO);
	glDeleteRenderbuffersEXT(1, &myDBO);

	glEnable(GL_TEXTURE_2D);
}
/*
 * main routine to display openGL scene
 * */
void DrawGLScene(void) {

	RenderToTexture(); 

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE);

	/* Draw the scene as a texture */

	glBegin(GL_QUADS);
	glTexCoord3f (0, 0, -1);
	glVertex3f	 (-1,1 , -1);
	glTexCoord3f (1, 0, -1);
	glVertex3f	 (1, 1, -1);
	glTexCoord3f (1, 1, -1);
	glVertex3f	 (1, -1, -1);
	glTexCoord3f (0, 1, -1);
	glVertex3f	 (-1, -1, -1);
	glEnd();
	
	glutSwapBuffers();
	return;	
}

/*
 * Init the GL runtime
 * */
bool GLInit(void) {

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* Tightly packed texture data. */
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glClearColor(0, 0, 1, 1);  /* Blue background */
	glClearDepth(1.0f);	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	return true;
}

/*
 * Set the camera and projection method
 */
bool GLInitCam(void) {

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1,1,-1,1,-10,1000);  
	glMatrixMode(GL_MODELVIEW);

	return true;
}

GLenum CHECK_FRAMEBUFFER_STATUS() {
	return glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); 
}


what's wrong with it ? thank you in advance

Share this post


Link to post
Share on other sites
Advertisement
When you draw with the fixed pipeline, even if you are using a texture, glColor has an effect. Every pixel will be calculated(I don't remember the formula) with the current color. Since the last color you used was (0,1,1), the red channel will not be drawn.


before you draw your FBO texture(at DrawGLScene) add glColor3f(1,1,1);

Share this post


Link to post
Share on other sites
Quote:
Original post by someboddy
When you draw with the fixed pipeline, even if you are using a texture, glColor has an effect. Every pixel will be calculated(I don't remember the formula) with the current color. Since the last color you used was (0,1,1), the red channel will not be drawn.


before you draw your FBO texture(at DrawGLScene) add glColor3f(1,1,1);


hello,
thank you SO much it works!

but i don't understand why exactly...openGL has so many tricky options to set!! Do you know where i could get some documentation relating to this ?

thank you

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!