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

前端 未结 3 1566
太阳男子
太阳男子 2020-12-14 13:16

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

3条回答
  •  一整个雨季
    2020-12-14 13:35

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

提交回复
热议问题