source: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreIlluminationManager.h @ 2379

Revision 2379, 19.0 KB checked in by szirmay, 17 years ago (diff)
Line 
1#pragma once
2
3#include "Ogre.h"
4#include "OgreTechniqueGroup.h"
5#include "OgreRenderable.h"
6
7
8using namespace Ogre;
9
10/**
11 @brief Structure to store path map cluster information for a subentity.
12*/
13struct PathMapClusters
14{
15        /**
16                @brief the number of clusters this subentity belongs to
17        */
18        unsigned int count;
19        /**
20                @brief the indices of the cluster this subentity belongs to.
21        */
22        unsigned int* clusters;
23        /**
24                @brief the name of the path map file this subentity uses
25        */
26        String pathMapTextureFilename;
27        /**
28                @brief the resolution of the path map file.
29        */
30        unsigned int pathMapResolution;
31};
32
33/**
34        @brief Structure of a path map entry point.
35*/
36struct PathMapEntryPoint
37{
38        /**
39                @brief the position of the entry point.
40        */
41    Vector3 position;
42        /**
43                @brief the normal of the entry point.
44        */
45        Vector3 normal;
46        /**
47                @brief the probability of the entry point.
48        */
49        float prob;
50};
51
52/**
53        @brief Implementation of IlluminationManager in an OGRE environment.
54*/
55class OgreIlluminationManager: public FrameListener
56{
57protected:
58        /**
59                @brief Protected constructor (OgreIlluminationManager is a singleton).
60        */
61        OgreIlluminationManager();
62        /**
63                @brief Protected destructor.
64        */
65        virtual ~OgreIlluminationManager();
66       
67        /**
68                @brief Searches for visible renderables with valid TechniqueGroups in a renderqueue.
69
70                @param rq pointer to the RenderQueue instance to search in
71        */
72        void fillVisibleList(  RenderQueue * rq);
73        /**
74                @brief creates a specific type of RenderTechnique for a Renderable's pass.
75
76                It searches all registered RenderTechniqueFactories.
77        */
78        void createTechnique(IllumTechniqueParams* params, Pass* pass, OgreRenderable* rend, OgreSharedRuns* sRuns);
79        /**
80                @brief A helper function to find the renderable object attached to a particle system (ONLY BILLBOARDSETS ARE SUPPORTED).
81
82                @param system pointer to the ParticleSystem instance to search in
83                @return pointer the connected BillboardSet instance
84        */
85        BillboardSet* findRenderableInParticleSystem(ParticleSystem* system);
86        /**
87                @brief Fires preAllUpdates for registered UpdateListeners.
88
89                This is called in each frame before updating the RenderTechniques.
90        */
91        void preAllUpdates();
92        /**
93                @brief Fires postAllUpdates for registered UpdateListeners.
94
95                This is called in each frame after updating the RenderTechniques.
96        */
97        void postAllUpdates();
98
99        /**
100                @brief registered RenderTechniqueFactories
101        */
102        std::list<RenderTechniqueFactory*> techniqueFactories;
103        /**
104                @brief The maximum bounding sphere radius that groupped objects ( see SharedRuns class ) can have
105                @see canJoin
106                @see joinRuns
107        */
108        float maxRad;
109        /**
110                @brief Size of the focusing map.
111
112                This map is used if the shadow maps should be focused.
113        */
114        unsigned int focusingMapSize;
115        /**
116                @brief Size of the shadow maps.
117        */
118        unsigned int shadowMapSizePoint;
119        unsigned int shadowMapSizeSpot;
120        unsigned int shadowMapSizeDirectional;
121        /**
122                @brief Size of area lights for soft shadows.
123        */
124        float areaLightRadius;
125        /**
126                @brief Sets if light space perspective shadow mapping should be used.
127        */
128        bool useLISPSMPoint;
129        bool useLISPSMSpot;
130        bool useLISPSMDirectional;
131        /**
132                @brief Sets if the shadow maps should be blurred.
133
134                Used in variance shadow mapping.
135        */
136        bool blurSMPoint;
137        bool blurSMSpot;
138        bool blurSMDirectional;
139        /**
140                @brief Sets if shadow maps should be focused.
141        */
142        bool focusingSMPoint;
143        bool focusingSMSpot;
144        bool focusingSMDirectional;
145        /**
146                @brief The material name that should be used during rendering the shadow maps.
147
148                There are several predefined materials that can be used to render shadow maps:
149                 - GTP/Basic/Depth : writes projected depth values of front facing polygons
150                 - GTP/Basic/DepthCCW : writes projected depth values of back facing polygons
151                 - GTP/Basic/Distance : writes distance values (from eyepoint) of front facing polygons
152                 - GTP/Basic/DistanceCCW : writes distance values (from eyepoint) of back facing polygons
153                 - GTP/Basic/Distance_Normalized : writes normalized distance values
154                        (distance values devided by projection farplane - which is set to the attenuation range in case of shadow maps) of front facing polygons
155                 - GTP/Basic/Distance_NormalizedCCW : writes normalized distance values of back facing polygons
156
157                 The default material is GTP/Basic/Distance_NormalizedCCW.
158                 Recommended materials for different light types:
159                 - spot and point lights : GTP/Basic/Distance_NormalizedCCW or GTP/Basic/Distance_Normalized
160                 - directional lights : GTP/Basic/Depth or GTP/Basic/DepthCCW
161        */
162        String shadowMapMaterialNamePoint;
163        String shadowMapMaterialNameSpot;
164        String shadowMapMaterialNameDirectional;
165        /**
166                @brief Size of the phase texture.
167        */
168        unsigned int phaseTextureSize;
169        /**
170                @brief Stores maximum bounding radius values for each rendering run type.
171        */
172        std::map<RenderingRunType,float> maxRads;
173        /**
174                @brief Stores PathMapClusters structures for each subentity.
175
176                The String key is the name of the subentity.
177        */
178        std::map<String, PathMapClusters> pathMapClusters;
179        /**
180                @brief PathMapEntryPoint list.
181        */
182        std::vector<PathMapEntryPoint> pathMapEntryPoints;
183        /**
184                @brief Stores cluster size for each path map cluster.
185        */
186        std::vector<unsigned int> pathMapClusterLengths;
187        /**
188                @brief The camera attached to the player.
189        */
190        Camera* mainCamera;
191        /**
192                @brief The viewport of the player camera.
193        */
194        Viewport* mainViewport;
195        /**
196                @brief VisibleFinderVisitor instance.
197               
198                Used for adding visible renderables with valid TechniqueGroups to the visibleObjects vector.
199        */
200        class VisibleFinderVisitor* visitor;
201        /**
202                @brief The one and only OgreIlluminationManager instance.
203        */
204        static OgreIlluminationManager* instance;
205        /**
206                @brief Vector containing visible renderables with valid TechniqueGroups that must be refreshed.
207        */
208        std::vector<const Renderable*> visibleObjects;
209        /**
210                @brief List containing SharedRuns roots.
211
212                It is the IlluminationManager's task to find the SharedRuns which can be joined.
213                Only the root SharedRuns needs to be checked.
214        */
215        std::list<SharedRuns*> sharedRunRoots;
216        /**
217                @brief Group of RenderingRuns that are used globaly.
218
219                Some RenderingRuns have only one instance per application (for example scene depth map).
220                These resources are shared between all RenderTechniques.
221        */
222        OgreSharedRuns globalSharedRuns;
223        /**
224                @brief Stores groups of RenderingRuns that are attached to individual light sources.
225
226                These resources need separate instances for each lightsource ( for example depth shadow maps).
227                They are grouped by the name of the lightsource.
228        */
229        std::map<String, OgreSharedRuns*> perLightRuns;
230        /**
231                @brief
232        */
233        std::map<GlobalTargetType, GlobalUseRenderTarget*> globalTargets;
234        /**
235                @brief Stores registered UpdateListeners.
236        */
237        std::vector<UpdateListener*> updateListeners;
238
239        bool joinRuns;
240
241public:
242
243        void joinSharedRuns(bool join){joinRuns = join;}
244        /**
245                @brief Registers an UpdateListener instance. @see UpdateListener
246        */
247        void addUpdateListener(UpdateListener* l){updateListeners.push_back(l);}
248        /**
249                @brief registers a RenderTechniqueFactory
250        */
251        void addRenderTechniqueFactory(RenderTechniqueFactory* factory)
252        {
253                techniqueFactories.push_back(factory);
254        }
255        /**
256                @brief retirieves the maximum bounding sphere radius with two SharedRuns can be joined.
257
258                Only valid fi all run types use the same radius. This can be set with calling setMaxJoinRadius().
259                @see setMaxJoinRadius
260        */
261        float getMaxJoinRadius(){return maxRad;}
262        /**
263                @brief Retirieves the maximum shared bounding sphere radius for a given run type.
264        */
265        float getMaxJoinRadius(RenderingRunType type){return maxRads[type];}
266        /**
267                @brief sets the maximum bounding sphere radius with two SharedRuns can be joined for all run type.
268        */
269        void setMaxJoinRadius(float rad)
270        {
271                std::map<RenderingRunType,float> ::iterator it = maxRads.begin();
272                std::map<RenderingRunType,float> ::iterator itend = maxRads.end();
273               
274                maxRad = rad;
275       
276                while(it != itend)
277                {
278                        (*it).second = maxRad;
279                        it++;
280                }
281               
282        }
283        /**
284                @brief Sets the maximum shared bounding sphere radius for a given run type.
285        */
286        void setMaxJoinRadius(RenderingRunType type, float rad){maxRads[type] = rad;}
287        /**
288                @see focusingMapSize
289        */
290        void setFocusingMapSize(unsigned int size){focusingMapSize = size;}
291        /**
292                @see phaseTextureSize
293        */
294        void setPhaseTextureSize(unsigned int size){phaseTextureSize = size;}
295        /**
296                @see shadowMapSize
297        */
298        void setShadowMapSize(unsigned int size)
299        {
300                shadowMapSizeDirectional = size;
301                shadowMapSizePoint = size;
302                shadowMapSizeSpot = size;
303        }
304        void setShadowMapSize(Light::LightTypes type, unsigned int size)
305        {
306                switch(type)
307                {
308                        case Light::LT_DIRECTIONAL:
309                                shadowMapSizeDirectional = size;break;
310                        case Light::LT_POINT:
311                                shadowMapSizePoint = size;break;
312                        case Light::LT_SPOTLIGHT:
313                                shadowMapSizeSpot = size;break;
314                }
315        }
316        /**
317                @brief Returns the one and only OgreIlluminationManager instance.
318        */
319        static OgreIlluminationManager& getSingleton();
320        /**
321                @brief The function to be called to render one frame.
322
323                This is the main refreshing function. It seasrches for visible objects, manages shared runs, updates render techniques and
324                finally renders the scene to framebuffer.
325
326                @param frameNumber current framenumber
327                @param rt the rendertarget window. Needed to find the viewports that need to be refresh.
328        */
329        void update(unsigned long frameNumber, RenderTarget* rt);
330        /**
331                @brief searches for RenderTechniques in materials and creates them for all objects.
332        */
333        void initTechniques();
334        /**
335                @brief searches for RenderTechniques in materials and creates them for an Entity.
336        */
337        void initTechniques(Entity* e);
338        /**
339                @brief searches for RenderTechniques in materials and creates them for a Billboardset.
340        */
341        void initTechniques(BillboardSet* bbs, ParticleSystem* sys);
342        /**
343                @brief Returns a pointer to the player camera.
344
345                @return pointer to the main player camera. Needed by RenderTechnique and RenderingRun classes.
346        */
347        Camera* getMainCamera(){return mainCamera;}
348        /**
349                @brief Returns a pointer to the viewport attached to the player camera.
350
351                @return pointer to the viewport. Needed by RenderTechnique and RenderingRun classes.
352        */
353        Viewport* getMainViewport(){return mainViewport;}
354        /**
355                @brief Sets the player camera.
356
357                @param camera pointer to the main player camera
358        */
359        void setMainCamera(Camera* camera){mainCamera = camera;}
360        /**
361                @brief Sets the viewport attached to the player camera.
362
363                @param viewport pointer to the viewport
364        */
365        void setMainViewport(Viewport* viewport){mainViewport = viewport;}
366        /**
367                @brief The function to be called when a shared run is splitted.
368
369                @param old pointer to the SharedRuns instance that is split
370                @param new1 pointer to one of the SharedRuns instance that remain after split
371                @param new2 pointer to the other SharedRuns instance that remain after split
372        */
373        void sharedRunSplit(SharedRuns* old, SharedRuns* new1, SharedRuns* new2);
374        /**
375                @brief The function to be called when two shared runs are joined.
376
377                @param old1 pointer to one of the SharedRuns instance that are joined
378                @param old2 pointer to the other SharedRuns instance that are joined
379                @param newsr pointer to the resulting parent SharedRuns instance
380        */
381        void sharedRunJoin(SharedRuns* old1, SharedRuns* old2, SharedRuns* newsr);
382        /**
383                @brief Joins shared runs if needed.
384
385                Searches the registered shared run roots and join them if necessary (they are close enough).
386        */
387        void joinSharedRuns();
388        /**
389                @brief Register a shared run object.
390               
391                Only called when new techniques are created.
392
393                @param runs pointer to the SharedRuns instance to add
394        */
395        void addSharedRuns(SharedRuns* runs);
396        /**
397                @brief Searches for the nearest object groups (SharedRuns) that are caustic casters from a given point.
398
399                @param position the point to obtain distances from
400                @param nearestcasters vector to put the nearest caustic caster SharedRuns to
401                @param maxCount the maximum number of nearest casters to search for
402        */
403        void getNearestCausticCasters(Vector3 position, std::vector<OgreSharedRuns*>* nearestcasters, unsigned int maxCount);
404        /**
405                @brief Creates a global RenderingRun of the given type.
406
407                If  a RenderingRun with the given type already exist there is nothing to do.
408
409                @param runType type enum of the RenderingRun to create
410        */
411        void createGlobalRun(RenderingRunType runType);
412        /**
413                @brief Returns the global RendderingRun with the given type
414
415                @param runType type enum of the RenderingRun to retrieve
416
417                @return pointer to the RenderingRun, NULL if no RenderingRun with the given type exists
418        */
419        RenderingRun* getGlobalRun(RenderingRunType runType);
420
421        // These GlobalUseRenderTargets are only used in fire render technique. Maybe it could be solved with global rendering runs too.
422        GlobalUseRenderTarget* getGlobalTarget(GlobalTargetType type);
423        void addGlobalTarget(GlobalTargetType type, GlobalUseRenderTarget* target);
424
425        /**
426                @brief Updates a global RenderingRun with the given type.
427
428                @param runType type enum of the RenderingRun to update
429                @param frameNum current framenumber
430        */
431        void updateGlobalRun(RenderingRunType runType, unsigned long frameNum);
432        /**
433                @brief Creates a RenderingRun attached to a lightsource with the given type.
434
435                @param lightName name of the lightsource
436                @param runType type enum of the RenderingRun to create
437        */
438        void createPerLightRun(String lightName, RenderingRunType runType);
439        /**
440                @brief Retuns a RenderingRun attached to a lightsource with the given type.
441
442                @param lightName name of the lightsource
443                @param runType type enum of the RenderingRun to return
444
445                @return pointer to the RenderingRun, NULL if no RenderingRun with the given type exists
446        */
447        RenderingRun* getPerLightRun(String lightName, RenderingRunType runType);
448        /**
449                @brief Updates a RenderingRun attached to a lightsource with the given type.
450
451                @param lightName name of the lightsource
452                @param runType type enum of the RenderingRun to update
453                @param frameNum current framenumber
454        */
455        void updatePerLightRun(String lightName, RenderingRunType runType, unsigned long frameNum);
456        /**
457                @brief Saves the phase texture to the given file.
458        */
459        void savePhaseTextureToFile(String filename);
460        /**
461                @brief Frame listener event handler function.
462               
463                Inherited from FrameListener. Called at the beginning of each frame.
464        */
465        bool frameStarted(const FrameEvent& evt)
466    {
467                update(Root::getSingleton().getCurrentFrameNumber(), mainViewport->getTarget());
468                return FrameListener::frameStarted(evt);
469        }       
470        /**
471                @see useLISPSM
472        */
473        bool getUseLISPSM(Light::LightTypes type)
474        {
475                switch(type)
476                {
477                        case Light::LT_DIRECTIONAL:
478                        return useLISPSMDirectional;
479                        case Light::LT_POINT:
480                        return useLISPSMPoint;
481                        case Light::LT_SPOTLIGHT:
482                        return useLISPSMSpot;
483                }
484        }
485        /**
486                @see focusingSM
487        */
488        bool getFocusingShadowMap(Light::LightTypes type)
489        {
490                switch(type)
491                {
492                        case Light::LT_DIRECTIONAL:
493                        return focusingSMDirectional;
494                        case Light::LT_POINT:
495                        return focusingSMPoint;
496                        case Light::LT_SPOTLIGHT:
497                        return focusingSMSpot;
498                }
499        }
500        /**
501                @see blurSM
502        */
503        bool getBlurShadowMap(Light::LightTypes type)
504        {
505                switch(type)
506                {
507                        case Light::LT_DIRECTIONAL:
508                        return blurSMDirectional;
509                        case Light::LT_POINT:
510                        return blurSMPoint;
511                        case Light::LT_SPOTLIGHT:
512                        return blurSMSpot;
513                }
514        }
515        /**
516                @see useLISPSM
517        */
518        void setUseLISPSM(bool use)
519        {
520                useLISPSMDirectional = use;
521                useLISPSMPoint = use;
522                useLISPSMSpot = use;
523        }
524        void setUseLISPSM(Light::LightTypes type, bool use)
525        {
526                switch(type)
527                {
528                        case Light::LT_DIRECTIONAL:
529                                useLISPSMDirectional = use;break;
530                        case Light::LT_POINT:
531                                useLISPSMPoint = use;break;
532                        case Light::LT_SPOTLIGHT:
533                                useLISPSMSpot = use;break;
534                }
535        }
536        /**
537                @see focusingSM
538        */
539        void setFocusingSM(bool use)
540        {
541                focusingSMDirectional = use;
542                focusingSMPoint = use;
543                focusingSMSpot = use;
544        }
545        void setFocusingSM(Light::LightTypes type, bool use)
546        {
547                switch(type)
548                {
549                        case Light::LT_DIRECTIONAL:
550                                focusingSMDirectional = use;break;
551                        case Light::LT_POINT:
552                                focusingSMPoint = use;break;
553                        case Light::LT_SPOTLIGHT:
554                                focusingSMSpot = use;break;
555                }
556        }
557        /**
558                @see blurSM
559        */
560        void setBlurShadowMap(bool use)
561        {
562                blurSMDirectional = use;
563                blurSMPoint = use;
564                blurSMSpot = use;
565        }
566        void setBlurShadowMap(Light::LightTypes type, bool use)
567        {
568                switch(type)
569                {
570                        case Light::LT_DIRECTIONAL:
571                                blurSMDirectional = use;break;
572                        case Light::LT_POINT:
573                                blurSMPoint = use;break;
574                        case Light::LT_SPOTLIGHT:
575                                blurSMSpot = use;break;
576                }
577        }
578        /**
579                @see shadowMapMaterialName
580        */
581        void setShadowMapMaterialName(String name)
582        {
583                shadowMapMaterialNameDirectional = name;
584                shadowMapMaterialNamePoint = name;
585                shadowMapMaterialNameSpot = name;
586        }
587        void setShadowMapMaterialName(Light::LightTypes type, String name)
588        {
589                switch(type)
590                {
591                        case Light::LT_DIRECTIONAL:
592                                shadowMapMaterialNameDirectional = name;break;
593                        case Light::LT_POINT:
594                                shadowMapMaterialNamePoint = name;break;
595                        case Light::LT_SPOTLIGHT:
596                                shadowMapMaterialNameSpot = name;break;
597                }
598        }
599        /**
600                @brief Registers a PathMapClusters structure for a given subentity.
601
602                @param subEntityName name of te subentity
603                @param clusters the PathMapClusters that belongs to the given subentity
604        */
605        void addPathMapClusters(String subEntityName, PathMapClusters clusters)
606        {
607                this->pathMapClusters[subEntityName] = clusters;
608        }
609        /**
610                @brief Returns the PathMapClusters structure registered for a given subentity.
611
612                @param subEntityName name of te subentity
613                @return pointer to the PathMapClusters structure that belongs to the given subentity
614        */
615        PathMapClusters* getPathMapClusters(String subEntityName)
616        {
617                return &pathMapClusters[subEntityName];
618        }
619        /**
620                @brief Adds a new PathMapEntryPoint cluster to the entrypoint list.
621        */
622        void addPathMapEntryPoint(PathMapEntryPoint p)
623        {
624                this->pathMapEntryPoints.push_back(p);
625        }
626        /**
627                @brief Returns the list of entrypoints.
628        */
629        std::vector<PathMapEntryPoint>& getPathMapEntryPoints()
630        {
631                return pathMapEntryPoints;
632        }
633        /**
634                @brief Adds a new cluster size.
635        */
636        void addPathMapClusterLength(unsigned int l)
637        {
638                this->pathMapClusterLengths.push_back(l);
639        }
640        /**
641                @brief Gets the number of clusters.
642        */
643        unsigned int getPathMapClusterLengthsSize()
644        {
645                return this->pathMapClusterLengths.size();
646        }
647        /**
648                @brief Gets the size of the given cluster.
649
650                @param index of the cluster
651                @return the size of the cluster
652        */
653        unsigned int getPathMapClusterLength(unsigned int index)
654        {
655                return pathMapClusterLengths.at(index);
656        }
657        /**
658                @see areaLightRadius
659        */
660        float getAreaLightRadius(){return areaLightRadius;}
661        /**
662                @see areaLightRadius
663        */
664        void setAreaLigtRadius(float radius){areaLightRadius = radius;}
665        /**
666                @brief Sets the fire rendertarget - frame buffer resolution ratio
667        */
668        inline void setFireRenderTargetSize(int size);
669
670        void freeAllResources();
671};
672
Note: See TracBrowser for help on using the repository browser.