Ignore:
Timestamp:
10/17/06 09:21:50 (18 years ago)
Author:
szirmay
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp

    r1130 r1628  
    4242                RenderTarget* rt = hpb->getRenderTarget(); 
    4343                Viewport* v = rt->addViewport(depthMapCamera); 
     44                v->setBackgroundColour(ColourValue::White); 
    4445                v->setOverlaysEnabled(false); 
    4546                rt->setAutoUpdated(false); 
    46  
    47                 texPtr = Ogre::TextureManager::getSingleton().createManual(blurredname,  
    48                                                                                                                                                         "default", 
    49                                                                                                                                                         TEX_TYPE_2D, 
    50                                                                                                                                                         resolutionX, 
    51                                                                                                                                                         resolutionY, 
    52                                                                                                                                                         6, 
    53                                                                                                                                                         0, 
    54                                                                                                                                                         PF_FLOAT16_RGBA, 
    55                                                                                                                                                         TU_RENDERTARGET); 
    56                 blurredDepthMapTexture = texPtr.getPointer(); 
    57                 hpb = (blurredDepthMapTexture->getBuffer()).getPointer(); 
    58                 rt = hpb->getRenderTarget(); 
    59                 v = rt->addViewport(depthMapCamera); 
    60                 v->setOverlaysEnabled(false); 
    61                 rt->setAutoUpdated(false); 
     47                 
     48                if(OgreIlluminationManager::getSingleton().getBlurShadowMap()) 
     49                { 
     50                        texPtr = Ogre::TextureManager::getSingleton().createManual(blurredname,  
     51                                                                                                                                                                "default", 
     52                                                                                                                                                                TEX_TYPE_2D, 
     53                                                                                                                                                                resolutionX, 
     54                                                                                                                                                                resolutionY, 
     55                                                                                                                                                                6, 
     56                                                                                                                                                                0, 
     57                                                                                                                                                                PF_FLOAT16_RGBA, 
     58                                                                                                                                                                TU_RENDERTARGET); 
     59                        blurredDepthMapTexture = texPtr.getPointer(); 
     60                        hpb = (blurredDepthMapTexture->getBuffer()).getPointer(); 
     61                        rt = hpb->getRenderTarget(); 
     62                        v = rt->addViewport(depthMapCamera); 
     63                        v->setOverlaysEnabled(false); 
     64                        rt->setAutoUpdated(false); 
     65                } 
    6266} 
    6367 
     
    8892        rt->update(); 
    8993        restoreMaterials(); 
    90  
    91         rt = blurredDepthMapTexture->getBuffer().getPointer()->getRenderTarget(); 
    92         Material*  mat = (Material*) MaterialManager::getSingleton().getByName("GameTools/Blur").getPointer(); 
    93         mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(depthMapTexture->getName()); 
    94         renderFullscreenQuad("GameTools/Blur", rt); 
    95          
     94         
     95        if(OgreIlluminationManager::getSingleton().getBlurShadowMap()) 
     96        { 
     97                rt = blurredDepthMapTexture->getBuffer().getPointer()->getRenderTarget(); 
     98                Material*  mat = (Material*) MaterialManager::getSingleton().getByName("GameTools/Blur").getPointer(); 
     99                mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(depthMapTexture->getName()); 
     100                renderFullscreenQuad("GameTools/Blur", rt); 
     101        }        
    96102         
    97103        //rt->writeContentsToFile("shadowmap1.dds"); 
     
    99105        //depthMapTexture->(); 
    100106} 
    101  
    102 void setnear() 
    103 { 
    104  
     107String OgreDepthShadowMapRenderingRun::getDepthMapTextureName() 
     108{ 
     109                if(OgreIlluminationManager::getSingleton().getBlurShadowMap()) 
     110                        return blurredname; 
     111 
     112                return name; 
    105113} 
    106114 
     
    110118        if(light!= 0) 
    111119        { 
     120                bool uselispsm = OgreIlluminationManager::getSingleton().getUseLISPSM(); 
    112121                if(light->getType() == Light::LT_DIRECTIONAL) 
    113122                { 
     
    118127                        frun->setCameraMatrices(viewcam->getViewMatrix(), viewcam->getProjectionMatrix()); 
    119128 
    120                         Vector3 lightpos = light->getPosition(); 
     129                        Vector3 lightpos = light->getParentSceneNode()->getWorldPosition(); 
    121130                        Vector3 lightdir = light->getDirection(); 
    122131                        lightdir.normalise();            
     
    125134                        Vector3 max;                     
    126135 
    127                         bool uselispsm = true; 
     136                         
    128137                        if(uselispsm) 
    129138                        { 
     
    138147                                 
    139148                                Vector3 left = lightdir.crossProduct(viewdir); 
     149                                left.normalise(); 
    140150                                Vector3 up = left.crossProduct(lightdir); 
    141151                                up.normalise(); 
     
    167177                                Matrix4 newViewMatrix(  left.x,      left.y,      left.z,                -left.dotProduct(pos), 
    168178                                                                                up.x,            up.y,            up.z,                  -up.dotProduct(pos), 
    169                                                                                 lightdir.x,  lightdir.y,  lightdir.z,    -lightdir.dotProduct(pos), 
     179                                                                                -lightdir.x,  -lightdir.y,  -lightdir.z,    lightdir.dotProduct(pos), 
    170180                                                                                0,           0,           0,             1); 
    171181 
     
    200210                        Vector3 scale = (max - min); 
    201211                        scale *= 1.5; // just for safety 
    202                         scale.z = 200; //TODO: get scene bounding box to set z scale     
     212                        if(uselispsm) 
     213                                scale.z += 1; 
     214                        else 
     215                                scale.z += 1000; //TODO: get scene bounding box to set z scale   
    203216                        Matrix4 projection = Matrix4::IDENTITY; 
    204                         projection.setScale(Vector3(2.0 / scale.x, 2.0 / scale.y, -2.0 / scale.z));                      
    205                         projection.setTrans(Vector3(-(max.x + min.x) / scale.x, 
    206                                                                                 -(max.y + min.y) / scale.y, 
    207                                                                                 -(max.z + min.z) / scale.z)); 
     217                        Matrix4 trans = Matrix4::IDENTITY; 
     218                        trans.setTrans(Vector3(-(max.x + min.x) / 2.0, 
     219                                                                   -(max.y + min.y) / 2.0, 
     220                                                                   -(max.z + min.z) / 2.0)); 
     221                        Matrix4 scaleM = Matrix4::IDENTITY; 
     222                        scaleM.setScale(Vector3(2.0 / scale.x, 2.0 / scale.y, -2.0 / scale.z)); 
     223                        projection = scaleM * trans;                     
    208224                        depthMapCamera->setCustomProjectionMatrix(true, projection);                     
    209225                } 
    210226                else if(light->getType() == Light::LT_SPOTLIGHT) 
    211227                { 
    212         /*              Vector3 dir = light->getDirection(); 
    213                         depthMapCamera->setDirection( dir ); 
    214                         Vector3 pos = light->getParentNode()->getWorldPosition(); 
    215                         depthMapCamera->setPosition(pos); 
    216                         depthMapCamera->setProjectionType(PT_PERSPECTIVE);               
    217                         depthMapCamera->setFOVy(light->getSpotlightOuterAngle());*/ 
     228                        Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera();                       
     229                         
     230                        OgreFocusingMapRenderingRun* frun = (OgreFocusingMapRenderingRun*) 
     231                                OgreIlluminationManager::getSingleton().getGlobalRun(ILLUMRUN_FOCUSING_MAP)->asOgreRenderingRun(); 
     232                        frun->setCameraMatrices(viewcam->getViewMatrix(), viewcam->getProjectionMatrix()); 
     233 
     234                        Vector3 lightpos = light->getParentSceneNode()->getWorldPosition (); 
     235                        Vector3 lightdir = light->getDirection(); 
     236                        lightdir.normalise();            
     237                        Radian lightangle = light->getSpotlightOuterAngle(); 
     238                                 
     239                        Vector3 min; 
     240                        Vector3 max; 
     241                         
     242                        if(uselispsm) 
     243                        { 
     244                                Camera* maincam = OgreIlluminationManager::getSingleton().getMainCamera(); 
     245                                Vector3 viewdir = maincam->getDirection(); 
     246                                viewdir.normalise(); 
     247                                Vector3 campos = maincam->getPosition(); 
     248                                float nearclip = maincam->getNearClipDistance(); 
     249 
     250                                float dotprod = viewdir.dotProduct(lightdir); 
     251                                float sinGamma = sqrt(1.0 - dotprod * dotprod); 
     252                                 
     253                                Vector3 left = lightdir.crossProduct(viewdir); 
     254                                Vector3 up = left.crossProduct(lightdir); 
     255                                up.normalise(); 
     256                                //left = lightdir.crossProduct(up); 
     257                                left.normalise(); 
     258                                 
     259                                depthMapCamera->setCustomProjectionMatrix(false); 
     260                                depthMapCamera->setProjectionType(PT_PERSPECTIVE); 
     261                                depthMapCamera->setFOVy(lightangle); 
     262                                depthMapCamera->setAspectRatio(1); 
     263                                depthMapCamera->setNearClipDistance(0.1); 
     264                                depthMapCamera->setFarClipDistance(1.0); 
     265 
     266                                Matrix4 projMatrix = depthMapCamera->getProjectionMatrixWithRSDepth(); 
     267 
     268                                Matrix4 viewMatrix( left.x,      left.y,      left.z,            0, 
     269                                                                        up.x,            up.y,            up.z,                  0, 
     270                                                                        -lightdir.x,  -lightdir.y,  -lightdir.z,    0, 
     271                                                                        0,           0,           0,             1);                             
     272                                 
     273                                frun->setLightMatrix(projMatrix * viewMatrix);                           
     274                                OgreIlluminationManager::getSingleton().updateGlobalRun( ILLUMRUN_FOCUSING_MAP, 
     275                                                                                                                                                        frameNum);                       
     276                                frun->getMinMax(min, max); 
     277 
     278                                float n; 
     279                                float f; 
     280                                float d = abs(max.y - min.y); 
     281                                         
     282                                float z_n = nearclip / sinGamma; 
     283                                float one_min_singamma = (1 - sinGamma); 
     284                                float fact = d * pow(one_min_singamma, 2); 
     285                                float z_f = z_n + d * sinGamma * fact; 
     286                                n = (z_n + sqrt(z_f * z_n)) / sinGamma; 
     287                                ///n = 100000; 
     288                                f = n + d; 
     289 
     290                                Vector3 pos = campos - up * (n - nearclip); 
     291                                Matrix4 newViewMatrix(  left.x,      left.y,      left.z,                -left.dotProduct(pos), 
     292                                                                                up.x,            up.y,            up.z,                  -up.dotProduct(pos), 
     293                                                                                -lightdir.x,  -lightdir.y,  -lightdir.z,    lightdir.dotProduct(pos), 
     294                                                                                0,           0,           0,             1); 
     295 
     296                                Matrix4 lispMat = Matrix4::IDENTITY; 
     297                                 
     298                                lispMat[1][1] = f / (f - n); 
     299                                lispMat[1][3] = f * n / (f - n); 
     300                                lispMat[3][1] = 1; 
     301                                lispMat[3][3] = 0; 
     302                                 
     303                                depthMapCamera->setCustomProjectionMatrix(true, Matrix4::IDENTITY); 
     304                                depthMapCamera->setCustomViewMatrix(true, lispMat * projMatrix * newViewMatrix); 
     305                        } 
     306                        else 
     307                        { 
     308                                depthMapCamera->setCustomProjectionMatrix(false); 
     309                                depthMapCamera->setCustomViewMatrix(false); 
     310                                depthMapCamera->setPosition(lightpos); 
     311                                depthMapCamera->setDirection(lightdir); 
     312                                depthMapCamera->setProjectionType(PT_PERSPECTIVE); 
     313                                depthMapCamera->setFOVy(lightangle); 
     314                                depthMapCamera->setAspectRatio(1); 
     315                                depthMapCamera->setNearClipDistance(0.1); 
     316                                depthMapCamera->setFarClipDistance(1.0); 
     317                        } 
     318                         
     319 
     320                        Matrix4 lightMatrix = depthMapCamera->getViewMatrix(); 
     321                        frun->setLightMatrix(lightMatrix); 
     322                         
     323                        OgreIlluminationManager::getSingleton().updateGlobalRun( ILLUMRUN_FOCUSING_MAP, 
     324                                                                                                                                                frameNum); 
     325 
     326                        frun->getMinMax(min, max);                       
     327                         
     328                        if(min.z < 0) 
     329                        { 
     330                                float farP= -1.5 * min.z; 
     331                                if(farP < 0.2) farP = 0.2; 
     332                                depthMapCamera->setFarClipDistance(farP); 
     333 
     334                                Vector3 scale = (max - min); 
     335                                scale *= 1.5; // just for safety 
     336                                 
     337                                Matrix4 projection = Matrix4::IDENTITY; 
     338                                Matrix4 trans = Matrix4::IDENTITY; 
     339                                trans.setTrans(Vector3(-(max.x + min.x) / 2.0, 
     340                                                                   -(max.y + min.y) / 2.0, 
     341                                                                     0)); 
     342                                Matrix4 scaleM = Matrix4::IDENTITY; 
     343                                scaleM.setScale(Vector3(2.0 / scale.x, 2.0 / scale.y, 1)); 
     344                                projection = scaleM * trans;                     
     345                                depthMapCamera->setCustomProjectionMatrix(true, projection * depthMapCamera->getProjectionMatrix());                                            
     346                        } 
    218347                } 
    219348                else//point light 
Note: See TracChangeset for help on using the changeset viewer.