如何在 OpenGL 中绘制视锥体

How To Draw A Frustum in OpenGL

我使用 C# 和 OpenTK 创建了一个平截头体 class,它运行良好。但是,我希望能够出于调试目的绘制截锥体的边界。我不知道如何从 Matrix 数据中获取顶点数据以进行绘制。我只是想知道是否有人知道该怎么做。

这是我的视锥体 class:

using System;
using GrimoireGDK.GameDevelopmentKit.Math.Geometry;
using OpenTK;

namespace GrimoireGameClient.Client.Window.Bindings {
    public class Frustum {
        private float[] _clipMatrix = new float[ 16 ];
        private float[ , ] _frustum = new float[ 6 , 4 ];

        public const int A = 0;
        public const int B = 1;
        public const int C = 2;
        public const int D = 3;

        public enum ClippingPlane : int {
            Right = 0 ,
            Left = 1 ,
            Bottom = 2 ,
            Top = 3 ,
            Back = 4 ,
            Front = 5
        }

        private void NormalizePlane( float[ , ] frustum , int side ) {
            float magnitude = ( float )Math.Sqrt( ( frustum[ side , 0 ] * frustum[ side , 0 ] ) + ( frustum[ side , 1 ] * frustum[ side , 1 ] )
                                                + ( frustum[ side , 2 ] * frustum[ side , 2 ] ) );
            frustum[ side , 0 ] /= magnitude;
            frustum[ side , 1 ] /= magnitude;
            frustum[ side , 2 ] /= magnitude;
            frustum[ side , 3 ] /= magnitude;
        }

        public bool PointVsFrustum( float x , float y , float z ) {
            for( int i = 0 ; i < 6 ; i++ ) {
                if( this._frustum[ i , 0 ] * x + this._frustum[ i , 1 ] * y + this._frustum[ i , 2 ] * z + this._frustum[ i , 3 ] <= 0.0f ) {
                    return false;
                }
            }
            return true;
        }

        public bool PointVsFrustum( Vector3 location ) {
            for( int i = 0 ; i < 6 ; i++ ) {
                if( this._frustum[ i , 0 ] * location.X + this._frustum[ i , 1 ] * location.Y + this._frustum[ i , 2 ] * location.Z + this._frustum[ i , 3 ] <= 0.0f ) {
                    return false;
                }
            }
            return true;
        }

        public bool SphereVsFrustum( float x , float y , float z , float radius ) {
            float d = 0;
            for( int p = 0 ; p < 6 ; p++ ) {
                d = _frustum[ p , 0 ] * x + _frustum[ p , 1 ] * y + _frustum[ p , 2 ] * z + _frustum[ p , 3 ];
                if( d <= -radius ) {
                    return false;
                }
            }
            return true;
        }

        public bool SphereVsFrustum( Vector3 location , float radius ) {
            float d = 0;
            for( int p = 0 ; p < 6 ; p++ ) {
                d = _frustum[ p , 0 ] * location.X + _frustum[ p , 1 ] * location.Y + _frustum[ p , 2 ] * location.Z + _frustum[ p , 3 ];
                if( d <= -radius ) {
                    return false;
                }
            }
            return true;
        }

        public bool VolumeVsFrustum( float x , float y , float z , float width , float height , float length ) {
            for( int i = 0 ; i < 6 ; i++ ) {
                if( _frustum[ i , A ] * ( x - width ) + _frustum[ i , B ] * ( y - height ) + _frustum[ i , C ] * ( z - length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x + width ) + _frustum[ i , B ] * ( y - height ) + _frustum[ i , C ] * ( z - length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x - width ) + _frustum[ i , B ] * ( y + height ) + _frustum[ i , C ] * ( z - length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x + width ) + _frustum[ i , B ] * ( y + height ) + _frustum[ i , C ] * ( z - length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x - width ) + _frustum[ i , B ] * ( y - height ) + _frustum[ i , C ] * ( z + length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x + width ) + _frustum[ i , B ] * ( y - height ) + _frustum[ i , C ] * ( z + length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x - width ) + _frustum[ i , B ] * ( y + height ) + _frustum[ i , C ] * ( z + length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x + width ) + _frustum[ i , B ] * ( y + height ) + _frustum[ i , C ] * ( z + length ) + _frustum[ i , D ] > 0 )
                    continue;
                return false;
            }
            return true;
        }

