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

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