Changeset 2911


Ignore:
Timestamp:
09/07/08 13:44:28 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj

    r2901 r2911  
    284284                        </File> 
    285285                        <File 
     286                                RelativePath=".\src\Polygon3.cpp" 
     287                                > 
     288                        </File> 
     289                        <File 
     290                                RelativePath=".\src\Polygon3.h" 
     291                                > 
     292                        </File> 
     293                        <File 
     294                                RelativePath=".\src\Polyhedron.cpp" 
     295                                > 
     296                        </File> 
     297                        <File 
     298                                RelativePath=".\src\Polyhedron.h" 
     299                                > 
     300                        </File> 
     301                        <File 
    286302                                RelativePath=".\src\SampleGenerator.cpp" 
    287303                                > 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp

    r2802 r2911  
     1#include "AxisAlignedBox3.h" 
     2#include "Plane3.h" 
     3#include "Polygon3.h" 
     4#include "Polyhedron.h" 
     5 
    16#include <cassert> 
    27#include <iostream> 
    3 #include "AxisAlignedBox3.h" 
    4 #include "Plane3.h" 
    5  
    68 
    79using namespace std; 
     
    5052        Minimize(mMin, newpt); 
    5153        Maximize(mMax, newpt); 
     54} 
     55 
     56 
     57void AxisAlignedBox3::Include(const Polygon3 &newpoly) 
     58{ 
     59        VertexArray::const_iterator it, it_end = newpoly.mVertices.end(); 
     60 
     61        for (it = newpoly.mVertices.begin(); it != it_end; ++ it) 
     62                Include(*it); 
     63} 
     64 
     65 
     66void AxisAlignedBox3::Include(const PolygonContainer &polys) 
     67{ 
     68        PolygonContainer::const_iterator it, it_end = polys.end(); 
     69 
     70        for (it = polys.begin(); it != it_end; ++ it) 
     71                Include(*(*it)); 
    5272} 
    5373 
     
    10261046} 
    10271047 
    1028 } 
    1029  
     1048 
     1049struct VertexData 
     1050{ 
     1051        Vector3 mVertex; 
     1052        float mAngle; 
     1053         
     1054        VertexData(Vector3 vtx, float angle): mVertex(vtx), mAngle(angle) 
     1055        {} 
     1056 
     1057        bool operator<(const VertexData &b) const  
     1058        { 
     1059                return mAngle > b.mAngle; 
     1060        } 
     1061}; 
     1062 
     1063// TODO: use a table to avoid normal and distance computations 
     1064Polygon3 *AxisAlignedBox3::CrossSection(const Plane3 &plane) const 
     1065{ 
     1066        Polygon3 *planePoly = new Polygon3(); 
     1067         
     1068        int side[8]; 
     1069        bool onFrontSide = false, onBackSide = false; 
     1070         
     1071        Vector3 vtx; 
     1072         
     1073        ////////////// 
     1074        //-- compute classification of vertices 
     1075 
     1076        for (int i = 0; i < 8; ++i) 
     1077        { 
     1078                GetVertex(i, vtx); 
     1079                side[i] = plane.Side(vtx); 
     1080                if (side[i] > 0) 
     1081                        onFrontSide = true; 
     1082                else if (side[i] < 0) 
     1083                        onBackSide = true; 
     1084                else // vertex coincident => push_back 
     1085                        planePoly->mVertices.push_back(vtx); 
     1086        } 
     1087 
     1088        /////////// 
     1089        //-- find intersections 
     1090 
     1091        if (onFrontSide && onBackSide) 
     1092        { 
     1093                Vector3 ptA, ptB; 
     1094                for (int i = 0; i < 12; ++ i) 
     1095                { 
     1096                        int aIdx, bIdx; 
     1097                        GetEdge(i, aIdx, bIdx); 
     1098 
     1099                        ptA = GetVertex(aIdx); 
     1100                        ptB = GetVertex(bIdx); 
     1101 
     1102                        int sideA = side[aIdx]; 
     1103                        int sideB = side[bIdx]; 
     1104 
     1105                        if (((sideA > 0) && (sideB < 0)) || (sideA < 0) && (sideB > 0)) 
     1106                                planePoly->mVertices.push_back(plane.FindIntersection(ptA, ptB));        
     1107                } 
     1108        } 
     1109 
     1110        // order intersections   
     1111        if (planePoly->mVertices.size() > 3) 
     1112        { 
     1113                Vector3 centerOfMass(0); 
     1114 
     1115                int i; 
     1116                // compute center of mass 
     1117                for (i = 0; i < (int)planePoly->mVertices.size(); ++ i) 
     1118                        centerOfMass += planePoly->mVertices[i]; 
     1119                 
     1120                centerOfMass /= (float)planePoly->mVertices.size(); 
     1121 
     1122                vector<VertexData> vertexData; 
     1123                Vector3 refVec = Normalize(centerOfMass - planePoly->mVertices[0]); 
     1124 
     1125                // compute angle to reference point 
     1126                for (i = 1; i < (int)planePoly->mVertices.size(); ++ i) 
     1127                { 
     1128                    float angle =  
     1129                      Angle(refVec, centerOfMass - planePoly->mVertices[i], plane.mNormal); 
     1130                     
     1131                    vertexData.push_back(VertexData(planePoly->mVertices[i], angle)); 
     1132                } 
     1133                 
     1134                std::stable_sort(vertexData.begin(), vertexData.end()); 
     1135 
     1136                // update vertices 
     1137                for (i = 1; i < (int)planePoly->mVertices.size(); ++ i) 
     1138                        planePoly->mVertices[i] = vertexData[i - 1].mVertex; 
     1139        } 
     1140        else if (planePoly->mVertices.size() == 3) 
     1141        { 
     1142                // fix orientation if needed 
     1143                if (DotProd(planePoly->GetNormal(), plane.mNormal) < 0) 
     1144                { 
     1145                        Vector3 v = planePoly->mVertices[1]; 
     1146                        planePoly->mVertices[1] = planePoly->mVertices[2]; 
     1147                        planePoly->mVertices[2] = v; 
     1148                } 
     1149        } 
     1150         
     1151        return planePoly; 
     1152} 
     1153 
     1154 
     1155Plane3 AxisAlignedBox3::GetPlane(const int face) const 
     1156{ 
     1157        switch (face)  
     1158        { 
     1159         
     1160        case 0: 
     1161                return Plane3(Vector3(-1, 0, 0), mMin); 
     1162        case 1:  
     1163                return Plane3(Vector3(1, 0, 0), mMax); 
     1164        case 2: 
     1165                return Plane3(Vector3(0, -1, 0), mMin); 
     1166        case 3: 
     1167                return Plane3(Vector3(0, 1, 0), mMax); 
     1168        case 4: 
     1169                return Plane3(Vector3(0, 0, -1), mMin); 
     1170        case 5: 
     1171                return Plane3(Vector3(0, 0, 1), mMax); 
     1172        } 
     1173 
     1174        // should not come here 
     1175        return Plane3(); 
     1176} 
     1177 
     1178 
     1179int AxisAlignedBox3::Side(const Plane3 &plane) const 
     1180{ 
     1181        int s = 0; 
     1182        // if exactly one of the vertices lies on the plane  
     1183        // and the others are on the same side, the plane is tangent 
     1184        // to the box on either side and not intersecting 
     1185        for (int i = 0; i < 8; ++ i) 
     1186        { 
     1187                Vector3 pt; 
     1188                GetVertex(i, pt); 
     1189 
     1190                int side = plane.Side(pt); 
     1191 
     1192                if (side != 0) 
     1193                { 
     1194                        if (side == -s) 
     1195                                return 0; // sign changed => intersects 
     1196 
     1197                        s = side; 
     1198                } 
     1199        } 
     1200 
     1201        return s; 
     1202} 
     1203 
     1204 
     1205Polyhedron *AxisAlignedBox3::CalcIntersection(Polyhedron *polyhedron) const 
     1206{ 
     1207        Polyhedron *oldPolyhedron = new Polyhedron(*polyhedron); 
     1208 
     1209        Polyhedron *newPolyhedron = NULL; 
     1210 
     1211        for (int i = 0; i < 6; ++ i) 
     1212        { 
     1213                Polyhedron *newPolyhedron = oldPolyhedron->CalcIntersection(GetPlane(i)); 
     1214                DEL_PTR(oldPolyhedron); 
     1215 
     1216                if (!newPolyhedron) 
     1217                        return NULL; 
     1218        } 
     1219 
     1220        return newPolyhedron; 
     1221} 
     1222 
     1223 
     1224} 
     1225 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h

    r2802 r2911  
    55#include "Matrix4x4.h" 
    66#include "Vector3.h" 
     7#include "common.h" 
     8# 
    79 
    810 
     
    1214struct Triangle3; 
    1315class Plane3; 
     16class Polygon3; 
     17class Polyhedron; 
     18 
    1419 
    1520/** Axis alignedd box class. 
     
    7984        void Include(const Vector3 &newpt); 
    8085        void Include(const AxisAlignedBox3 &bbox); 
     86        void Include(const Polygon3 &newpoly); 
     87        void Include(const PolygonContainer &polys); 
     88         
    8189        /** Expand the axis-aligned box to include given values in particular axis. 
    8290        */ 
     
    8593        */ 
    8694        bool Includes(const AxisAlignedBox3 &b) const; 
     95 
    8796        /** Returns true if this point is inside box. 
    8897        */ 
     
    167176        */ 
    168177        float GetMinVisibleDistance(const Plane3 &near) const; 
     178        /** Returns cross section of the plane as a polygon. 
     179        */ 
     180        Polygon3 *AxisAlignedBox3::CrossSection(const Plane3 &plane) const; 
     181        /** Returns the supporting plane of this face. 
     182        */ 
     183        Plane3 AxisAlignedBox3::GetPlane(const int face) const; 
     184        /** Returns  
     185                0 if box intersects plane 
     186                1 if box in front of plane 
     187                -1 if box behind plane 
     188        */ 
     189        int Side(const Plane3 &plane) const; 
     190        /**  Calculates the intersection of the polyhedron with the box. 
     191        */ 
     192        Polyhedron *CalcIntersection(Polyhedron *polyhedron) const; 
    169193 
    170194 
     
    178202        friend inline bool OverlapS(const AxisAlignedBox3 &,const AxisAlignedBox3 &); 
    179203 
    180         /** Overlap returns 1 if the two axis-aligned boxes overlap for a given 
    181         epsilon. If eps > 0.0, then the boxes has to have the real intersection 
     204        /** Overlap returns 1 if the two axis-aligned boxes overlap for a given epsilon.  
     205        If eps > 0.0, then the boxes has to have the real intersection 
    182206        box, if eps < 0.0, then the boxes need not intersect really, they 
    183207        can be at eps distance in the projection. 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp

    r2897 r2911  
    6565 
    6666static Plane3 sNearPlane; 
    67 static Camera::Frustum sFrustum; 
     67static Frustum sFrustum; 
    6868 
    6969/// these values are valid for all nodes 
     
    364364 
    365365 
     366static void CalcNPVertexIndices(const Frustum &frustum, int *indices) 
     367{ 
     368        for (int i = 0; i < 6; ++ i) 
     369        { 
     370                // n-vertex 
     371                indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(frustum.mClipPlanes[i].mNormal); 
     372                // p-vertex 
     373                indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(frustum.mClipPlanes[i].mNormal);    
     374        } 
     375} 
     376 
     377 
    366378void Bvh::InitFrame(Camera *cam) 
    367379{ 
     
    370382 
    371383        cam->CalcFrustum(sFrustum); 
    372         sFrustum.CalcNPVertexIndices(sClipPlaneAABBVertexIndices); 
     384        CalcNPVertexIndices(sFrustum, sClipPlaneAABBVertexIndices); 
    373385 
    374386        // store near plane 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp

    r2893 r2911  
    22#include "Camera.h" 
    33#include "glInterface.h" 
    4  
     4#include "Polygon3.h" 
     5#include "Matrix4x4.h" 
     6#include "Polyhedron.h" 
    57 
    68namespace CHCDemoEngine  
     
    107109 
    108110        matProjectionView = matViewing * matProjectionView; 
    109                  
    110         float planes[6][4]; 
    111  
    112  
    113         ////////// 
    114         //-- extract the plane equations 
    115  
    116         for (int i = 0; i < 4; ++ i) 
    117         { 
    118                 planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane 
    119                 planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane 
    120                 planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane 
    121                 planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane 
    122                 planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane 
    123                 planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane 
    124         } 
    125  
     111 
     112        frustum = Frustum(matProjectionView); 
    126113 
    127114        //////////// 
     
    132119                // the clipping planes look outward the frustum, 
    133120                // so distances > 0 mean that a point is outside 
    134                 float fInvLength = -1.0f /  
    135                         sqrt(planes[i][0] * planes[i][0] +       
    136                              planes[i][1] * planes[i][1] +       
    137                                  planes[i][2] * planes[i][2]); 
    138  
    139                 planes[i][0] *= fInvLength; 
    140                 planes[i][1] *= fInvLength; 
    141                 planes[i][2] *= fInvLength; 
    142                 planes[i][3] *= fInvLength; 
    143  
    144                 frustum.mClipPlanes[i].mNormal = Vector3(planes[i][0], planes[i][1], planes[i][2]); 
    145                 frustum.mClipPlanes[i].mD = planes[i][3]; 
    146         } 
    147 } 
    148  
    149  
    150 void Camera::Frustum::CalcNPVertexIndices(int *indices) 
    151 { 
    152         for (int i = 0; i < 6; ++ i) 
    153         { 
    154                 // n-vertex 
    155                 indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal); 
    156                 // p-vertex 
    157                 indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal);    
     121                const float invLength = -1.0f / Magnitude(frustum.mClipPlanes[i].mNormal); 
     122         
     123                frustum.mClipPlanes[i].mD *= invLength; 
     124                frustum.mClipPlanes[i].mNormal *= invLength; 
    158125        } 
    159126} 
     
    174141 
    175142 
    176 void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 
    177                                                    Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) 
     143void Camera::ComputePointsInternal(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 
     144                                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr, 
     145                                                                   const Vector3 &view, const Vector3 &right, const Vector3 &up, 
     146                                                                   const Vector3 &pos) const 
    178147{ 
    179148        float z_near = mNear; 
     
    188157        const float h_far = w_far / GetAspect(); 
    189158 
    190         const Vector3 view = GetDirection(); 
    191         const Vector3 fc = mPosition + view * z_far;  
    192          
    193         const Vector3 up = GetUpVector(); 
    194         const Vector3 right = GetRightVector(); 
    195  
     159        const Vector3 fc = pos + view * z_far;  
     160         
    196161        Vector3 t1, t2; 
    197162 
     
    204169        fbr = fc - t1 + t2; 
    205170 
    206         const Vector3 nc = mPosition + view * z_near; 
     171        const Vector3 nc = pos + view * z_near; 
    207172         
    208173        t1 = h_near * 0.5f * up; 
     
    216181 
    217182 
     183void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 
     184                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const 
     185{ 
     186        ComputePointsInternal(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr,  
     187                                                  GetDirection(), GetRightVector(), GetUpVector(), GetPosition()); 
     188} 
     189 
     190 
     191void Camera::ComputeBasePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 
     192                                                           Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const 
     193{ 
     194        ComputePointsInternal(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr,  
     195                GetBaseDirection(), GetBaseRightVector(), GetBaseUpVector(), Vector3(0, 0, 0)); 
     196} 
     197 
     198 
     199Polyhedron *Camera::CalcClippedFrustum(const AxisAlignedBox3 &box) const 
     200{ 
     201        Vector3 ftl, ftr, fbl, fbr; 
     202        Vector3 ntl, ntr, nbl, nbr; 
     203 
     204        VertexArray sides[6]; 
     205 
     206        ComputeBasePoints(ftl, ftr, fbl, fbr,ntl, ntr, nbl, nbr); 
     207        //ComputePoints(ftl, ftr, fbl, fbr,ntl, ntr, nbl, nbr); 
     208 
     209        for (int i = 0; i < 6; i ++) 
     210                sides[i].resize(4); 
     211         
     212        // left, right 
     213        sides[0][0] = ntl; sides[0][1] = ftl; sides[0][2] = fbl; sides[0][3] = nbl; 
     214        sides[1][0] = ntr; sides[1][1] = ftr; sides[1][2] = fbr; sides[1][3] = nbr; 
     215        // bottom, top 
     216        sides[2][0] = ntl; sides[2][1] = ftl; sides[2][2] = ftr; sides[2][3] = ntr; 
     217        sides[3][0] = nbl; sides[3][1] = fbl; sides[3][2] = fbr; sides[3][3] = nbr; 
     218        // near, far 
     219        sides[4][0] = ntl; sides[4][1] = ntr; sides[4][2] = nbr; sides[4][3] = nbl;  
     220        sides[5][0] = ntl; sides[5][1] = ntr; sides[5][2] = nbr; sides[5][3] = nbl; 
     221 
     222 
     223        ////////// 
     224        //-- compute polyhedron 
     225 
     226        PolygonContainer polygons; 
     227 
     228        for (int i = 0; i < 6; ++ i) 
     229        { 
     230                Polygon3 *poly = new Polygon3(sides[i]); 
     231                polygons.push_back(poly); 
     232        } 
     233 
     234        Polyhedron *p = new Polyhedron(polygons); 
     235 
     236        //Polyhedron *clippedPolyhedron = box.CalcIntersection(po); 
     237        //DEL_PTR(p); 
     238 
     239        //return clippedPolyhedron; 
     240        return p; 
     241} 
     242 
     243 
    218244void Camera::SetOrtho(bool ortho) 
    219245{ 
     
    240266        Vector3 ndir = -Normalize(dir); 
    241267 
    242         //mPitch = atan2(dir.x, dir.z); 
    243         //mYaw = atan2(dir.y, sqrt((dir.x * dir.x) + (dir.z * dir.z))); 
    244  
    245         //mPitch = atan2(dir.x, dir.y); 
    246268        mPitch = -atan2(ndir.x, ndir.y); 
    247269        mYaw = atan2(ndir.z, sqrt((ndir.x * ndir.x) + (ndir.y * ndir.y))); 
     
    277299Vector3 Camera::GetRightVector() const  
    278300{  
    279         return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]); 
    280                  
    281 } 
    282  
    283  
    284 } 
    285  
     301        return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]);            
     302} 
     303 
     304 
     305Vector3 Camera::GetBaseDirection() const 
     306{  
     307        return -Vector3(mBaseOrientation.x[0][2], mBaseOrientation.x[1][2], mBaseOrientation.x[2][2]); 
     308} 
     309 
     310 
     311Vector3 Camera::GetBaseUpVector() const  
     312{  
     313        return Vector3(mBaseOrientation.x[0][1], mBaseOrientation.x[1][1], mBaseOrientation.x[2][1]); 
     314} 
     315 
     316 
     317Vector3 Camera::GetBaseRightVector() const  
     318{  
     319        return Vector3(mBaseOrientation.x[0][0], mBaseOrientation.x[1][0], mBaseOrientation.x[2][0]); 
     320} 
     321 
     322 
     323Frustum::Frustum(const Matrix4x4 &trafo) 
     324{ 
     325        ////////// 
     326        //-- extract the plane equations 
     327 
     328        for (int i = 0; i < 4; ++ i) 
     329        { 
     330                mClipPlanes[Frustum::RIGHT_PLANE][i] = trafo.x[i][3] - trafo.x[i][0]; 
     331                mClipPlanes[Frustum::LEFT_PLANE][i] = trafo.x[i][3] + trafo.x[i][0];  
     332                mClipPlanes[Frustum::BOTTOM_PLANE][i] = trafo.x[i][3] + trafo.x[i][1]; 
     333                mClipPlanes[Frustum::TOP_PLANE][i] = trafo.x[i][3] - trafo.x[i][1];  
     334                mClipPlanes[Frustum::FAR_PLANE][i] = trafo.x[i][3] - trafo.x[i][2];  
     335                mClipPlanes[Frustum::NEAR_PLANE][i] = trafo.x[i][3] + trafo.x[i][2]; 
     336        } 
     337} 
     338 
     339 
     340void Frustum::EnclosePolyhedron(const Polyhedron &polyhedron) 
     341{ 
     342        VertexArray vertices; 
     343        polyhedron.CollectVertices(vertices); 
     344 
     345        for (int i = 0; i < 6; ++ i) 
     346        { 
     347                Plane3 &plane = mClipPlanes[i]; 
     348 
     349                //cout << "p" << i << " " << plane.mD  << endl; 
     350 
     351                VertexArray::const_iterator it, it_end = vertices.end(); 
     352 
     353                float minDist = 1e20; 
     354 
     355                for (it = vertices.begin(); it != it_end; ++ it) 
     356                { 
     357                        float dist = plane.Distance(*it); 
     358 
     359                        if (dist < minDist) 
     360                        { 
     361                                cout << "minDist: " << dist << endl; 
     362                                minDist = dist; 
     363                        } 
     364                } 
     365 
     366                plane.mD += minDist; 
     367        } 
     368} 
     369 
     370 
     371void Frustum::ExtractTransformation(Matrix4x4 &m) const 
     372{ 
     373        for (int i = 0; i < 4; ++ i) 
     374        { 
     375                m.x[i][0] = (mClipPlanes[LEFT_PLANE][i] - mClipPlanes[RIGHT_PLANE][i]) * 0.5f; 
     376                m.x[i][1] = (mClipPlanes[BOTTOM_PLANE][i] - mClipPlanes[TOP_PLANE][i]) * 0.5f; 
     377                m.x[i][2] = (mClipPlanes[NEAR_PLANE][i] - mClipPlanes[FAR_PLANE][i]) * 0.5f; 
     378                m.x[i][3] = (mClipPlanes[LEFT_PLANE][i] + mClipPlanes[RIGHT_PLANE][i]) * 0.5f; 
     379        } 
     380} 
     381 
     382 
     383 
     384} 
     385 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h

    r2838 r2911  
    1010{ 
    1111 
     12class Matrix4x4; 
     13class Polyhedron; 
     14 
     15/** Class representing a frustum. 
     16*/ 
     17class Frustum 
     18{ 
     19        friend class Camera; 
     20 
     21public: 
     22 
     23        Frustum() {}; 
     24 
     25        Frustum(const Matrix4x4 &trafo); 
     26 
     27        enum { RIGHT_PLANE, LEFT_PLANE, BOTTOM_PLANE, TOP_PLANE, FAR_PLANE, NEAR_PLANE}; 
     28        /** Resizes the current frustum so that it fully encloses the given polyhedron 
     29        */ 
     30        void EnclosePolyhedron(const Polyhedron &polyhedron); 
     31 
     32        void ExtractTransformation(Matrix4x4 &m) const; 
     33 
     34 
     35        /// the 6 clip planes 
     36        Plane3 mClipPlanes[6]; 
     37}; 
     38 
    1239 
    1340class Camera 
    1441{ 
    1542public: 
    16          
    17         /** Small struct representing a frustum. 
    18         */ 
    19         struct Frustum 
    20         { 
    21                 /// the 6 clip planes 
    22                 Plane3 mClipPlanes[6]; 
    23  
    24                 void CalcNPVertexIndices(int *indices); 
    25         }; 
    2643         
    2744        Camera(); 
     
    3350 
    3451        inline Vector3 GetPosition() const { return mPosition; } 
     52         
    3553        Vector3 GetDirection() const; 
    3654        Vector3 GetUpVector() const; 
    3755        Vector3 GetRightVector() const; 
     56 
     57        Vector3 GetBaseDirection() const; 
     58        Vector3 GetBaseUpVector() const; 
     59        Vector3 GetBaseRightVector() const; 
    3860 
    3961        inline float GetFov() const { return mFovy; } 
     
    5173        */ 
    5274        void GetModelViewMatrix(Matrix4x4 &mat); 
    53  
     75        /** Calculates a frustum from the projection and the modelview matrix. 
     76        */ 
    5477        void CalcFrustum(Frustum &frustum); 
    5578        /** Computes the extremal points of this frustum. 
    5679        */ 
    5780        void ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 
    58                                Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr); 
    59          
     81                               Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const; 
     82        /** Computes the extremal points of the base frustum. 
     83        */ 
     84        void ComputeBasePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 
     85                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const; 
    6086        /** Returns the near plane. 
    6187        */ 
     
    83109        void SetDirection(const Vector3 &direction); 
    84110 
     111        /** Calculates the intersection of the frustum with the box, 
     112                returns resultin polyhedron as array of polygons. 
     113        */ 
     114        Polyhedron *CalcClippedFrustum(const AxisAlignedBox3 &box) const; 
    85115 
    86116 
     
    91121        void CalculateFromPitchAndYaw(); 
    92122 
     123        void ComputePointsInternal(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 
     124                                                           Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr, 
     125                                                           const Vector3 &view, const Vector3 &right, const Vector3 &up, 
     126                                                           const Vector3 &pos) const; 
    93127 
    94128        //////////////// 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.h

    r2853 r2911  
    88{ 
    99 
    10 /** 3D Plane. 
     10/** Class describing a 3D Plane. 
    1111*/ 
    1212class Plane3  
     
    3535        /** Returns 1 if v is on front side, -1 if on back side, 0 if on plane. 
    3636        */ 
    37         int Side(const Vector3 &v, const float threshold = 1e-6) const; 
     37        int Side(const Vector3 &v, float threshold = 1e-6) const; 
    3838        /** Finds intersection of line segment between points a and b with plane. 
    3939            @param a start point 
     
    5252        float FindT(const Vector3 &a, const Vector3 &b) const; 
    5353 
     54        operator const float*() const  
     55        {  
     56                return (const float*) this;  
     57        } 
     58 
     59        float& operator[] (const int inx)  
     60        { 
     61                return (&mNormal.x)[inx]; 
     62        } 
     63 
     64        const float &operator[] (const int inx) const  
     65        { 
     66                return *(&mNormal.x + inx); 
     67        } 
    5468 
    5569        /////////// 
     
    5973 
    6074        friend bool PlaneIntersection(const Plane3 &p1, const Plane3 &p2); 
     75         
    6176 
    6277        friend std::ostream &operator<<(std::ostream &s, const Plane3 &p)  
     
    7388}; 
    7489 
     90 
    7591} 
    7692#endif 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.h

    r2903 r2911  
    2828}; 
    2929 
    30 /** Class generating random samples on a disc or a sphere, respectively. 
     30/** Class generating random samples on a disc or in a sphere, respectively. 
    3131*/ 
    3232class SampleGenerator 
     
    6161 
    6262 
     63/** This class generates random samples on a disc respecting 
     64        the poisson disc condition (all samples at least distance d apart) 
     65        according to some d related to the number of samples. 
     66*/ 
    6367class PoissonDiscSampleGenerator: public SampleGenerator 
    6468{ 
     
    7478}; 
    7579 
    76  
     80/** This class generates random spherical samples. 
     81*/ 
    7782class SphericalSampleGenerator: public SampleGenerator 
    7883{ 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp

    r2899 r2911  
    44#include "RenderTraverser.h" 
    55#include "Light.h" 
     6#include "Polygon3.h" 
     7#include "Polyhedron.h" 
     8 
    69#include <IL/il.h> 
    710#include <assert.h> 
    811 
     12 
    913using namespace std; 
    1014 
     
    1216namespace CHCDemoEngine 
    1317{ 
     18 
     19 
     20static Polyhedron *polyhedron = NULL; 
    1421 
    1522 
     
    97104 
    98105 
    99 void ShadowMap::ComputeShadowMap(RenderTraverser *renderer) 
     106void ShadowMap::DrawPolys() 
     107{ 
     108        if (!polyhedron) return; 
     109 
     110        cout << "here3" << endl; 
     111 
     112        for (size_t i = 0; i < polyhedron->NumPolygons(); ++ i) 
     113        { 
     114                float r = (float)i / polyhedron->NumPolygons(); 
     115                float g = 1; 
     116                float b = 1; 
     117 
     118                glColor3f(r, g, b); 
     119 
     120                glBegin(GL_LINE_LOOP); 
     121 
     122                Polygon3 *poly = polyhedron->GetPolygons()[i]; 
     123 
     124                for (size_t j = 0; j < poly->mVertices.size(); ++ j) 
     125                { 
     126                        Vector3 v = poly->mVertices[j]; 
     127                        glVertex3d(v.x, v.y, v.z); 
     128                } 
     129 
     130                glEnd(); 
     131        } 
     132} 
     133 
     134 
     135void ShadowMap::CalcFocussedFrustum(const Matrix4x4 &camProjView,  
     136                                                                        const Matrix4x4 &lightProjView,  
     137                                                                        Matrix4x4 &focussed) 
     138{ 
     139        /////////////////// 
     140        //-- First step: calc clipped frustum 
     141 
     142        DEL_PTR(polyhedron); 
     143        polyhedron = mCamera->CalcClippedFrustum(mSceneBox); 
     144 
     145        if (polyhedron) 
     146        { 
     147                // second step: focus light frustum on clipped frustum 
     148                Frustum lightFrustum(lightProjView); 
     149 
     150                for (int i = 0; i < 6; ++ i) 
     151                { 
     152                        // the clipping planes look outward the frustum, 
     153                        // so distances > 0 mean that a point is outside 
     154                        const float invLength = 1.0f / Magnitude(lightFrustum.mClipPlanes[i].mNormal); 
     155 
     156                        lightFrustum.mClipPlanes[i].mD *= invLength; 
     157                        lightFrustum.mClipPlanes[i].mNormal *= invLength; 
     158                } 
     159 
     160                //lightFrustum.EnclosePolyhedron(*polyhedron); 
     161                lightFrustum.ExtractTransformation(focussed); 
     162        } 
     163} 
     164 
     165 
     166void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView) 
    100167{ 
    101168        const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f); 
     
    107174        Vector3 pos = mSceneBox.Center(); 
    108175 
     176        Matrix4x4 camView; 
     177        mCamera->GetModelViewMatrix(camView); 
     178 
    109179        pos -= mLight->GetDirection() * Magnitude(mSceneBox.Diagonal() * 0.5f); 
    110         mShadowCam->SetPosition(pos); 
     180        //mShadowCam->SetPosition(pos); 
    111181 
    112182        mFbo->Bind(); 
     
    153223        mShadowCam->GetProjectionMatrix(lightProj); 
    154224 
     225        Matrix4x4 oldLightProjView = lightView * lightProj; 
     226        Matrix4x4 newLightProj; 
     227 
     228        CalcFocussedFrustum(projView, lightProj, newLightProj); 
     229        //CalcFocussedFrustum(projView, oldLightProjView, newLightProj); 
     230 
     231        Matrix4x4 dummyMat; 
     232        glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat.x); 
     233 
     234 
     235        cout << "old:\n" << dummyMat << endl; 
     236 
     237 
     238        glMatrixMode(GL_PROJECTION); 
     239        glLoadMatrixf((float *)newLightProj.x); 
     240         
     241        glMatrixMode(GL_MODELVIEW); 
     242        //glLoadIdentity(); 
     243 
     244        Matrix4x4 dummyMat3; 
     245        glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat3.x); 
     246 
     247        cout << "new:\n" << newLightProj << endl; 
     248 
     249        lightView = camView; 
     250        mLightProjView = lightView * newLightProj; 
     251        //mLightProjView = newLightProj; 
     252 
     253        //cout << "old: \n" << oldLightProjView << " new: \n " << mLightProjView << endl; 
     254         
     255 
    155256        static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f, 
    156257                                                                0.0f, 0.5f, 0.0f, 0.5f, 
    157258                                                                0.0f, 0.0f, 0.5f, 0.5f, 
    158                                                                 0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1] 
    159  
    160         Matrix4x4 lightProjView = lightView * lightProj; 
    161         mTextureMatrix = lightProjView * biasMatrix;  
     259                                                                0.0f, 0.0f, 0.0f, 1.0f); 
     260 
     261        mTextureMatrix = mLightProjView * biasMatrix;  
     262        //mTextureMatrix = oldLightProjView * biasMatrix;  
     263 
     264 
    162265 
    163266 
     
    176279 
    177280        glPopAttrib(); 
    178          
    179         /*float *data = new float[mSize * mSize]; 
     281        /* 
     282        float *data = new float[mSize * mSize]; 
    180283 
    181284        GrabDepthBuffer(data, mFbo->GetDepthTex()); 
     
    183286 
    184287        delete [] data; 
    185         */ 
     288         
    186289        PrintGLerror("shadow map"); 
    187  
     290*/ 
    188291        FrameBufferObject::Release(); 
    189292} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h

    r2897 r2911  
    3434        /** Computes the shadow map 
    3535        */ 
    36         void ComputeShadowMap(RenderTraverser *traverser); 
     36        void ComputeShadowMap(RenderTraverser *traverser, const Matrix4x4 &projView); 
    3737        /** Returns computed shadow texture. 
    3838        */ 
     
    5353 
    5454 
     55        static void DrawPolys(); 
     56 
     57 
    5558protected: 
     59 
     60        void CalcFocussedFrustum(const Matrix4x4 &camProjView,  
     61                                                         const Matrix4x4 &lightProjView,  
     62                                                         Matrix4x4 &focussed); 
     63 
    5664 
    5765        /// the scene bounding box 
     
    6977        /// the scene camera 
    7078        Camera *mCamera; 
     79 
     80        Matrix4x4 mLightProjView; 
    7181}; 
    7282 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Vector3.h

    r2844 r2911  
    22#define _Vector3_h__ 
    33 
    4 #include <iostream> 
     4 
    55#include "common.h" 
    66#include <math.h> 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp

    r2891 r2911  
    55#include "Bvh.h" 
    66#include "RenderState.h" 
     7#include "ShadowMapping.h" 
    78 
    89#include <stack> 
     
    150151        glDisable(GL_DEPTH_TEST); 
    151152 
    152         RenderFrustum(); 
     153        //RenderFrustum(); 
     154 
     155        ShadowMap::DrawPolys(); 
    153156 
    154157        Vector3 pos = mCamera->GetPosition(); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2905 r2911  
    4444static Environment env; 
    4545 
    46 #define MAX_DEPTH_CONST myfar //10.0f; 
     46#define MAX_DEPTH_CONST 10.0f 
    4747 
    4848// fbo 
     
    336336        camera = new Camera(winWidth, winHeight, fov); 
    337337        camera->SetNear(nearDist); 
    338         camera->SetFar(1000); 
     338        camera->SetFar(100000); 
    339339 
    340340        camera->SetDirection(camDir); 
     
    933933 
    934934                        // the scene is rendered withouth any shading    
    935                         shadowMap->ComputeShadowMap(shadowTraverser); 
     935                        shadowMap->ComputeShadowMap(shadowTraverser, matProjectionView); 
    936936 
    937937                        // change back state 
     
    15451545 
    15461546        // hack: set far plane for viz 
    1547         camera->SetFar(0.35f * Magnitude(box.Diagonal())); 
     1547        //camera->SetFar(0.35f * Magnitude(box.Diagonal())); 
     1548        camera->SetFar(1); 
    15481549 
    15491550        const float offs = box.Size().x * 0.3f; 
     
    15931594        visualization->Render(); 
    15941595 
    1595          
    15961596         
    15971597        // reset previous settings 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h

    r2903 r2911  
    2626class OcclusionQuery; 
    2727class SceneEntity; 
     28 
     29class Shape; 
     30struct LODLevel; 
     31class Polygon3; 
     32 
    2833 
    2934 
     
    483488typedef std::queue<BvhNode *> BvhNodeQueue; 
    484489 
     490 
     491typedef std::vector<Shape *> ShapeContainer; 
     492typedef std::vector<LODLevel *> LODLevelContainer; 
     493typedef std::vector<Polygon3 *> PolygonContainer; 
     494 
    485495static std::ofstream Debug("debug.log"); 
    486496 
     
    488498#define INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES 1000 
    489499 
    490 class Shape; 
    491 typedef std::vector<Shape *> ShapeContainer; 
    492  
    493 struct LODLevel; 
    494 typedef std::vector<LODLevel *> LODLevelContainer; 
    495  
    496 } 
    497  
    498 #endif 
    499  
    500  
    501  
    502  
    503  
    504  
    505  
    506  
     500 
     501} 
     502 
     503#endif 
     504 
     505 
     506 
     507 
     508 
     509 
     510 
     511 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r2895 r2911  
    102102         
    103103        float4 lightSpacePos = mul(shadowMatrix, position); 
     104        lightSpacePos /= lightSpacePos.w; 
    104105 
    105106        float shadowDepth[9]; 
     
    123124        OUT.color = col; 
    124125         
    125         float depth = lightSpacePos.z / lightSpacePos.w; 
     126        float depth = lightSpacePos.z; 
    126127 
    127128        float d = 0.0f; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r2904 r2911  
    8888                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); 
    8989 
     90#if 0 
    9091                // if surface normal perpenticular to view dir, approx. half of the samples will not count 
    9192                // => compensate for this (on the other hand, projected sampling area could be larger!) 
    92                 //const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
    93                 //total_ao += cos_angle * distance_intensity * view_correction; 
    94  
     93                const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
     94                total_ao += cos_angle * distance_intensity * view_correction; 
     95#endif 
    9596                total_ao += cos_angle * distance_intensity; 
    96  
    97                 //total_ao /= j; 
    9897        } 
    9998 
Note: See TracChangeset for help on using the changeset viewer.