FBO and Lighting problem

Started by
2 comments, last by hayden 15 years, 4 months ago
Hi :) i'm a total noob programming FBO's and i'm trying to render a 3D scene using it. The scene, containing a ground, a torus and some spheres, has lighting and material properties. This is how the scene should be rendered, HERE And after using FBO this is the result, HERE So i suppose i'm doing something wrong about the lights while rendering. If someone could briefly check my code maybe you can say what's my critical error... i tried everything discover and had no results till now :S // Function SetupRC
void SetupRC()
    {
    int iSphere;
    	    
    // Grayish background
    glClearColor(fLowLight[0], fLowLight[1], fLowLight[2], fLowLight[3]);
         
    // Cull backs of polygons
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glEnable(GL_CULL_FACE);	
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    
    // Setup light parameters
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight);
    glLightfv(GL_LIGHT0, GL_AMBIENT, fLowLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    

   //code removed...

}
// Function InitFBO
void InitFBO(struct FBO *fbo, int width, int height)
{
	int i;
	GLenum status; 
	GLuint attachment;

	glGenFramebuffersEXT(1, &(fbo->frame));
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo->frame);

	glGenRenderbuffersEXT(1, &(fbo->depth));
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo->depth);
	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height); 
	

	glGenTextures(2, fbo->texid);
	for(i=0; i <2; i++){
		if (i == 0)
			attachment = GL_COLOR_ATTACHMENT0_EXT;
		else
			attachment = GL_COLOR_ATTACHMENT1_EXT;

		glBindTexture(GL_TEXTURE_2D, fbo->texid);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  WIN_WIDTH, WIN_HEIGHT, 0, GL_RGB, GL_FLOAT, NULL);
		
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
				
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachment, GL_TEXTURE_2D, fbo->texid, 0);

		
	}
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbo->depth);
	

	//code removed...

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
//Function RenderScene
void RenderScene(void)
{ 	

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_obj.frame);
    glPushAttrib(GL_VIEWPORT_BIT);
    glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(35.0f, (float)WIN_WIDTH/(float)WIN_HEIGHT, 1.0f, 50.0f);
    glMatrixMode(GL_MODELVIEW);

    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	

    glLoadIdentity();    
	
    //Drawing scene			
    glPushMatrix();
	           
        // Position light before any other transformations
        glLightfv(GL_LIGHT0, GL_POSITION, fLightPos);
        
        glColor3f(0.60f, .40f, .10f);
        DrawGround();
        
        // Draw inhabitants normally
        DrawInhabitants(0);

    glPopMatrix();
    	
    glPopAttrib();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

    glClearColor(0.0f, 0.0f, 0.2f, 0.5f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
    glLoadIdentity();
	
    glBindTexture(GL_TEXTURE_2D, fbo_obj.texid[0]);
    glEnable(GL_TEXTURE_2D);
	
    //Render the texture
    glPushMatrix();		
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glTranslatef(0.0f,0.0f,-1.0f); 
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);   glVertex2f(-1.0f, -1.0f);
	glTexCoord2f(1.0f, 0.0f);	glVertex2f(1.0f, -1.0f);
	glTexCoord2f(1.0f, 1.0f);	glVertex2f(1.0f, 1.0f);
	glTexCoord2f(0.0f, 1.0f);	glVertex2f(-1.0f, 1.0f);
	glEnd();
    glPopMatrix();

    glDisable(GL_TEXTURE_2D);
    glutSwapBuffers();
}
Thanks for your time and help :) hayden [Edited by - hayden on December 8, 2008 12:49:58 PM]
Advertisement
From the look of the pictures it seems like the rendering works fine.
the most likely reason that the image is darkened is because you haven't changed the way the texture is applied to the quad, i think the default is GL_MODULATE but you need to set it to GL_REPLACE (or GL_DECAL)... search for glTexEnv
yes... it works :)

Today I have almost the same problem :)

Ofcourse this problem does not occur when using shaders, it is only in fixed pipeline mode.

Se my programming blog: Code and Graphics

Quote:Original post by areddevil
From the look of the pictures it seems like the rendering works fine.
the most likely reason that the image is darkened is because you haven't changed the way the texture is applied to the quad, i think the default is GL_MODULATE but you need to set it to GL_REPLACE (or GL_DECAL)... search for glTexEnv


Thanks for this info areddevil, didn't know about that...
I put glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); together with glTexParameteri in fbs and textures setup.

Now i'm getting a perfect rendering but only the first rendered frame. If the window is refreshed it goes totally blank with background color. I can't find the reason for this, it must be pretty simple :S

//Function RenderScene
void RenderScene(void){ 	    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_obj.frame);    glActiveTexture(GL_TEXTURE0);    glPushAttrib(GL_VIEWPORT_BIT);    glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluPerspective(35.0f, (float)WIN_WIDTH/(float)WIN_HEIGHT, 1.0f, 50.0f);    glMatrixMode(GL_MODELVIEW);    //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer    glLoadIdentity();    	glPushMatrix();	                   // Position light before any other transformations        glLightfv(GL_LIGHT0, GL_POSITION, fLightPos);                glColor3f(0.60f, .40f, .10f);        DrawGround();                // Draw inhabitants normally        DrawInhabitants(0);    glPopMatrix();    glPopAttrib();        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);glActiveTexture(GL_TEXTURE0);    //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	        glLoadIdentity();    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();	    glBindTexture(GL_TEXTURE_2D, fbo_obj.texid[0]);	    //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);    glPushMatrix();        glTranslatef(0.0f,0.0f,-1.0f);         glBegin(GL_QUADS);	    glTexCoord2f(0.0f, 0.0f);   glVertex2f(-1.0f, -1.0f);	    glTexCoord2f(1.0f, 0.0f);	glVertex2f(1.0f, -1.0f);	    glTexCoord2f(1.0f, 1.0f);	glVertex2f(1.0f, 1.0f);	    glTexCoord2f(0.0f, 1.0f);	glVertex2f(-1.0f, 1.0f);        glEnd();    glPopMatrix();    glutSwapBuffers();}



Can someone help?


Thanks for your time and help :)
hayden

This topic is closed to new replies.

Advertisement