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 osgUtil
13 {
14  class CullVisitor;
15 }
16 
17 namespace SceneUtil
18 {
19 
28  class LightSource : public osg::Node
29  {
30  // double buffered osg::Light's, since one of them may be in use by the draw thread at any given time
31  osg::ref_ptr<osg::Light> mLight[2];
32 
33  // LightSource will affect objects within this radius
34  float mRadius;
35 
36  int mId;
37 
38  public:
39 
40  META_Node(SceneUtil, LightSource)
41 
42  LightSource();
43 
44  LightSource(const LightSource& copy, const osg::CopyOp& copyop);
45 
46  float getRadius() const
47  {
48  return mRadius;
49  }
50 
52  void setRadius(float radius)
53  {
54  mRadius = radius;
55  }
56 
60  osg::Light* getLight(unsigned int frame)
61  {
62  return mLight[frame % 2];
63  }
64 
69  void setLight(osg::Light* light)
70  {
71  mLight[0] = light;
72  mLight[1] = new osg::Light(*light);
73  }
74 
76  int getId() const
77  {
78  return mId;
79  }
80  };
81 
83  class LightManager : public osg::Group
84  {
85  public:
86 
87  META_Node(SceneUtil, LightManager)
88 
89  LightManager();
90 
91  LightManager(const LightManager& copy, const osg::CopyOp& copyop);
92 
97  void setLightingMask (unsigned int mask);
98 
99  unsigned int getLightingMask() const;
100 
102  void setStartLight(int start);
103 
104  int getStartLight() const;
105 
107  void update();
108 
110  void addLight(LightSource* lightSource, const osg::Matrixf& worldMat, unsigned int frameNum);
111 
113  {
115  osg::Matrixf mWorldMatrix;
116  };
117 
118  const std::vector<LightSourceTransform>& getLights() const;
119 
121  {
123  osg::BoundingSphere mViewBound;
124  };
125 
126  const std::vector<LightSourceViewBound>& getLightsInViewSpace(osg::Camera* camera, const osg::RefMatrix* viewMatrix);
127 
128  typedef std::vector<const LightSourceViewBound*> LightList;
129 
130  osg::ref_ptr<osg::StateSet> getLightListStateSet(const LightList& lightList, unsigned int frameNum);
131 
132  private:
133  // Lights collected from the scene graph. Only valid during the cull traversal.
134  std::vector<LightSourceTransform> mLights;
135 
136  typedef std::vector<LightSourceViewBound> LightSourceViewBoundCollection;
137  std::map<osg::observer_ptr<osg::Camera>, LightSourceViewBoundCollection> mLightsInViewSpace;
138 
139  // < Light list hash , StateSet >
140  typedef std::map<size_t, osg::ref_ptr<osg::StateSet> > LightStateSetMap;
142 
144 
145  unsigned int mLightingMask;
146  };
147 
157  class LightListCallback : public osg::NodeCallback
158  {
159  public:
161  : mLightManager(NULL)
162  , mLastFrameNumber(0)
163  {}
164  LightListCallback(const LightListCallback& copy, const osg::CopyOp& copyop)
165  : osg::Object(copy, copyop), osg::NodeCallback(copy, copyop)
167  , mLastFrameNumber(0)
169  {}
170 
171  META_Object(SceneUtil, LightListCallback)
172 
173  void operator()(osg::Node* node, osg::NodeVisitor* nv);
174 
175  bool pushLightState(osg::Node* node, osgUtil::CullVisitor* nv);
176 
177  std::set<SceneUtil::LightSource*>& getIgnoredLightSources() { return mIgnoredLightSources; }
178 
179  private:
181  unsigned int mLastFrameNumber;
183  std::set<SceneUtil::LightSource*> mIgnoredLightSources;
184  };
185 
186 }
187 
188 #endif
Definition: lightmanager.hpp:157
Definition: lightmanager.hpp:120
void copy(const btTransform &src, Nif::Transformation &dst)
Definition: testbulletnifloader.cpp:269
LightSource * mLightSource
Definition: lightmanager.hpp:114
META_Node(SceneUtil, LightManager) LightManager()
std::set< SceneUtil::LightSource * > mIgnoredLightSources
Definition: lightmanager.hpp:183
void setLightingMask(unsigned int mask)
Definition: lightmanager.cpp:178
osg::Light * getLight(unsigned int frame)
Definition: lightmanager.hpp:60
unsigned int mLightingMask
Definition: lightmanager.hpp:145
osg::ref_ptr< osg::Light > mLight[2]
Definition: lightmanager.hpp:31
std::map< size_t, osg::ref_ptr< osg::StateSet > > LightStateSetMap
Definition: lightmanager.hpp:140
float getRadius() const
Definition: lightmanager.hpp:46
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:339
unsigned int mLastFrameNumber
Definition: lightmanager.hpp:181
void setRadius(float radius)
The LightSource will affect objects within this radius.
Definition: lightmanager.hpp:52
int getStartLight() const
Definition: lightmanager.cpp:353
Definition: lightmanager.hpp:112
LightSource * mLightSource
Definition: lightmanager.hpp:122
std::vector< const LightSourceViewBound * > LightList
Definition: lightmanager.hpp:128
std::vector< LightSourceTransform > mLights
Definition: lightmanager.hpp:134
void update()
Internal use only, called automatically by the LightManager's UpdateCallback.
Definition: lightmanager.cpp:188
Definition: lightmanager.hpp:28
LightListCallback()
Definition: lightmanager.hpp:160
LightStateSetMap mStateSetCache[2]
Definition: lightmanager.hpp:141
const std::vector< LightSourceTransform > & getLights() const
Definition: lightmanager.cpp:262
Decorator node implementing the rendering of any number of LightSources that can be anywhere in the s...
Definition: lightmanager.hpp:83
std::vector< LightSourceViewBound > LightSourceViewBoundCollection
Definition: lightmanager.hpp:136
int mStartLight
Definition: lightmanager.hpp:143
osg::ref_ptr< osg::StateSet > getLightListStateSet(const LightList &lightList, unsigned int frameNum)
Definition: lightmanager.cpp:220
LightManager(const LightManager &copy, const osg::CopyOp &copyop)
Definition: lightmanager.cpp:163
META_Object(SceneUtil, LightListCallback) void operator()(osg pushLightState(osg::Node *node, osgUtil::CullVisitor *nv) bool
Definition: lightmanager.cpp:393
std::map< osg::observer_ptr< osg::Camera >, LightSourceViewBoundCollection > mLightsInViewSpace
Definition: lightmanager.hpp:137
const std::vector< LightSourceViewBound > & getLightsInViewSpace(osg::Camera *camera, const osg::RefMatrix *viewMatrix)
Definition: lightmanager.cpp:267
osg::Matrixf mWorldMatrix
Definition: lightmanager.hpp:115
int getId() const
Get the unique ID for this light source.
Definition: lightmanager.hpp:76
std::set< SceneUtil::LightSource * > & getIgnoredLightSources()
Definition: lightmanager.hpp:177
unsigned int getLightingMask() const
Definition: lightmanager.cpp:183
META_Node(SceneUtil, LightSource) LightSource()
osg::BoundingSphere mViewBound
Definition: lightmanager.hpp:123
float mRadius
Definition: lightmanager.hpp:34
int mId
Definition: lightmanager.hpp:36
Definition: clone.hpp:23
void setLight(osg::Light *light)
Definition: lightmanager.hpp:69
void addLight(LightSource *lightSource, const osg::Matrixf &worldMat, unsigned int frameNum)
Internal use only, called automatically by the LightSource's UpdateCallback.
Definition: lightmanager.cpp:201
LightManager * mLightManager
Definition: lightmanager.hpp:180
LightListCallback(const LightListCallback &copy, const osg::CopyOp &copyop)
Definition: lightmanager.hpp:164
LightManager::LightList mLightList
Definition: lightmanager.hpp:182