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

↘锁芯ラ 提交于 2019-11-28 20:55:10
Mikola

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;
    }

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!