[790] | 1 | #include "OgreDepthShadowRecieverRenderTechnique.h"
|
---|
| 2 | #include "OgreTechniqueGroup.h"
|
---|
| 3 | #include "OgreIlluminationManager.h"
|
---|
| 4 | #include "OgreDepthShadowMapRenderingRun.h"
|
---|
| 5 |
|
---|
| 6 | OgreDepthShadowRecieverRenderTechnique::OgreDepthShadowRecieverRenderTechnique(
|
---|
| 7 | int maxlights,
|
---|
| 8 | String shadowVertexProgram,
|
---|
| 9 | String shadowFragmentProgram,
|
---|
| 10 | Pass* pass,
|
---|
| 11 | OgreRenderable* parentRenderable,
|
---|
| 12 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
| 13 | :RenderTechnique( parentRenderable, parentTechniqueGroup),
|
---|
| 14 | OgreRenderTechnique(pass, parentRenderable, parentTechniqueGroup),
|
---|
| 15 | DepthShadowRecieverRenderTechnique(parentRenderable, parentTechniqueGroup)
|
---|
| 16 | {
|
---|
| 17 | this->maxlights = maxlights;
|
---|
| 18 | this->shadowVertexProgram = shadowVertexProgram ;
|
---|
| 19 | this->shadowFragmentProgram = shadowFragmentProgram;
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | //insert new passes
|
---|
| 23 | Ogre::Technique* techn = pass->getParent();
|
---|
| 24 | Technique::PassIterator it = techn->getPassIterator();
|
---|
| 25 |
|
---|
| 26 | int index = 0;
|
---|
| 27 | while(it.hasMoreElements())
|
---|
| 28 | {
|
---|
| 29 | if( it.getNext() == pass)
|
---|
| 30 | break;
|
---|
| 31 | index++;
|
---|
| 32 | it.moveNext();
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | index++;
|
---|
| 36 | for(int i = 0; i < maxlights; i++)
|
---|
| 37 | {
|
---|
| 38 | int lastpass = techn->getNumPasses();
|
---|
| 39 | Pass* newpass = techn->createPass();
|
---|
| 40 | passes.push_back(newpass);
|
---|
| 41 |
|
---|
| 42 | newpass->setVertexProgram(shadowVertexProgram);
|
---|
| 43 | newpass->setFragmentProgram(shadowFragmentProgram);
|
---|
| 44 |
|
---|
| 45 | GpuProgramParameters* Vparams = newpass->getVertexProgramParameters().getPointer();
|
---|
| 46 | Vparams->setNamedAutoConstant("worldViewProj",
|
---|
| 47 | GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
|
---|
| 48 | Vparams->setNamedAutoConstant("world",
|
---|
| 49 | GpuProgramParameters::ACT_WORLD_MATRIX);
|
---|
| 50 | GpuProgramParameters* Fparams = newpass->getFragmentProgramParameters().getPointer();
|
---|
| 51 |
|
---|
| 52 | TextureUnitState* st = newpass->createTextureUnitState();
|
---|
| 53 | st->setTextureFiltering(TFO_BILINEAR);
|
---|
[1055] | 54 | st = newpass->createTextureUnitState();
|
---|
| 55 | st->setTextureFiltering(TFO_BILINEAR);
|
---|
[790] | 56 |
|
---|
| 57 | newpass->setSceneBlending(SBT_MODULATE);
|
---|
[1055] | 58 | newpass->setDepthBias(7);
|
---|
[790] | 59 | techn->movePass(lastpass, index);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | OgreDepthShadowRecieverRenderTechnique::~OgreDepthShadowRecieverRenderTechnique()
|
---|
| 65 | {
|
---|
| 66 |
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | void OgreDepthShadowRecieverRenderTechnique::update(unsigned long frameNum)
|
---|
| 70 | {
|
---|
| 71 | LightList lights;
|
---|
| 72 | Root::getSingleton()._getCurrentSceneManager()->_populateLightList(
|
---|
| 73 | ((OgreSharedRuns*) sharedRuns)->getRootPosition(),
|
---|
| 74 | 100000.0,
|
---|
| 75 | lights);
|
---|
| 76 |
|
---|
| 77 | //fill passes
|
---|
| 78 | for(unsigned int i = 0; i < passes.size(); i++)
|
---|
| 79 | {
|
---|
| 80 | if(lights.size() > i)
|
---|
| 81 | {
|
---|
| 82 | //create run if not exists
|
---|
| 83 | OgreIlluminationManager::getSingleton().createPerLightRun(lights.at(i)->getName(),
|
---|
| 84 | ILLUMRUN_DEPTH_SHADOWMAP);
|
---|
| 85 | //update light depth map
|
---|
| 86 | OgreIlluminationManager::getSingleton().updatePerLightRun(lights.at(i)->getName(),
|
---|
| 87 | ILLUMRUN_DEPTH_SHADOWMAP,
|
---|
| 88 | frameNum);
|
---|
| 89 |
|
---|
| 90 | //set texture to pass
|
---|
| 91 | OgreDepthShadowMapRenderingRun* depthRun =
|
---|
| 92 | (OgreDepthShadowMapRenderingRun*) OgreIlluminationManager::getSingleton()
|
---|
| 93 | .getPerLightRun(lights.at(i)->getName(),
|
---|
| 94 | ILLUMRUN_DEPTH_SHADOWMAP)->asOgreRenderingRun();
|
---|
| 95 |
|
---|
| 96 | passes.at(i)->getTextureUnitState(0)->setTextureName(
|
---|
[1055] | 97 | depthRun->getDepthMapTextureName(0));
|
---|
| 98 | passes.at(i)->getTextureUnitState(1)->setTextureName(
|
---|
| 99 | depthRun->getDepthMapTextureName(1));
|
---|
[790] | 100 |
|
---|
| 101 | //TODO: set matrices
|
---|
| 102 | GpuProgramParametersSharedPtr fpParams = passes.at(i)->getFragmentProgramParameters();
|
---|
[1055] | 103 | fpParams->setNamedConstant("lightView", depthRun->getLightViewMatrix(0));
|
---|
| 104 | fpParams->setNamedConstant("lightViewProj", depthRun->getLightViewProjMatrix(0));
|
---|
| 105 | fpParams->setNamedConstant("lightView2", depthRun->getLightViewMatrix(1));
|
---|
| 106 | fpParams->setNamedConstant("lightViewProj2", depthRun->getLightViewProjMatrix(1));
|
---|
[790] | 107 |
|
---|
| 108 | passes.at(i)->setActive(true);
|
---|
| 109 | }
|
---|
| 110 | else
|
---|
| 111 | passes.at(i)->setActive(false);
|
---|
| 112 |
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[836] | 116 |
|
---|
| 117 | ///Technique parsers
|
---|
| 118 | namespace DepthShadowRecieverParsers
|
---|
| 119 | {
|
---|
| 120 | void parseMaxLights(String& params, RenderTechniqueFactory* factory)
|
---|
| 121 | {
|
---|
| 122 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
| 123 | f->maxlights = StringConverter::parseInt(params);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void parseVertexProgram(String& params, RenderTechniqueFactory* factory)
|
---|
| 127 | {
|
---|
| 128 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
| 129 | f->shadowVertexProgram = params;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | void parseFragmentProgram(String& params, RenderTechniqueFactory* factory)
|
---|
| 133 | {
|
---|
| 134 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
| 135 | f->shadowFragmentProgram = params;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | OgreDepthShadowRecieverRenderTechniqueFactory::OgreDepthShadowRecieverRenderTechniqueFactory()
|
---|
| 140 | {
|
---|
| 141 | typeName = "DepthShadowReciever";
|
---|
| 142 |
|
---|
| 143 | using namespace DepthShadowRecieverParsers;
|
---|
| 144 | //register parsers
|
---|
| 145 | this->attributeParsers.insert(AttribParserList::value_type("max_light_count", (ILLUM_ATTRIBUTE_PARSER) parseMaxLights));
|
---|
| 146 | this->attributeParsers.insert(AttribParserList::value_type("vertex_program_name", (ILLUM_ATTRIBUTE_PARSER) parseVertexProgram));
|
---|
| 147 | this->attributeParsers.insert(AttribParserList::value_type("fragment_program_name", (ILLUM_ATTRIBUTE_PARSER) parseFragmentProgram));
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | OgreRenderTechnique* OgreDepthShadowRecieverRenderTechniqueFactory::createInstance(
|
---|
| 151 | IllumTechniqueParams* params,
|
---|
| 152 | Pass* pass,
|
---|
| 153 | OgreRenderable* parentRenderable,
|
---|
| 154 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
| 155 | {
|
---|
| 156 | //reset parameters
|
---|
| 157 | maxlights = 1;
|
---|
| 158 | shadowVertexProgram = "GameTools/ShadowMap/ShadowVS";
|
---|
| 159 | shadowFragmentProgram = "GameTools/ShadowMap/ShadowPS";
|
---|
| 160 |
|
---|
| 161 | parseParams(params);
|
---|
| 162 |
|
---|
| 163 | OgreDepthShadowRecieverRenderTechnique* result = new OgreDepthShadowRecieverRenderTechnique(
|
---|
| 164 | maxlights,
|
---|
| 165 | shadowVertexProgram,
|
---|
| 166 | shadowFragmentProgram,
|
---|
| 167 | pass,
|
---|
| 168 | parentRenderable,
|
---|
| 169 | parentTechniqueGroup);
|
---|
| 170 |
|
---|
| 171 | return result;
|
---|
| 172 | }
|
---|
| 173 |
|
---|