Render to FBO with correct z Depth

I am trying to render a 3D object to an FBO. I use a shader to render the object, and it is rendered, but in the order it was drawn. Is there a way to account for the correct z depths so that the further vertexes do not render over the closer ones, even though they were drawn second? Here is a simple example image to explain the problem.

The green circle is drawn at a z depth of 100 and the red circle is at zero, so that green circle should be in front of the red circle. In this simple example it would be easy to just sort on the z value, but not sure how to approach the problem with a more complex 3D object. Any help or direction is greatly appreciated.

  
  
glEnable(GL_DEPTH_TEST);  
	fbo.begin();  
	ofClear(0, 0, 0, 1);  
	glScalef(.25, .25, .25);  
	  
	glPushMatrix();  
	glTranslatef(0, 0, 100);  
	ofSetColor(100, 255, 20);  
	ofCircle(mouseX, mouseY, 60);  
	glPopMatrix();  
	  
	ofSetColor(255, 0, 0);  
	ofCircle(ofGetWidth()*.5, ofGetHeight()*.5, 200);  
	  
	fbo.end();  
	glScalef(1.f, 1.f, 1.f);  
	glDisable(GL_DEPTH_TEST);  
	  
	ofSetColor(255, 255, 255);  
	fbo.draw(10, 10);  
  

Thanks!

Hi

I got same problem.

My ofxFBOTexture addon bind depth renderbuffers before binding framebuffer.

so bind framebuffer first

  
  
	// create & setup FBO  
	glGenFramebuffersEXT(1, &fbo);  
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);	  
  

then bind depth renderbuffer to it

  
  
	//THEO Create the render buffer for depth  
	glGenRenderbuffersEXT(1, &depthBuffer);  
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer);  
	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, texData.tex_w, texData.tex_h);  
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer);  
  

If you are using your own fbo class,
probably you need to bind depth renderbuffer to your fbo…

Hope this help :slight_smile:

Akira

Thanks. That was the problem. :slight_smile:

thanks, that also worked for me.