        public bool VolumeVsFrustum( Vector3 location, float width , float height , float length ) {
            for( int i = 0 ; i < 6 ; i++ ) {
                if( _frustum[ i , A ] * ( location.X - width ) + _frustum[ i , B ] * ( location.Y - height ) + _frustum[ i , C ] * ( location.Z - length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X + width ) + _frustum[ i , B ] * ( location.Y - height ) + _frustum[ i , C ] * ( location.Z - length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X - width ) + _frustum[ i , B ] * ( location.Y + height ) + _frustum[ i , C ] * ( location.Z - length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X + width ) + _frustum[ i , B ] * ( location.Y + height ) + _frustum[ i , C ] * ( location.Z - length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X - width ) + _frustum[ i , B ] * ( location.Y - height ) + _frustum[ i , C ] * ( location.Z + length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X + width ) + _frustum[ i , B ] * ( location.Y - height ) + _frustum[ i , C ] * ( location.Z + length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X - width ) + _frustum[ i , B ] * ( location.Y + height ) + _frustum[ i , C ] * ( location.Z + length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X + width ) + _frustum[ i , B ] * ( location.Y + height ) + _frustum[ i , C ] * ( location.Z + length ) + _frustum[ i , D ] > 0 )
                    continue;
                return false;
            }
            return true;
        }

