2

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

superpanda
  • 21
  • 4
  • The `gl_ModelViewMatrix` goes to _view_ space, not world space. Which is why it is called "Model View": it goes from model to view. – Nicol Bolas Sep 24 '11 at 00:24

2 Answers2

2

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.

kvark
  • 5,291
  • 2
  • 24
  • 33
  • Thank you for your answer :D I was trying to do this in shaders...and I did not quite get you..:( – superpanda Sep 27 '11 at 12:22
  • I explained step-by-step what you originally asked for and what @Cristobal considered impossible. As mentioned, this scheme works in my engine for updating bounding boxes. The point is: it works. And if you don't understand it - please, ask more specific questions (if still interested). – kvark Sep 27 '11 at 14:19
  • i am working with xip builder and what i can do is to replace the standard shader by loading my own shader files, which are vertex shader and fragment shader. And I am working with volume data not mesh. would that be possible to get your method work in the shaders? Thanks :D – superpanda Sep 27 '11 at 14:40
  • Yes, of course, I'm doing it in shaders. And be it some data or a mesh - doesn't matter. Here is my geometry shader for BBox update: "http://code.google.com/p/kri/source/browse/gpu/cull/box_g.glsl". It's very basic, the main action is done on the client OpenGL state setup. – kvark Sep 27 '11 at 15:00
  • for me only the vertex/fragment shader are available...geometry shader is not in the frame :( – superpanda Sep 27 '11 at 15:09
  • That's not a problem. Geometry shader just splits the vertex data into 2 streams. You can achieve the same effect by issuing 2 draw calls. – kvark Sep 27 '11 at 15:12
1

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.

labotsirc
  • 722
  • 7
  • 21
  • Thank you for the answer. could you give me a hint about which uniform varialbes to use for the transformation from the normalized near/far to world near/far? – superpanda Sep 23 '11 at 07:43
  • 1
    @superpanda There are only 3 uniform values listed there, and you would use *all* of them. The problem is that you need more than that if you want to go to view space. You need your projection matrix, for starters (or rather, the inverse of it). Honestly, it would be much easier to just pass it in. Those near and far values are derived from the view space near and far values you use with `glFrustum` or `gluPerspective`. – Nicol Bolas Sep 24 '11 at 00:24
  • Yes pass the projection matrix as Nicol suggested. – labotsirc Sep 24 '11 at 13:45
  • thanks, pass the projection matrix! I solved this problem by referring to this blog page:http://olivers.posterous.com/linear-depth-in-glsl-for-real – superpanda Sep 27 '11 at 12:21