I have a shader that renders the "texture" of a very realistic water on a tile in the 3D world using a Frame Buffer.

The problem is that, for example, I only want to render the part that isn't below the terrain, only for optimization. Does it make sense and how do I do that?

  • 2
    \$\begingroup\$ Ideas: early z-pass or stencil buffer \$\endgroup\$ – Lasse Dec 20 '15 at 10:23

If you're drawing your terrain before the water tile, the terrain is writing to the z buffer (glDepthMask), and the water tile has depth testing enabled (glEnable(GL_DEPTH_TEST)) with only fragments closer to or equal to what's already in the depth buffer passing the test (glDepthFunc(GL_LESS or GL_LEQUAL)), and your water shader doesn't modify its depth output, then most drivers (perhaps all, but I can't recall if this is a requirement) will discard fragments that are behind what's already been rendered without having to fully process them.

If you're adding the water texture in a post-process stage, and thus the depth buffer or the depth of the surface of the water are not available to you, then you could mask out areas of the frame buffer using the stencil buffer. This could be done in a post-process stage after rendering the 3D world but before doing the water-stage, or you could create the stencil buffer mask as you draw the 3D world in the first place.

| improve this answer | |
  • \$\begingroup\$ Glad I could help :) \$\endgroup\$ – Jibb Smart Dec 21 '15 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.