        public bool VolumeVsFrustum( Vector3 location , BoundingVolume volume ) {
            for( int i = 0 ; i < 6 ; i++ ) {
                if( _frustum[ i , A ] * ( location.X - volume.Width ) + _frustum[ i , B ] * ( location.Y - volume.Height ) + _frustum[ i , C ] * ( location.Z - volume.Length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X + volume.Width ) + _frustum[ i , B ] * ( location.Y - volume.Height ) + _frustum[ i , C ] * ( location.Z - volume.Length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X - volume.Width ) + _frustum[ i , B ] * ( location.Y + volume.Height ) + _frustum[ i , C ] * ( location.Z - volume.Length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X + volume.Width ) + _frustum[ i , B ] * ( location.Y + volume.Height ) + _frustum[ i , C ] * ( location.Z - volume.Length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X - volume.Width ) + _frustum[ i , B ] * ( location.Y - volume.Height ) + _frustum[ i , C ] * ( location.Z + volume.Length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X + volume.Width ) + _frustum[ i , B ] * ( location.Y - volume.Height ) + _frustum[ i , C ] * ( location.Z + volume.Length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X - volume.Width ) + _frustum[ i , B ] * ( location.Y + volume.Height ) + _frustum[ i , C ] * ( location.Z + volume.Length ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( location.X + volume.Width ) + _frustum[ i , B ] * ( location.Y + volume.Height ) + _frustum[ i , C ] * ( location.Z + volume.Length ) + _frustum[ i , D ] > 0 )
                    continue;
                return false;
            }
            return true;
        }

        public bool CubeVsFrustum( float x , float y , float z , float size ) {
            for( int i = 0 ; i < 6 ; i++ ) {
                if( _frustum[ i , A ] * ( x - size ) + _frustum[ i , B ] * ( y - size ) + _frustum[ i , C ] * ( z - size ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x + size ) + _frustum[ i , B ] * ( y - size ) + _frustum[ i , C ] * ( z - size ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x - size ) + _frustum[ i , B ] * ( y + size ) + _frustum[ i , C ] * ( z - size ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x + size ) + _frustum[ i , B ] * ( y + size ) + _frustum[ i , C ] * ( z - size ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x - size ) + _frustum[ i , B ] * ( y - size ) + _frustum[ i , C ] * ( z + size ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x + size ) + _frustum[ i , B ] * ( y - size ) + _frustum[ i , C ] * ( z + size ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x - size ) + _frustum[ i , B ] * ( y + size ) + _frustum[ i , C ] * ( z + size ) + _frustum[ i , D ] > 0 )
                    continue;
                if( _frustum[ i , A ] * ( x + size ) + _frustum[ i , B ] * ( y + size ) + _frustum[ i , C ] * ( z + size ) + _frustum[ i , D ] > 0 )
                    continue;
                return false;
            }
            return true;
        }


        public void CalculateFrustum( Matrix4 projectionMatrix , Matrix4 modelViewMatrix ) {
            _clipMatrix[ 0 ] = ( modelViewMatrix.M11 * projectionMatrix.M11 ) + ( modelViewMatrix.M12 * projectionMatrix.M21 ) + ( modelViewMatrix.M13 * projectionMatrix.M31 ) + ( modelViewMatrix.M14 * projectionMatrix.M41 );
            _clipMatrix[ 1 ] = ( modelViewMatrix.M11 * projectionMatrix.M12 ) + ( modelViewMatrix.M12 * projectionMatrix.M22 ) + ( modelViewMatrix.M13 * projectionMatrix.M32 ) + ( modelViewMatrix.M14 * projectionMatrix.M42 );
            _clipMatrix[ 2 ] = ( modelViewMatrix.M11 * projectionMatrix.M13 ) + ( modelViewMatrix.M12 * projectionMatrix.M23 ) + ( modelViewMatrix.M13 * projectionMatrix.M33 ) + ( modelViewMatrix.M14 * projectionMatrix.M43 );
            _clipMatrix[ 3 ] = ( modelViewMatrix.M11 * projectionMatrix.M14 ) + ( modelViewMatrix.M12 * projectionMatrix.M24 ) + ( modelViewMatrix.M13 * projectionMatrix.M34 ) + ( modelViewMatrix.M14 * projectionMatrix.M44 );

            _clipMatrix[ 4 ] = ( modelViewMatrix.M21 * projectionMatrix.M11 ) + ( modelViewMatrix.M22 * projectionMatrix.M21 ) + ( modelViewMatrix.M23 * projectionMatrix.M31 ) + ( modelViewMatrix.M24 * projectionMatrix.M41 );
            _clipMatrix[ 5 ] = ( modelViewMatrix.M21 * projectionMatrix.M12 ) + ( modelViewMatrix.M22 * projectionMatrix.M22 ) + ( modelViewMatrix.M23 * projectionMatrix.M32 ) + ( modelViewMatrix.M24 * projectionMatrix.M42 );
            _clipMatrix[ 6 ] = ( modelViewMatrix.M21 * projectionMatrix.M13 ) + ( modelViewMatrix.M22 * projectionMatrix.M23 ) + ( modelViewMatrix.M23 * projectionMatrix.M33 ) + ( modelViewMatrix.M24 * projectionMatrix.M43 );
            _clipMatrix[ 7 ] = ( modelViewMatrix.M21 * projectionMatrix.M14 ) + ( modelViewMatrix.M22 * projectionMatrix.M24 ) + ( modelViewMatrix.M23 * projectionMatrix.M34 ) + ( modelViewMatrix.M24 * projectionMatrix.M44 );

            _clipMatrix[ 8 ] = ( modelViewMatrix.M31 * projectionMatrix.M11 ) + ( modelViewMatrix.M32 * projectionMatrix.M21 ) + ( modelViewMatrix.M33 * projectionMatrix.M31 ) + ( modelViewMatrix.M34 * projectionMatrix.M41 );
            _clipMatrix[ 9 ] = ( modelViewMatrix.M31 * projectionMatrix.M12 ) + ( modelViewMatrix.M32 * projectionMatrix.M22 ) + ( modelViewMatrix.M33 * projectionMatrix.M32 ) + ( modelViewMatrix.M34 * projectionMatrix.M42 );
            _clipMatrix[ 10 ] = ( modelViewMatrix.M31 * projectionMatrix.M13 ) + ( modelViewMatrix.M32 * projectionMatrix.M23 ) + ( modelViewMatrix.M33 * projectionMatrix.M33 ) + ( modelViewMatrix.M34 * projectionMatrix.M43 );
            _clipMatrix[ 11 ] = ( modelViewMatrix.M31 * projectionMatrix.M14 ) + ( modelViewMatrix.M32 * projectionMatrix.M24 ) + ( modelViewMatrix.M33 * projectionMatrix.M34 ) + ( modelViewMatrix.M34 * projectionMatrix.M44 );

            _clipMatrix[ 12 ] = ( modelViewMatrix.M41 * projectionMatrix.M11 ) + ( modelViewMatrix.M42 * projectionMatrix.M21 ) + ( modelViewMatrix.M43 * projectionMatrix.M31 ) + ( modelViewMatrix.M44 * projectionMatrix.M41 );
            _clipMatrix[ 13 ] = ( modelViewMatrix.M41 * projectionMatrix.M12 ) + ( modelViewMatrix.M42 * projectionMatrix.M22 ) + ( modelViewMatrix.M43 * projectionMatrix.M32 ) + ( modelViewMatrix.M44 * projectionMatrix.M42 );
            _clipMatrix[ 14 ] = ( modelViewMatrix.M41 * projectionMatrix.M13 ) + ( modelViewMatrix.M42 * projectionMatrix.M23 ) + ( modelViewMatrix.M43 * projectionMatrix.M33 ) + ( modelViewMatrix.M44 * projectionMatrix.M43 );
            _clipMatrix[ 15 ] = ( modelViewMatrix.M41 * projectionMatrix.M14 ) + ( modelViewMatrix.M42 * projectionMatrix.M24 ) + ( modelViewMatrix.M43 * projectionMatrix.M34 ) + ( modelViewMatrix.M44 * projectionMatrix.M44 );

            _frustum[ ( int )ClippingPlane.Right , 0 ] = _clipMatrix[ 3 ] - _clipMatrix[ 0 ];
            _frustum[ ( int )ClippingPlane.Right , 1 ] = _clipMatrix[ 7 ] - _clipMatrix[ 4 ];
            _frustum[ ( int )ClippingPlane.Right , 2 ] = _clipMatrix[ 11 ] - _clipMatrix[ 8 ];
            _frustum[ ( int )ClippingPlane.Right , 3 ] = _clipMatrix[ 15 ] - _clipMatrix[ 12 ];
            NormalizePlane( _frustum , ( int )ClippingPlane.Right );

            _frustum[ ( int )ClippingPlane.Left , 0 ] = _clipMatrix[ 3 ] + _clipMatrix[ 0 ];
            _frustum[ ( int )ClippingPlane.Left , 1 ] = _clipMatrix[ 7 ] + _clipMatrix[ 4 ];
            _frustum[ ( int )ClippingPlane.Left , 2 ] = _clipMatrix[ 11 ] + _clipMatrix[ 8 ];
            _frustum[ ( int )ClippingPlane.Left , 3 ] = _clipMatrix[ 15 ] + _clipMatrix[ 12 ];
            NormalizePlane( _frustum , ( int )ClippingPlane.Left );

            _frustum[ ( int )ClippingPlane.Bottom , 0 ] = _clipMatrix[ 3 ] + _clipMatrix[ 1 ];
            _frustum[ ( int )ClippingPlane.Bottom , 1 ] = _clipMatrix[ 7 ] + _clipMatrix[ 5 ];
            _frustum[ ( int )ClippingPlane.Bottom , 2 ] = _clipMatrix[ 11 ] + _clipMatrix[ 9 ];
            _frustum[ ( int )ClippingPlane.Bottom , 3 ] = _clipMatrix[ 15 ] + _clipMatrix[ 13 ];
            NormalizePlane( _frustum , ( int )ClippingPlane.Bottom );

            _frustum[ ( int )ClippingPlane.Top , 0 ] = _clipMatrix[ 3 ] - _clipMatrix[ 1 ];
            _frustum[ ( int )ClippingPlane.Top , 1 ] = _clipMatrix[ 7 ] - _clipMatrix[ 5 ];
            _frustum[ ( int )ClippingPlane.Top , 2 ] = _clipMatrix[ 11 ] - _clipMatrix[ 9 ];
            _frustum[ ( int )ClippingPlane.Top , 3 ] = _clipMatrix[ 15 ] - _clipMatrix[ 13 ];
            NormalizePlane( _frustum , ( int )ClippingPlane.Top );

            _frustum[ ( int )ClippingPlane.Back , 0 ] = _clipMatrix[ 3 ] - _clipMatrix[ 2 ];
            _frustum[ ( int )ClippingPlane.Back , 1 ] = _clipMatrix[ 7 ] - _clipMatrix[ 6 ];
            _frustum[ ( int )ClippingPlane.Back , 2 ] = _clipMatrix[ 11 ] - _clipMatrix[ 10 ];
            _frustum[ ( int )ClippingPlane.Back , 3 ] = _clipMatrix[ 15 ] - _clipMatrix[ 14 ];
            NormalizePlane( _frustum , ( int )ClippingPlane.Back );

            _frustum[ ( int )ClippingPlane.Front , 0 ] = _clipMatrix[ 3 ] + _clipMatrix[ 2 ];
            _frustum[ ( int )ClippingPlane.Front , 1 ] = _clipMatrix[ 7 ] + _clipMatrix[ 6 ];
            _frustum[ ( int )ClippingPlane.Front , 2 ] = _clipMatrix[ 11 ] + _clipMatrix[ 10 ];
            _frustum[ ( int )ClippingPlane.Front , 3 ] = _clipMatrix[ 15 ] + _clipMatrix[ 14 ];
            NormalizePlane( _frustum , ( int )ClippingPlane.Front );
        }

        /// <summary>
        /// Draw the frustum for debugging purposes.
        /// </summary>
        public void Draw() {
            // Implement drawing code here.
        }
    }
}

有谁知道如何绘制视锥体?而且我不在乎它是否以立即模式完成;事实上,我几乎更喜欢那样。这是我正在寻找的视觉输出类型:

这些建议都不完全是您要问的,但它们可能会帮助您考虑一下。

据我所知(假设代码有效,如您所说),_frustum 数组定义了平截头体两侧的 6 个平面。

因此,您可以通过计算适当平面的交点来简单地计算体积角上的 8 个点(如此处讨论:Point of intersection of three planes 3D - C#

我怀疑当你计算方程式时,它们会根据 clipmatrix 中的数据简化为一些相对简单的表达式。

或者,如果您知道透视矩阵定义的平截头体(在相对于视点的简单规范坐标中,以 z 轴为中心),您可以简单地反转 modelViewMatrix 并将这些点乘以它,从而得到世界坐标中的点。

看起来你有六个平面组成平截头体(上、下、左、右、前、后)被描述为四个分量,平面的法向量和沿该向量到原点的距离( xyzD).

一个简单的方法:给定任意三个平面,您可以通过一系列点积和叉积找到相关的角点(因此左上角、左上角、后角等)。一个很好的参考是 MonoGame.BoundingFrustum.IntersectionPoint 方法(麻省理工学院许可证),它(在最近的优化之前)是:

Vector3 IntersectionPoint(ref Plane a, ref Plane b, ref Plane c)
{
    // Formula used
    //                d1 ( N2 * N3 ) + d2 ( N3 * N1 ) + d3 ( N1 * N2 )
    //P =   ---------------------------------------------------------------------
    //                             N1 . ( N2 * N3 )
    //
    // Note: N refers to the normal, d refers to the displacement. '.' means dot product. '*' means cross product

    Vector3 v1, v2, v3;
    float f = -Vector3.Dot(a.Normal, Vector3.Cross(b.Normal, c.Normal));

    v1 = (a.D * (Vector3.Cross(b.Normal, c.Normal)));
    v2 = (b.D * (Vector3.Cross(c.Normal, a.Normal)));
    v3 = (c.D * (Vector3.Cross(a.Normal, b.Normal)));

    Vector3 vec = new Vector3(v1.X + v2.X + v3.X, v1.Y + v2.Y + v3.Y, v1.Z + v2.Z + v3.Z);
    return vec / f;
}

您可以对所有八个角重复此操作,然后将角连接在一起以绘制平截头体系列 lines/edges。

上面的 link 是一个很好的参考 - 事实上,MonoGame 代码及其 XNA 祖先都是 "fast enough" 几何处理的很好参考,MonoGame 的 MIT 许可证是 very permissive。它们唯一的缺点往往是在坐标系惯用手、矩阵乘法顺序(行矩阵与列矩阵)等方面的假设。

我敢肯定,要获得完整的图片,您需要使用不同的相机查看结果,就像您提供的示例视觉输出一样。