Jump to content
  • Advertisement
Sign in to follow this  
carmellose

OpenGL render to texture and glClearColor problem

This topic is 3527 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 simple render to texture of a series of cubes. I believed it worked, but when i tryed to change the background color of the texture i saw it does not. Actually, i try to set the bg color to green in the texture, but it has absolutly no effect. here's my code (the relevant parts) :
void DrawObject(void) {

	int i;
	float z=3;
	/*
	 * 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);
	

	glDisable(GL_TEXTURE_2D);
	glEnable(GL_DEPTH_TEST);

        // ===> HEre i tryed to set the background to green .... no effect at all ?!?!??
 	glClearColor(0,1,0,1);
	/*
	 * draw objects here
	 */ 
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();

	gluPerspective(60,SX/(float)SY,z-.1f,100);

	glMatrixMode(GL_MODELVIEW); 

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushMatrix();

	glLoadIdentity();

	glTranslatef(3,0,-3);
	glRotatef(30,0,1,0);

#define NOBJECTS (6)
	for (i=0;i<NOBJECTS;++i) {

		glTranslatef(0,0,-z);

		glBegin(GL_QUADS);				
		glColor3f(0.0f,1.0f,0.0f);			
		glVertex3f( 1.0f, 1.0f,-1.0f);			
		glVertex3f(-1.0f, 1.0f,-1.0f);		
		glVertex3f(-1.0f, 1.0f, 1.0f);	
		glVertex3f( 1.0f, 1.0f, 1.0f);

		//etc for the rest of the faces ...
                // ...
                // ...

		glEnd();	
	}
#undef NOBJECTS
	
	//revert to previous modelview matrix
	glPopMatrix();
	//revert to previous projection matrix
	glMatrixMode(GL_PROJECTION); 
	glPopMatrix();
	//switch back to the modelview 
	glMatrixMode(GL_MODELVIEW);
	//
	glPopAttrib();

}


void RenderToTexture (void) {


        //FBO initialization ...
        // ...
        // ...

	/***************************************
	 * 	Render To Texture 
	 ***************************************/
	// (draw something here, rendering to texture)
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, myDBO);
	DrawObject();  
		
	//Bind 0, which means render to back buffer
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

}

/*
 * main routine to display openGL scene
 * */
void DrawGLScene(void) {

	RenderToTexture(); 

	/* Draw the scene as a texture */

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	
	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) {	

	glViewport(0,0,SX,SY);

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

	glClearColor(0, 1, 0, 0);  
	glClearDepth(1.0f);	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	return true;
}




what did i do wrong ? i don't get it! thanks in advance if anyone has ideas cheers

Share this post


Link to post
Share on other sites
Advertisement
The problem you're facing is quite simple. You don't render anything when clear the buffer. If you want a green background, disable depth testing, then render a single quad that will fill the entire screen, and that will render the green background you're looking for.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gorax
The problem you're facing is quite simple. You don't render anything when clear the buffer. If you want a green background, disable depth testing, then render a single quad that will fill the entire screen, and that will render the green background you're looking for.


hello,
does that mean i can't use glClearColor/glClear when rendering to a texture ??!?

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!