OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
lightmanager.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_COMPONENTS_SCENEUTIL_LIGHTMANAGER_H
2 #define OPENMW_COMPONENTS_SCENEUTIL_LIGHTMANAGER_H
3 
4 #include <set>
5 
6 #include <osg/Light>
7 
8 #include <osg/Group>
9 #include <osg/NodeVisitor>
10 #include <osg/observer_ptr>
11 
12 namespace SceneUtil
13 {
14 
23  class LightSource : public osg::Node
24  {
25  // double buffered osg::Light's, since one of them may be in use by the draw thread at any given time
26  osg::ref_ptr<osg::Light> mLight[2];
27 
28  // LightSource will affect objects within this radius
29  float mRadius;
30 
31  int mId;
32 
33  public:
34 
36 
37  LightSource();
38 
39  LightSource(const LightSource& copy, const osg::CopyOp& copyop);
40 
41  float getRadius() const
42  {
43  return mRadius;
44  }
45 
47  void setRadius(float radius)
48  {
49  mRadius = radius;
50  }
51 
55  osg::Light* getLight(unsigned int frame)
56  {
57  return mLight[frame % 2];
58  }
59 
64  void setLight(osg::Light* light)
65  {
66  mLight[0] = light;
67  mLight[1] = osg::clone(light);
68  }
69 
71  int getId() const
72  {
73  return mId;
74  }
75  };
76 
78  class LightManager : public osg::Group
79  {
80  public:
81 
83 
84  LightManager();
85 
86  LightManager(const LightManager& copy, const osg::CopyOp& copyop);
87 
92  void setLightingMask (unsigned int mask);
93 
94  unsigned int getLightingMask() const;
95 
97  void setStartLight(int start);
98 
99  int getStartLight() const;
100 
102  void update();
103 
105  void addLight(LightSource* lightSource, const osg::Matrixf& worldMat, unsigned int frameNum);
106 
108  {
110  osg::Matrixf mWorldMatrix;
111  };
112 
113  const std::vector<LightSourceTransform>& getLights() const;
114 
116  {
118  osg::BoundingSphere mViewBound;
119  };
120 
121  const std::vector<LightSourceViewBound>& getLightsInViewSpace(osg::Camera* camera, const osg::RefMatrix* viewMatrix);
122 
123  typedef std::vector<const LightSourceViewBound*> LightList;
124 
125  osg::ref_ptr<osg::StateSet> getLightListStateSet(const LightList& lightList, unsigned int frameNum);
126 
127  private:
128  // Lights collected from the scene graph. Only valid during the cull traversal.
129  std::vector<LightSourceTransform> mLights;
130 
131  typedef std::vector<LightSourceViewBound> LightSourceViewBoundCollection;
132  std::map<osg::observer_ptr<osg::Camera>, LightSourceViewBoundCollection> mLightsInViewSpace;
133 
134  // < Light list hash , StateSet >
135  typedef std::map<size_t, osg::ref_ptr<osg::StateSet> > LightStateSetMap;
137 
139 
140  unsigned int mLightingMask;
141  };
142 
151  class LightListCallback : public osg::NodeCallback
152  {
153  public:
155  : mLightManager(NULL)
156  , mLastFrameNumber(0)
157  {}
158  LightListCallback(const LightListCallback& copy, const osg::CopyOp& copyop)
159  : osg::Object(copy, copyop), osg::NodeCallback(copy, copyop)
161  , mLastFrameNumber(0)
163  {}
164 
165  META_Object(SceneUtil, LightListCallback)
166 
167  void operator()(osg::Node* node, osg::NodeVisitor* nv);
168 
169  std::set<SceneUtil::LightSource*>& getIgnoredLightSources() { return mIgnoredLightSources; }
170 
171  private:
173  unsigned int mLastFrameNumber;
175  std::set<SceneUtil::LightSource*> mIgnoredLightSources;
176  };
177 
178 }
179 
180 #endif
Definition: lightmanager.hpp:151
Definition: lightmanager.hpp:115
LightSource * mLightSource
Definition: lightmanager.hpp:109
std::set< SceneUtil::LightSource * > mIgnoredLightSources
Definition: lightmanager.hpp:175
void setLightingMask(unsigned int mask)
Definition: lightmanager.cpp:161
osg::Light * getLight(unsigned int frame)
Definition: lightmanager.hpp:55
unsigned int mLightingMask
Definition: lightmanager.hpp:140
osg::ref_ptr< osg::Light > mLight[2]
Definition: lightmanager.hpp:26
std::map< size_t, osg::ref_ptr< osg::StateSet > > LightStateSetMap
Definition: lightmanager.hpp:135
float getRadius() const
Definition: lightmanager.hpp:41
void setStartLight(int start)
Set the first light index that should be used by this manager, typically the number of directional li...
Definition: lightmanager.cpp:265
unsigned int mLastFrameNumber
Definition: lightmanager.hpp:173
void setRadius(float radius)
The LightSource will affect objects within this radius.
Definition: lightmanager.hpp:47
int getStartLight() const
Definition: lightmanager.cpp:281
Definition: lightmanager.hpp:107
LightSource * mLightSource
Definition: lightmanager.hpp:117
std::vector< const LightSourceViewBound * > LightList
Definition: lightmanager.hpp:123
std::vector< LightSourceTransform > mLights
Definition: lightmanager.hpp:129
void update()
Internal use only, called automatically by the LightManager's UpdateCallback.
Definition: lightmanager.cpp:171
Definition: lightmanager.hpp:23
LightListCallback()
Definition: lightmanager.hpp:154
META_Node(SceneUtil, SceneUtil::LightManager) LightManager()
LightStateSetMap mStateSetCache[2]
Definition: lightmanager.hpp:136
const std::vector< LightSourceTransform > & getLights() const
Definition: lightmanager.cpp:236
META_Object(SceneUtil, LightListCallback) void operator()(osg getIgnoredLightSources() std::set< SceneUtil::LightSource * > &
Definition: lightmanager.hpp:169
Decorator node implementing the rendering of any number of LightSources that can be anywhere in the s...
Definition: lightmanager.hpp:78
std::vector< LightSourceViewBound > LightSourceViewBoundCollection
Definition: lightmanager.hpp:131
int mStartLight
Definition: lightmanager.hpp:138
osg::ref_ptr< osg::StateSet > getLightListStateSet(const LightList &lightList, unsigned int frameNum)
Definition: lightmanager.cpp:195
LightManager(const LightManager &copy, const osg::CopyOp &copyop)
Definition: lightmanager.cpp:146
std::map< osg::observer_ptr< osg::Camera >, LightSourceViewBoundCollection > mLightsInViewSpace
Definition: lightmanager.hpp:132
const std::vector< LightSourceViewBound > & getLightsInViewSpace(osg::Camera *camera, const osg::RefMatrix *viewMatrix)
Definition: lightmanager.cpp:241
osg::Matrixf mWorldMatrix
Definition: lightmanager.hpp:110
int getId() const
Get the unique ID for this light source.
Definition: lightmanager.hpp:71
int clone(Arguments &info)
Definition: esmtool.cpp:429
unsigned int getLightingMask() const
Definition: lightmanager.cpp:166
osg::BoundingSphere mViewBound
Definition: lightmanager.hpp:118
META_Node(SceneUtil, SceneUtil::LightSource) LightSource()
float mRadius
Definition: lightmanager.hpp:29
int mId
Definition: lightmanager.hpp:31
Definition: clone.hpp:23
void setLight(osg::Light *light)
Definition: lightmanager.hpp:64
void addLight(LightSource *lightSource, const osg::Matrixf &worldMat, unsigned int frameNum)
Internal use only, called automatically by the LightSource's UpdateCallback.
Definition: lightmanager.cpp:184
LightManager * mLightManager
Definition: lightmanager.hpp:172
LightListCallback(const LightListCallback &copy, const osg::CopyOp &copyop)
Definition: lightmanager.hpp:158
LightManager::LightList mLightList
Definition: lightmanager.hpp:174