PDA

View Full Version : Culling error? Deferred Shading (Geometry Stage)



Prompt
06-24-2009, 02:06 AM
Hi there! :)

I'm developing a Deferred Shading and when I show my 4 buffers appear to be a problem with culling.

drawMaterial only:
http://img31.imageshack.us/img31/5214/drawp.th.png (http://img31.imageshack.us/img31/5214/drawp.png)

And my FBOs:
http://img8.imageshack.us/img8/3774/buffers.th.png (http://img8.imageshack.us/img8/3774/buffers.png)

drawMaterial code (image 1)


//*///-----------------------------------------------------------------------------------------
void DeferredShader::drawMaterial( )
{
glViewport( 0, 0, m_width, m_height);

GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT/*, GL_DEPTH_ATTACHMENT_EXT*/ };
glDrawBuffers( 4, buffers );
checkFBO( );

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

glEnable(GL_DEPTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glDepthRange(0.0, 1.0); // The default z mapping
glStencilMask(GL_FALSE);

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);

glCullFace(GL_BACK);

//glShadeModel(GL_SMOOTH);
//glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D);

glEnable(GL_ALPHA_TEST);
glDepthMask(GL_TRUE);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glPushMatrix( );
// draw raw scene geometry
m_pMRTShader->setUp( m_pMRTShader->getConfiguration( ) );
m_pMRTShader->beginTextures( );

m_pMRTShader->begin( );

// Formato para el shader
static Entity3D xForm;
const Matrix4 mEffectFormat = xForm.getWorldMatrix( ).transpose( );

glMultiTexCoord4fv(GL_TEXTURE1, mEffectFormat[0]);
glMultiTexCoord4fv(GL_TEXTURE2, mEffectFormat[1]);
glMultiTexCoord4fv(GL_TEXTURE3, mEffectFormat[2]);
glutSolidTeapot( 2000.0f );

m_pMRTShader->endTextures( );
m_pMRTShader->end( );
glPopMatrix( );
}


show FBOs source code (image 2):


//*///-----------------------------------------------------------------------------------------
void DeferredShader::showTest( )
{
glViewport(0, 0, m_width, m_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0.0, 1.0, 1.0, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

m_pFBOTestShader->begin( );
m_pFBOTestShader->setUniform1i("FBO", 0);
glActiveTexture(GL_TEXTURE0);

glBindTexture(GL_TEXTURE_2D, m_mrtTexture[0]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex2f(0.0f, 0.0f);
glTexCoord2i(1, 1); glVertex2f(0.5f, 0.0f);
glTexCoord2i(1, 0); glVertex2f(0.5f, 0.5f);
glTexCoord2i(0, 0); glVertex2f(0.0f, 0.5f);
glEnd( );

glBindTexture(GL_TEXTURE_2D, m_mrtTexture[1]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex2f(0.5f, 0.0f);
glTexCoord2i(1, 1); glVertex2f(1.0f, 0.0f);
glTexCoord2i(1, 0); glVertex2f(1.0f, 0.5f);
glTexCoord2i(0, 0); glVertex2f(0.5f, 0.5f);
glEnd( );

glBindTexture(GL_TEXTURE_2D, m_mrtTexture[2]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex2f(0.0f, 0.5f);
glTexCoord2i(1, 1); glVertex2f(0.5f, 0.5f);
glTexCoord2i(1, 0); glVertex2f(0.5f, 1.0f);
glTexCoord2i(0, 0); glVertex2f(0.0f, 1.0f);
glEnd( );

glBindTexture(GL_TEXTURE_2D, m_mrtTexture[3]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex2f(0.5f, 0.5f);
glTexCoord2i(1, 1); glVertex2f(1.0f, 0.5f);
glTexCoord2i(1, 0); glVertex2f(1.0f, 1.0f);
glTexCoord2i(0, 0); glVertex2f(0.5f, 1.0f);
glEnd( );

m_pFBOTestShader->end( );
}

I test some combinations like, disable culling, change render CW, CCW and nothing appear to run ok.

Thanks in advance.

ZbuffeR
06-24-2009, 03:09 AM
/*, GL_DEPTH_ATTACHMENT_EXT*/

You explicitely commented the depth buffer here, are your sure you wanted that ?

Prompt
06-24-2009, 03:26 AM
Ok, this solve the problem. But then I have other :p

http://img31.imageshack.us/img31/2509/depthattach.th.png (http://img31.imageshack.us/img31/2509/depthattach.png)


// create depth texture
glGenTextures( 1, &m_mrtTexture[3] );
glBindTexture( GL_TEXTURE_2D, m_mrtTexture[3] );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_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_DEPTH_COMPONENT24, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0 );
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, m_mrtTexture[3], 0 );
checkFBO( );


// initialize depth renderbuffer
glGenRenderbuffersEXT( 1, &m_depthFBO );
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, m_depthFBO );
glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, m_width, m_height );
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_depthFBO );
checkFBO( );

with: GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_DEPTH_ATTACHMENT_EXT/*GL_COLOR_ATTACHMENT3_EXT*/ };

I'm doing something wrong?
With GL_COLOR_ATTACHMENT3_EXT:


glGenTextures( 1, &m_mrtTexture[3] );
glBindTexture( GL_TEXTURE_2D, m_mrtTexture[3] );
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_RGBA16F, m_width, m_height, 0, GL_RGBA, GL_FLOAT, NULL );
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT3_EXT , GL_TEXTURE_2D, m_mrtTexture[3], 0 );
checkFBO( );

Prompt
06-24-2009, 03:28 AM
PS: with DEPTH ATTACH I commented the code in my GLSL for gl_FragData[3] to don't override source.

Thanks! :)

Prompt
06-24-2009, 05:00 AM
Now is solved :D

With glDrawBuffers is not possible to do GL_DEPTH_ATTACHMENT_EXT, I have problem with that.

Finally:


glGenTextures( 1, &m_mrtTexture[3] );
glBindTexture( GL_TEXTURE_2D, m_mrtTexture[3] );
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_RGBA16F, m_width, m_height, 0, GL_RGBA, GL_FLOAT, NULL );
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT3_EXT , GL_TEXTURE_2D, m_mrtTexture[3], 0 );
checkFBO( );
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

// create a render buffer as our depthbuffer and attach it
glGenRenderbuffersEXT( 1, &m_depthFBO );
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_depthFBO);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, m_width, m_height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_depthFBO);


And:


GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT };
glDrawBuffers( 4, buffers );


In my shader I have this code for to see depth values:


float d =((zNear * vpos.z + zFar) / -vpos.z); // detph
gl_FragData[3] = float_to_color(d);


Thanks for all ^^

http://img3.imageshack.us/img3/3370/depthsolved.th.png (http://img3.imageshack.us/img3/3370/depthsolved.png)