1
votes

The main steps for depth testing from my understanding: 1) enable depth testing and how we want to depth test 2) create the frame buffer object and make sure it has a depth attached to it 3) bind our frame buffer object ( make sure to clear it before rendering ) 4) draw stuff

And that should be it no? our frame buffer depth attachment should have depth data? But I always get straight 1's default depth clear color

step 1:

glEnable(GL_DEPTH_TEST);
glDepthFunc( GL_LEQUAL );

step 2:

//create the frame buffer object
glGenFramebuffers(1, &m_uifboHandle);

// Initialize FBO
glBindFramebuffer(GL_FRAMEBUFFER, m_uifboHandle);

//create 2 texture handles 1 for diffuse, 1 for depth
unsigned int m_uiTextureHandle[2];
glGenTextures( 2, m_uiTextureHandle );

//create the diffuse texture
glBindTexture( GL_TEXTURE_2D, m_uiTextureHandle[0]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, uiWidth, uiHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_uiTextureHandle[0], 0); 

.

//create the depth buffer
glBindTexture(GL_TEXTURE_2D, m_uiTextureHandle[1]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, uiWidth, uiHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_uiTextureHandle[1], 0);

//go back to default binding
glBindFramebuffer(GL_FRAMEBUFFER, 0);

step 3:

//bind the frame buffer object
glBindFramebuffer( GL_FRAMEBUFFER, m_uifboHandle );

//clear it
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

step 4:

//draw things

Are these not the steps?
Am i missing something?
I've tried a few different tutorials.
I can't get any depth to render to a texture I keep getting straight 1's over and over.

1
Try to add filtering parameters after binding your depth texture, just like you did after binding your diffuse texture.thp9
added it. still nothingFranky Rivera

1 Answers

0
votes

The framebuffer probably is not complete. Try checking for completeness. Moreover your code was:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, uiWidth, uiHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

However, it should be (watch the RGB-RGBA):

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, uiWidth, uiHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);