PDA

View Full Version : FBO problem



apapaxionga
03-26-2012, 07:54 AM
i render a scene including a teapot and ambient light .Without using fbo the whole scene seems ok!When i using fbo to render the scene into a texture,the whole scene become dark but the light still have effect. Why?


void InitFBO()
{
glGenFramebuffersEXT(1, &fb);
glGenTextures(1, &tex2);
glGenRenderbuffersEXT(1, &depth_rb);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);

// init texture
glBindTexture(texTarget, tex2);
glTexImage2D(texTarget, 0, texInternalFormat, texWidth, texHeight, 0,
GL_RGB, GL_FLOAT, NULL);

glTexParameterf(texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_GEN_MODE,GL_RE PLACE);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
texTarget, tex2, 0);

glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_rb);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, texWidth, texHeight);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, depth_rb);


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

void display()
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
glEnable(GL_LIGHT0);
CreateObject();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glEnable(GL_TEXTURE_2D);
glBindTexture(texTarget,tex2);

glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(1,0);
glVertex2f(1,0);
glTexCoord2f(1,1);
glVertex2f(1,1);
glTexCoord2f(0,1);
glVertex2f(0,1);
glEnd();
//glPopMatrix();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}

tonyo_au
03-26-2012, 09:19 PM
Where are you setting up the lighting parameters


glLightfv(GL_LIGHT0,GL_POSITION,light.pos); //updates the light's position
glLightfv(GL_LIGHT0,GL_DIFFUSE,light.diffuse); //updates the light's diffuse colour
glLightfv(GL_LIGHT0,GL_SPECULAR,light.specular); //updates the light's specular colour
glLightfv(GL_LIGHT0,GL_AMBIENT,light.ambient); //updates the light's ambient colour

apapaxionga
03-27-2012, 12:43 AM
i turn on the light in the initial stage, the problem now is that the light still works,but it is dark than the scene without using FBO.


void InitLight()
{
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0,GL_POSITION,position);
glEnable(GL_LIGHTING);

}
void init_opengl()
{
glewInit();
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective


InitFBO();
if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
printf("Texture Failed"); // If Texture Didn't Load Return FALSE
}
InitLight();
//glEnable(GL_TEXTURE_2D);
}

tonyo_au
03-27-2012, 12:55 AM
It might be the
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

it has to be done after you bind the frame buffer otherwise it just clears the back buffer.

I always set the colour as well but I don't think you have to.

thokra
03-27-2012, 01:33 AM
Have you checked the FBO for completeness?

apapaxionga
03-27-2012, 02:20 AM
well,i change the position of glClear,but it did not work.

apapaxionga
03-27-2012, 02:22 AM
Yes , i have tried that. The total code is here,the GPU part can be neglected.


#include <GL/glew.h>
#include <GL/glut.h>
#include<gl/GLaux.h>
#include <Cg/cg.h>
#include <Cg/cgGL.h>
#include<stdio.h>
#include<stdlib.h>
GLuint tex2;
GLuint fb,rboId;
GLuint depth_rb;
int texWidth =1024;
int texHeight =1024;
int screenWidth =1024;
int screenHeight=1024;
GLenum texInternalFormat = GL_RGB;
GLenum texTarget = GL_TEXTURE_2D;

CGcontext cg_context;
CGprofile cg_vprofile,cg_fprofile;
CGprogram vprog,fprog;

GLuint texture[1];
GLfloat zPlane[]={0.0f,0.0f,1.0f,0.0f};
GLfloat light_ambient[]={1.0f,0.8f,0.7f,1.0f};
GLfloat light_diffuse[]={1.0f,1.0f,1.0f,1.0f};
GLfloat light_specular[]={1.0f,1.0f,1.0f,1.0f};
GLfloat position[]={3.0f,3.0f,3.0f};
void EnableTexGen()
{
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
}
void CreateTexCoord()
{
glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEA R);
glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEA R);
glTexGenfv(GL_S,GL_OBJECT_PLANE,zPlane);
glTexGenfv(GL_T,GL_OBJECT_PLANE,zPlane);
}
void DisableTexGen()
{
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
}

AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle

if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}

File=fopen(Filename,"r"); // Check To See If The File Exists

if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}

return NULL; // If Load Failed Return NULL
}

int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator

AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("Data/Crate.bmp"))
{
Status=TRUE; // Set The Status To TRUE

glGenTextures(1, &amp;texture[0]); // Create The Texture

// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}

if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}

free(TextureImage[0]); // Free The Image Structure
}

return Status; // Return The Status
}

void cgErrorCallback() {
CGerror lastError = cgGetError();
if(lastError)
{
printf("%s\n", cgGetErrorString(lastError));
printf("%s\n", cgGetLastListing(cg_context));
exit(1);
}
}

void InitGPU()
{
cg_context = cgCreateContext();
cgSetErrorCallback(cgErrorCallback);

cg_vprofile = cgGLGetLatestProfile(CG_GL_VERTEX);
cg_fprofile = cgGLGetLatestProfile(CG_GL_FRAGMENT);

vprog = cgCreateProgramFromFile(cg_context, CG_SOURCE, "isosurf.cg", cg_vprofile, "SampleFieldVS", 0);
cgGLLoadProgram(vprog);

fprog = cgCreateProgramFromFile(cg_context, CG_SOURCE, "isosurf.cg", cg_fprofile, "MetaballPS", 0);
cgGLLoadProgram(fprog);
}

void InitFBO()
{
glGenFramebuffersEXT(1, &amp;fb);
glGenTextures(1, &amp;tex2);
glGenRenderbuffersEXT(1, &amp;depth_rb);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);

// init texture
glBindTexture(texTarget, tex2);
glTexImage2D(texTarget, 0, texInternalFormat, texWidth, texHeight, 0,
GL_RGB, GL_FLOAT, NULL);

glTexParameterf(texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_GEN_MODE,GL_RE PLACE);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
texTarget, tex2, 0);

glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_rb);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, texWidth, texHeight);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, depth_rb);


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void InitLight()
{
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
glLightfv(GL_LIGHT0,GL_POSITION,position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

}
void init_opengl()
{
glewInit();
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective


InitFBO();
//if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
//{
// printf("Texture Failed"); // If Texture Didn't Load Return FALSE
//}
InitLight();
glEnable(GL_TEXTURE_2D);
glMaterialfv(GL_FRONT,GL_DIFFUSE,light_diffuse);
//glEnable(GL_TEXTURE_2D);
}
void resize(int w,int h)
{
glViewport(0,0,screenHeight,screenWidth);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective(45,(float)w/float(h),0.1,100);
gluOrtho2D(0,1,0,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


}
void idle()
{
glutPostRedisplay();
}
void CreateObject()
{

//glEnable(GL_TEXTURE_2D);
//CreateTexCoord();
//EnableTexGen();
//glBindTexture(GL_TEXTURE_2D,texture[0]);
glPushMatrix();
glTranslatef(0.5,0.5,-0.8);
glutSolidTeapot(0.3);
glPopMatrix();

//DisableTexGen();
//glDisable(GL_TEXTURE_2D);
}
void ScreenAlignQuad()
{

glBindTexture(texTarget,tex2);

glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(1,0);
glVertex2f(1,0);
glTexCoord2f(1,1);
glVertex2f(1,1);
glTexCoord2f(0,1);
glVertex2f(0,1);
glEnd();
//glPopMatrix();
glBindTexture(texTarget,0);
//glDisable(GL_TEXTURE_2D);
}
void display()
{


glLoadIdentity();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
//InitLight();
//glEnable(GL_LIGHT0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
CreateObject();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

ScreenAlignQuad();
glFlush();
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&amp;argc, argv);
glutInitWindowSize(screenWidth,screenHeight);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutCreateWindow("cg_isosurf");

init_opengl();
glutDisplayFunc(display);
glutIdleFunc(idle);
glutReshapeFunc(resize);

glutMainLoop();

return 0;
}

apapaxionga
03-27-2012, 02:34 AM
it works!i should use glDisable(GL_LiGHTING) after rendering the teapot to the FBO. So the light will not affect the screenAlignQuad!
the display function should be like this:
void display()
{


glLoadIdentity();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
//InitLight();
glEnable(GL_LIGHTING);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
CreateObject();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glDisable(GL_LIGHTING);
ScreenAlignQuad();
glFlush();
glutSwapBuffers();
}

tonyo_au
03-27-2012, 04:15 AM
Pleased to here it