Question

This might be a simple question. As a newbie on GLSL, I would rather ask here. Now, in the vertex shader, I can get the position in world coordinate system in the following way:

    gl_Position = ftransform();
    posWorld = gl_ModelViewMatrix * gl_Vertex;

The question is: now can I can the max/min value of the posWorld among all the vertices? So that I can get a range of the vertex depth, but not the range of depth buffer.

If this is not possible, how can I get the z value of near/far plane in world coordinate system?

with best regards,

Jian

Was it helpful?

Solution

Yes it is possible with OpenGL. I'm doing a similar technique for calculating object's bounding box on GPU. Here are the steps:

  1. Arrange a render-buffer of size 1x1 type RGBA_32F in its own FBO. Set as a render target (no depth/stencil, just a single color plane). It can be a pixel of a bigger texture, in which case you'll need to setup the viewport correctly.
  2. Clear with basic value. For 'min' it will be some huge number, for 'max' it's negative huge.
  3. Set up the blending function 'min' or 'max' correspondingly with coefficients (1,1).
  4. Draw your mesh with a shader that produces a point with (0,0,0,1) coordinate. Output the color containing your original vertex world position.

You can go further optimizing from here. For example, you can get both 'min' and 'max' in one draw call by utilizing the geometry shader and negating the position for one of the output pixels.

OTHER TIPS

From what i know, i think this needs to be done manually with an algorithm based on parallel reduction. I would like someone to confirm if there exists or not an OpenGL or GLSL function that already does this.

on the other hand, you can have access to the normalized near/far planes within a fragment shader, http://www.opengl.org/wiki/GLSL_Predefined_Variables#Fragment_shader_uniforms.
and with the help of some uniform variables you can get the world far/near.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top