Wooni
Wooni

Reputation: 501

accesing to RGB and depth buffer of fragment shader : GLSL

continue to this question :

GLSL : accessing framebuffer to get RGB and change it

Is it possible to develop program based on GLSL as follows?

  1. draw object1
  2. get depth buffer using shader (also save rgb)
  3. draw object 1, object 2 simultaneously
  4. get detph
  5. check if depths are different (depth 2 vs depth 4)
  6. draw object 1 : for the range that depth isn't changed -> draw as original RGB : for the range that depth is changed -> draw with different RGB

I confirmed this algorithm which distinguishes object 1 is hidden by other object using glut functions. I used glReadbuffer, glDrawbuffer functions. However those are too slow, I want to use GLSL.

Upvotes: 2

Views: 457

Answers (1)

BDL
BDL

Reputation: 22167

If the only goal is to render object1 with a different color set when it is hidden behind object2 (which are the pixels of object1 where depth has changed) I would go for a completely different approach.

  1. Draw object2 with depth-write only (glDrawBuffer(GL_NONE) or glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE))
  2. Draw object1 with depth test set to glDepthFunc(GL_GREATER) and the coloring you want when object1 is behind object2
  3. Draw object1 with depth test set to glDepthFunc(GL_LESS) and the coloring you want when object1 is in front of object2.

In contrast to the algorithm you described in the question, there is no need for any read-back operations or additional framebuffers

Upvotes: 2

Related Questions