Esempio n. 1
0
        /// <summary>
        /// http://www.iquilezles.org/www/articles/frustumcorrect/frustumcorrect.htm
        /// </summary>
        /// <param name="ps"></param>
        /// <returns></returns>
        public ObjectClipResult TestBox(Vector4[] ps)
        {
            float mMinX, mMinY, mMinZ, mMaxX, mMaxY, mMaxZ;

            mMinX = ps.Select(v => v.X).Min();
            mMinY = ps.Select(v => v.Y).Min();
            mMinZ = ps.Select(v => v.Z).Min();
            mMaxX = ps.Select(v => v.X).Max();
            mMaxY = ps.Select(v => v.Y).Max();
            mMaxZ = ps.Select(v => v.Z).Max();

            // check box outside/inside of frustum
            int boxout;
            bool boxpartial = false;

            for (int i = 0; i < 6; i++)
            {
                boxout = 0;
                boxout += ((Vector4.Dot(clippingPlane[i], new Vector4(mMinX, mMinY, mMinZ, 1.0f)) < 0.0) ? 1 : 0);
                boxout += ((Vector4.Dot(clippingPlane[i], new Vector4(mMaxX, mMinY, mMinZ, 1.0f)) < 0.0) ? 1 : 0);
                boxout += ((Vector4.Dot(clippingPlane[i], new Vector4(mMinX, mMaxY, mMinZ, 1.0f)) < 0.0) ? 1 : 0);
                boxout += ((Vector4.Dot(clippingPlane[i], new Vector4(mMaxX, mMaxY, mMinZ, 1.0f)) < 0.0) ? 1 : 0);
                boxout += ((Vector4.Dot(clippingPlane[i], new Vector4(mMinX, mMinY, mMaxZ, 1.0f)) < 0.0) ? 1 : 0);
                boxout += ((Vector4.Dot(clippingPlane[i], new Vector4(mMaxX, mMinY, mMaxZ, 1.0f)) < 0.0) ? 1 : 0);
                boxout += ((Vector4.Dot(clippingPlane[i], new Vector4(mMinX, mMaxY, mMaxZ, 1.0f)) < 0.0) ? 1 : 0);
                boxout += ((Vector4.Dot(clippingPlane[i], new Vector4(mMaxX, mMaxY, mMaxZ, 1.0f)) < 0.0) ? 1 : 0);
                if (boxout == 8) return ObjectClipResult.TotallyOutside;
                if (boxout > 0) boxpartial = true;
            }

            if (!boxpartial) return ObjectClipResult.TotallyInside;

            int boxout2;
            boxout2=0; for( int i=0; i<8; i++ ) boxout2 += ((corner[i].X > mMaxX)?1:0); if( boxout2==8 ) return ObjectClipResult.TotallyOutside;
            boxout2=0; for( int i=0; i<8; i++ ) boxout2 += ((corner[i].X < mMinX)?1:0); if( boxout2==8 ) return ObjectClipResult.TotallyOutside;
            boxout2=0; for( int i=0; i<8; i++ ) boxout2 += ((corner[i].Y > mMaxY)?1:0); if( boxout2==8 ) return ObjectClipResult.TotallyOutside;
            boxout2=0; for( int i=0; i<8; i++ ) boxout2 += ((corner[i].Y < mMinY)?1:0); if( boxout2==8 ) return ObjectClipResult.TotallyOutside;
            boxout2=0; for( int i=0; i<8; i++ ) boxout2 += ((corner[i].Z > mMaxZ)?1:0); if( boxout2==8 ) return ObjectClipResult.TotallyOutside;
            boxout2=0; for( int i=0; i<8; i++ ) boxout2 += ((corner[i].Z < mMinZ)?1:0); if( boxout2==8 ) return ObjectClipResult.TotallyOutside;

            return ObjectClipResult.PartiallyInside;
        }