OSGDitherShadowMapHandler.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 "OSGDitherShadowMapHandler.h"
00043 #include "OSGRenderAction.h"
00044 #include "OSGShadowStage.h"
00045 #include "OSGShadowStageData.h"
00046 #include "OSGSpotLight.h"
00047
00048 OSG_BEGIN_NAMESPACE
00049
00050 #include "ShaderCode/OSGDitherShadowMapShaderCode.cinl"
00051
00052
00053 DitherShadowMapHandler::DitherShadowMapHandler(ShadowStage     *pSource,
00054                                                ShadowStageData *pData  ) :
00055      Inherited    (pSource,
00056                    pData  ),
00057     _blender      (NULL   ),
00058     _matrixDeco   (NULL   ),
00059     _shadowSHL    (NULL   ),
00060     _shadowSHL2   (NULL   ),
00061     _shadowSHL3   (NULL   ),
00062     _shadowSHL4   (NULL   ),
00063     _shadowCubeSHL(NULL   ),
00064     _pPoly        (NULL   ),
00065     _firstRun     (1      )
00066 {
00067     _uiMode = ShadowStage::DITHER_SHADOW_MAP;
00068
00069     _shadowFactorMap2O     = TextureObjChunk::createLocal();
00070     _shadowFactorMapImage2 = Image          ::createLocal();
00071
00072     _shadowFactorMap2O->setImage         (_shadowFactorMapImage2);
00073     _shadowFactorMap2O->setInternalFormat(GL_RGB);
00074     _shadowFactorMap2O->setExternalFormat(GL_RGB);
00075     _shadowFactorMap2O->setMinFilter     (GL_LINEAR);
00076     _shadowFactorMap2O->setMagFilter     (GL_LINEAR);
00077     _shadowFactorMap2O->setWrapS         (GL_REPEAT);
00078     _shadowFactorMap2O->setWrapT         (GL_REPEAT);
00079     _shadowFactorMap2O->setTarget        (GL_TEXTURE_2D);
00080
00081
00082     //SHL Chunk 1
00083     _shadowSHL = SimpleSHLChunk::createLocal();
00084     _shadowSHL->setVertexProgram  (_dither_shadow_vp);
00085     _shadowSHL->setFragmentProgram(_dither_shadow_fp);
00086
00087     _shadowSHL2 = SimpleSHLChunk::createLocal();
00088     _shadowSHL2->setVertexProgram  (_dither_shadow2_vp);
00089     _shadowSHL2->setFragmentProgram(_dither_shadow2_fp);
00090
00091     _shadowSHL3 = SimpleSHLChunk::createLocal();
00092     _shadowSHL3->setVertexProgram  (_dither_shadow3_vp);
00093     _shadowSHL3->setFragmentProgram(_dither_shadow3_fp);
00094
00095     _shadowSHL4 = SimpleSHLChunk::createLocal();
00096     _shadowSHL4->setVertexProgram  (_dither_shadow4_vp);
00097     _shadowSHL4->setFragmentProgram(_dither_shadow4_fp);
00098
00099     //SHL Chunk 3
00100     _shadowCubeSHL = SimpleSHLChunk::createLocal();
00101     _shadowCubeSHL->setVertexProgram  (_dither_cubeshadow_vp);
00102     _shadowCubeSHL->setFragmentProgram(_dither_cubeshadow_fp);
00103
00104     _pPoly = PolygonChunk::createLocal();
00105
00106     _unlitMat->addChunk(_pPoly);
00107
00108     _matrixDeco = MatrixCameraDecorator::createLocal();
00109
00110 }
00111
00112 DitherShadowMapHandler::~DitherShadowMapHandler(void)
00113 {
00114     _blender          = NULL;
00115     _matrixDeco       = NULL;
00116     _shadowSHL        = NULL;
00117     _shadowSHL2       = NULL;
00118     _shadowSHL3       = NULL;
00119     _shadowSHL4       = NULL;
00120     _shadowCubeSHL    = NULL;
00121     _pPoly            = NULL;
00122
00123     _vShadowCmat      .clear();
00124     _vShadowSHLVar    .clear();
00125     _vShadowSHLVar2   .clear();
00126     _vShadowSHLVar3   .clear();
00127     _vShadowSHLVar4   .clear();
00128     _vShadowCubeSHLVar.clear();
00129 }
00130
00131
00132 void DitherShadowMapHandler::createShadowMapsFBO(
00133     RenderAction *a,
00134     DrawEnv      *pEnv)
00135 {
00136 #ifdef SHADOWCHECK
00137     glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
00138     glShadeModel(GL_FLAT);
00139     glDisable(GL_LIGHTING);
00140     glDepthMask(GL_TRUE);
00141 #endif
00142 
00143     // disable all lights more speed
00144     std::vector<bool> vLocalLightStates;
00145
00146     const ShadowStageData::LightStore  &vLights      =
00147         _pStageData->getLights();
00148
00149     const ShadowStageData::LStateStore &vLightStates =
00150         _pStageData->getLightStates();
00151
00152     const ShadowStageData::StatusStore &vRealPLight  =
00153         _pStageData->getRealPointLight();
00154
00155     const ShadowStageData::CamStore    &vLCams       =
00156         _pStageData->getLightCameras();
00157
00158     const ShadowStageData::StatusStore &vExclActive  =
00159         _pStageData->getExcludeNodeActive();
00160
00161
00162     for(UInt32 i = 0;i < vLights.size();++i)
00163     {
00164         // store old states.
00165         vLocalLightStates.push_back(vLights[i].second->getOn());
00166         vLights[i].second->setOn(false);
00167     }
00168
00169     // deactivate exclude nodes:
00170     for(UInt32 i = 0;i < _pStage->getMFExcludeNodes()->size();++i)
00171     {
00172         Node *exnode = _pStage->getExcludeNodes(i);
00173
00174         if(exnode != NULL)
00175             exnode->setTravMask(0);
00176     }
00177
00178
00179     ShadowStageData::ShadowMapStore &vShadowMaps = _pStageData->getShadowMaps();
00180
00181     for(UInt32 i = 0;i < vLights.size();++i)
00182     {
00183         if(vLightStates[i])
00184         {
00185             if(_pStage->getGlobalShadowIntensity() != 0.0 ||
00186                vLights[i].second->getShadowIntensity() != 0.0)
00187             {
00188                 //------Setting up Window to fit size of ShadowMap-------------
00189
00190
00191                 if(vLights[i].second->getType() != PointLight::getClassType() ||
00192                    vRealPLight[i]               == false                       )
00193                 {
00194                     a->pushPartition();
00195                     {
00196                         RenderPartition   *pPart    = a->getActivePartition();
00197
00198                         pPart->addPreRenderCallback(
00199                             &ShadowTreeHandler::setupAmbientModelAndMasks);
00200                         pPart->addPostRenderCallback(
00201                             &ShadowTreeHandler::endAmbientModelAndMasks);
00202
00203                         pPart->setRenderTarget(vShadowMaps[i].pFBO);
00204
00205                         pPart->setWindow  (a->getWindow());
00206
00207                         pPart->calcViewportDimension(0.f,
00208                                                      0.f,
00209                                                      _pStage->getMapSize()-1,
00210                                                      _pStage->getMapSize()-1,
00211
00212                                                      _pStage->getMapSize(),
00213                                                      _pStage->getMapSize() );
00214
00215
00216                         Matrix m, t;
00217
00218                         // set the projection
00219                         vLCams[i]->getProjection(
00220                             m,
00221                             pPart->getViewportWidth (),
00222                             pPart->getViewportHeight());
00223
00224                         vLCams[i]->getProjectionTranslation(
00225                             t,
00226                             pPart->getViewportWidth (),
00227                             pPart->getViewportHeight());
00228
00229                         pPart->setupProjection(m, t);
00230
00231                         vLCams[i]->getViewing(
00232                             m,
00233                             pPart->getViewportWidth (),
00234                             pPart->getViewportHeight());
00235
00236
00237                         pPart->setupViewing(m);
00238
00239                         pPart->setNear     (vLCams[i]->getNear());
00240                         pPart->setFar      (vLCams[i]->getFar ());
00241
00242                         pPart->calcFrustum();
00243
00244                         pPart->setBackground(_pClearBackground);
00245
00246                         Node *light  = vLights[i].first;
00247                         Node *parent = light->getParent();
00248
00249                         if(parent != NULL)
00250                         {
00251                             a->pushMatrix(parent->getToWorld());
00252                         }
00253
00254
00255                         a->overrideMaterial(_unlitMat, a->getActNode());
00256                         _pStage->recurse(a, light);
00257                         a->overrideMaterial( NULL,       a->getActNode());
00258
00259                         if(parent != NULL)
00260                         {
00261                             a->popMatrix();
00262                         }
00263                     }
00264                     a->popPartition();
00265                 }
00266                 else
00267                 {
00268                     for(UInt32 j = 0;j < 6;j++)
00269                     {
00270                         //Offset berechnen
00271                         UInt32  xOffset, yOffset;
00272                         if(j == 0)
00273                         {
00274                             xOffset = 0;
00275                             yOffset = 0;
00276                         }
00277                         else if(j == 1)
00278                         {
00279                             xOffset = _PLMapSize;
00280                             yOffset = 0;
00281                         }
00282                         else if(j == 2)
00283                         {
00284                             xOffset = 2 * _PLMapSize;
00285                             yOffset = 0;
00286                         }
00287                         else if(j == 3)
00288                         {
00289                             xOffset = 3 * _PLMapSize;
00290                             yOffset = 0;
00291                         }
00292                         else if(j == 4)
00293                         {
00294                             xOffset = 0;
00295                             yOffset = _PLMapSize;
00296                         }
00297                         else
00298                         {
00299                             xOffset = _PLMapSize;
00300                             yOffset = _PLMapSize;
00301                         }
00302
00303                         _matrixDeco->setDecoratee    ( vLCams[i]    );
00304                         _matrixDeco->setPreProjection(_aCubeTrans[j]);
00305
00306                         a->pushPartition();
00307                         {
00308                             RenderPartition   *pPart = a->getActivePartition();
00309
00310                             pPart->addPreRenderCallback(
00311                                 &ShadowTreeHandler::setupAmbientModelAndMasks);
00312                             pPart->addPostRenderCallback(
00313                                 &ShadowTreeHandler::endAmbientModelAndMasks);
00314
00315                             pPart->setRenderTarget(vShadowMaps[i].pFBO);
00316
00317                             pPart->setWindow  (a->getWindow());
00318
00319                             pPart->calcViewportDimension(
00320                                 xOffset,
00321                                 yOffset,
00322                                 xOffset + _PLMapSize,
00323                                 yOffset + _PLMapSize,
00324
00325                                 _pStage->getMapSize(),
00326                                 _pStage->getMapSize() );
00327
00328
00329                             Matrix m, t;
00330
00331                             // set the projection
00332                             _matrixDeco->getProjection          (
00333                                 m,
00334                                 pPart->getViewportWidth (),
00335                                 pPart->getViewportHeight());
00336
00337                             _matrixDeco->getProjectionTranslation(
00338                                 t,
00339                                 pPart->getViewportWidth (),
00340                                 pPart->getViewportHeight());
00341
00342                             pPart->setupProjection(m, t);
00343
00344                             _matrixDeco->getViewing(
00345                                 m,
00346                                 pPart->getViewportWidth (),
00347                                 pPart->getViewportHeight());
00348
00349                             pPart->setupViewing(m);
00350
00351                             pPart->setNear     (_matrixDeco->getNear());
00352                             pPart->setFar      (_matrixDeco->getFar ());
00353
00354                             pPart->calcFrustum();
00355
00356                             pPart->setBackground(_pClearBackground);
00357
00358                             Node *light  = vLights[i].first;
00359                             Node *parent = light->getParent();
00360
00361                             if(parent != NULL)
00362                             {
00363                                 a->pushMatrix(parent->getToWorld());
00364                             }
00365
00366
00367                             a->overrideMaterial(_unlitMat, a->getActNode());
00368                             _pStage->recurse(a, light);
00369                             a->overrideMaterial( NULL,       a->getActNode());
00370
00371                             if(parent != NULL)
00372                             {
00373                                 a->popMatrix();
00374                             }
00375                         }
00376                         a->popPartition();
00377                     }
00378                                     }
00379             }
00380         }
00381     }
00382
00383     //-------Restoring old states of Window and Viewport----------
00384
00385     // activate exclude nodes:
00386     for(UInt32 i = 0;i < _pStage->getMFExcludeNodes()->size();++i)
00387     {
00388         Node *exnode = _pStage->getExcludeNodes(i);
00389
00390         if(exnode != NULL)
00391         {
00392             if(vExclActive[i])
00393             {
00394                 exnode->setTravMask(TypeTraits<UInt32>::BitsSet);
00395             }
00396         }
00397     }
00398
00399     // enable all lights.
00400     for(UInt32 i = 0;i < vLights.size();++i)
00401     {
00402         // restore old states.
00403         vLights[i].second->setOn(vLocalLightStates[i]);
00404     }
00405
00406 #ifdef SHADOWCHECK
00407     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
00408     glShadeModel(GL_SMOOTH);
00409     glEnable(GL_LIGHTING);
00410 #endif
00411 }
00412
00413
00414 void DitherShadowMapHandler::createColorMapFBO(RenderAction *a,
00415                                                DrawEnv      *pEnv)
00416 {
00417     a->pushPartition((RenderPartition::CopyWindow      |
00418                       RenderPartition::CopyViewing     |
00419                       RenderPartition::CopyProjection  |
00420                       RenderPartition::CopyFrustum     |
00421                       RenderPartition::CopyNearFar     |
00422                       RenderPartition::CopyViewportSize),
00423                      RenderPartition::StateSorting);
00424     {
00425         RenderPartition *pPart = a->getActivePartition();
00426
00427         pPart->addPreRenderCallback (&ShadowTreeHandler::setupAmbientModel);
00428         pPart->addPostRenderCallback(&ShadowTreeHandler::endAmbientModel  );
00429
00430         pPart->setRenderTarget(_pSceneFBO);
00431         pPart->setDrawBuffer  (GL_COLOR_ATTACHMENT0_EXT);
00432
00433         Node *parent = a->getActNode()->getParent();
00434
00435         if(parent != NULL)
00436         {
00437             a->pushMatrix(parent->getToWorld());
00438         }
00439
00440         pPart->setBackground(a->getBackground());
00441
00442         _pStage->recurseFromThis(a);
00443
00444         if(parent != NULL)
00445         {
00446             a->popMatrix();
00447         }
00448     }
00449     a->popPartition();
00450 }
00451
00452 void DitherShadowMapHandler::createShadowFactorMapFBO(RenderAction *a,
00453                                                       DrawEnv      *pEnv)
00454 {
00455     _activeFactorMap = 0;
00456
00457     const ShadowStageData::LightStore  &vLights      =
00458         _pStageData->getLights();
00459
00460     const ShadowStageData::LStateStore &vLightStates =
00461         _pStageData->getLightStates();
00462
00463     const ShadowStageData::StatusStore &vRealPLight  =
00464         _pStageData->getRealPointLight();
00465
00466     const ShadowStageData::CamStore    &vLCams       =
00467         _pStageData->getLightCameras();
00468
00469
00470     //Finde alle aktiven Lichtquellen
00471     Real32 activeLights = 0;
00472
00473     if(_pStage->getGlobalShadowIntensity() != 0.0)
00474     {
00475         for(UInt32 i = 0;i < vLights.size();i++)
00476         {
00477             if(vLightStates[i] != 0)
00478                 activeLights++;
00479         }
00480     }
00481     else
00482     {
00483         for(UInt32 i = 0;i < vLights.size();i++)
00484         {
00485             if(vLightStates[i] != 0 &&
00486                vLights[i].second->getShadowIntensity() != 0.0)
00487             {
00488                 activeLights++;
00489             }
00490         }
00491     }
00492
00493     bool bCA1Cleared = false;
00494     bool bCA2Cleared = false;
00495
00496     UInt32 uiPLightCount = 0;
00497
00498     ShadowStageData::ShadowMapStore &vShadowMaps = _pStageData->getShadowMaps();
00499
00500     //Zuerst alle echte Pointlights
00501     for(UInt32 i = 0;i < vLights.size();i++)
00502     {
00503         if(vLightStates[i] != 0)
00504         {
00505             if((_pStage->getGlobalShadowIntensity  () != 0.0 ||
00506                 vLights[i].second->getShadowIntensity() != 0.0) &&
00507                vRealPLight[i])
00508             {
00509                 Real32  shadowIntensity;
00510                 if(_pStage->getGlobalShadowIntensity() != 0.0)
00511                 {
00512                     shadowIntensity = (_pStage->getGlobalShadowIntensity() /
00513                                        activeLights);
00514                 }
00515                 else
00516                 {
00517                     shadowIntensity =
00518                         (vLights[i].second->getShadowIntensity() /
00519                          activeLights);
00520                 }
00521
00522                 Matrix  LVM, LPM, CVM;
00523
00524                 vLCams[i]->getViewing(
00525                     LVM,
00526                     pEnv->getPixelWidth(),
00527                     pEnv->getPixelHeight());
00528
00529                 vLCams[i]->getProjection(
00530                     LPM,
00531                     pEnv->getPixelWidth(),
00532                     pEnv->getPixelHeight());
00533
00534                 CVM = pEnv->getCameraViewing();
00535
00536                 Matrix  iCVM = CVM;
00537                 iCVM.invert();
00538
00539                 Real32  texFactor;
00540                 if(vLights[i].second->getType() == PointLight::getClassType() ||
00541                    vLights[i].second->getType() == SpotLight ::getClassType()  )
00542                 {
00543                     texFactor = Real32(_width) / Real32(_height);
00544                 }
00545                 else
00546                 {
00547                     texFactor = 1.0;
00548                 }
00549
00550                 Matrix  shadowMatrix = LPM;
00551                 shadowMatrix.mult(LVM);
00552                 shadowMatrix.mult(iCVM);
00553
00554                 Real32  xFactor = 1.0;
00555                 Real32  yFactor = 1.0;
00556
00557                 Matrix m = pEnv->getCameraToWorld();
00558
00559                 Matrix  shadowMatrixOP = LVM;
00560                 shadowMatrix.mult(iCVM);
00561
00562                 Matrix  shadowMatrixA = LPM;
00563                 shadowMatrixA.mult(_aCubeTrans[0]);
00564                 shadowMatrixA.mult(LVM);
00565                 shadowMatrixA.mult(iCVM);
00566
00567                 Matrix  shadowMatrixB = LPM;
00568                 shadowMatrixB.mult(_aCubeTrans[1]);
00569                 shadowMatrixB.mult(LVM);
00570                 shadowMatrixB.mult(iCVM);
00571
00572                 Matrix  shadowMatrixC = LPM;
00573                 shadowMatrixC.mult(_aCubeTrans[2]);
00574                 shadowMatrixC.mult(LVM);
00575                 shadowMatrixC.mult(iCVM);
00576
00577                 Matrix  shadowMatrixD = LPM;
00578                 shadowMatrixD.mult(_aCubeTrans[3]);
00579                 shadowMatrixD.mult(LVM);
00580                 shadowMatrixD.mult(iCVM);
00581
00582                 Matrix  shadowMatrixE = LPM;
00583                 shadowMatrixE.mult(_aCubeTrans[4]);
00584                 shadowMatrixE.mult(LVM);
00585                 shadowMatrixE.mult(iCVM);
00586
00587                 Matrix  shadowMatrixF = LPM;
00588                 shadowMatrixF.mult(_aCubeTrans[5]);
00589                 shadowMatrixF.mult(LVM);
00590                 shadowMatrixF.mult(iCVM);
00591
00592                 if(_vShadowCubeSHLVar.size() == uiPLightCount)
00593                 {
00594                     _vShadowCubeSHLVar.push_back(
00595                         SimpleSHLVariableChunk::createLocal());
00596                 }
00597
00598                 OSG_ASSERT(uiPLightCount < _vShadowCubeSHLVar.size());
00599
00600                 _shadowCubeSHL->addUniformVariable("shadowMap", 0);
00601                 _shadowCubeSHL->addUniformVariable("oldFactorMap", 1);
00602                 _shadowCubeSHL->addUniformVariable("firstRun", _firstRun);
00603                 _shadowCubeSHL->addUniformVariable("intensity",
00604                                                     shadowIntensity);
00605                 _shadowCubeSHL->addUniformVariable("texFactor", texFactor);
00606                 _shadowCubeSHL->addUniformVariable("lightPMA", shadowMatrixA);
00607                 _shadowCubeSHL->addUniformVariable("lightPMB", shadowMatrixB);
00608                 _shadowCubeSHL->addUniformVariable("lightPMC", shadowMatrixC);
00609                 _shadowCubeSHL->addUniformVariable("lightPMD", shadowMatrixD);
00610                 _shadowCubeSHL->addUniformVariable("lightPME", shadowMatrixE);
00611                 _shadowCubeSHL->addUniformVariable("lightPMF", shadowMatrixF);
00612                 _shadowCubeSHL->addUniformVariable("lightPMOP",
00613                                                     shadowMatrixOP);
00614                 _shadowCubeSHL->addUniformVariable("KKtoWK", m);
00615                 _shadowCubeSHL->addUniformVariable("mapSize",
00616                                                     Real32(_PLMapSize));
00617                 _shadowCubeSHL->addUniformVariable("xFactor",
00618                                                     Real32(xFactor));
00619                 _shadowCubeSHL->addUniformVariable("yFactor",
00620                                                     Real32(yFactor));
00621
00622                 if(_vShadowCmat.size() == uiPLightCount)
00623                 {
00624                     _vShadowCmat.push_back(
00625                         ChunkMaterial::createLocal());
00626                 }
00627
00628                 OSG_ASSERT( uiPLightCount < _vShadowCmat.size());
00629
00630                 _vShadowCmat[uiPLightCount]->clearChunks();
00631                 _vShadowCmat[uiPLightCount]->addChunk(_shadowCubeSHL);
00632
00633                 _vShadowCmat[uiPLightCount]->addChunk(vShadowMaps[i].pTexO);
00634                 _vShadowCmat[uiPLightCount]->addChunk(vShadowMaps[i].pTexE);
00635
00636                 if(_activeFactorMap == 0)
00637                 {
00638                     _vShadowCmat[uiPLightCount]->addChunk(_shadowFactorMap2O);
00639                 }
00640                 else
00641                 {
00642                     _vShadowCmat[uiPLightCount]->addChunk(_shadowFactorMapO);
00643                 }
00644
00645                 GLenum dBuffers = GL_COLOR_ATTACHMENT1_EXT;
00646
00647                 if(_activeFactorMap == 0)
00648                     dBuffers = GL_COLOR_ATTACHMENT1_EXT;
00649                 else
00650                     dBuffers = GL_COLOR_ATTACHMENT2_EXT;
00651
00652                 a->pushPartition((RenderPartition::CopyWindow      |
00653                                   RenderPartition::CopyViewing     |
00654                                   RenderPartition::CopyProjection  |
00655                                   RenderPartition::CopyFrustum     |
00656                                   RenderPartition::CopyNearFar     |
00657                                   RenderPartition::CopyViewportSize),
00658                                  RenderPartition::StateSorting);
00659                 {
00660                     RenderPartition *pPart = a->getActivePartition();
00661
00662                     pPart->addPreRenderCallback (
00663                         &ShadowTreeHandler::setupAmbientModel);
00664                     pPart->addPostRenderCallback(
00665                         &ShadowTreeHandler::endAmbientModel  );
00666
00667                     pPart->setRenderTarget(_pSceneFBO);
00668                     pPart->setDrawBuffer  ( dBuffers );
00669
00670                     Node *light  = vLights[i].first;
00671                     Node *parent = light->getParent();
00672
00673                     if(parent != NULL)
00674                     {
00675                         a->pushMatrix(parent->getToWorld());
00676                     }
00677
00678                     if(_activeFactorMap == 0 && bCA1Cleared == false)
00679                     {
00680                         pPart->setBackground(_pClearBackground);
00681                         bCA1Cleared = true;
00682                     }
00683                     else if(bCA2Cleared == false)
00684                     {
00685                         pPart->setBackground(_pClearBackground);
00686                         bCA2Cleared = true;
00687                     }
00688
00689                     commitChanges();
00690
00691                     a->overrideMaterial(_vShadowCmat[uiPLightCount],
00692                                          a->getActNode());
00693                     _pStage->recurse(a, light);
00694                     a->overrideMaterial( NULL,
00695                                          a->getActNode());
00696
00697                     if(parent != NULL)
00698                     {
00699                         a->popMatrix();
00700                     }
00701                 }
00702                 a->popPartition();
00703
00704                 _firstRun = 0;
00705
00706                 if(_activeFactorMap == 0)
00707                     _activeFactorMap = 1;
00708                 else
00709                     _activeFactorMap = 0;
00710
00711                 ++uiPLightCount;
00712             }
00713         }
00714     }
00715
00716     std::vector<Real32> shadowIntensityF;
00717     std::vector<Real32> texFactorF;
00718     std::vector<Real32> mapFactorF;
00719     std::vector<Matrix> shadowMatrixF;
00720
00721     UInt32              lightCounter = 0;
00722
00723     Real32              xFactor = 1.0;
00724     Real32              yFactor = 1.0;
00725
00726     //Jetzt alle normalen Lichtquellen
00727     for(UInt32 i = 0;i < vLights.size();i++)
00728     {
00729         if(vLightStates[i] != 0 &&
00730            ((_pStage->getGlobalShadowIntensity() != 0.0 ||
00731              vLights[i].second->getShadowIntensity() != 0.0) &&
00732             vRealPLight[i] == false))
00733         {
00734
00735             Real32  shadowIntensity;
00736
00737             if(_pStage->getGlobalShadowIntensity() != 0.0)
00738             {
00739                 shadowIntensity = (_pStage->getGlobalShadowIntensity() /
00740                                    activeLights);
00741             }
00742             else
00743             {
00744                 shadowIntensity =
00745                     (vLights[i].second->getShadowIntensity() /
00746                      activeLights);
00747             }
00748
00749             shadowIntensityF.push_back(shadowIntensity);
00750
00751             Matrix  LVM, LPM, CVM;
00752
00753             vLCams[i]->getViewing(LVM,
00754                                   pEnv->getPixelWidth(),
00755                                   pEnv->getPixelHeight());
00756             vLCams[i]->getProjection(LPM,
00757                                      pEnv->getPixelWidth(),
00758                                      pEnv->getPixelHeight());
00759
00760             CVM = pEnv->getCameraViewing();
00761
00762             Matrix  iCVM = CVM;
00763             iCVM.invert();
00764
00765             Real32  texFactor;
00766             if(vLights[i].second->getType() == PointLight::getClassType() ||
00767                vLights[i].second->getType() == SpotLight ::getClassType()  )
00768             {
00769                 texFactor = Real32(_width) / Real32(_height);
00770             }
00771             else
00772             {
00773                 texFactor = 1.0;
00774             }
00775
00776             texFactorF.push_back(texFactor);
00777
00778             Matrix  shadowMatrix = LPM;
00779             shadowMatrix.mult(LVM);
00780             shadowMatrix.mult(iCVM);
00781
00782             shadowMatrixF.push_back(shadowMatrix);
00783
00784             Real32  mapFactor;
00785             mapFactor =
00786                 Real32(_pStage->getMapSize()) /
00787                 Real32(vShadowMaps[i].pImage->getWidth());
00788
00789             mapFactorF.push_back(mapFactor);
00790             lightCounter++;
00791         }
00792     }
00793
00794     if(lightCounter != 0)
00795     {
00796         UInt32 renderTimes = 1;
00797         UInt32 uiMatCount  = uiPLightCount;
00798
00799         UInt32 uiVarCount  = 0;
00800         UInt32 uiVarCount2 = 0;
00801         UInt32 uiVarCount3 = 0;
00802         UInt32 uiVarCount4 = 0;
00803
00804         if(lightCounter > 4)
00805             renderTimes = UInt32(ceil(Real32(lightCounter) / 4.0f));
00806
00807         for(UInt32 i = 0;i < renderTimes;i++)
00808         {
00809
00810             GLenum dBuffers = GL_COLOR_ATTACHMENT1_EXT;
00811
00812             if(_activeFactorMap == 0)
00813                 dBuffers = GL_COLOR_ATTACHMENT1_EXT;
00814             else
00815                 dBuffers = GL_COLOR_ATTACHMENT2_EXT;
00816
00817             UInt32  lightOffset = lightCounter - (i * 4);
00818
00819             //clear chunk and add Textures
00820
00821             if(_vShadowCmat.size() == uiMatCount)
00822             {
00823                 _vShadowCmat.push_back(ChunkMaterial::createLocal());
00824             }
00825
00826             OSG_ASSERT( uiMatCount < _vShadowCmat.size());
00827
00828             _vShadowCmat[uiMatCount]->clearChunks();
00829
00830             UInt32  lightNum = 0;
00831
00832             for(UInt32 j = 0;j < vLights.size();j++)
00833             {
00834                 if(vLightStates[j] != 0)
00835                 {
00836                     if((_pStage->getGlobalShadowIntensity  () != 0.0 ||
00837                         vLights[j].second->getShadowIntensity() != 0.0) &&
00838                        vRealPLight[j] == false)
00839                     {
00840                         if(lightNum >= (i * 4) && lightNum < ((i + 1) * 4))
00841                         {
00842                             _vShadowCmat[uiMatCount]->addChunk(
00843                                 vShadowMaps[j].pTexO);
00844                             _vShadowCmat[uiMatCount]->addChunk(
00845                                 vShadowMaps[j].pTexE);
00846                         }
00847                         lightNum++;
00848                     }
00849                 }
00850             }
00851
00852             if(lightOffset == 1)
00853             {
00854                 _vShadowCmat[uiMatCount]->addChunk(_shadowSHL);
00855
00856                 if(_activeFactorMap == 0)
00857                 {
00858                     _vShadowCmat[uiMatCount]->addChunk(_shadowFactorMap2O);
00859                 }
00860                 else
00861                 {
00862                     _vShadowCmat[uiMatCount]->addChunk(_shadowFactorMapO);
00863                 }
00864
00865                 if(_vShadowSHLVar.size() == uiVarCount)
00866                 {
00867                     _vShadowSHLVar.push_back(
00868                         SimpleSHLVariableChunk::createLocal());
00869                 }
00870
00871                 OSG_ASSERT(uiVarCount < _vShadowSHLVar.size());
00872
00873                 _shadowSHL->addUniformVariable("oldFactorMap", 1);
00874                 _shadowSHL->addUniformVariable("shadowMap", 0);
00875                 _shadowSHL->addUniformVariable("firstRun", _firstRun);
00876                 _shadowSHL->addUniformVariable("intensity",
00877                                                 shadowIntensityF[0 + (i * 4)]);
00878                 _shadowSHL->addUniformVariable("texFactor", texFactorF[0 +
00879                                                 (i * 4)]);
00880                 _shadowSHL->addUniformVariable("lightPM", shadowMatrixF[0 +
00881                                                 (i * 4)]);
00882                 _shadowSHL->addUniformVariable("xFactor", Real32(xFactor));
00883                 _shadowSHL->addUniformVariable("yFactor", Real32(yFactor));
00884                 _shadowSHL->addUniformVariable("mapSize",
00885                                                 Real32(_pStage->getMapSize
00886                                                        ()));
00887                 _shadowSHL->addUniformVariable("mapFactor",
00888                                                 Real32(mapFactorF[0 +
00889                                                        (i * 4)]));
00890
00891                 ++uiVarCount;
00892             }
00893
00894             else if(lightOffset == 2)
00895             {
00896                 _vShadowCmat[uiMatCount]->addChunk(_shadowSHL2);
00897
00898                 if(_activeFactorMap == 0)
00899                 {
00900                     _vShadowCmat[uiMatCount]->addChunk(_shadowFactorMap2O);
00901                 }
00902                 else
00903                 {
00904                     _vShadowCmat[uiMatCount]->addChunk(_shadowFactorMapO);
00905                 }
00906
00907                 if(_vShadowSHLVar2.size() == uiVarCount2)
00908                 {
00909                     _vShadowSHLVar2.push_back(
00910                         SimpleSHLVariableChunk::createLocal());
00911                 }
00912
00913                 OSG_ASSERT(uiVarCount2 < _vShadowSHLVar2.size());
00914
00915                 _shadowSHL2->addUniformVariable("oldFactorMap", 2);
00916                 _shadowSHL2->addUniformVariable("shadowMap1", 0);
00917                 _shadowSHL2->addUniformVariable("shadowMap2", 1);
00918                 _shadowSHL2->addUniformVariable("firstRun", _firstRun);
00919                 _shadowSHL2->addUniformVariable("intensity1",
00920                                                  shadowIntensityF[0 +
00921                                                  (i * 4)]);
00922                 _shadowSHL2->addUniformVariable("intensity2",
00923                                                  shadowIntensityF[1 +
00924                                                  (i * 4)]);
00925                 _shadowSHL2->addUniformVariable("texFactor1", texFactorF[0 +
00926                                                  (i * 4)]);
00927                 _shadowSHL2->addUniformVariable("texFactor2", texFactorF[1 +
00928                                                  (i * 4)]);
00929                 _shadowSHL2->addUniformVariable("lightPM1", shadowMatrixF[0 +
00930                                                  (i * 4)]);
00931                 _shadowSHL2->addUniformVariable("lightPM2", shadowMatrixF[1 +
00932                                                  (i * 4)]);
00933                 _shadowSHL2->addUniformVariable("xFactor", Real32(xFactor));
00934                 _shadowSHL2->addUniformVariable("yFactor", Real32(yFactor));
00935                 _shadowSHL2->addUniformVariable("mapSize",
00936                                                  Real32(
00937                                                  _pStage->getMapSize()));
00938                 _shadowSHL2->addUniformVariable("mapFactor1",
00939                                                  Real32(mapFactorF[0 + (i *
00940                                                                         4)]));
00941                 _shadowSHL2->addUniformVariable("mapFactor2",
00942                                                  Real32(mapFactorF[1 + (i *
00943                                                                         4)]));
00944                 ++uiVarCount2;
00945             }
00946
00947             else if(lightOffset == 3)
00948             {
00949                 _vShadowCmat[uiMatCount]->addChunk(_shadowSHL3);
00950
00951                 if(_activeFactorMap == 0)
00952                 {
00953                     _vShadowCmat[uiMatCount]->addChunk(_shadowFactorMap2O);
00954                 }
00955                 else
00956                 {
00957                     _vShadowCmat[uiMatCount]->addChunk(_shadowFactorMapO);
00958                 }
00959
00960                 if(_vShadowSHLVar3.size() == uiVarCount3)
00961                 {
00962                     _vShadowSHLVar3.push_back(
00963                         SimpleSHLVariableChunk::createLocal());
00964                 }
00965
00966                 OSG_ASSERT(uiVarCount3 < _vShadowSHLVar3.size());
00967
00968                 _shadowSHL3->addUniformVariable("oldFactorMap", 3);
00969                 _shadowSHL3->addUniformVariable("shadowMap1", 0);
00970                 _shadowSHL3->addUniformVariable("shadowMap2", 1);
00971                 _shadowSHL3->addUniformVariable("shadowMap3", 2);
00972                 _shadowSHL3->addUniformVariable("firstRun", _firstRun);
00973                 _shadowSHL3->addUniformVariable("intensity1",
00974                                                  shadowIntensityF[0 +
00975                                                  (i * 4)]);
00976                 _shadowSHL3->addUniformVariable("intensity2",
00977                                                  shadowIntensityF[1 +
00978                                                  (i * 4)]);
00979                 _shadowSHL3->addUniformVariable("intensity3",
00980                                                  shadowIntensityF[2 +
00981                                                  (i * 4)]);
00982                 _shadowSHL3->addUniformVariable("texFactor1", texFactorF[0 +
00983                                                  (i * 4)]);
00984                 _shadowSHL3->addUniformVariable("texFactor2", texFactorF[1 +
00985                                                  (i * 4)]);
00986                 _shadowSHL3->addUniformVariable("texFactor3", texFactorF[2 +
00987                                                  (i * 4)]);
00988                 _shadowSHL3->addUniformVariable("lightPM1", shadowMatrixF[0 +
00989                                                  (i * 4)]);
00990                 _shadowSHL3->addUniformVariable("lightPM2", shadowMatrixF[1 +
00991                                                  (i * 4)]);
00992                 _shadowSHL3->addUniformVariable("lightPM3", shadowMatrixF[2 +
00993                                                  (i * 4)]);
00994                 _shadowSHL3->addUniformVariable("xFactor", Real32(xFactor));
00995                 _shadowSHL3->addUniformVariable("yFactor", Real32(yFactor));
00996                 _shadowSHL3->addUniformVariable("mapSize",
00997                                                  Real32(
00998                                                  _pStage->getMapSize()));
00999                 _shadowSHL3->addUniformVariable("mapFactor1",
01000                                                  Real32(mapFactorF[0 + (i *
01001                                                                         4)]));
01002                 _shadowSHL3->addUniformVariable("mapFactor2",
01003                                                  Real32(mapFactorF[1 + (i *
01004                                                                         4)]));
01005                 _shadowSHL3->addUniformVariable("mapFactor3",
01006                                                  Real32(mapFactorF[2 + (i *
01007                                                                         4)]));
01008
01009                 ++uiVarCount3;
01010             }
01011
01012             else
01013             {
01014                 _vShadowCmat[uiMatCount]->addChunk(_shadowSHL4);
01015
01016                 if(_activeFactorMap == 0)
01017                 {
01018                     _vShadowCmat[uiMatCount]->addChunk(_shadowFactorMap2O);
01019                 }
01020                 else
01021                 {
01022                     _vShadowCmat[uiMatCount]->addChunk(_shadowFactorMapO);
01023                 }
01024
01025                 if(_vShadowSHLVar4.size() == uiVarCount4)
01026                 {
01027                     _vShadowSHLVar4.push_back(
01028                         SimpleSHLVariableChunk::createLocal());
01029                 }
01030
01031                 OSG_ASSERT(uiVarCount4 < _vShadowSHLVar4.size());
01032
01033                 _shadowSHL4->addUniformVariable("oldFactorMap", 4);
01034                 _shadowSHL4->addUniformVariable("shadowMap1", 0);
01035                 _shadowSHL4->addUniformVariable("shadowMap2", 1);
01036                 _shadowSHL4->addUniformVariable("shadowMap3", 2);
01037                 _shadowSHL4->addUniformVariable("shadowMap4", 3);
01038                 _shadowSHL4->addUniformVariable("firstRun", _firstRun);
01039                 _shadowSHL4->addUniformVariable("intensity1",
01040                                                  shadowIntensityF[0 +
01041                                                  (i * 4)]);
01042                 _shadowSHL4->addUniformVariable("intensity2",
01043                                                  shadowIntensityF[1 +
01044                                                  (i * 4)]);
01045                 _shadowSHL4->addUniformVariable("intensity3",
01046                                                  shadowIntensityF[2 +
01047                                                  (i * 4)]);
01048                 _shadowSHL4->addUniformVariable("intensity4",
01049                                                  shadowIntensityF[3 +
01050                                                  (i * 4)]);
01051                 _shadowSHL4->addUniformVariable("texFactor1", texFactorF[0 +
01052                                                  (i * 4)]);
01053                 _shadowSHL4->addUniformVariable("texFactor2", texFactorF[1 +
01054                                                  (i * 4)]);
01055                 _shadowSHL4->addUniformVariable("texFactor3", texFactorF[2 +
01056                                                  (i * 4)]);
01057                 _shadowSHL4->addUniformVariable("texFactor4", texFactorF[3 +
01058                                                  (i * 4)]);
01059                 _shadowSHL4->addUniformVariable("lightPM1", shadowMatrixF[0 +
01060                                                  (i * 4)]);
01061                 _shadowSHL4->addUniformVariable("lightPM2", shadowMatrixF[1 +
01062                                                  (i * 4)]);
01063                 _shadowSHL4->addUniformVariable("lightPM3", shadowMatrixF[2 +
01064                                                  (i * 4)]);
01065                 _shadowSHL4->addUniformVariable("lightPM4", shadowMatrixF[3 +
01066                                                  (i * 4)]);
01067                 _shadowSHL4->addUniformVariable("xFactor", Real32(xFactor));
01068                 _shadowSHL4->addUniformVariable("yFactor", Real32(yFactor));
01069                 _shadowSHL4->addUniformVariable("mapSize",
01070                                                  Real32(
01071                                                  _pStage->getMapSize()));
01072                 _shadowSHL4->addUniformVariable("mapFactor1",
01073                                                  Real32(mapFactorF[0 + (i *
01074                                                                         4)]));
01075                 _shadowSHL4->addUniformVariable("mapFactor2",
01076                                                  Real32(mapFactorF[1 + (i *
01077                                                                         4)]));
01078                 _shadowSHL4->addUniformVariable("mapFactor3",
01079                                                  Real32(mapFactorF[2 + (i *
01080                                                                         4)]));
01081                 _shadowSHL4->addUniformVariable("mapFactor4",
01082                                                  Real32(mapFactorF[3 + (i *
01083                                                                         4)]));
01084
01085                 ++uiVarCount4;
01086             }
01087
01088             a->pushPartition((RenderPartition::CopyWindow      |
01089                               RenderPartition::CopyViewing     |
01090                               RenderPartition::CopyProjection  |
01091                               RenderPartition::CopyFrustum     |
01092                               RenderPartition::CopyNearFar     |
01093                               RenderPartition::CopyViewportSize),
01094                              RenderPartition::StateSorting);
01095             {
01096                 RenderPartition *pPart = a->getActivePartition();
01097
01098                 pPart->addPreRenderCallback (
01099                     &ShadowTreeHandler::setupAmbientModel);
01100                 pPart->addPostRenderCallback(
01101                     &ShadowTreeHandler::endAmbientModel  );
01102
01103                 pPart->setRenderTarget(_pSceneFBO);
01104                 pPart->setDrawBuffer  ( dBuffers );
01105
01106                 Node *light  = vLights[i].first;
01107                 Node *parent = light->getParent();
01108
01109                 if(parent != NULL)
01110                 {
01111                     a->pushMatrix(parent->getToWorld());
01112                 }
01113
01114                 if(_activeFactorMap == 0 && bCA1Cleared == false)
01115                 {
01116                     pPart->setBackground(_pClearBackground);
01117                     bCA1Cleared = true;
01118                 }
01119                 else if(bCA2Cleared == false)
01120                 {
01121                     pPart->setBackground(_pClearBackground);
01122                     bCA2Cleared = true;
01123                 }
01124
01125                 commitChanges();
01126
01127                 a->overrideMaterial(_vShadowCmat[uiMatCount], a->getActNode());
01128                 _pStage->recurse(a, light);
01129                 a->overrideMaterial( NULL,                    a->getActNode());
01130
01131                 if(parent != NULL)
01132                 {
01133                     a->popMatrix();
01134                 }
01135             }
01136             a->popPartition();
01137
01138             _firstRun = 0;
01139             if(_activeFactorMap == 0)
01140                 _activeFactorMap = 1;
01141             else
01142                 _activeFactorMap = 0;
01143
01144             ++uiMatCount;
01145         }
01146     }
01147     _firstRun = 0;
01148     shadowIntensityF.clear();
01149     texFactorF.clear();
01150     mapFactorF.clear();
01151     shadowMatrixF.clear();
01152 }
01153
01154
01155 void DitherShadowMapHandler::render(RenderAction *a,
01156                                     DrawEnv      *pEnv)
01157 {
01158     const ShadowStageData::LightStore  &vLights      =
01159         _pStageData->getLights();
01160
01161     const ShadowStageData::NodeStore   &vTransparents =
01162         _pStageData->getTransparents();
01163
01164
01165     if(_pStageData->getShadowMaps().size() != vLights.size())
01166     {
01167         initShadowMaps();
01168     }
01169
01170     if(_bShadowMapsConfigured == false)
01171     {
01172         configureShadowMaps();
01173     }
01174
01175     if(_uiMapSize != _pStage->getMapSize())
01176     {
01177         updateShadowMapSize();
01178     }
01179
01180     if(_pSceneFBO == NULL)
01181         initSceneFBO(pEnv, true);
01182
01183     if(_width  != pEnv->getPixelWidth () ||
01184        _height != pEnv->getPixelHeight()  )
01185     {
01186         updateSceneFBOSize(pEnv, true);
01187     }
01188
01189     commitChanges();
01190
01191
01192     if(_pStage->getMapSize() / 4 > _maxPLMapSize)
01193         _PLMapSize = _maxPLMapSize;
01194     else
01195         _PLMapSize = _pStage->getMapSize() / 4;
01196
01197
01198     //Used for ShadowFactorMap
01199     _firstRun = 1;
01200
01201
01202     if(_pStage->getMapAutoUpdate() == true ||
01203        _pStage->_trigger_update    == true  )
01204     {
01205         _pPoly->setOffsetFill  (true                   );
01206         _pPoly->setOffsetFactor(_pStage->getOffFactor());
01207         _pPoly->setOffsetBias  (_pStage->getOffBias  ());
01208
01209
01210         createColorMapFBO(a, pEnv);
01211
01212
01213         //deactivate transparent Nodes
01214         for(UInt32 t = 0;t < vTransparents.size();++t)
01215         {
01216             vTransparents[t]->setTravMask(0);
01217         }
01218
01219
01220         createShadowMapsFBO(a, pEnv);
01221
01222
01223         // switch on all transparent geos
01224         for(UInt32 t = 0;t < vTransparents.size();++t)
01225         {
01226             vTransparents[t]->setTravMask(TypeTraits<UInt32>::BitsSet);
01227         }
01228
01229
01230         createShadowFactorMapFBO(a, pEnv);
01231
01232         _pStage->_trigger_update = false;
01233     }
01234
01235     setupDrawCombineMap2(a);
01236 }
01237
01238 OSG_END_NAMESPACE