OSGPerspectiveShadowMapHandler.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *               Copyright (C) 2000-2002 by the OpenSG Forum                 *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 #include <stdlib.h>
00040 #include <stdio.h>
00041
00042 #include "OSGPerspectiveShadowMapHandler.h"
00043 #include "OSGShadowStage.h"
00044 #include "OSGShadowStageData.h"
00045 #include "OSGRenderAction.h"
00046 #include "OSGSpotLight.h"
00047 #include "OSGDirectionalLight.h"
00048 #include "OSGMatrixUtility.h"
00049
00050 OSG_BEGIN_NAMESPACE
00051
00052 #include "ShaderCode/OSGPerspectiveShadowMapShaderCode.cinl"
00053
00054 PerspectiveShadowMapHandler::PerspectiveShadowMapHandler(
00055     ShadowStage     *pSource,
00056     ShadowStageData *pData  ) :
00057
00058      Inherited     (pSource,
00059                     pData  ),
00060     _perspectiveLPM(       ),
00061     _perspectiveLVM(       ),
00062     _tiledeco      (NULL   ),
00063     _matrixDeco    (NULL   ),
00064     _blender       (NULL   ),
00065     _matrixCam2    (NULL   ),
00066     _shadowCmat    (NULL   ),
00067     _shadowSHL     (NULL   ),
00068     _shadowCubeSHL (NULL   ),
00069     _shadowSHL2    (NULL   ),
00070     _shadowSHL3    (NULL   ),
00071     _shadowSHL4    (NULL   ),
00072     _shadowSHL5    (NULL   ),
00073     _shadowSHL6    (NULL   ),
00074     _shadowSHL7    (NULL   ),
00075     _pPoly         (NULL   ),
00076     _firstRun      (0      )
00077 {
00078     _uiMode = ShadowStage::PERSPECTIVE_SHADOW_MAP;
00079
00080     _blender = BlendChunk::createLocal();
00081
00082     _blender->setAlphaFunc(GL_GEQUAL);
00083     _blender->setAlphaValue(0.99f);
00084
00085     _matrixCam2 = MatrixCamera::createLocal();
00086
00087
00088     _shadowFactorMap2O     = TextureObjChunk::createLocal();
00089     _shadowFactorMapImage2 = Image          ::createLocal();
00090
00091     _shadowFactorMap2O->setImage         (_shadowFactorMapImage2);
00092     _shadowFactorMap2O->setInternalFormat(GL_RGB);
00093     _shadowFactorMap2O->setExternalFormat(GL_RGB);
00094     _shadowFactorMap2O->setMinFilter     (GL_LINEAR);
00095     _shadowFactorMap2O->setMagFilter     (GL_LINEAR);
00096     _shadowFactorMap2O->setWrapS         (GL_REPEAT);
00097     _shadowFactorMap2O->setWrapT         (GL_REPEAT);
00098     _shadowFactorMap2O->setTarget        (GL_TEXTURE_2D);
00099
00100
00101     //SHL Chunk 1
00102     _shadowSHL = SimpleSHLChunk::createLocal();
00103     _shadowSHL->setVertexProgram  (_lisp_shadow_vp);
00104     _shadowSHL->setFragmentProgram(_lisp_shadow_fp);
00105
00106     _shadowSHL2 = SimpleSHLChunk::createLocal();
00107     _shadowSHL2->setVertexProgram  (_lisp_shadow2_vp);
00108     _shadowSHL2->setFragmentProgram(_lisp_shadow2_fp);
00109
00110     _shadowSHL3 = SimpleSHLChunk::createLocal();
00111     _shadowSHL3->setVertexProgram  (_lisp_shadow3_vp);
00112     _shadowSHL3->setFragmentProgram(_lisp_shadow3_fp);
00113
00114     _shadowSHL4 = SimpleSHLChunk::createLocal();
00115     _shadowSHL4->setVertexProgram  (_lisp_shadow4_vp);
00116     _shadowSHL4->setFragmentProgram(_lisp_shadow4_fp);
00117
00118     _shadowSHL5 = SimpleSHLChunk::createLocal();
00119     _shadowSHL5->setVertexProgram  (_lisp_shadow5_vp);
00120     _shadowSHL5->setFragmentProgram(_lisp_shadow5_fp);
00121
00122     _shadowSHL6 = SimpleSHLChunk::createLocal();
00123     _shadowSHL6->setVertexProgram  (_lisp_shadow6_vp);
00124     _shadowSHL6->setFragmentProgram(_lisp_shadow6_fp);
00125
00126     _shadowSHL7 = SimpleSHLChunk::createLocal();
00127     _shadowSHL7->setVertexProgram  (_lisp_shadow7_vp);
00128     _shadowSHL7->setFragmentProgram(_lisp_shadow7_fp);
00129
00130
00131     //SHL Chunk 3
00132     _shadowCubeSHL = SimpleSHLChunk::createLocal();
00133     _shadowCubeSHL->setVertexProgram  (_lisp_shadowCube_vp);
00134     _shadowCubeSHL->setFragmentProgram(_lisp_shadowCube_fp);
00135
00136     _shadowCmat = ChunkMaterial::createLocal();
00137
00138     _pPoly = PolygonChunk::createLocal();
00139
00140     _unlitMat->addChunk(_pPoly);
00141
00142     _matrixDeco = MatrixCameraDecorator::createLocal();
00143
00144 }
00145
00146 PerspectiveShadowMapHandler::~PerspectiveShadowMapHandler(void)
00147 {
00148     _tiledeco         = NULL;
00149     _matrixDeco       = NULL;
00150     _blender          = NULL;
00151     _matrixCam2       = NULL;
00152     _shadowCmat       = NULL;
00153     _shadowSHL        = NULL;
00154     _shadowCubeSHL    = NULL;
00155     _shadowSHL2       = NULL;
00156     _shadowSHL3       = NULL;
00157     _shadowSHL4       = NULL;
00158     _shadowSHL5       = NULL;
00159     _shadowSHL6       = NULL;
00160     _shadowSHL7       = NULL;
00161     _pPoly            = NULL;
00162 }
00163
00164
00165 void PerspectiveShadowMapHandler::calcPerspectiveSpot(Matrix  &_LPM,
00166                                                       Matrix  &_LVM,
00167                                                       UInt32    num,
00168                                                       DrawEnv *pEnv)
00169 {
00170     const ShadowStageData::LightStore  &vLights      =
00171         _pStageData->getLights();
00172
00173     const ShadowStageData::CamStore    &vLCams       =
00174         _pStageData->getLightCameras();
00175
00176     Matrix  CPM, CVM;
00177
00178     CVM = pEnv->getCameraViewing();
00179     CPM = pEnv->getCameraProjection();
00180
00181     Matrix  LPM, LVM;
00182
00183     vLCams[num]->getViewing(LVM,
00184                             pEnv->getWindow()->getWidth(),
00185                             pEnv->getWindow()->getHeight());
00186
00187     vLCams[num]->getProjection(LPM,
00188                                pEnv->getWindow()->getWidth(),
00189                                pEnv->getWindow()->getHeight());
00190
00191     Matrix  LPVM;
00192     LPVM = LPM;
00193     LPVM.mult(LVM);
00194
00195     Matrix  eyeProjView = CPM;
00196     eyeProjView.mult(CVM);
00197     Matrix  invEyeProjView = eyeProjView;
00198     invEyeProjView.invert();
00199
00200     //Scene AABox Extrempunkte holen (Weltkoordinaten)
00201     Pnt3f   sceneMax = _pStageData->getLightRoot(num)->getVolume().getMax();
00202     Pnt3f   sceneMin = _pStageData->getLightRoot(num)->getVolume().getMin();
00203
00204     Pnt3f   bb0(sceneMin[0], sceneMin[1], sceneMin[2]);
00205     Pnt3f   bb1(sceneMax[0], sceneMin[1], sceneMin[2]);
00206     Pnt3f   bb2(sceneMax[0], sceneMax[1], sceneMin[2]);
00207     Pnt3f   bb3(sceneMin[0], sceneMax[1], sceneMin[2]);
00208     Pnt3f   bb4(sceneMin[0], sceneMin[1], sceneMax[2]);
00209     Pnt3f   bb5(sceneMax[0], sceneMin[1], sceneMax[2]);
00210     Pnt3f   bb6(sceneMax[0], sceneMax[1], sceneMax[2]);
00211     Pnt3f   bb7(sceneMin[0], sceneMax[1], sceneMax[2]);
00212
00213     //LightPos holen
00214     Pnt3f   lPos(0,0,0);
00215
00216     bool    isDirect = false;
00217     bool    useStd = false;
00218
00219     if(vLights[num].second->getType() == DirectionalLight::getClassType())
00220     {
00221         isDirect = true;
00222     }
00223
00224     if(!isDirect)
00225     {
00226         if(vLights[num].second->getType() == PointLight::getClassType())
00227         {
00228             PointLight *tmpPoint;
00229
00230             tmpPoint = dynamic_cast<PointLight *>(vLights[num].second.get());
00231
00232             lPos = tmpPoint->getPosition();
00233
00234             if(tmpPoint->getBeacon() != NULL)
00235             {
00236                 Matrix  m = tmpPoint->getBeacon()->getToWorld();
00237                 m.mult(lPos, lPos);
00238             }
00239         }
00240
00241         if(vLights[num].second->getType() == SpotLight::getClassType())
00242         {
00243             SpotLight *tmpSpot;
00244
00245             tmpSpot = dynamic_cast<SpotLight *>(vLights[num].second.get());
00246
00247             lPos = tmpSpot->getPosition();
00248             if(tmpSpot->getBeacon() != NULL)
00249             {
00250                 Matrix  m = tmpSpot->getBeacon()->getToWorld();
00251                 m.mult(lPos, lPos);
00252             }
00253         }
00254
00255         // Wenn Lichtposition innerhalb der Scene-BB ist standard 
00256         // Shadow Mapping verwenden
00257
00258         if(lPos[0] > sceneMin[0] && lPos[1] > sceneMin[1] &&
00259            lPos[2] > sceneMin[2] && lPos[0] < sceneMax[0] &&
00260            lPos[1] < sceneMax[1] && lPos[2] < sceneMax[2])
00261         {
00262             _LPM = LPM;
00263             _LVM = LVM;
00264             useStd = true;
00265         }
00266         //beleuchtet das Spotlight die komplette Szene? 
00267         //Wenn nicht -> standard Shadow Mapping
00268         else if(vLights[num].second->getType() == SpotLight::getClassType() &&
00269                 !bbInsideFrustum(sceneMin, sceneMax, LPVM))
00270         {
00271             _LPM = LPM;
00272             _LVM = LVM;
00273             useStd = true;
00274         }
00275     }
00276
00277     if(!useStd)
00278     {
00279
00280         //Lichtrichtung im Post-Perspektivischen Raum des Lichtes
00281         Vec3f   lightDir(0,0,1);
00282
00283         //Kamerapunkt holen
00284         Pnt3f   eyePos(0,0,0);
00285
00286         Matrix m2 = pEnv->getCameraToWorld();
00287
00288         m2.mult(eyePos, eyePos);
00289
00290         LPVM.multFull(eyePos, eyePos);
00291
00292         //Ist die Kamera hinter der Lichtquelle?
00293         bool    cBehindL = false;
00294         if(eyePos[2] < -1)
00295             cBehindL = true;
00296         else if(eyePos[2] > 1)
00297             cBehindL = true;
00298
00299         if(cBehindL)
00300         {
00301             //Trick: Near = -Far / Far = Near
00302             //Gibt leider Probleme im Grenzbereich ...
00303             /*double tmpNear;
00304                tmpNear = _pStage->_lightCameras[num]->getNear();
00305                _shadowVP->_lightCameras[num]->setNear(
00306                -_shadowVP->_lightCameras[num]->getFar());
00307                _shadowVP->_lightCameras[num]->setFar(tmpNear);
00308                _shadowVP->_lightCameras[num]->getProjection(
00309               LPM,_shadowVP->getParent()->getWidth(),
00310               _shadowVP->getParent()->getHeight());
00311                LPVM = LPM;
00312                LPVM.mult(LVM);
00313                lightDir = Vec3f(0,0,-1);*/
00314
00315             //Standard Shadow Mapping benutzen
00316             _LVM = LVM;
00317             _LPM = LPM;
00318         }
00319         else
00320         {
00321             //ViewDir
00322             Vec3f               viewDir(0,0,-1);
00323
00324             Matrix m3 = pEnv->getCameraToWorld();
00325
00326             m3  .mult(viewDir, viewDir);
00327             LPVM.mult(viewDir, viewDir);
00328             viewDir.normalize();
00329
00330             //Body berechnen
00331             std::vector<Pnt3f>  points;
00332             calcHull2(&points, invEyeProjView, sceneMin, sceneMax, LPVM);
00333
00334             //Kopie erstellen
00335             std::vector<Pnt3f>  pointsCopy;
00336             for(UInt32 i = 0;i < points.size();i++)
00337                 pointsCopy.push_back(points[i]);
00338
00339             bool                correctAngle = false;
00340             if((osgAbs(viewDir[0]) - osgAbs(lightDir[0])) < 0.01 &&
00341                (osgAbs(viewDir[1]) - osgAbs(lightDir[1])) < 0.01 &&
00342                (osgAbs(viewDir[2]) - osgAbs(lightDir[2])) < 0.01)
00343             {
00344                 viewDir[0] += 0.01f;
00345                 correctAngle = true;
00346             }
00347
00348             double              dotProd = viewDir.dot(lightDir);
00349             double              sinGamma;
00350             sinGamma = sqrt(1.0 - dotProd * dotProd);
00351             if(correctAngle)
00352                 viewDir[0] -= 0.01f;
00353
00354             //up berechnen
00355             Vec3f               up99;
00356             calcUpVec2(up99, viewDir, lightDir);
00357
00358             Matrix              lView22;
00359             MatrixLookAt(lView22, eyePos, eyePos + lightDir, up99);
00360             lView22.invert();
00361
00362             // Punkte ins neue KKS transformieren
00363             for(UInt32 i = 0;i < points.size();i++)
00364             {
00365                 Pnt3f   tmpPnt(points[i][0], points[i][1], points[i][2]);
00366                 lView22.multFull(tmpPnt, tmpPnt);
00367                 points[i] = tmpPnt;
00368             }
00369
00370             // BBox um Punkte bestimmen
00371             Pnt3f               min99, max99;
00372             calcCubicHull2(min99, max99, &points);
00373
00374             Real32              factor = 1.0 / sinGamma;
00375
00376             Real32              nearDist = 1.0 - sinGamma;
00377             if(nearDist < 0.01)
00378                 nearDist = 0.01f;
00379
00380             Real32              z_n = factor * nearDist;
00381             Real32              d = osgAbs(max99[1] - min99[1]);
00382             Real32              z_f = z_n + d * sinGamma;
00383             Real32              n = (z_n + sqrt(z_f * z_n)) / sinGamma;
00384             Real32              f = n + d;
00385
00386             Pnt3f               pos99;
00387
00388             pos99[0] = eyePos[0] + (up99[0] * -(n - nearDist));
00389             pos99[1] = eyePos[1] + (up99[1] * -(n - nearDist));
00390             pos99[2] = eyePos[2] + (up99[2] * -(n - nearDist));
00391
00392             Matrix              lView222;
00393             MatrixLookAt(lView222, pos99, pos99 + lightDir, up99);
00394             lView222.invert();
00395
00396             // Lisp-Matrix erstellen
00397             Matrix              lispMtx99(1,0,0,0,
00398                                           0,(f + n) / (f - n),0,-2 * f * n /
00399                                           (f - n),
00400                                           0,0,1,0,
00401                                           0,1,0,0);
00402
00403             for(UInt32 i = 0;i < pointsCopy.size();i++)
00404             {
00405                 Pnt3f   tmpPnt = pointsCopy[i];
00406                 lView222 .multFull(tmpPnt, tmpPnt);
00407                 lispMtx99.multFull(tmpPnt, tmpPnt);
00408                 pointsCopy[i] = tmpPnt;
00409             }
00410
00411             calcCubicHull2(min99, max99, &pointsCopy);
00412
00413             Matrix              lProj;
00414             scaleTranslateToFit2(lProj, min99, max99);
00415
00416             Matrix              rh2lh;
00417             rh2lh.setIdentity();
00418             rh2lh.setScale(1.0, 1.0, -1.0);
00419
00420             lProj.mult(lispMtx99);
00421             lProj.multLeft(rh2lh);
00422
00423             points.clear();
00424             pointsCopy.clear();
00425
00426             _LPM = lProj;
00427             _LPM.mult(lView222);
00428             _LPM.mult(LPM);
00429             _LVM = LVM;
00430         }
00431     }
00432 }
00433
00434 void PerspectiveShadowMapHandler::calcHull2(
00435     std::vector<Pnt3f> *points,
00436     Matrix              invEyeProjMatrix,
00437     Pnt3f               sceneMin,
00438     Pnt3f               sceneMax,
00439     Matrix              LPVM)
00440 {
00441     // Die Funktion clippt das Kamera Frustum mit der Szenen Bounding Box 
00442     // und liefert eine Punktmenge zurck
00443
00444     //Unit Cube
00445     Pnt3f   vf0(-1,-1,-1);
00446     Pnt3f   vf1(1,-1,-1);
00447     Pnt3f   vf2(1,1,-1);
00448     Pnt3f   vf3(-1,1,-1);
00449     Pnt3f   vf4(-1,-1,1);
00450     Pnt3f   vf5(1,-1,1);
00451     Pnt3f   vf6(1,1,1);
00452     Pnt3f   vf7(-1,1,1);
00453
00454     //transform to World Coordinates
00455     invEyeProjMatrix.multFull(vf0, vf0);
00456     invEyeProjMatrix.multFull(vf1, vf1);
00457     invEyeProjMatrix.multFull(vf2, vf2);
00458     invEyeProjMatrix.multFull(vf3, vf3);
00459     invEyeProjMatrix.multFull(vf4, vf4);
00460     invEyeProjMatrix.multFull(vf5, vf5);
00461     invEyeProjMatrix.multFull(vf6, vf6);
00462     invEyeProjMatrix.multFull(vf7, vf7);
00463
00464     //Scene Bounding Box Points
00465     Pnt3f   bb0(sceneMin[0], sceneMin[1], sceneMin[2]);
00466     Pnt3f   bb1(sceneMax[0], sceneMin[1], sceneMin[2]);
00467     Pnt3f   bb2(sceneMax[0], sceneMax[1], sceneMin[2]);
00468     Pnt3f   bb3(sceneMin[0], sceneMax[1], sceneMin[2]);
00469     Pnt3f   bb4(sceneMin[0], sceneMin[1], sceneMax[2]);
00470     Pnt3f   bb5(sceneMax[0], sceneMin[1], sceneMax[2]);
00471     Pnt3f   bb6(sceneMax[0], sceneMax[1], sceneMax[2]);
00472     Pnt3f   bb7(sceneMin[0], sceneMax[1], sceneMax[2]);
00473
00474     //Build Planes of BBox
00475     Plane   bboxSides[6];
00476     //Front
00477     bboxSides[0] = Plane(bb0, bb1, bb2);
00478     //Back
00479     bboxSides[1] = Plane(bb7, bb6, bb5);
00480     //Left
00481     bboxSides[2] = Plane(bb0, bb3, bb7);
00482     //Right
00483     bboxSides[3] = Plane(bb2, bb1, bb5);
00484     //Top
00485     bboxSides[4] = Plane(bb6, bb7, bb3);
00486     //Bottom
00487     bboxSides[5] = Plane(bb5, bb1, bb0);
00488
00489     //Build Planes of View Frustum
00490     //Front
00491     Pnt3f   vfI[6][20];
00492     Pnt3f   vfO[20];
00493     UInt32  outCount;
00494
00495     //Define View Frustum as Clip Polygons
00496     //Front
00497     vfI[0][0] = vf0;
00498     vfI[0][1] = vf1;
00499     vfI[0][2] = vf2;
00500     vfI[0][3] = vf3;
00501     //Back
00502     vfI[1][0] = vf4;
00503     vfI[1][1] = vf5;
00504     vfI[1][2] = vf6;
00505     vfI[1][3] = vf7;
00506     //Left
00507     vfI[2][0] = vf0;
00508     vfI[2][1] = vf4;
00509     vfI[2][2] = vf7;
00510     vfI[2][3] = vf3;
00511     //Right
00512     vfI[3][0] = vf1;
00513     vfI[3][1] = vf5;
00514     vfI[3][2] = vf6;
00515     vfI[3][3] = vf2;
00516     //Top
00517     vfI[4][0] = vf3;
00518     vfI[4][1] = vf2;
00519     vfI[4][2] = vf6;
00520     vfI[4][3] = vf7;
00521     //Bottom
00522     vfI[5][0] = vf0;
00523     vfI[5][1] = vf1;
00524     vfI[5][2] = vf5;
00525     vfI[5][3] = vf4;
00526
00527     //Punktgroesse merken
00528     UInt32  vfSize[6];
00529     for(UInt32 i = 0;i < 6;i++)
00530         vfSize[i] = 4;
00531
00532     // speichert die anzahl der durchgefhrten Clips
00533     UInt32  sumClip = 0;
00534
00535     for(UInt32 i = 0;i < 6;i++)
00536     {
00537         //Kamerafrustum
00538         outCount = vfSize[i];
00539
00540         for(UInt32 j = 0;j < 6;j++)
00541         {
00542             //BBox
00543             if(vfSize[i] != 0)
00544             {
00545                 outCount = bboxSides[j].clip(vfI[i], vfO, vfSize[i]);
00546                 sumClip++;
00547                 vfSize[i] = outCount;
00548
00549                 for(UInt32 k = 0;k < outCount;k++)
00550                 {
00551                     vfI[i][k] = vfO[k];
00552                 }
00553             }
00554         }
00555
00556         for(UInt32 k = 0;k < vfSize[i];k++)
00557         {
00558             points->push_back(vfO[k]);
00559         }
00560     }
00561
00562     if(pntInFrontOf(vf0, vf1, vf2, bb0) && pntInFrontOf(vf7, vf6, vf5, bb0) &&
00563        pntInFrontOf(vf0, vf3, vf7, bb0) && pntInFrontOf(vf2, vf1, vf5, bb0) &&
00564        pntInFrontOf(vf6, vf7, vf3, bb0) && pntInFrontOf(vf5, vf1, vf0, bb0))
00565         points->push_back(bb0);
00566     if(pntInFrontOf(vf0, vf1, vf2, bb1) && pntInFrontOf(vf7, vf6, vf5, bb1) &&
00567        pntInFrontOf(vf0, vf3, vf7, bb1) && pntInFrontOf(vf2, vf1, vf5, bb1) &&
00568        pntInFrontOf(vf6, vf7, vf3, bb1) && pntInFrontOf(vf5, vf1, vf0, bb1))
00569         points->push_back(bb1);
00570     if(pntInFrontOf(vf0, vf1, vf2, bb2) && pntInFrontOf(vf7, vf6, vf5, bb2) &&
00571        pntInFrontOf(vf0, vf3, vf7, bb2) && pntInFrontOf(vf2, vf1, vf5, bb2) &&
00572        pntInFrontOf(vf6, vf7, vf3, bb2) && pntInFrontOf(vf5, vf1, vf0, bb2))
00573         points->push_back(bb2);
00574     if(pntInFrontOf(vf0, vf1, vf2, bb3) && pntInFrontOf(vf7, vf6, vf5, bb3) &&
00575        pntInFrontOf(vf0, vf3, vf7, bb3) && pntInFrontOf(vf2, vf1, vf5, bb3) &&
00576        pntInFrontOf(vf6, vf7, vf3, bb3) && pntInFrontOf(vf5, vf1, vf0, bb3))
00577         points->push_back(bb3);
00578     if(pntInFrontOf(vf0, vf1, vf2, bb4) && pntInFrontOf(vf7, vf6, vf5, bb4) &&
00579        pntInFrontOf(vf0, vf3, vf7, bb4) && pntInFrontOf(vf2, vf1, vf5, bb4) &&
00580        pntInFrontOf(vf6, vf7, vf3, bb4) && pntInFrontOf(vf5, vf1, vf0, bb4))
00581         points->push_back(bb4);
00582     if(pntInFrontOf(vf0, vf1, vf2, bb5) && pntInFrontOf(vf7, vf6, vf5, bb5) &&
00583        pntInFrontOf(vf0, vf3, vf7, bb5) && pntInFrontOf(vf2, vf1, vf5, bb5) &&
00584        pntInFrontOf(vf6, vf7, vf3, bb5) && pntInFrontOf(vf5, vf1, vf0, bb5))
00585         points->push_back(bb5);
00586     if(pntInFrontOf(vf0, vf1, vf2, bb6) && pntInFrontOf(vf7, vf6, vf5, bb6) &&
00587        pntInFrontOf(vf0, vf3, vf7, bb6) && pntInFrontOf(vf2, vf1, vf5, bb6) &&
00588        pntInFrontOf(vf6, vf7, vf3, bb6) && pntInFrontOf(vf5, vf1, vf0, bb6))
00589         points->push_back(bb6);
00590     if(pntInFrontOf(vf0, vf1, vf2, bb7) && pntInFrontOf(vf7, vf6, vf5, bb7) &&
00591        pntInFrontOf(vf0, vf3, vf7, bb7) && pntInFrontOf(vf2, vf1, vf5, bb7) &&
00592        pntInFrontOf(vf6, vf7, vf3, bb7) && pntInFrontOf(vf5, vf1, vf0, bb7))
00593         points->push_back(bb7);
00594
00595     //Body in LK transformieren
00596     for(UInt32 i = 0;i < points->size();i++)
00597     {
00598         Pnt3f   tmpPnt((*points)[i]);
00599         LPVM.multFull(tmpPnt, tmpPnt);
00600         (*points)[i] = tmpPnt;
00601     }
00602
00603     UInt32  sumPoints = points->size();
00604     for(UInt32 i = 0;i < sumPoints;i++)
00605     {
00606         Pnt3f   pntCut2 = (*points)[i];
00607         pntCut2[2] = -1.0;
00608         points->push_back(pntCut2);
00609     }
00610 }
00611
00612 void PerspectiveShadowMapHandler::calcBodyVec(Vec3f              &dir,
00613                                               Pnt3f               eyePos,
00614                                               std::vector<Pnt3f> *points)
00615 {
00616     for(UInt32 i = 0;i < points->size();i++)
00617     {
00618         Vec3f   p;
00619         for(UInt32 j = 0;j < 3;j++)
00620         {
00621             p[j] = (*points)[i][j] - eyePos[j];
00622         }
00623         dir[0] += p[0];
00624         dir[1] += p[1];
00625         dir[2] += p[2];
00626     }
00627     dir.normalize();
00628 }
00629
00630 void PerspectiveShadowMapHandler::printMatrix(Matrix m)
00631 {
00632     printf("Printing Matrix...\n");
00633     Real32  *mPtr = m.getValues();
00634     printf(" %f, %f, %f, %f \n"
00635            " %f, %f, %f, %f \n"
00636            " %f, %f, %f, %f \n"
00637            " %f, %f, %f, %f \n",
00638            mPtr[0],  mPtr[1],  mPtr[2],  mPtr[3],
00639            mPtr[4],  mPtr[5],  mPtr[6],  mPtr[7],
00640            mPtr[8],  mPtr[9],  mPtr[10], mPtr[11],
00641            mPtr[12], mPtr[13], mPtr[14], mPtr[15]);
00642 }
00643
00644 void PerspectiveShadowMapHandler::printPoint(Pnt3f p)
00645 {
00646     printf("Printing Point...\n");
00647     printf("%f, %f, %f\n", p.x(), p.y(), p.z());
00648 }
00649
00650 void PerspectiveShadowMapHandler::calcUpVec2(Vec3f &up,
00651                                              Vec3f  viewDir,
00652                                              Vec3f  lightDir)
00653 {
00654     Vec3f   left;
00655
00656     left = lightDir.cross(viewDir);
00657     up   = left.cross(lightDir);
00658
00659     up.normalize();
00660 }
00661
00662 bool PerspectiveShadowMapHandler::pntInFrontOf(Pnt3f p1,
00663                                                Pnt3f p2,
00664                                                Pnt3f p3,
00665                                                Pnt3f p)
00666 {
00667     Vec3f   dir1, dir2, testdir, norm;
00668     dir1 = p2 - p1;
00669     dir2 = p3 - p2;
00670     dir1.normalize();
00671     dir2.normalize();
00672     norm = dir1.cross(dir2);
00673     norm.normalize();
00674     testdir = p2 - p;
00675     testdir.normalize();
00676     if(testdir.dot(norm) < 0)
00677         return false;
00678     else
00679         return true;
00680 }
00681
00682 void PerspectiveShadowMapHandler::calcCubicHull2(Pnt3f              &min,
00683                                                  Pnt3f              &max,
00684                                                  std::vector<Pnt3f> *points)
00685 {
00686     min = Pnt3f(1000000.0, 1000000.0, 1000000.0);
00687     max = Pnt3f(-1000000.0, -1000000.0, -1000000.0);
00688
00689     for(UInt32 i = 1;i < points->size();i++)
00690     {
00691         for(UInt32 j = 0;j < 3;j++)
00692         {
00693             if((*points)[i][j] < min[j])
00694                 min[j] = (*points)[i][j];
00695             if((*points)[i][j] > max[j])
00696                 max[j] = (*points)[i][j];
00697         }
00698     }
00699 }
00700
00701 void PerspectiveShadowMapHandler::scaleTranslateToFit2(      Matrix &mat,
00702                                                        const Pnt3f   vMin,
00703                                                        const Pnt3f   vMax)
00704 {
00705     Real32  output[16];
00706     output[ 0] = 2 / (vMax[0] - vMin[0]);
00707     output[ 4] = 0;
00708     output[ 8] = 0;
00709     output[12] = -(vMax[0] + vMin[0]) / (vMax[0] - vMin[0]);
00710
00711     output[ 1] = 0;
00712     output[ 5] = 2 / (vMax[1] - vMin[1]);
00713     output[ 9] = 0;
00714     output[13] = -(vMax[1] + vMin[1]) / (vMax[1] - vMin[1]);
00715
00716     output[ 2] = 0;
00717     output[ 6] = 0;
00718     output[10] = 2 / (vMax[2] - vMin[2]);
00719     output[14] = -(vMax[2] + vMin[2]) / (vMax[2] - vMin[2]);
00720
00721     output[ 3] = 0;
00722     output[ 7] = 0;
00723     output[11] = 0;
00724     output[15] = 1;
00725     mat.setValueTransposed(output[0], output[1], output[2], output[3],
00726                            output[4], output[5], output[6], output[7],
00727                            output[8], output[9], output[10], output[11],
00728                            output[12], output[13], output[14], output[15]);
00729 }
00730
00731 bool PerspectiveShadowMapHandler::bbInsideFrustum(Pnt3f  sceneMin,
00732                                                   Pnt3f  sceneMax,
00733                                                   Matrix LPVM)
00734 {
00735     Matrix  iLPVM(LPVM);
00736     iLPVM.invert();
00737
00738     LPVM.multFull(sceneMin, sceneMin);
00739     LPVM.multFull(sceneMax, sceneMax);
00740
00741     //Scene Bounding Box Points
00742     Pnt3f   bb0(sceneMin[0], sceneMin[1], sceneMin[2]);
00743     Pnt3f   bb1(sceneMax[0], sceneMin[1], sceneMin[2]);
00744     Pnt3f   bb2(sceneMax[0], sceneMax[1], sceneMin[2]);
00745     Pnt3f   bb3(sceneMin[0], sceneMax[1], sceneMin[2]);
00746     Pnt3f   bb4(sceneMin[0], sceneMin[1], sceneMax[2]);
00747     Pnt3f   bb5(sceneMax[0], sceneMin[1], sceneMax[2]);
00748     Pnt3f   bb6(sceneMax[0], sceneMax[1], sceneMax[2]);
00749     Pnt3f   bb7(sceneMin[0], sceneMax[1], sceneMax[2]);
00750
00751     bool    isIn[8];
00752     for(UInt32 i = 0;i < 8;i++)
00753         isIn[i] = false;
00754
00755     if(bb0[0] < 1 && bb0[0] > -1 && bb0[1] < 1 && bb0[1] > -1 && bb0[2] < 1 &&
00756        bb0[2] > -1)
00757         isIn[0] = true;
00758     if(bb1[0] < 1 && bb1[0] > -1 && bb1[1] < 1 && bb1[1] > -1 && bb1[2] < 1 &&
00759        bb1[2] > -1)
00760         isIn[1] = true;
00761     if(bb2[0] < 1 && bb2[0] > -1 && bb2[1] < 1 && bb2[1] > -1 && bb2[2] < 1 &&
00762        bb2[2] > -1)
00763         isIn[2] = true;
00764     if(bb3[0] < 1 && bb3[0] > -1 && bb3[1] < 1 && bb3[1] > -1 && bb3[2] < 1 &&
00765        bb3[2] > -1)
00766         isIn[3] = true;
00767     if(bb4[0] < 1 && bb4[0] > -1 && bb4[1] < 1 && bb4[1] > -1 && bb4[2] < 1 &&
00768        bb4[2] > -1)
00769         isIn[4] = true;
00770     if(bb5[0] < 1 && bb5[0] > -1 && bb5[1] < 1 && bb5[1] > -1 && bb5[2] < 1 &&
00771        bb5[2] > -1)
00772         isIn[5] = true;
00773     if(bb6[0] < 1 && bb6[0] > -1 && bb6[1] < 1 && bb6[1] > -1 && bb6[2] < 1 &&
00774        bb6[2] > -1)
00775         isIn[6] = true;
00776     if(bb7[0] < 1 && bb7[0] > -1 && bb7[1] < 1 && bb7[1] > -1 && bb7[2] < 1 &&
00777        bb7[2] > -1)
00778         isIn[7] = true;
00779
00780     if(isIn[0] && isIn[1] && isIn[2] && isIn[3] && isIn[4] && isIn[5] &&
00781        isIn[6] && isIn[7])
00782         return true;
00783     else
00784         return false;
00785 }
00786
00787
00788 void PerspectiveShadowMapHandler::createShadowMapsFBO(RenderAction *a,
00789                                                       DrawEnv      *pEnv)
00790 {
00791     // disable all lights more speed
00792     std::vector<bool> vLocalLightStates;
00793
00794     const ShadowStageData::LightStore  &vLights      =
00795         _pStageData->getLights();
00796
00797     const ShadowStageData::LStateStore &vLightStates =
00798         _pStageData->getLightStates();
00799
00800     const ShadowStageData::StatusStore &vRealPLight  =
00801         _pStageData->getRealPointLight();
00802
00803     const ShadowStageData::CamStore    &vLCams       =
00804         _pStageData->getLightCameras();
00805
00806     const ShadowStageData::StatusStore &vExclActive  =
00807         _pStageData->getExcludeNodeActive();
00808
00809     for(UInt32 i = 0;i < vLights.size();++i)
00810     {
00811         // store old states.
00812         vLocalLightStates.push_back(vLights[i].second->getOn());
00813         vLights[i].second->setOn(false);
00814     }
00815
00816     // deactivate exclude nodes:
00817     for(UInt32 i = 0;i < _pStage->getMFExcludeNodes()->size();++i)
00818     {
00819         Node *exnode = _pStage->getExcludeNodes(i);
00820
00821         if(exnode != NULL)
00822             exnode->setTravMask(0);
00823     }
00824
00825     ShadowStageData::ShadowMapStore &vShadowMaps = _pStageData->getShadowMaps();
00826
00827     for(UInt32 i = 0;i < vLights.size();++i)
00828     {
00829         if(vLightStates[i])
00830         {
00831             if(_pStage->getGlobalShadowIntensity()    != 0.0 ||
00832                 vLights[i].second->getShadowIntensity() != 0.0)
00833             {
00834                 //------Setting up Window to fit size of ShadowMap-------------
00835
00836                 if(vLights[i].second->getType() != PointLight::getClassType()||
00837                    vRealPLight[i] == false)
00838                 {
00839
00840                     _matrixCam2->setProjectionMatrix(_perspectiveLPM[i]);
00841                     _matrixCam2->setModelviewMatrix (_perspectiveLVM[i]);
00842
00843                     a->pushPartition();
00844                     {
00845                         RenderPartition   *pPart    = a->getActivePartition();
00846
00847                         pPart->addPreRenderCallback(
00848                             &ShadowTreeHandler::setupAmbientModelAndMasks);
00849                         pPart->addPostRenderCallback(
00850                             &ShadowTreeHandler::endAmbientModelAndMasks);
00851
00852                         pPart->setRenderTarget(vShadowMaps[i].pFBO);
00853
00854                         pPart->setWindow  (a->getWindow());
00855
00856                         pPart->calcViewportDimension(0.f,
00857                                                      0.f,
00858                                                      _pStage->getMapSize()-1,
00859                                                      _pStage->getMapSize()-1,
00860
00861                                                      _pStage->getMapSize(),
00862                                                      _pStage->getMapSize() );
00863
00864
00865                         Matrix m, t;
00866
00867                         // set the projection
00868                         _matrixCam2->getProjection(
00869                             m,
00870                             pPart->getViewportWidth (),
00871                             pPart->getViewportHeight());
00872
00873                         _matrixCam2->getProjectionTranslation(
00874                             t,
00875                             pPart->getViewportWidth (),
00876                             pPart->getViewportHeight());
00877
00878                         pPart->setupProjection(m, t);
00879
00880                         _matrixCam2->getViewing(
00881                             m,
00882                             pPart->getViewportWidth (),
00883                             pPart->getViewportHeight());
00884
00885
00886                         pPart->setupViewing(m);
00887
00888                         pPart->setNear     (_matrixCam2->getNear());
00889                         pPart->setFar      (_matrixCam2->getFar ());
00890
00891                         pPart->calcFrustum();
00892
00893                         pPart->setBackground(_pClearBackground);
00894
00895                         Node *light  = vLights[i].first;
00896                         Node *parent = light->getParent();
00897
00898                         if(parent != NULL)
00899                         {
00900                             a->pushMatrix(parent->getToWorld());
00901                         }
00902
00903
00904                         a->overrideMaterial(_unlitMat, a->getActNode());
00905                         _pStage->recurse(a, light);
00906                         a->overrideMaterial( NULL,       a->getActNode());
00907
00908                         if(parent != NULL)
00909                         {
00910                             a->popMatrix();
00911                         }
00912                     }
00913                     a->popPartition();
00914
00915                 }
00916                 else
00917                 {
00918                     for(UInt32 j = 0;j < 6;j++)
00919                     {
00920                         //Offset berechnen
00921                         UInt32  xOffset, yOffset;
00922                         if(j == 0)
00923                         {
00924                             xOffset = 0;
00925                             yOffset = 0;
00926                         }
00927                         else if(j == 1)
00928                         {
00929                             xOffset = _PLMapSize;
00930                             yOffset = 0;
00931                         }
00932                         else if(j == 2)
00933                         {
00934                             xOffset = 2 * _PLMapSize;
00935                             yOffset = 0;
00936                         }
00937                         else if(j == 3)
00938                         {
00939                             xOffset = 3 * _PLMapSize;
00940                             yOffset = 0;
00941                         }
00942                         else if(j == 4)
00943                         {
00944                             xOffset = 0;
00945                             yOffset = _PLMapSize;
00946                         }
00947                         else
00948                         {
00949                             xOffset = _PLMapSize;
00950                             yOffset = _PLMapSize;
00951                         }
00952
00953                         _matrixDeco->setDecoratee    (vLCams[i]     );
00954                         _matrixDeco->setPreProjection(_aCubeTrans[j]);
00955
00956                         a->pushPartition();
00957                         {
00958                             RenderPartition   *pPart = a->getActivePartition();
00959
00960                             pPart->addPreRenderCallback(
00961                                 &ShadowTreeHandler::setupAmbientModelAndMasks);
00962                             pPart->addPostRenderCallback(
00963                                 &ShadowTreeHandler::endAmbientModelAndMasks);
00964
00965                             pPart->setRenderTarget(vShadowMaps[i].pFBO);
00966
00967                             pPart->setWindow  (a->getWindow());
00968
00969                             pPart->calcViewportDimension(
00970                                 xOffset,
00971                                 yOffset,
00972                                 xOffset + _PLMapSize,
00973                                 yOffset + _PLMapSize,
00974
00975                                 _pStage->getMapSize(),
00976                                 _pStage->getMapSize() );
00977
00978
00979                             Matrix m, t;
00980
00981                             // set the projection
00982                             _matrixDeco->getProjection(
00983                                 m,
00984                                 pPart->getViewportWidth (),
00985                                 pPart->getViewportHeight());
00986
00987                             _matrixDeco->getProjectionTranslation(
00988                                 t,
00989                                 pPart->getViewportWidth (),
00990                                 pPart->getViewportHeight());
00991
00992                             pPart->setupProjection(m, t);
00993
00994                             _matrixDeco->getViewing(
00995                                 m,
00996                                 pPart->getViewportWidth (),
00997                                 pPart->getViewportHeight());
00998
00999                             pPart->setupViewing(m);
01000
01001                             pPart->setNear     (
01002                                 _matrixDeco->getNear());
01003                             pPart->setFar      (
01004                                 _matrixDeco->getFar ());
01005
01006                             pPart->calcFrustum();
01007
01008                             pPart->setBackground(_pClearBackground);
01009
01010                             Node *light  = vLights[i].first;
01011                             Node *parent = light->getParent();
01012
01013                             if(parent != NULL)
01014                             {
01015                                 a->pushMatrix(parent->getToWorld());
01016                             }
01017
01018
01019                             a->overrideMaterial(_unlitMat, a->getActNode());
01020                             _pStage->recurse(a, light);
01021                             a->overrideMaterial( NULL,       a->getActNode());
01022
01023                             if(parent != NULL)
01024                             {
01025                                 a->popMatrix();
01026                             }
01027                         }
01028                         a->popPartition();
01029
01030                     }
01031                 }
01032             }
01033         }
01034     }
01035
01036     //-------Restoring old states of Window and Viewport----------
01037
01038     // enable all lights.
01039     for(UInt32 i = 0;i < vLights.size();++i)
01040     {
01041         // restore old states.
01042         vLights[i].second->setOn(vLocalLightStates[i]);
01043     }
01044
01045     // activate exclude nodes:
01046     for(UInt32 i = 0;i < _pStage->getMFExcludeNodes()->size();++i)
01047     {
01048         Node *exnode = _pStage->getExcludeNodes(i);
01049
01050         if(exnode != NULL)
01051         {
01052             if(vExclActive[i])
01053             {
01054                 exnode->setTravMask(TypeTraits<UInt32>::BitsSet);
01055             }
01056         }
01057     }
01058 }
01059
01060
01061
01062 void PerspectiveShadowMapHandler::createColorMapFBO(RenderAction *a,
01063                                                     DrawEnv      *pEnv)
01064 {
01065     a->pushPartition((RenderPartition::CopyWindow      |
01066                       RenderPartition::CopyViewing     |
01067                       RenderPartition::CopyProjection  |
01068                       RenderPartition::CopyFrustum     |
01069                       RenderPartition::CopyNearFar     |
01070                       RenderPartition::CopyViewportSize),
01071                      RenderPartition::StateSorting);
01072     {
01073         RenderPartition *pPart = a->getActivePartition();
01074
01075         pPart->addPreRenderCallback (&ShadowTreeHandler::setupAmbientModel);
01076         pPart->addPostRenderCallback(&ShadowTreeHandler::endAmbientModel  );
01077
01078         pPart->setRenderTarget(_pSceneFBO);
01079         pPart->setDrawBuffer  (GL_COLOR_ATTACHMENT0_EXT);
01080
01081         Node *parent = a->getActNode()->getParent();
01082
01083         if(parent != NULL)
01084         {
01085             a->pushMatrix(parent->getToWorld());
01086         }
01087
01088         pPart->setBackground(a->getBackground());
01089
01090         _pStage->recurseFromThis(a);
01091
01092         if(parent != NULL)
01093         {
01094             a->popMatrix();
01095         }
01096     }
01097     a->popPartition();
01098
01099 }
01100
01101
01102 void PerspectiveShadowMapHandler::createShadowFactorMapFBO(
01103     RenderAction *a,
01104     DrawEnv      *pEnv)
01105 {
01106     _activeFactorMap = 0;
01107
01108     const ShadowStageData::LightStore  &vLights      =
01109         _pStageData->getLights();
01110
01111     const ShadowStageData::LStateStore &vLightStates =
01112         _pStageData->getLightStates();
01113
01114     const ShadowStageData::StatusStore &vRealPLight  =
01115         _pStageData->getRealPointLight();
01116
01117     const ShadowStageData::CamStore    &vLCams       =
01118         _pStageData->getLightCameras();
01119
01120
01121     //Finde alle aktiven Lichtquellen
01122     Real32              activeLights = 0;
01123
01124     if(_pStage->getGlobalShadowIntensity() != 0.0)
01125     {
01126         for(UInt32 i = 0;i < vLights.size();i++)
01127         {
01128             if(vLightStates[i] != 0)
01129                 activeLights++;
01130         }
01131     }
01132     else
01133     {
01134         for(UInt32 i = 0;i < vLights.size();i++)
01135         {
01136             if(vLightStates[i]                              != 0 &&
01137                vLights     [i].second->getShadowIntensity() != 0.0)
01138             {
01139                 activeLights++;
01140             }
01141         }
01142     }
01143
01144
01145     bool bCA1Cleared = false;
01146     bool bCA2Cleared = false;
01147
01148     ShadowStageData::ShadowMapStore &vShadowMaps = _pStageData->getShadowMaps();
01149
01150     //Zuerst alle echte Pointlights
01151     for(UInt32 i = 0;i < vLights.size();i++)
01152     {
01153         if(vLightStates[i] != 0)
01154         {
01155             if((_pStage->getGlobalShadowIntensity() != 0.0 ||
01156                 vLights[i].second->getShadowIntensity() != 0.0) &&
01157                vRealPLight[i])
01158             {
01159                 Real32  shadowIntensity;
01160                 if(_pStage->getGlobalShadowIntensity() != 0.0)
01161                 {
01162                     shadowIntensity = (_pStage->getGlobalShadowIntensity() /
01163                                        activeLights);
01164                 }
01165                 else
01166                 {
01167                     shadowIntensity =
01168                         (vLights[i].second->getShadowIntensity() /
01169                          activeLights);
01170                 }
01171
01172                 Matrix  LVM, LPM, CVM;
01173
01174                 vLCams[i]->getViewing(
01175                     LVM,
01176                     pEnv->getPixelWidth(),
01177                     pEnv->getPixelHeight());
01178
01179                 vLCams[i]->getProjection(
01180                     LPM,
01181                     pEnv->getPixelWidth(),
01182                     pEnv->getPixelHeight());
01183
01184                 CVM = pEnv->getCameraViewing();
01185
01186                 Matrix  iCVM = CVM;
01187                 iCVM.invert();
01188
01189                 Real32  texFactor;
01190                 if(vLights[i].second->getType() == PointLight::getClassType()||
01191                    vLights[i].second->getType() == SpotLight ::getClassType() )
01192                 {
01193                     texFactor = Real32(_width) / Real32(_height);
01194                 }
01195                 else
01196                 {
01197                     texFactor = 1.0;
01198                 }
01199
01200                 Matrix  shadowMatrix = LPM;
01201                 shadowMatrix.mult(LVM);
01202                 shadowMatrix.mult(iCVM);
01203
01204                 Real32  xFactor = 1.0;
01205                 Real32  yFactor = 1.0;
01206
01207                 Matrix m = pEnv->getCameraToWorld();
01208
01209                 Matrix  shadowMatrixOP = LVM;
01210                 shadowMatrix.mult(iCVM);
01211
01212                 Matrix  shadowMatrixA = LPM;
01213                 shadowMatrixA.mult(_aCubeTrans[0]);
01214                 shadowMatrixA.mult(LVM);
01215                 shadowMatrixA.mult(iCVM);
01216
01217                 Matrix  shadowMatrixB = LPM;
01218                 shadowMatrixB.mult(_aCubeTrans[1]);
01219                 shadowMatrixB.mult(LVM);
01220                 shadowMatrixB.mult(iCVM);
01221
01222                 Matrix  shadowMatrixC = LPM;
01223                 shadowMatrixC.mult(_aCubeTrans[2]);
01224                 shadowMatrixC.mult(LVM);
01225                 shadowMatrixC.mult(iCVM);
01226
01227                 Matrix  shadowMatrixD = LPM;
01228                 shadowMatrixD.mult(_aCubeTrans[3]);
01229                 shadowMatrixD.mult(LVM);
01230                 shadowMatrixD.mult(iCVM);
01231
01232                 Matrix  shadowMatrixE = LPM;
01233                 shadowMatrixE.mult(_aCubeTrans[4]);
01234                 shadowMatrixE.mult(LVM);
01235                 shadowMatrixE.mult(iCVM);
01236
01237                 Matrix  shadowMatrixF = LPM;
01238                 shadowMatrixF.mult(_aCubeTrans[5]);
01239                 shadowMatrixF.mult(LVM);
01240                 shadowMatrixF.mult(iCVM);
01241
01242                 _shadowCubeSHL->addUniformVariable("shadowMap", 0);
01243                 _shadowCubeSHL->addUniformVariable("oldFactorMap", 1);
01244                 _shadowCubeSHL->addUniformVariable("firstRun", _firstRun);
01245                 _shadowCubeSHL->addUniformVariable("intensity",
01246                                                     shadowIntensity);
01247                 _shadowCubeSHL->addUniformVariable("texFactor", texFactor);
01248                 _shadowCubeSHL->addUniformVariable("lightPMA", shadowMatrixA);
01249                 _shadowCubeSHL->addUniformVariable("lightPMB", shadowMatrixB);
01250                 _shadowCubeSHL->addUniformVariable("lightPMC", shadowMatrixC);
01251                 _shadowCubeSHL->addUniformVariable("lightPMD", shadowMatrixD);
01252                 _shadowCubeSHL->addUniformVariable("lightPME", shadowMatrixE);
01253                 _shadowCubeSHL->addUniformVariable("lightPMF", shadowMatrixF);
01254                 _shadowCubeSHL->addUniformVariable("lightPMOP",
01255                                                     shadowMatrixOP);
01256                 _shadowCubeSHL->addUniformVariable("KKtoWK", m);
01257                 _shadowCubeSHL->addUniformVariable("xFactor",
01258                                                     Real32(xFactor));
01259                 _shadowCubeSHL->addUniformVariable("yFactor",
01260                                                     Real32(yFactor));
01261
01262                 _shadowCmat->clearChunks();
01263                 _shadowCmat->addChunk(_shadowCubeSHL);
01264                 _shadowCmat->addChunk(vShadowMaps[i].pTexO);
01265                 _shadowCmat->addChunk(vShadowMaps[i].pTexE);
01266
01267                 if(_activeFactorMap == 0)
01268                 {
01269                     _shadowCmat->addChunk(_shadowFactorMap2O);
01270                 }
01271                 else
01272                 {
01273                     _shadowCmat->addChunk(_shadowFactorMapO);
01274                 }
01275
01276                 GLenum  dBuffers = GL_COLOR_ATTACHMENT1_EXT;
01277
01278                 if(_activeFactorMap == 0)
01279                     dBuffers = GL_COLOR_ATTACHMENT1_EXT;
01280                 else
01281                     dBuffers = GL_COLOR_ATTACHMENT2_EXT;
01282
01283                 a->pushPartition((RenderPartition::CopyWindow      |
01284                                   RenderPartition::CopyViewing     |
01285                                   RenderPartition::CopyProjection  |
01286                                   RenderPartition::CopyFrustum     |
01287                                   RenderPartition::CopyNearFar     |
01288                                   RenderPartition::CopyViewportSize),
01289                                  RenderPartition::StateSorting);
01290                 {
01291                     RenderPartition *pPart = a->getActivePartition();
01292
01293                     pPart->addPreRenderCallback (
01294                         &ShadowTreeHandler::setupAmbientModel);
01295                     pPart->addPostRenderCallback(
01296                         &ShadowTreeHandler::endAmbientModel  );
01297
01298                     pPart->setRenderTarget(_pSceneFBO);
01299                     pPart->setDrawBuffer  ( dBuffers );
01300
01301                     Node *light  = vLights[i].first;
01302                     Node *parent = light->getParent();
01303
01304                     if(parent != NULL)
01305                     {
01306                         a->pushMatrix(parent->getToWorld());
01307                     }
01308
01309                     if(_activeFactorMap == 0 && bCA1Cleared == false)
01310                     {
01311                         pPart->setBackground(_pClearBackground);
01312                         bCA1Cleared = true;
01313                     }
01314                     else if(bCA2Cleared == false)
01315                     {
01316                         pPart->setBackground(_pClearBackground);
01317                         bCA2Cleared = true;
01318                     }
01319
01320                     a->overrideMaterial(_shadowCmat, a->getActNode());
01321                     _pStage->recurse(a, light);
01322                     a->overrideMaterial( NULL,       a->getActNode());
01323
01324                     if(parent != NULL)
01325                     {
01326                         a->popMatrix();
01327                     }
01328                 }
01329                 a->popPartition();
01330
01331                 _firstRun = 0;
01332
01333                 if(_activeFactorMap == 0)
01334                     _activeFactorMap = 1;
01335                 else
01336                     _activeFactorMap = 0;
01337             }
01338         }
01339     }
01340
01341     std::vector<Real32> shadowIntensityF;
01342     std::vector<Real32> mapFactorF;
01343     std::vector<Matrix> shadowMatrixF;
01344
01345     UInt32              lightCounter = 0;
01346
01347     Real32              xFactor = 1.0;
01348     Real32              yFactor = 1.0;
01349
01350     //Jetzt alle normalen Lichtquellen
01351     for(UInt32 i = 0;i < vLights.size();i++)
01352     {
01353         if(vLightStates[i] != 0)
01354         {
01355             if((_pStage->getGlobalShadowIntensity() != 0.0 ||
01356                 vLights[i].second->getShadowIntensity() != 0.0) &&
01357                vRealPLight[i] == false)
01358             {
01359
01360                 Real32  shadowIntensity;
01361                 if(_pStage->getGlobalShadowIntensity() != 0.0)
01362                 {
01363                     shadowIntensity = (_pStage->getGlobalShadowIntensity() /
01364                                        activeLights);
01365                 }
01366                 else
01367                 {
01368                     shadowIntensity =
01369                         (vLights[i].second->getShadowIntensity() /
01370                          activeLights);
01371                 }
01372
01373                 shadowIntensityF.push_back(shadowIntensity);
01374
01375                 Matrix  LVM, LPM, CVM;
01376                 LVM = _perspectiveLVM[i];
01377                 LPM = _perspectiveLPM[i];
01378
01379                 CVM = pEnv->getCameraViewing();
01380
01381                 Matrix  iCVM = CVM;
01382                 iCVM.invert();
01383
01384                 Matrix  shadowMatrix = LPM;
01385                 shadowMatrix.mult(LVM);
01386                 shadowMatrix.mult(iCVM);
01387                 shadowMatrixF.push_back(shadowMatrix);
01388
01389                 Real32  mapFactor;
01390                 mapFactor =
01391                     Real32(_pStage->getMapSize()) /
01392                     Real32(vShadowMaps[i].pImage->getWidth());
01393                 mapFactorF.push_back(mapFactor);
01394                 lightCounter++;
01395             }
01396         }
01397     }
01398
01399     if(lightCounter != 0)
01400     {
01401         GLenum  dBuffers = GL_COLOR_ATTACHMENT1_EXT;
01402
01403         if(_activeFactorMap == 0)
01404             dBuffers = GL_COLOR_ATTACHMENT1_EXT;
01405         else
01406             dBuffers = GL_COLOR_ATTACHMENT2_EXT;
01407
01408         UInt32  renderTimes = 1;
01409         if(lightCounter > 7)
01410             renderTimes = UInt32(ceil(Real32(lightCounter) / 7.0f));
01411
01412         for(UInt32 i = 0;i < renderTimes;i++)
01413         {
01414
01415             UInt32  lightOffset = lightCounter - (i * 7);
01416
01417             //clear chunk and add Textures
01418             _shadowCmat->clearChunks();
01419
01420             UInt32  lightNum = 0;
01421             for(UInt32 j = 0;j < vLights.size();j++)
01422             {
01423                 if(vLightStates[j] != 0)
01424                 {
01425                     if((_pStage->getGlobalShadowIntensity() != 0.0 ||
01426                         vLights[j].second->getShadowIntensity() != 0.0) &&
01427                        vRealPLight[j] == false)
01428                     {
01429                         if(lightNum >= (i * 7) && lightNum < ((i + 1) * 7))
01430                         {
01431                             _shadowCmat->addChunk(
01432                                 vShadowMaps[j].pTexO);
01433                             _shadowCmat->addChunk(
01434                                 vShadowMaps[j].pTexE);
01435                         }
01436                         lightNum++;
01437                     }
01438                 }
01439             }
01440
01441             if(lightOffset == 1)
01442             {
01443                 _shadowCmat->addChunk(_shadowSHL);
01444                 if(_activeFactorMap == 0)
01445                 {
01446                     _shadowCmat->addChunk(_shadowFactorMap2O);
01447                 }
01448                 else
01449                 {
01450                     _shadowCmat->addChunk(_shadowFactorMapO);
01451                 }
01452
01453                 _shadowSHL->addUniformVariable("oldFactorMap", 1);
01454                 _shadowSHL->addUniformVariable("shadowMap", 0);
01455                 _shadowSHL->addUniformVariable("firstRun", _firstRun);
01456                 _shadowSHL->addUniformVariable("intensity",
01457                                                 shadowIntensityF[0 + (i * 7)]);
01458                 _shadowSHL->addUniformVariable("lightPM", shadowMatrixF[0 +
01459                                                 (i * 7)]);
01460                 _shadowSHL->addUniformVariable("xFactor", Real32(xFactor));
01461                 _shadowSHL->addUniformVariable("yFactor", Real32(yFactor));
01462                 _shadowSHL->addUniformVariable("mapFactor",
01463                                                 Real32(mapFactorF[0 +
01464                                                        (i * 7)]));
01465             }
01466
01467             else if(lightOffset == 2)
01468             {
01469                 _shadowCmat->addChunk(_shadowSHL2);
01470                 if(_activeFactorMap == 0)
01471                 {
01472                     _shadowCmat->addChunk(_shadowFactorMap2O);
01473                 }
01474                 else
01475                 {
01476                     _shadowCmat->addChunk(_shadowFactorMapO);
01477                 }
01478
01479                 _shadowSHL2->addUniformVariable("oldFactorMap", 2);
01480                 _shadowSHL2->addUniformVariable("shadowMap1", 0);
01481                 _shadowSHL2->addUniformVariable("shadowMap2", 1);
01482                 _shadowSHL2->addUniformVariable("firstRun", _firstRun);
01483                 _shadowSHL2->addUniformVariable("intensity1",
01484                                                  shadowIntensityF[0 +
01485                                                  (i * 7)]);
01486                 _shadowSHL2->addUniformVariable("intensity2",
01487                                                  shadowIntensityF[1 +
01488                                                  (i * 7)]);
01489                 _shadowSHL2->addUniformVariable("lightPM1", shadowMatrixF[0 +
01490                                                  (i * 7)]);
01491                 _shadowSHL2->addUniformVariable("lightPM2", shadowMatrixF[1 +
01492                                                  (i * 7)]);
01493                 _shadowSHL2->addUniformVariable("xFactor", Real32(xFactor));
01494                 _shadowSHL2->addUniformVariable("yFactor", Real32(yFactor));
01495                 _shadowSHL2->addUniformVariable("mapFactor1",
01496                                                  Real32(mapFactorF[0 + (i *
01497                                                                         7)]));
01498                 _shadowSHL2->addUniformVariable("mapFactor2",
01499                                                  Real32(mapFactorF[1 + (i *
01500                                                                         7)]));
01501             }
01502
01503             else if(lightOffset == 3)
01504             {
01505                 _shadowCmat->addChunk(_shadowSHL3);
01506                 if(_activeFactorMap == 0)
01507                 {
01508                     _shadowCmat->addChunk(_shadowFactorMap2O);
01509                 }
01510                 else
01511                 {
01512                     _shadowCmat->addChunk(_shadowFactorMapO);
01513                 }
01514
01515                 _shadowSHL3->addUniformVariable("oldFactorMap", 3);
01516                 _shadowSHL3->addUniformVariable("shadowMap1", 0);
01517                 _shadowSHL3->addUniformVariable("shadowMap2", 1);
01518                 _shadowSHL3->addUniformVariable("shadowMap3", 2);
01519                 _shadowSHL3->addUniformVariable("firstRun", _firstRun);
01520                 _shadowSHL3->addUniformVariable("intensity1",
01521                                                  shadowIntensityF[0 +
01522                                                  (i * 7)]);
01523                 _shadowSHL3->addUniformVariable("intensity2",
01524                                                  shadowIntensityF[1 +
01525                                                  (i * 7)]);
01526                 _shadowSHL3->addUniformVariable("intensity3",
01527                                                  shadowIntensityF[2 +
01528                                                  (i * 7)]);
01529                 _shadowSHL3->addUniformVariable("lightPM1", shadowMatrixF[0 +
01530                                                  (i * 7)]);
01531                 _shadowSHL3->addUniformVariable("lightPM2", shadowMatrixF[1 +
01532                                                  (i * 7)]);
01533                 _shadowSHL3->addUniformVariable("lightPM3", shadowMatrixF[2 +
01534                                                  (i * 7)]);
01535                 _shadowSHL3->addUniformVariable("xFactor", Real32(xFactor));
01536                 _shadowSHL3->addUniformVariable("yFactor", Real32(yFactor));
01537                 _shadowSHL3->addUniformVariable("mapFactor1",
01538                                                  Real32(mapFactorF[0 + (i *
01539                                                                         7)]));
01540                 _shadowSHL3->addUniformVariable("mapFactor2",
01541                                                  Real32(mapFactorF[1 + (i *
01542                                                                         7)]));
01543                 _shadowSHL3->addUniformVariable("mapFactor3",
01544                                                  Real32(mapFactorF[2 + (i *
01545                                                                         7)]));
01546             }
01547
01548             else if(lightCounter == 4)
01549             {
01550                 _shadowCmat->addChunk(_shadowSHL4);
01551                 if(_activeFactorMap == 0)
01552                 {
01553                     _shadowCmat->addChunk(_shadowFactorMap2O);
01554                 }
01555                 else
01556                 {
01557                     _shadowCmat->addChunk(_shadowFactorMapO);
01558                 }
01559
01560                 _shadowSHL4->addUniformVariable("oldFactorMap", 4);
01561                 _shadowSHL4->addUniformVariable("shadowMap1", 0);
01562                 _shadowSHL4->addUniformVariable("shadowMap2", 1);
01563                 _shadowSHL4->addUniformVariable("shadowMap3", 2);
01564                 _shadowSHL4->addUniformVariable("shadowMap4", 3);
01565                 _shadowSHL4->addUniformVariable("firstRun", _firstRun);
01566                 _shadowSHL4->addUniformVariable("intensity1",
01567                                                  shadowIntensityF[0 +
01568                                                  (i * 7)]);
01569                 _shadowSHL4->addUniformVariable("intensity2",
01570                                                  shadowIntensityF[1 +
01571                                                  (i * 7)]);
01572                 _shadowSHL4->addUniformVariable("intensity3",
01573                                                  shadowIntensityF[2 +
01574                                                  (i * 7)]);
01575                 _shadowSHL4->addUniformVariable("intensity4",
01576                                                  shadowIntensityF[3 +
01577                                                  (i * 7)]);
01578                 _shadowSHL4->addUniformVariable("lightPM1", shadowMatrixF[0 +
01579                                                  (i * 7)]);
01580                 _shadowSHL4->addUniformVariable("lightPM2", shadowMatrixF[1 +
01581                                                  (i * 7)]);
01582                 _shadowSHL4->addUniformVariable("lightPM3", shadowMatrixF[2 +
01583                                                  (i * 7)]);
01584                 _shadowSHL4->addUniformVariable("lightPM4", shadowMatrixF[3 +
01585                                                  (i * 7)]);
01586                 _shadowSHL4->addUniformVariable("xFactor", Real32(xFactor));
01587                 _shadowSHL4->addUniformVariable("yFactor", Real32(yFactor));
01588                 _shadowSHL4->addUniformVariable("mapFactor1",
01589                                                  Real32(mapFactorF[0 + (i *
01590                                                                         7)]));
01591                 _shadowSHL4->addUniformVariable("mapFactor2",
01592                                                  Real32(mapFactorF[1 + (i *
01593                                                                         7)]));
01594                 _shadowSHL4->addUniformVariable("mapFactor3",
01595                                                  Real32(mapFactorF[2 + (i *
01596                                                                         7)]));
01597                 _shadowSHL4->addUniformVariable("mapFactor4",
01598                                                  Real32(mapFactorF[3 + (i *
01599                                                                         7)]));
01600             }
01601
01602             else if(lightCounter == 5)
01603             {
01604                 _shadowCmat->addChunk(_shadowSHL5);
01605                 if(_activeFactorMap == 0)
01606                 {
01607                     _shadowCmat->addChunk(_shadowFactorMap2O);
01608                 }
01609                 else
01610                 {
01611                     _shadowCmat->addChunk(_shadowFactorMapO);
01612                 }
01613
01614                 _shadowSHL5->addUniformVariable("oldFactorMap", 5);
01615                 _shadowSHL5->addUniformVariable("shadowMap1", 0);
01616                 _shadowSHL5->addUniformVariable("shadowMap2", 1);
01617                 _shadowSHL5->addUniformVariable("shadowMap3", 2);
01618                 _shadowSHL5->addUniformVariable("shadowMap4", 3);
01619                 _shadowSHL5->addUniformVariable("shadowMap5", 4);
01620                 _shadowSHL5->addUniformVariable("firstRun", _firstRun);
01621                 _shadowSHL5->addUniformVariable("intensity1",
01622                                                  shadowIntensityF[0 +
01623                                                  (i * 7)]);
01624                 _shadowSHL5->addUniformVariable("intensity2",
01625                                                  shadowIntensityF[1 +
01626                                                  (i * 7)]);
01627                 _shadowSHL5->addUniformVariable("intensity3",
01628                                                  shadowIntensityF[2 +
01629                                                  (i * 7)]);
01630                 _shadowSHL5->addUniformVariable("intensity4",
01631                                                  shadowIntensityF[3 +
01632                                                  (i * 7)]);
01633                 _shadowSHL5->addUniformVariable("intensity5",
01634                                                  shadowIntensityF[4 +
01635                                                  (i * 7)]);
01636                 _shadowSHL5->addUniformVariable("lightPM1", shadowMatrixF[0 +
01637                                                  (i * 7)]);
01638                 _shadowSHL5->addUniformVariable("lightPM2", shadowMatrixF[1 +
01639                                                  (i * 7)]);
01640                 _shadowSHL5->addUniformVariable("lightPM3", shadowMatrixF[2 +
01641                                                  (i * 7)]);
01642                 _shadowSHL5->addUniformVariable("lightPM4", shadowMatrixF[3 +
01643                                                  (i * 7)]);
01644                 _shadowSHL5->addUniformVariable("lightPM5", shadowMatrixF[4 +
01645                                                  (i * 7)]);
01646                 _shadowSHL5->addUniformVariable("xFactor", Real32(xFactor));
01647                 _shadowSHL5->addUniformVariable("yFactor", Real32(yFactor));
01648                 _shadowSHL5->addUniformVariable("mapFactor1",
01649                                                  Real32(mapFactorF[0 + (i *
01650                                                                         7)]));
01651                 _shadowSHL5->addUniformVariable("mapFactor2",
01652                                                  Real32(mapFactorF[1 + (i *
01653                                                                         7)]));
01654                 _shadowSHL5->addUniformVariable("mapFactor3",
01655                                                  Real32(mapFactorF[2 + (i *
01656                                                                         7)]));
01657                 _shadowSHL5->addUniformVariable("mapFactor4",
01658                                                  Real32(mapFactorF[3 + (i *
01659                                                                         7)]));
01660                 _shadowSHL5->addUniformVariable("mapFactor5",
01661                                                  Real32(mapFactorF[4 + (i *
01662                                                                         7)]));
01663             }
01664
01665             else if(lightCounter == 6)
01666             {
01667                 _shadowCmat->addChunk(_shadowSHL6);
01668                 if(_activeFactorMap == 0)
01669                 {
01670                     _shadowCmat->addChunk(_shadowFactorMap2O);
01671                 }
01672                 else
01673                 {
01674                     _shadowCmat->addChunk(_shadowFactorMapO);
01675                 }
01676
01677                 _shadowSHL6->addUniformVariable("oldFactorMap", 6);
01678                 _shadowSHL6->addUniformVariable("shadowMap1", 0);
01679                 _shadowSHL6->addUniformVariable("shadowMap2", 1);
01680                 _shadowSHL6->addUniformVariable("shadowMap3", 2);
01681                 _shadowSHL6->addUniformVariable("shadowMap4", 3);
01682                 _shadowSHL6->addUniformVariable("shadowMap5", 4);
01683                 _shadowSHL6->addUniformVariable("shadowMap6", 5);
01684                 _shadowSHL6->addUniformVariable("firstRun", _firstRun);
01685                 _shadowSHL6->addUniformVariable("intensity1",
01686                                                  shadowIntensityF[0 +
01687                                                  (i * 7)]);
01688                 _shadowSHL6->addUniformVariable("intensity2",
01689                                                  shadowIntensityF[1 +
01690                                                  (i * 7)]);
01691                 _shadowSHL6->addUniformVariable("intensity3",
01692                                                  shadowIntensityF[2 +
01693                                                  (i * 7)]);
01694                 _shadowSHL6->addUniformVariable("intensity4",
01695                                                  shadowIntensityF[3 +
01696                                                  (i * 7)]);
01697                 _shadowSHL6->addUniformVariable("intensity5",
01698                                                  shadowIntensityF[4 +
01699                                                  (i * 7)]);
01700                 _shadowSHL6->addUniformVariable("intensity6",
01701                                                  shadowIntensityF[5 +
01702                                                  (i * 7)]);
01703                 _shadowSHL6->addUniformVariable("lightPM1", shadowMatrixF[0 +
01704                                                  (i * 7)]);
01705                 _shadowSHL6->addUniformVariable("lightPM2", shadowMatrixF[1 +
01706                                                  (i * 7)]);
01707                 _shadowSHL6->addUniformVariable("lightPM3", shadowMatrixF[2 +
01708                                                  (i * 7)]);
01709                 _shadowSHL6->addUniformVariable("lightPM4", shadowMatrixF[3 +
01710                                                  (i * 7)]);
01711                 _shadowSHL6->addUniformVariable("lightPM5", shadowMatrixF[4 +
01712                                                  (i * 7)]);
01713                 _shadowSHL6->addUniformVariable("lightPM6", shadowMatrixF[5 +
01714                                                  (i * 7)]);
01715                 _shadowSHL6->addUniformVariable("xFactor", Real32(xFactor));
01716                 _shadowSHL6->addUniformVariable("yFactor", Real32(yFactor));
01717                 _shadowSHL6->addUniformVariable("mapFactor1",
01718                                                  Real32(mapFactorF[0 + (i *
01719                                                                         7)]));
01720                 _shadowSHL6->addUniformVariable("mapFactor2",
01721                                                  Real32(mapFactorF[1 + (i *
01722                                                                         7)]));
01723                 _shadowSHL6->addUniformVariable("mapFactor3",
01724                                                  Real32(mapFactorF[2 + (i *
01725                                                                         7)]));
01726                 _shadowSHL6->addUniformVariable("mapFactor4",
01727                                                  Real32(mapFactorF[3 + (i *
01728                                                                         7)]));
01729                 _shadowSHL6->addUniformVariable("mapFactor5",
01730                                                  Real32(mapFactorF[4 + (i *
01731                                                                         7)]));
01732                 _shadowSHL6->addUniformVariable("mapFactor6",
01733                                                  Real32(mapFactorF[5 + (i *
01734                                                                         7)]));
01735             }
01736
01737             else
01738             {
01739                 _shadowCmat->addChunk(_shadowSHL7);
01740                 if(_activeFactorMap == 0)
01741                 {
01742                     _shadowCmat->addChunk(_shadowFactorMap2O);
01743                 }
01744                 else
01745                 {
01746                     _shadowCmat->addChunk(_shadowFactorMapO);
01747                 }
01748
01749                 _shadowSHL7->addUniformVariable("oldFactorMap", 7);
01750                 _shadowSHL7->addUniformVariable("shadowMap1", 0);
01751                 _shadowSHL7->addUniformVariable("shadowMap2", 1);
01752                 _shadowSHL7->addUniformVariable("shadowMap3", 2);
01753                 _shadowSHL7->addUniformVariable("shadowMap4", 3);
01754                 _shadowSHL7->addUniformVariable("shadowMap5", 4);
01755                 _shadowSHL7->addUniformVariable("shadowMap6", 5);
01756                 _shadowSHL7->addUniformVariable("shadowMap7", 6);
01757                 _shadowSHL7->addUniformVariable("firstRun", _firstRun);
01758                 _shadowSHL7->addUniformVariable("intensity1",
01759                                                  shadowIntensityF[0 +
01760                                                  (i * 7)]);
01761                 _shadowSHL7->addUniformVariable("intensity2",
01762                                                  shadowIntensityF[1 +
01763                                                  (i * 7)]);
01764                 _shadowSHL7->addUniformVariable("intensity3",
01765                                                  shadowIntensityF[2 +
01766                                                  (i * 7)]);
01767                 _shadowSHL7->addUniformVariable("intensity4",
01768                                                  shadowIntensityF[3 +
01769                                                  (i * 7)]);
01770                 _shadowSHL7->addUniformVariable("intensity5",
01771                                                  shadowIntensityF[4 +
01772                                                  (i * 7)]);
01773                 _shadowSHL7->addUniformVariable("intensity6",
01774                                                  shadowIntensityF[5 +
01775                                                  (i * 7)]);
01776                 _shadowSHL7->addUniformVariable("intensity7",
01777                                                  shadowIntensityF[6 +
01778                                                  (i * 7)]);
01779                 _shadowSHL7->addUniformVariable("lightPM1", shadowMatrixF[0 +
01780                                                  (i * 7)]);
01781                 _shadowSHL7->addUniformVariable("lightPM2", shadowMatrixF[1 +
01782                                                  (i * 7)]);
01783                 _shadowSHL7->addUniformVariable("lightPM3", shadowMatrixF[2 +
01784                                                  (i * 7)]);
01785                 _shadowSHL7->addUniformVariable("lightPM4", shadowMatrixF[3 +
01786                                                  (i * 7)]);
01787                 _shadowSHL7->addUniformVariable("lightPM5", shadowMatrixF[4 +
01788                                                  (i * 7)]);
01789                 _shadowSHL7->addUniformVariable("lightPM6", shadowMatrixF[5 +
01790                                                  (i * 7)]);
01791                 _shadowSHL7->addUniformVariable("lightPM7", shadowMatrixF[6 +
01792                                                  (i * 7)]);
01793                 _shadowSHL7->addUniformVariable("xFactor", Real32(xFactor));
01794                 _shadowSHL7->addUniformVariable("yFactor", Real32(yFactor));
01795                 _shadowSHL7->addUniformVariable("mapFactor1",
01796                                                  Real32(mapFactorF[0 + (i *
01797                                                                         7)]));
01798                 _shadowSHL7->addUniformVariable("mapFactor2",
01799                                                  Real32(mapFactorF[1 + (i *
01800                                                                         7)]));
01801                 _shadowSHL7->addUniformVariable("mapFactor3",
01802                                                  Real32(mapFactorF[2 + (i *
01803                                                                         7)]));
01804                 _shadowSHL7->addUniformVariable("mapFactor4",
01805                                                  Real32(mapFactorF[3 + (i *
01806                                                                         7)]));
01807                 _shadowSHL7->addUniformVariable("mapFactor5",
01808                                                  Real32(mapFactorF[4 + (i *
01809                                                                         7)]));
01810                 _shadowSHL7->addUniformVariable("mapFactor6",
01811                                                  Real32(mapFactorF[5 + (i *
01812                                                                         7)]));
01813                 _shadowSHL7->addUniformVariable("mapFactor7",
01814                                                  Real32(mapFactorF[6 + (i *
01815                                                                         7)]));
01816             }
01817
01818
01819             a->pushPartition((RenderPartition::CopyWindow      |
01820                               RenderPartition::CopyViewing     |
01821                               RenderPartition::CopyProjection  |
01822                               RenderPartition::CopyFrustum     |
01823                               RenderPartition::CopyNearFar     |
01824                               RenderPartition::CopyViewportSize),
01825                              RenderPartition::StateSorting);
01826             {
01827                 RenderPartition *pPart = a->getActivePartition();
01828
01829                 pPart->addPreRenderCallback (
01830                     &ShadowTreeHandler::setupAmbientModel);
01831                 pPart->addPostRenderCallback(
01832                     &ShadowTreeHandler::endAmbientModel  );
01833
01834                 pPart->setRenderTarget(_pSceneFBO);
01835                 pPart->setDrawBuffer  ( dBuffers );
01836
01837                 Node *light  = vLights[i].first;
01838                 Node *parent = light->getParent();
01839
01840                 if(parent != NULL)
01841                 {
01842                     a->pushMatrix(parent->getToWorld());
01843                 }
01844
01845                 if(_activeFactorMap == 0 && bCA1Cleared == false)
01846                 {
01847                     pPart->setBackground(_pClearBackground);
01848                     bCA1Cleared = true;
01849                 }
01850                 else if(bCA2Cleared == false)
01851                 {
01852                     pPart->setBackground(_pClearBackground);
01853                     bCA2Cleared = true;
01854                 }
01855
01856                 a->overrideMaterial(_shadowCmat, a->getActNode());
01857                 _pStage->recurse(a, light);
01858                 a->overrideMaterial( NULL,       a->getActNode());
01859
01860                 if(parent != NULL)
01861                 {
01862                     a->popMatrix();
01863                 }
01864             }
01865             a->popPartition();
01866
01867             _firstRun = 0;
01868             if(_activeFactorMap == 0)
01869                 _activeFactorMap = 1;
01870             else
01871                 _activeFactorMap = 0;
01872         }
01873     }
01874
01875     _firstRun = 0;
01876
01877     shadowIntensityF.clear();
01878     mapFactorF.clear();
01879     shadowMatrixF.clear();
01880 }
01881
01882
01883 void PerspectiveShadowMapHandler::render(RenderAction *a,
01884                                          DrawEnv      *pEnv)
01885 {
01886     const ShadowStageData::LightStore  &vLights      =
01887         _pStageData->getLights();
01888
01889     const ShadowStageData::NodeStore   &vTransparents =
01890         _pStageData->getTransparents();
01891
01892     const ShadowStageData::LStateStore &vLightStates =
01893         _pStageData->getLightStates();
01894
01895
01896     if(_pStageData->getShadowMaps().size() != vLights.size())
01897     {
01898         initShadowMaps();
01899     }
01900
01901     if(_bShadowMapsConfigured == false)
01902     {
01903         configureShadowMaps();
01904     }
01905
01906     if(_uiMapSize != _pStage->getMapSize())
01907     {
01908         updateShadowMapSize();
01909     }
01910
01911     if(_pSceneFBO == NULL)
01912         initSceneFBO(pEnv, true);
01913
01914     if(_width  != pEnv->getPixelWidth () ||
01915        _height != pEnv->getPixelHeight()  )
01916     {
01917         updateSceneFBOSize(pEnv, true);
01918     }
01919
01920     commitChanges();
01921
01922     if(_pStage->getMapSize() / 4 > _maxPLMapSize)
01923         _PLMapSize = _maxPLMapSize;
01924     else
01925         _PLMapSize = _pStage->getMapSize() / 4;
01926
01927     _firstRun = 1;
01928
01929
01930     //Matrizen fr alle Lichter berechnen
01931     for(UInt32 i = 0;i < vLights.size();i++)
01932     {
01933         if(vLightStates[i] != 0 &&
01934            (vLights[i].second->getShadowIntensity() != 0.0 ||
01935             _pStage->getGlobalShadowIntensity() != 0.0))
01936         {
01937             Matrix  _LPM, _LVM;
01938             calcPerspectiveSpot(_LPM, _LVM, i, pEnv);
01939             _perspectiveLPM.push_back(_LPM);
01940             _perspectiveLVM.push_back(_LVM);
01941
01942         }
01943         else
01944         {
01945             Matrix  _LPM, _LVM;
01946             _LPM.setIdentity();
01947             _LVM.setIdentity();
01948             _perspectiveLPM.push_back(_LPM);
01949             _perspectiveLVM.push_back(_LVM);
01950         }
01951     }
01952
01953     if(_pStage->getMapAutoUpdate() == true ||
01954        _pStage->_trigger_update    == true  )
01955     {
01956         _pPoly->setOffsetFill  (true                   );
01957         _pPoly->setOffsetFactor(_pStage->getOffFactor());
01958         _pPoly->setOffsetBias  (_pStage->getOffBias  ());
01959
01960         createColorMapFBO(a, pEnv);
01961
01962
01963         //deactivate transparent Nodes
01964         for(UInt32 t = 0;t < vTransparents.size();++t)
01965         {
01966             vTransparents[t]->setTravMask(0);
01967         }
01968
01969
01970         createShadowMapsFBO(a, pEnv);
01971
01972
01973         // switch on all transparent geos
01974         for(UInt32 t = 0;t < vTransparents.size();++t)
01975         {
01976             vTransparents[t]->setTravMask(TypeTraits<UInt32>::BitsSet);
01977         }
01978
01979
01980         createShadowFactorMapFBO(a, pEnv);
01981
01982         _pStage->_trigger_update = false;
01983     }
01984
01985     setupDrawCombineMap2(a);
01986
01987     _perspectiveLPM.clear();
01988     _perspectiveLVM.clear();
01989 }
01990
01991 OSG_END_NAMESPACE