1 | #include "OgreIlluminationManager.h"
|
---|
2 | #include "OgreParticleSystemRenderer.h"
|
---|
3 | #include "OgreBillboardParticleRenderer.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | OgreIlluminationManager* OgreIlluminationManager::instance = NULL;
|
---|
7 |
|
---|
8 | Vector3 sortfrom;
|
---|
9 | RenderingRunType sortType;
|
---|
10 |
|
---|
11 | class sortFromVector
|
---|
12 | {
|
---|
13 | public:
|
---|
14 | bool operator()(OgreSharedRuns* a, OgreSharedRuns* b)
|
---|
15 | {
|
---|
16 | float dist1 = (a->getRootPosition() - sortfrom).length();
|
---|
17 | float dist2 = (b->getRootPosition() - sortfrom).length();
|
---|
18 |
|
---|
19 | return dist1 > dist2;
|
---|
20 | }
|
---|
21 | };
|
---|
22 |
|
---|
23 | class sortFromVectorWithRunType
|
---|
24 | {
|
---|
25 | public:
|
---|
26 | bool operator()(OgreSharedRuns* a, OgreSharedRuns* b)
|
---|
27 | {
|
---|
28 | float dist1 = (a->getRootPosition(sortType) - sortfrom).length();
|
---|
29 | float dist2 = (b->getRootPosition(sortType) - sortfrom).length();
|
---|
30 |
|
---|
31 | return dist1 > dist2;
|
---|
32 | }
|
---|
33 | };
|
---|
34 |
|
---|
35 |
|
---|
36 | class VisibleFinderVisitor : public QueuedRenderableVisitor
|
---|
37 | {
|
---|
38 | private:
|
---|
39 | std::vector<const Renderable*>* visibleObjects;
|
---|
40 | public:
|
---|
41 |
|
---|
42 | VisibleFinderVisitor(std::vector<const Renderable*>* visibleObjectsVector)
|
---|
43 | {
|
---|
44 | this->visibleObjects = visibleObjectsVector;
|
---|
45 | }
|
---|
46 |
|
---|
47 | void visit(const Renderable* r)
|
---|
48 | {
|
---|
49 | OgreTechniqueGroup* tg = (OgreTechniqueGroup*) r->getRenderTechniqueGroup();
|
---|
50 |
|
---|
51 | if(tg != 0)
|
---|
52 | {
|
---|
53 | tg->validateSharedRuns();
|
---|
54 | visibleObjects->push_back(r);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | bool visit(const Pass* p)
|
---|
58 | {
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 | void visit(const RenderablePass* rp)
|
---|
62 | {
|
---|
63 | Renderable* r = rp->renderable;
|
---|
64 |
|
---|
65 | OgreTechniqueGroup* tg = (OgreTechniqueGroup*) r->getRenderTechniqueGroup();
|
---|
66 |
|
---|
67 | if(tg != 0)
|
---|
68 | {
|
---|
69 | tg->validateSharedRuns();
|
---|
70 | visibleObjects->push_back(r);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | };
|
---|
75 |
|
---|
76 | OgreIlluminationManager::OgreIlluminationManager()
|
---|
77 | {
|
---|
78 | visitor = new VisibleFinderVisitor(&visibleObjects);
|
---|
79 | maxRad = 400;
|
---|
80 | }
|
---|
81 |
|
---|
82 | OgreIlluminationManager::~OgreIlluminationManager()
|
---|
83 | {
|
---|
84 |
|
---|
85 | }
|
---|
86 |
|
---|
87 | OgreIlluminationManager& OgreIlluminationManager::getSingleton()
|
---|
88 | {
|
---|
89 | if(instance == NULL)
|
---|
90 | instance= new OgreIlluminationManager();
|
---|
91 |
|
---|
92 | return *instance;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void OgreIlluminationManager::fillVisibleList( RenderQueue * rq )
|
---|
96 | {
|
---|
97 | visibleObjects.clear();
|
---|
98 |
|
---|
99 | RenderQueue::QueueGroupIterator queueIt = rq->_getQueueGroupIterator();
|
---|
100 |
|
---|
101 | while (queueIt.hasMoreElements())
|
---|
102 | {
|
---|
103 | RenderQueueGroup* pGroup = queueIt.getNext();
|
---|
104 |
|
---|
105 | RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
|
---|
106 |
|
---|
107 | while (groupIt.hasMoreElements())
|
---|
108 | {
|
---|
109 | RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
|
---|
110 | const QueuedRenderableCollection& solids = pPriorityGrp->getSolidsBasic();
|
---|
111 | const QueuedRenderableCollection& transparents = pPriorityGrp->getTransparents();
|
---|
112 |
|
---|
113 | solids.acceptVisitor(visitor, QueuedRenderableCollection::OM_PASS_GROUP);
|
---|
114 | transparents.acceptVisitor(visitor, QueuedRenderableCollection::OM_SORT_ASCENDING);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | rq->clear();
|
---|
119 | }
|
---|
120 |
|
---|
121 | void OgreIlluminationManager::update(unsigned long frameNumber, RenderTarget* rt)
|
---|
122 | {
|
---|
123 |
|
---|
124 | RenderSystem* renderSystem = Root::getSingleton().getRenderSystem();
|
---|
125 | SceneManager* sceneManager = Root::getSingleton()._getCurrentSceneManager();
|
---|
126 |
|
---|
127 | for(unsigned short i = 0; i<rt->getNumViewports();i++)
|
---|
128 | {
|
---|
129 | //find visible objects from the camera
|
---|
130 | RenderQueue* rq = sceneManager->getRenderQueue();
|
---|
131 | rq->clear();
|
---|
132 | RenderQueue::QueueGroupIterator groupIter = rq->_getQueueGroupIterator();
|
---|
133 | while (groupIter.hasMoreElements())
|
---|
134 | {
|
---|
135 | RenderQueueGroup* g = groupIter.getNext();
|
---|
136 | //g->addOrganisationMode(QueuedRenderableCollection::OM_SORT_ASCENDING);
|
---|
137 | g->defaultOrganisationMode();
|
---|
138 | }
|
---|
139 |
|
---|
140 | sceneManager->_findVisibleObjects(rt->getViewport(i)->getCamera(),false);
|
---|
141 | fillVisibleList(rq);
|
---|
142 |
|
---|
143 | //int l = visibleObjects.size(); //debug
|
---|
144 |
|
---|
145 | joinSharedRuns();
|
---|
146 |
|
---|
147 | //int ll = sharedRunRoots.size(); //debug
|
---|
148 |
|
---|
149 | //update precomputings
|
---|
150 | std::vector<const Renderable*>::iterator iter = visibleObjects.begin();
|
---|
151 | const std::vector<const Renderable*>::iterator iend = visibleObjects.end();
|
---|
152 |
|
---|
153 | while(iter != iend)
|
---|
154 | {
|
---|
155 | const Renderable* rend = *iter;
|
---|
156 | OgreTechniqueGroup* techniqueGroup = (OgreTechniqueGroup*) rend->getRenderTechniqueGroup();
|
---|
157 | if(techniqueGroup != 0)
|
---|
158 | {
|
---|
159 | techniqueGroup->update(frameNumber);
|
---|
160 | }
|
---|
161 | iter++;
|
---|
162 | }
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|
166 | Root::getSingleton().renderOneFrame();
|
---|
167 | //Root::getSingleton()._updateAllRenderTargets();
|
---|
168 |
|
---|
169 |
|
---|
170 | }
|
---|
171 |
|
---|
172 | void OgreIlluminationManager::sharedRunSplit(SharedRuns* old, SharedRuns* new1, SharedRuns* new2)
|
---|
173 | {
|
---|
174 | sharedRunRoots.remove(old);
|
---|
175 | sharedRunRoots.push_back(new1);
|
---|
176 | sharedRunRoots.push_back(new2);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void OgreIlluminationManager::sharedRunJoin(SharedRuns* old1, SharedRuns* old2, SharedRuns* newsr)
|
---|
180 | {
|
---|
181 | sharedRunRoots.remove(old1);
|
---|
182 | sharedRunRoots.remove(old2);
|
---|
183 | sharedRunRoots.push_back(newsr);
|
---|
184 | }
|
---|
185 |
|
---|
186 | void OgreIlluminationManager::joinSharedRuns()
|
---|
187 | {
|
---|
188 | std::list<SharedRuns*>::iterator it1 = sharedRunRoots.begin();
|
---|
189 | std::list<SharedRuns*>::iterator itend = sharedRunRoots.end();
|
---|
190 |
|
---|
191 | bool again = false;
|
---|
192 |
|
---|
193 | while(it1 != itend)
|
---|
194 | {
|
---|
195 | std::list<SharedRuns*>::iterator it2 = sharedRunRoots.begin();
|
---|
196 |
|
---|
197 | while(it2 != itend)
|
---|
198 | {
|
---|
199 | if( *it1 != *it2 && OgreSharedRuns::canJoin(*it1, *it2))
|
---|
200 | {
|
---|
201 | SharedRuns* newruns = (*it1)->joinRuns(*it2);
|
---|
202 | sharedRunJoin(*it1, *it2, newruns);
|
---|
203 | again = true;
|
---|
204 | break;
|
---|
205 | }
|
---|
206 | it2++;
|
---|
207 | }
|
---|
208 |
|
---|
209 | if(again)
|
---|
210 | break;
|
---|
211 |
|
---|
212 | it1++;
|
---|
213 | }
|
---|
214 | if(again)
|
---|
215 | joinSharedRuns();
|
---|
216 |
|
---|
217 | }
|
---|
218 |
|
---|
219 | void OgreIlluminationManager::addSharedRuns(SharedRuns* runs)
|
---|
220 | {
|
---|
221 | sharedRunRoots.push_back(runs);
|
---|
222 | }
|
---|
223 |
|
---|
224 | void OgreIlluminationManager::getNearestCausticCasters(Vector3 position, std::vector<OgreSharedRuns*>* nearestcasters, unsigned int maxCount)
|
---|
225 | {
|
---|
226 | sortfrom = position;
|
---|
227 | std::vector<OgreSharedRuns*> allcasters;
|
---|
228 | //fill casters
|
---|
229 | std::list<SharedRuns*>::iterator it = sharedRunRoots.begin();
|
---|
230 | std::list<SharedRuns*>::iterator itend = sharedRunRoots.end();
|
---|
231 | while(it != itend)
|
---|
232 | {
|
---|
233 | OgreSharedRuns* sr = (OgreSharedRuns*) (*it);
|
---|
234 |
|
---|
235 | sr->findSharedRootsForType(ILLUMRUN_CAUSTIC_CUBEMAP, allcasters);
|
---|
236 |
|
---|
237 | it++;
|
---|
238 | }
|
---|
239 |
|
---|
240 | //sort
|
---|
241 | std::stable_sort(allcasters.begin(), allcasters.end(), sortFromVector());
|
---|
242 |
|
---|
243 | for(unsigned int i = 0; i < maxCount && i < allcasters.size(); i++)
|
---|
244 | {
|
---|
245 | nearestcasters->push_back(allcasters.at(i));
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | void OgreIlluminationManager::createGlobalRun(RenderingRunType runType)
|
---|
250 | {
|
---|
251 | switch(runType)
|
---|
252 | {
|
---|
253 | case ILLUMRUN_SCENE_CAMERA_DEPTH:
|
---|
254 | if(globalSharedRuns.getRun(ILLUMRUN_SCENE_CAMERA_DEPTH) == 0)
|
---|
255 | {
|
---|
256 | OgreSceneCameraDepthRenderingRun* run = new OgreSceneCameraDepthRenderingRun
|
---|
257 | (&globalSharedRuns, "ILLUMMODULE_SCENE_CAMERA_DEPTH", mainViewport);
|
---|
258 | globalSharedRuns.addRun(ILLUMRUN_SCENE_CAMERA_DEPTH, run);
|
---|
259 | }
|
---|
260 | break;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | RenderingRun* OgreIlluminationManager::getGlobalRun(RenderingRunType runType)
|
---|
265 | {
|
---|
266 | return globalSharedRuns.getRun(runType);
|
---|
267 | }
|
---|
268 |
|
---|
269 | void OgreIlluminationManager::updateGlobalRun(RenderingRunType runType, unsigned long frameNum)
|
---|
270 | {
|
---|
271 | globalSharedRuns.updateRun(runType, frameNum);
|
---|
272 | }
|
---|
273 |
|
---|
274 | void OgreIlluminationManager::createPerLightRun(String lightName, RenderingRunType runType)
|
---|
275 | {
|
---|
276 | OgreSharedRuns* runs = 0;
|
---|
277 |
|
---|
278 | if(perLightRuns.find(lightName) == perLightRuns.end())
|
---|
279 | {///create sharedruns
|
---|
280 | OgreSharedRuns* newruns = new OgreSharedRuns();
|
---|
281 | perLightRuns[lightName] = newruns;
|
---|
282 | }
|
---|
283 |
|
---|
284 | runs = perLightRuns[lightName];
|
---|
285 |
|
---|
286 | switch(runType)
|
---|
287 | {
|
---|
288 | case ILLUMRUN_DEPTH_SHADOWMAP:
|
---|
289 | if(runs->getRun(ILLUMRUN_DEPTH_SHADOWMAP) == 0)
|
---|
290 | {
|
---|
291 | SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
|
---|
292 | OgreDepthShadowMapRenderingRun* run = new OgreDepthShadowMapRenderingRun(
|
---|
293 | runs,
|
---|
294 | lightName + "DEPTH_SHADOW_MAP",
|
---|
295 | sm->getLight(lightName),
|
---|
296 | 2048, //TODO
|
---|
297 | 2048, //TODO
|
---|
298 | "GameTools/ShadowMapDepth" //TODO
|
---|
299 | );
|
---|
300 | runs->addRun(ILLUMRUN_DEPTH_SHADOWMAP, run);
|
---|
301 |
|
---|
302 | }
|
---|
303 | break;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | RenderingRun* OgreIlluminationManager::getPerLightRun(String lightName, RenderingRunType runType)
|
---|
308 | {
|
---|
309 | return perLightRuns[lightName]->getRun(runType);
|
---|
310 | }
|
---|
311 |
|
---|
312 | void OgreIlluminationManager::updatePerLightRun(String lightName, RenderingRunType runType, unsigned long frameNum)
|
---|
313 | {
|
---|
314 | perLightRuns[lightName]->updateRun(runType, frameNum);
|
---|
315 | }
|
---|
316 |
|
---|