Hi,
I'm implementing a simple deferred rendering, and I have some troubles about it. I just use framebuffers and MRT in order to write into three float textures (using the ATI_texture_float or ARB_texture_float) : one for position, one for normal and one for materials. The first and second one works perfectly, but the third is strange. This is the unlit scene, rendered directly without any framebuffer, in direct mode :
http://www.monsterup.com/image.php?url=upload/1201557157.jpg
And here is the result in the texture framebuffer :
http://www.monsterup.com/image.php?url=upload/1201557047.jpg
As you can see, the colors aren't so "flashy". Here some code. Firstly, this is how I initialize the textures :
[source="cpp"]
// Tracking des couleurs
glEnable (GL_COLOR_MATERIAL);
glEnable (GL_TEXTURE);
glEnable (GL_TEXTURE_RECTANGLE_ARB);
// On créé un frambuffer
glGenFramebuffersEXT (1, &fbo);
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, fbo);
// On génère les textures
glGenTextures(1, &posTexture);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, posTexture);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA_FLOAT16_ATI, 800, 600, 0, GL_RGBA, GL_FLOAT, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, posTexture, 0);
glGenTextures(1, &normTexture);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, normTexture);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA_FLOAT16_ATI, 800, 600, 0, GL_RGBA, GL_FLOAT, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_RECTANGLE_ARB, normTexture, 0);
glGenTextures(1, &matTexture);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, matTexture);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA_FLOAT16_ATI, 800, 600, 0, GL_RGBA, GL_FLOAT, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_RECTANGLE_ARB, matTexture, 0);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status != GL_FRAMEBUFFER_COMPLETE_EXT)
std::cerr << "Erreur dans le framebuffer";
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, 0);
This is really simple code, and I don't think there's something wrong here.
And here is how I draw everything (the direct mode is just here for simplicity, furthemore I don't show how I apply the texture to the screen, I don't think there are errors in there since it works for normal and position textures) :
[source="cpp]// On charge la matrice identité
glLoadIdentity ();
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearDepth (1.0f);
glClearColor (0.4, 0.4, 0.4, 0.0);
// On place la caméra
gluLookAt (0.0f, 0.0f, 25.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glUseProgram (programDeferred);
GLenum buffers[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};
glDrawBuffers (3, buffers);
glDisable (GL_TEXTURE);
glDisable (GL_TEXTURE_RECTANGLE_ARB);
glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
glColor3f (0.8f, 0.8f, 0.8f);
glBegin (GL_QUADS);
// Mur du fond, gris
glNormal3f (0.0, 0.0, 1.0);
glVertex3f (-10.0f, -10.0f, 0.0f);
glVertex3f (10.0f, -10.0f, 0.0f);
glVertex3f (10.0f, 10.0f, 0.0f);
glVertex3f (-10.0f, 10.0f, 0.0f);
// Plafond, gris
glNormal3f (0.0, -1.0, 0.0);
glVertex3f (-10.0, 10.0, 0.0);
glVertex3f (10.0, 10.0, 0.0);
glVertex3f (10.0, 10.0, 10.0);
glVertex3f (-10.0, 10.0, 10.0);
// Sol, gris
glNormal3f (0.0, 1.0, 0.0);
glVertex3f (-10.0, -10.0, 0.0);
glVertex3f (10.0, -10.0, 0.0);
glVertex3f (10.0, -10.0, 10.0);
glVertex3f (-10.0, -10.0, 10.0);
glEnd ();
glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
glColor3f (0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
// Mur de droite, vert
glNormal3f (-1.0, 0.0, 0.0);
glVertex3f (10.0, -10.0, 0.0);
glVertex3f (10.0, 10.0, 0.0);
glVertex3f (10.0, 10.0, 10.0);
glVertex3f (10.0, -10.0, 10.0);
glEnd ();
glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
glColor3f (1.0f, 0.0f, 0.0f);
glBegin (GL_QUADS);
// Mur de gauche, rouge
glNormal3f (1.0, 0.0, 0.0);
glVertex3f (-10.0, -10.0, 0.0);
glVertex3f (-10.0, 10.0, 0.0);
glVertex3f (-10.0, 10.0, 10.0);
glVertex3f (-10.0, -10.0, 10.0);
glEnd ();
glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
glColor3f (0.5f, 0.5f, 0.5f);
gluSphere (sphere, 3, 20, 20);
//glUseProgram (0);
glUseProgram (0);
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
glEnable (GL_TEXTURE);
glEnable (GL_TEXTURE_RECTANGLE_ARB);
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, matTexture);
// Draw texture here
I had some troubles with glColorMaterial, as in the beginning, everything was black, and after reading the doc I realized that calling glColorMaterial from a glBegin/glEnd bloc is illegal, so I moved everything outside, and now I have colors, but those colors aren't good :/. The vertex and pixel shaders are quite easy :
fragment :
varying vec3 norm;
varying vec3 pos;
void main()
{
gl_FragData[0] = vec4 (pos.xyz, 1.0); // Position
gl_FragData[1] = vec4 (norm.xyz, 1.0); // Normale
gl_FragData[2] = gl_FrontMaterial.diffuse; // Materiel
}
vertex :
varying vec3 norm;
varying vec3 pos;
void main()
{
gl_Position = ftransform();
norm = normalize (gl_NormalMatrix * gl_Normal);
pos = vec3 (gl_ModelViewMatrix * gl_Vertex);
}
Can somebody help me ? I really don't know why it doesn't work.
NB : I have also try with glMaterial, it doesn't work more. I try also with ARB_texture_float instead of ATI_texture_float, same results, and try also ati_draw_buffers instead of arb_draw_buffers, same results, and finally I've tried to only draw into the pos texture (glDrawBuffer (GL_COLOR_ATTACHMENT2_EXT) ), but the colors are still wrong.
Thanks for your help !
[Edited by - Bakura on January 29, 2008 11:23:31 AM]