Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an object lies outside the clipping volume in OpenGL?

I'm really confused about OpenGL's modelview transformation. I understand all the transformation processes, but when it comes to projection matrix, I'm lost :(

If I have a point P (x, y, z), how can I check to see if this point will be drawn on a clipping volume defined by either by parallel clipping volume or perspective clipping volume? What's the mathematical background behind this process?

like image 377
Chan Avatar asked Jun 10 '11 01:06

Chan


3 Answers

Apply the model-view-projection matrix to the object, then check if it lies outside the clip coordinate frustum, which is defined by the planes:

    -w < x < w
    -w < y < w
     0 < z < w

So if you have a point p which is a vec3, and a model-view-projection matrix, M, then in GLSL it would look like this:

    bool in_frustum(mat4 M, vec3 p) {
        vec4 Pclip = M * vec4(p, 1.);
        return abs(Pclip.x) < Pclip.w && 
               abs(Pclip.y) < Pclip.w && 
               0 < Pclip.z && 
               Pclip.z < Pclip.w;
    }
like image 95
Mikola Avatar answered Nov 07 '22 11:11

Mikola


To determine if a given point will be visible on the screen, you test it against the viewing frustum. See this frustum culling tutorial:

http://www.lighthouse3d.com/tutorials/view-frustum-culling/

like image 34
Julio Gorgé Avatar answered Nov 07 '22 10:11

Julio Gorgé


For anyone relying on the accepted answer, it is incorrect (at least in current implementations). OpenGL clips in the z plane the same as the x and y as -w < z < w (https://www.khronos.org/opengl/wiki/Vertex_Post-Processing).

The two tests for z should then be: std::abs(Pclip.z) < Pclip.w

Checking for zero will exclude all the drawn points that are closer to the near field clip plane than the far field clip plane.

like image 2
Paul Childs Avatar answered Nov 07 '22 11:11

Paul Childs