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_ANISOTROPIC);
|
---|
54 |
|
---|
55 | newpass->setSceneBlending(SBT_MODULATE);
|
---|
56 | //newpass->setSceneBlending(SBF_ONE, SBF_ZERO);
|
---|
57 | newpass->setDepthBias(1);
|
---|
58 | techn->movePass(lastpass, index);
|
---|
59 | }
|
---|
60 |
|
---|
61 | }
|
---|
62 |
|
---|
63 | OgreDepthShadowRecieverRenderTechnique::~OgreDepthShadowRecieverRenderTechnique()
|
---|
64 | {
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | void OgreDepthShadowRecieverRenderTechnique::update(unsigned long frameNum)
|
---|
69 | {
|
---|
70 | LightList lights;
|
---|
71 | Root::getSingleton()._getCurrentSceneManager()->_populateLightList(
|
---|
72 | ((OgreSharedRuns*) sharedRuns)->getRootPosition(),
|
---|
73 | 100000.0,
|
---|
74 | lights);
|
---|
75 |
|
---|
76 | //fill passes
|
---|
77 | for(unsigned int i = 0; i < passes.size(); i++)
|
---|
78 | {
|
---|
79 | if(lights.size() > i)
|
---|
80 | {
|
---|
81 | //create run if not exists
|
---|
82 | OgreIlluminationManager::getSingleton().createPerLightRun(lights.at(i)->getName(),
|
---|
83 | ILLUMRUN_DEPTH_SHADOWMAP);
|
---|
84 | //update light depth map
|
---|
85 | OgreIlluminationManager::getSingleton().updatePerLightRun(lights.at(i)->getName(),
|
---|
86 | ILLUMRUN_DEPTH_SHADOWMAP,
|
---|
87 | frameNum);
|
---|
88 |
|
---|
89 | //set texture to pass
|
---|
90 | OgreDepthShadowMapRenderingRun* depthRun =
|
---|
91 | (OgreDepthShadowMapRenderingRun*) OgreIlluminationManager::getSingleton()
|
---|
92 | .getPerLightRun(lights.at(i)->getName(),
|
---|
93 | ILLUMRUN_DEPTH_SHADOWMAP)->asOgreRenderingRun();
|
---|
94 |
|
---|
95 | passes.at(i)->getTextureUnitState(0)->setTextureName(
|
---|
96 | depthRun->getDepthMapTextureName());
|
---|
97 |
|
---|
98 | //TODO: set matrices
|
---|
99 | GpuProgramParametersSharedPtr fpParams = passes.at(i)->getFragmentProgramParameters();
|
---|
100 | GpuProgramParametersSharedPtr vpParams = passes.at(i)->getVertexProgramParameters();
|
---|
101 |
|
---|
102 | //fpParams->setNamedConstant("lightViewProj", depthRun->getLightViewProjMatrix());
|
---|
103 | vpParams->setNamedConstant("lightViewProj", depthRun->getLightViewProjMatrix());
|
---|
104 |
|
---|
105 | passes.at(i)->setActive(true);
|
---|
106 | }
|
---|
107 | else
|
---|
108 | passes.at(i)->setActive(false);
|
---|
109 |
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | ///Technique parsers
|
---|
115 | namespace DepthShadowRecieverParsers
|
---|
116 | {
|
---|
117 | void parseMaxLights(String& params, RenderTechniqueFactory* factory)
|
---|
118 | {
|
---|
119 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
120 | f->maxlights = StringConverter::parseInt(params);
|
---|
121 | }
|
---|
122 |
|
---|
123 | void parseVertexProgram(String& params, RenderTechniqueFactory* factory)
|
---|
124 | {
|
---|
125 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
126 | f->shadowVertexProgram = params;
|
---|
127 | }
|
---|
128 |
|
---|
129 | void parseFragmentProgram(String& params, RenderTechniqueFactory* factory)
|
---|
130 | {
|
---|
131 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
132 | f->shadowFragmentProgram = params;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | OgreDepthShadowRecieverRenderTechniqueFactory::OgreDepthShadowRecieverRenderTechniqueFactory()
|
---|
137 | {
|
---|
138 | typeName = "DepthShadowReciever";
|
---|
139 |
|
---|
140 | using namespace DepthShadowRecieverParsers;
|
---|
141 | //register parsers
|
---|
142 | this->attributeParsers.insert(AttribParserList::value_type("max_light_count", (ILLUM_ATTRIBUTE_PARSER) parseMaxLights));
|
---|
143 | this->attributeParsers.insert(AttribParserList::value_type("vertex_program_name", (ILLUM_ATTRIBUTE_PARSER) parseVertexProgram));
|
---|
144 | this->attributeParsers.insert(AttribParserList::value_type("fragment_program_name", (ILLUM_ATTRIBUTE_PARSER) parseFragmentProgram));
|
---|
145 | }
|
---|
146 |
|
---|
147 | OgreRenderTechnique* OgreDepthShadowRecieverRenderTechniqueFactory::createInstance(
|
---|
148 | IllumTechniqueParams* params,
|
---|
149 | Pass* pass,
|
---|
150 | OgreRenderable* parentRenderable,
|
---|
151 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
152 | {
|
---|
153 | //reset parameters
|
---|
154 | maxlights = 1;
|
---|
155 | shadowVertexProgram = "GameTools/ShadowMap/ShadowVS";
|
---|
156 | shadowFragmentProgram = "GameTools/ShadowMap/ShadowPS";
|
---|
157 |
|
---|
158 | parseParams(params);
|
---|
159 |
|
---|
160 | OgreDepthShadowRecieverRenderTechnique* result = new OgreDepthShadowRecieverRenderTechnique(
|
---|
161 | maxlights,
|
---|
162 | shadowVertexProgram,
|
---|
163 | shadowFragmentProgram,
|
---|
164 | pass,
|
---|
165 | parentRenderable,
|
---|
166 | parentTechniqueGroup);
|
---|
167 |
|
---|
168 | return result;
|
---|
169 | }
|
---|
170 |
|
---|