Caelum
PrecipitationController.h
1 /*
2 This file is part of Caelum.
3 See http://www.ogre3d.org/wiki/index.php/Caelum
4 
5 Copyright (c) 2008 Caelum team. See Contributors.txt for details.
6 
7 Caelum is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Caelum is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with Caelum. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef CAELUM__PRECIPITATION_CONTROLLER_H
22 #define CAELUM__PRECIPITATION_CONTROLLER_H
23 
24 #include "CaelumPrerequisites.h"
25 #include "FastGpuParamRef.h"
26 
27 namespace Caelum
28 {
32  {
33  Ogre::ColourValue Colour;
34  Ogre::Real Speed;
35  Ogre::String Name;
36  };
37 
42  {
43  PRECTYPE_DRIZZLE = 0,
44  PRECTYPE_RAIN = 1,
45  PRECTYPE_SNOW = 2,
46  PRECTYPE_SNOWGRAINS = 3,
47  PRECTYPE_ICECRYSTALS = 4,
48  PRECTYPE_ICEPELLETS = 5,
49  PRECTYPE_HAIL = 6,
50  PRECTYPE_SMALLHAIL = 7,
51 
52  PRECTYPE_CUSTOM = 8,
53  };
54 
61  class CAELUM_EXPORT PrecipitationController
62  {
63  private:
64  friend class PrecipitationInstance;
65 
66  Ogre::SceneManager *mSceneMgr;
67  Ogre::Vector3 mWindSpeed;
68  Ogre::Real mIntensity;
69  Ogre::Real mSpeed;
70  Ogre::ColourValue mColour;
71  PrecipitationType mPresetType;
72  Ogre::String mTextureName;
73  Ogre::Vector3 mCameraSpeedScale;
74  Ogre::Vector3 mFallingDirection;
75 
76  Ogre::Real mAutoDisableThreshold;
77  bool mHardDisableCompositor;
78 
79  Ogre::ColourValue mSceneColour;
80  Real mInternalTime;
81 
82  // Only meant for the instance ctl in auto-camera-speed mode.
83  Real mSecondsSinceLastFrame;
84  inline Real getSecondsSinceLastFrame() { return mSecondsSinceLastFrame; }
85 
86  public:
88  static const String COMPOSITOR_NAME;
89 
91  static const String MATERIAL_NAME;
92 
94  static bool isPresetType (PrecipitationType value);
95 
97  static const PrecipitationPresetParams& getPresetParams (PrecipitationType value);
98 
100  void setParams (const PrecipitationPresetParams& params);
101 
103  void setPresetType (PrecipitationType value);
104 
109  PrecipitationType getPresetType () const;
110 
111  // Texture name, part of a preset
112  void setTextureName(const Ogre::String& textureName);
113  const Ogre::String getTextureName() const;
114 
116  void setColour(const Ogre::ColourValue& color);
117  const Ogre::ColourValue getColour() const;
118 
120  void setSpeed(Real value);
121  Real getSpeed() const;
122 
124  void setIntensity(Real value);
125  Real getIntensity() const;
126 
128  void setWindSpeed(const Ogre::Vector3 &value);
129  const Ogre::Vector3 getWindSpeed() const;
130 
137  void setFallingDirection(const Ogre::Vector3 &value) { mFallingDirection = value; }
138  const Ogre::Vector3 getFallingDirection() const { return mFallingDirection; }
139 
141  void setManualCameraSpeed(const Ogre::Vector3 &value);
142 
144  void setAutoCameraSpeed();
145 
151  inline void setAutoDisableThreshold (Real value) { mAutoDisableThreshold = value; }
152  inline Real getAutoDisableThreshold () const { return mAutoDisableThreshold; }
153 
164  inline void setCameraSpeedScale (const Ogre::Vector3& value) {
165  mCameraSpeedScale = value;
166  }
167  inline const Ogre::Vector3 getCameraSpeedScale () const {
168  return mCameraSpeedScale;
169  }
170 
173  inline void setCameraSpeedScale (Ogre::Real value) {
174  setCameraSpeedScale(Ogre::Vector3::UNIT_SCALE * value);
175  }
176 
180  void update(Real secondsSinceLastFrame, Ogre::ColourValue colour);
181 
183  Ogre::SceneManager *sceneMgr);
185 
186  public:
187  typedef std::map<Ogre::Viewport*, PrecipitationInstance*> ViewportInstanceMap;
188  ViewportInstanceMap mViewportInstanceMap;
189 
190  public:
192  PrecipitationInstance* createViewportInstance(Ogre::Viewport* viewport);
193 
195  void destroyViewportInstance(Ogre::Viewport* viewport);
196 
198  PrecipitationInstance* getViewportInstance(Ogre::Viewport* viewport);
199 
201  void destroyAllViewportInstances();
202  };
203 
207  class PrecipitationInstance: private Ogre::CompositorInstance::Listener
208  {
209  private:
210  friend class PrecipitationController;
211 
212  PrecipitationController* mParent;
213  Ogre::Viewport* mViewport;
214  Ogre::CompositorInstance* mCompInst;
215  Ogre::Camera* mLastCamera;
216  Ogre::Vector3 mLastCameraPosition;
217  Ogre::Vector3 mCameraSpeed;
218  bool mAutoCameraSpeed;
219 
220  virtual void notifyMaterialSetup(uint pass_id, Ogre::MaterialPtr &mat);
221  virtual void notifyMaterialRender(uint pass_id, Ogre::MaterialPtr &mat);
222 
225  void _updateMaterialParams(
226  const Ogre::MaterialPtr& mat,
227  const Ogre::Camera* cam,
228  const Ogre::Vector3& camSpeed);
229 
230  // Add remove the compositors. Do nothing if already added/removed
231  void createCompositor ();
232  void destroyCompositor ();
233 
234  // Check if the compositor should be enabled
235  bool shouldBeEnabled () const;
236 
238  void _update();
239 
240  public:
241  inline Ogre::Viewport* getViewport() const { return mViewport; }
242  inline PrecipitationController* getParent() const { return mParent; }
243  inline Ogre::CompositorInstance* getCompositorInstance() const { return mCompInst; }
244 
246  bool getAutoCameraSpeed();
247 
253  void setAutoCameraSpeed();
254 
256  void setManualCameraSpeed(const Ogre::Vector3& value);
257 
259  const Ogre::Vector3 getCameraSpeed();
260 
261  PrecipitationInstance(PrecipitationController* parent, Ogre::Viewport* view);
262  virtual ~PrecipitationInstance();
263 
264  private:
265  struct Params {
266  void setup(Ogre::GpuProgramParametersSharedPtr fpParams);
267 
268  Ogre::GpuProgramParametersSharedPtr fpParams;
269  FastGpuParamRef precColor;
270  FastGpuParamRef intensity;
271  FastGpuParamRef dropSpeed;
272  FastGpuParamRef corner1;
273  FastGpuParamRef corner2;
274  FastGpuParamRef corner3;
275  FastGpuParamRef corner4;
276  FastGpuParamRef deltaX;
277  FastGpuParamRef deltaY;
278  } mParams;
279 
280  };
281 }
282 
283 #endif //CAELUM__PRECIPITATION_CONTROLLER_H
void setAutoDisableThreshold(Real value)
Automatically disable compositors when intensity is low.
Compositor-based precipitation controller.
PrecipitationType
An enumeration of the available precipitation presets.
void setCameraSpeedScale(const Ogre::Vector3 &value)
Automatically scale camera speed.
void setFallingDirection(const Ogre::Vector3 &value)
The basic direction for falling precipitation.
Per-viewport instance of precipitation.
static const String MATERIAL_NAME
Name of the compositor material.
An optimized reference to a gpu shared parameter.
Preset parameters for a certain type of precipitation.
void setCameraSpeedScale(Ogre::Real value)
Set an equal camera speed scale in all dimensions.
Caelum namespace.
Definition: Astronomy.cpp:24
static const String COMPOSITOR_NAME
Name of the compositor resource.