fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / Techniques / OSGClusterShadingStage.inl
blob5624ee19d9453c9acf6d282bed1d74561bea82bc
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *               Copyright (C) 2000-2013 by the OpenSG Forum                 *
6  *                                                                           *
7  * contact: dirk@opensg.org, gerrit.voss@vossg.org, carsten_neumann@gmx.net  *
8  *                                                                           *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
11  *                                License                                    *
12  *                                                                           *
13  * This library is free software; you can redistribute it and/or modify it   *
14  * under the terms of the GNU Library General Public License as published    *
15  * by the Free Software Foundation, version 2.                               *
16  *                                                                           *
17  * This library is distributed in the hope that it will be useful, but       *
18  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
20  * Library General Public License for more details.                          *
21  *                                                                           *
22  * You should have received a copy of the GNU Library General Public         *
23  * License along with this library; if not, write to the Free Software       *
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
25  *                                                                           *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
28  *                                Changes                                    *
29  *                                                                           *
30  *                                                                           *
31  *                                                                           *
32  *                                                                           *
33  *                                                                           *
34  *                                                                           *
35 \*---------------------------------------------------------------------------*/
37 //---------------------------------------------------------------------------
38 //  Includes
39 //---------------------------------------------------------------------------
41 OSG_BEGIN_NAMESPACE
43 /*---------------------  Plane and Frustums Test --------------------------*/
46 // Check to see if a point is fully behind (inside the negative halfspace of) a plane.
48 inline bool ClusterShadingStage::PointInsidePlane(
49     const Pnt3f& p, 
50     const Plane& plane)
52     Real32 val = plane.getNormal().dot(p) - plane.getDistanceFromOrigin();
53     return val < 0;
57 // Check to see if a sphere is fully behind (inside the negative halfspace of) a plane.
59 inline bool ClusterShadingStage::SphereInsidePlane(
60     const Sphere& sphere, 
61     const Plane&  plane)
63     Real32 val = plane.getNormal().dot(sphere.c) - plane.getDistanceFromOrigin();
64     return val < -sphere.r;
68 // Check to see if a cone if fully behind (inside the negative halfspace of) a plane.
70 inline bool ClusterShadingStage::ConeInsidePlane(
71     const Cone&  cone, 
72     const Plane& plane)
74     // Compute the farthest point on the end of the cone to the positive space of the plane.
75     Vec3f m = plane.getNormal().cross(cone.d).cross(cone.d);
76     Pnt3f Q = cone.T + cone.d * cone.h - m * cone.r;
78     // The cone is in the negative halfspace of the plane if both
79     // the tip of the cone and the farthest point on the end of the cone to the 
80     // positive halfspace of the plane are both inside the negative halfspace 
81     // of the plane.
82     return PointInsidePlane(cone.T, plane) && PointInsidePlane(Q, plane);
86 // Check to see of a point light is partially contained within the frustum.
88 inline bool ClusterShadingStage::SphereInsideFrustum(
89     const Sphere&  sphere, 
90     const Frustum& frustum, 
91     Real32         zNear, 
92     Real32         zFar)
94     bool result = true;
96     if (sphere.c.z() - sphere.r > zNear || zFar > sphere.c.z() + sphere.r)
97     {
98         result = false;
99     }
101     for (int i = 0; i < 4 && result; i++)
102     {
103         if (SphereInsidePlane(sphere, frustum.planes[i]))
104         {
105             result = false;
106         }
107     }
109     return result;
112 inline bool ClusterShadingStage::ConeInsideFrustum(
113     const Cone&    cone, 
114     const Frustum& frustum, 
115     Real32         zNear, 
116     Real32         zFar)
118     bool result = true;
120     Plane nearPlane(Vec3f(0, 0,-1),-zNear);
121     Plane farPlane (Vec3f(0, 0, 1), zFar );
123     if (ConeInsidePlane(cone, nearPlane) || ConeInsidePlane(cone, farPlane))
124     {
125         result = false;
126     }
128     for (int i = 0; i < 4 && result; i++)
129     {
130         if (ConeInsidePlane(cone, frustum.planes[i]))
131         {
132             result = false;
133         }
134     }
136     return result;
139 OSG_END_NAMESPACE