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

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