Jump to content
  • Advertisement
Sign in to follow this  

OpenGL How to add primitives to texture with occlusion testing?

This topic is 1201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my first post here, sorry if this is the wrong place to ask.


I am relatively new to opengl, but have to use an old gl implementation which applies filtered blending to a given object.
The result I get from this implementation is a texture drawn onto a screen quad. This texture has some black (untextured) "holes" in it.
Therefore I rendered my model with a nice phong shading into the attached texture of a FBO (I also attached a renderbuffer for depth).
Then I wrote a shader which gets the greyshading texture and the "black-hole"-texture (which I put into another FBOs texture) to combine them and fill the holes with the grey shading texture.

So now I have the wanted texture.
This works fine so far, but my next task is that I want to add primitives (rectangles...) to the scene.
The problem here is that I need occlusion testing for those primitives to determine where they intersect with my previously rendered object.
But since I only have a texture with no depth values at this moment, I wonder where and how I could get the depth values from and how to do the occlusion/depth-testing.

I thought about taking the depth values from the grey shading FBO since the model was rendered there with a renderbuffer depth attachment.
Sadly I did not understand so far, how this attached renderbuffer works, and if there really are depth values in it which I could use for further rendering.

Since I am not totally confident in my opengl skills and how this FBO of mine works with the depth attachment, I want to show you the rough outline of the code I have right now. 
My greyshading FBO is created like this:
  //the FBO
  glGenFramebuffers(1, &mGreyShadingFBO);
  glBindFramebuffer(GL_FRAMEBUFFER, mGreyShadingFBO);

  //greyshading texture
  glGenTextures(1, &mGreyShadingTex);
  glBindTexture(GL_TEXTURE_2D, mGreyShadingTex);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 640, 480, 0, GL_RGB, GL_FLOAT, 0);
  glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mGreyShadingTex, 0);

  //greyshading depth buffer
  glGenRenderbuffers(1, &mGreyShadingRB);
  glBindRenderbuffer(GL_RENDERBUFFER, mGreyShadingRB);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 640, 480);
  // Set the list of draw buffers
  GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
  glDrawBuffers(1, DrawBuffers);
I got most of this code from http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/
but since I have a relatively old ati graphics card I had to adjust some parts of the code.
In that tutorial they talk about the depth values a bit, but I just can not figure out how to get them in the first place and then how to get them into my a final FBO with my combined texture to add those primitives...
Do I just have to attach another texture with glTexImage2D as GL_DEPTH_COMPONENT and it will be filled with depth values during rendering automatically?

I render into the greyShading FBO by:
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mGreyShadingFBO);
  glViewport(0, 0, 640, 480);

Then I combine the grey shading texture with the black-hole texture and draw it to the screen.
  glUseProgram(finalShaderProgram);     //draws grey values instead of black at holes
  glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
  glBindTexture(GL_TEXTURE_2D, mGreyShadingTex);
  glBindTexture(GL_TEXTURE_2D, blackHoleTex);
  drawScreenQuad(inputWidth, inputHeight);
Some questions occurred to me when looking on the code:
Does it make sense to do it like this, or should I render the image into a renderbuffer instead of a texture?
Is the depth automatically stored into my renderbuffer with the depth attachment during rendering? If yes, how do I access it?

My plan was to attach the final texture (instead of drawing it to the screen) to a FBO and add the depth to this FBO as well to be able to add those primitives with depth/occlusion testing.
But as said, I neither understand how to get the depth information nor how to use it for adding my primitives. 

Let me know, if some pictures or more code would help to get a clearer picture of my problem.
Hope you can point me into the right direction on how to add some primitives to the given texture while doing the depth/occlusion testing with the model where I got the texture from.
Thanks in advance. And thanks for all the information I already got from this community.



any ideas why the site for writing topics/replying to messages on gamedev is totally messed up on my chrome and i have to use the IE to fully see the menu?

Share this post

Link to post
Share on other sites
Since I could not read the content of the depth attachment (was empty all the time) I now just read the depth with the help from gl_FragCoord.z and store it into a rgb texture attachment of my fbo. So now I have a FBO with two texture attachments (as attachment 0 and 1). The GL_COLOR_ATTACHMENT0 has the rgb texture and the GL_COLOR_ATTACHMENT1 has the depth values (in r, g and b). My plan would now be to render the primitives into a separate FBO (same thing with color and depth textures) and then combine them. Does this work? and how should i approach this? I want to be able to translate/rotate the primitives added around the first object in the first FBO.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!