RigsofRods
Soft-body Physics Simulation
GPUManager.cpp
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------------------
3 This source file is part of SkyX.
4 Visit http://www.paradise-studios.net/products/skyx/
5 
6 Copyright (C) 2009-2012 Xavier Verguín González <xavyiy@gmail.com>
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt.
21 --------------------------------------------------------------------------------
22 */
23 
24 #include "GPUManager.h"
25 
26 #include "SkyX.h"
27 
28 namespace SkyX
29 {
31  : mSkyX(s)
32  , mGroundPasses(std::vector<Ogre::Pass*>())
33  {
35  }
36 
38  {
39  }
40 
41  void GPUManager::addGroundPass(Ogre::Pass* GroundPass, const Ogre::Real& AtmosphereRadius, const Ogre::SceneBlendType& SBT)
42  {
43  GroundPass->setVertexProgram("SkyX_Ground_VP");
45  {
46  GroundPass->setFragmentProgram("SkyX_Ground_LDR_FP");
47  }
48  else
49  {
50  GroundPass->setFragmentProgram("SkyX_Ground_HDR_FP");
51  }
52 
53  GroundPass->getVertexProgramParameters()->setNamedConstant("uSkydomeRadius", AtmosphereRadius*10);
54 
55  GroundPass->setLightingEnabled(false);
56 
57  GroundPass->setDepthCheckEnabled(true);
58  GroundPass->setDepthWriteEnabled(false);
59 
60  GroundPass->setSceneBlending(SBT);
61 
63  mGroundPasses.push_back(GroundPass);
64 
66  }
67 
69  {
70  Ogre::String fp_name = "SkyX_Ground_HDR_FP";
71 
73  {
74  fp_name = "SkyX_Ground_LDR_FP";
75  }
76 
77  for(unsigned int k = 0; k < mGroundPasses.size(); k++)
78  {
79  mGroundPasses.at(k)->setFragmentProgram(fp_name);
80  }
81 
82  bool gammaCorrection = mSkyX->getLightingMode() == SkyX::LM_HDR;
83 
84  // SkyX_Starfield.png
85  static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().getByName(getSkydomeMaterialName()))
86  ->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setHardwareGammaEnabled(gammaCorrection);
87 
88  // SkyX_Moon.png and SkyX_MoonHalo.png
89  static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().getByName(getMoonMaterialName()))
90  ->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setHardwareGammaEnabled(gammaCorrection);
91  static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().getByName(getMoonMaterialName()))
92  ->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setHardwareGammaEnabled(gammaCorrection);
93 
94  _setTextureHWGammaCorrection("SkyX_Starfield.png", gammaCorrection);
95  _setTextureHWGammaCorrection("SkyX_Moon.png", gammaCorrection);
96  _setTextureHWGammaCorrection("SkyX_MoonHalo.png", gammaCorrection);
97  }
98 
99  void GPUManager::setGpuProgramParameter(const GpuProgram &GpuP, const Ogre::String &Name, const int &Value, const bool& UpdateGroundPasses)
100  {
101  if (!mSkyX->getMeshManager()->isCreated())
102  {
103  return;
104  }
105 
106  Ogre::GpuProgramParametersSharedPtr Parameters;
107 
108  switch (GpuP)
109  {
110  case GPUP_VERTEX:
111  {
112  Parameters = mSkydomeMaterial->getTechnique(0)->getPass(0)->getVertexProgramParameters();
113  }
114  break;
115 
116  case GPUP_FRAGMENT:
117  {
118  Parameters = mSkydomeMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
119  }
120  break;
121  }
122 
123  Parameters->setNamedConstant(Name, Value);
124 
125  if (!UpdateGroundPasses)
126  {
127  return;
128  }
129 
130  std::vector<Ogre::Pass*>::iterator PassIt;
131 
132  for(PassIt = mGroundPasses.begin(); PassIt != mGroundPasses.end(); PassIt++)
133  {
134  if (!(*PassIt))
135  {
136  mGroundPasses.erase(PassIt);
137  continue;
138  }
139 
140  switch (GpuP)
141  {
142  case GPUP_VERTEX:
143  {
144  Parameters = (*PassIt)->getVertexProgramParameters();
145  }
146  break;
147 
148  case GPUP_FRAGMENT:
149  {
150  Parameters = (*PassIt)->getFragmentProgramParameters();
151  }
152  break;
153  }
154 
155  Parameters->setNamedConstant(Name, Value);
156  }
157  }
158 
159  void GPUManager::setGpuProgramParameter(const GpuProgram &GpuP, const Ogre::String &Name, const Ogre::Real &Value, const bool& UpdateGroundPasses)
160  {
161  if (!mSkyX->getMeshManager()->isCreated())
162  {
163  return;
164  }
165 
166  Ogre::GpuProgramParametersSharedPtr Parameters;
167 
168  switch (GpuP)
169  {
170  case GPUP_VERTEX:
171  {
172  Parameters = mSkydomeMaterial->getTechnique(0)->getPass(0)->getVertexProgramParameters();
173  }
174  break;
175 
176  case GPUP_FRAGMENT:
177  {
178  Parameters = mSkydomeMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
179  }
180  break;
181  }
182 
183  Parameters->setNamedConstant(Name, Value);
184 
185  if (!UpdateGroundPasses)
186  {
187  return;
188  }
189 
190  std::vector<Ogre::Pass*>::iterator PassIt;
191 
192  for(PassIt = mGroundPasses.begin(); PassIt != mGroundPasses.end(); PassIt++)
193  {
194  if (!(*PassIt))
195  {
196  mGroundPasses.erase(PassIt);
197  continue;
198  }
199 
200  switch (GpuP)
201  {
202  case GPUP_VERTEX:
203  {
204  Parameters = (*PassIt)->getVertexProgramParameters();
205  }
206  break;
207 
208  case GPUP_FRAGMENT:
209  {
210  Parameters = (*PassIt)->getFragmentProgramParameters();
211  }
212  break;
213  }
214 
215  Parameters->setNamedConstant(Name, Value);
216  }
217  }
218 
219  void GPUManager::setGpuProgramParameter(const GpuProgram &GpuP, const Ogre::String &Name, const Ogre::Vector2 &Value, const bool& UpdateGroundPasses)
220  {
221  if (!mSkyX->getMeshManager()->isCreated())
222  {
223  return;
224  }
225 
226  Ogre::GpuProgramParametersSharedPtr Parameters;
227 
228  switch (GpuP)
229  {
230  case GPUP_VERTEX:
231  {
232  Parameters = mSkydomeMaterial->getTechnique(0)->getPass(0)->getVertexProgramParameters();
233  }
234  break;
235 
236  case GPUP_FRAGMENT:
237  {
238  Parameters = mSkydomeMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
239  }
240  break;
241  }
242 
243  float Value_[2] = {Value.x, Value.y};
244 
245  Parameters->setNamedConstant(Name, Value_, 1, 2);
246 
247  if (!UpdateGroundPasses)
248  {
249  return;
250  }
251 
252  std::vector<Ogre::Pass*>::iterator PassIt;
253 
254  for(PassIt = mGroundPasses.begin(); PassIt != mGroundPasses.end(); PassIt++)
255  {
256  if (!(*PassIt))
257  {
258  mGroundPasses.erase(PassIt);
259  continue;
260  }
261 
262  switch (GpuP)
263  {
264  case GPUP_VERTEX:
265  {
266  Parameters = (*PassIt)->getVertexProgramParameters();
267  }
268  break;
269 
270  case GPUP_FRAGMENT:
271  {
272  Parameters = (*PassIt)->getFragmentProgramParameters();
273  }
274  break;
275  }
276 
277  Parameters->setNamedConstant(Name, Value_, 1, 2);
278  }
279  }
280 
281  void GPUManager::setGpuProgramParameter(const GpuProgram &GpuP, const Ogre::String &Name, const Ogre::Vector3 &Value, const bool& UpdateGroundPasses)
282  {
283  if (!mSkyX->getMeshManager()->isCreated())
284  {
285  return;
286  }
287 
288  Ogre::GpuProgramParametersSharedPtr Parameters;
289 
290  switch (GpuP)
291  {
292  case GPUP_VERTEX:
293  {
294  Parameters = mSkydomeMaterial->getTechnique(0)->getPass(0)->getVertexProgramParameters();
295  }
296  break;
297 
298  case GPUP_FRAGMENT:
299  {
300  Parameters = mSkydomeMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
301  }
302  break;
303  }
304 
305  Parameters->setNamedConstant(Name, Value);
306 
307  if (!UpdateGroundPasses)
308  {
309  return;
310  }
311 
312  std::vector<Ogre::Pass*>::iterator PassIt;
313 
314  for(PassIt = mGroundPasses.begin(); PassIt != mGroundPasses.end(); PassIt++)
315  {
316  if (!(*PassIt))
317  {
318  mGroundPasses.erase(PassIt);
319  continue;
320  }
321 
322  switch (GpuP)
323  {
324  case GPUP_VERTEX:
325  {
326  Parameters = (*PassIt)->getVertexProgramParameters();
327  }
328  break;
329 
330  case GPUP_FRAGMENT:
331  {
332  Parameters = (*PassIt)->getFragmentProgramParameters();
333  }
334  break;
335  }
336 
337  Parameters->setNamedConstant(Name, Value);
338  }
339  }
340 
341  const Ogre::String GPUManager::getSkydomeMaterialName() const
342  {
343  Ogre::String starfield = (mSkyX->isStarfieldEnabled()) ? "STARFIELD_" : "";
344 
345  return (mSkyX->getLightingMode() == SkyX::LM_LDR) ? "SkyX_Skydome_" + starfield + "LDR" : "SkyX_Skydome_" + starfield + "HDR";
346  }
347 
348  void GPUManager::_setTextureHWGammaCorrection(const Ogre::String& n, const bool& g)
349  {
350  Ogre::TexturePtr tex = Ogre::TextureManager::getSingleton().getByName(n);
351 
352  if (!tex.isNull())
353  {
354  if (g)
355  {
356  if (!tex->isHardwareGammaEnabled())
357  {
358  tex->setHardwareGammaEnabled(true);
359  tex->reload();
360  }
361  }
362  else
363  {
364  if (tex->isHardwareGammaEnabled())
365  {
366  tex->setHardwareGammaEnabled(false);
367  tex->reload();
368  }
369  }
370  }
371  }
372 }
SkyX::GPUManager::~GPUManager
~GPUManager()
Destructor.
Definition: GPUManager.cpp:37
SkyX::AtmosphereManager::getOptions
const Options & getOptions() const
Get current options.
Definition: AtmosphereManager.h:139
SkyX::SkyX::LM_HDR
@ LM_HDR
High dynamic range.
Definition: SkyX.h:101
SkyX::GPUManager::getSkydomeMaterialName
const Ogre::String getSkydomeMaterialName() const
Get skydome material name.
Definition: GPUManager.cpp:341
SkyX::SkyX::getLightingMode
const LightingMode & getLightingMode() const
Get lighting mode.
Definition: SkyX.h:275
SkyX::GPUManager::GpuProgram
GpuProgram
Gpu program enum.
Definition: GPUManager.h:38
SkyX::GPUManager::mSkydomeMaterial
Ogre::MaterialPtr mSkydomeMaterial
Skydome material.
Definition: GPUManager.h:134
SkyX::SkyX::LM_LDR
@ LM_LDR
Low dynamic range.
Definition: SkyX.h:99
SkyX
Definition: AtmosphereManager.cpp:30
SkyX::GPUManager::_updateFP
void _updateFP()
Update fragment program materials.
Definition: GPUManager.cpp:68
SkyX::GPUManager::_setTextureHWGammaCorrection
void _setTextureHWGammaCorrection(const Ogre::String &n, const bool &g)
Set texture HW gamma correction.
Definition: GPUManager.cpp:348
SkyX::SkyX::getMeshManager
MeshManager * getMeshManager()
Get mesh manager.
Definition: SkyX.h:183
SkyX::SkyX::isStarfieldEnabled
const bool & isStarfieldEnabled() const
Is the starfield enable?
Definition: SkyX.h:288
SkyX::GPUManager::setGpuProgramParameter
void setGpuProgramParameter(const GpuProgram &GpuP, const Ogre::String &Name, const int &Value, const bool &UpdateGroundPasses=true)
Set gpu program int parameter.
Definition: GPUManager.cpp:99
GPUManager.h
SkyX::GPUManager::mSkyX
SkyX * mSkyX
SkyX parent pointer.
Definition: GPUManager.h:139
SkyX::GPUManager::mGroundPasses
std::vector< Ogre::Pass * > mGroundPasses
Ground pass vector.
Definition: GPUManager.h:136
SkyX.h
SkyX::GPUManager::GPUP_VERTEX
@ GPUP_VERTEX
Definition: GPUManager.h:41
SkyX::MeshManager::isCreated
const bool & isCreated() const
Is _createGeometry() called?
Definition: MeshManager.h:137
SkyX::SkyX::getAtmosphereManager
AtmosphereManager * getAtmosphereManager()
Get atmosphere manager.
Definition: SkyX.h:191
SkyX::AtmosphereManager::_update
void _update(const Options &NewOptions, const bool &ForceToUpdateAll=false)
Update atmoshpere.
Definition: AtmosphereManager.cpp:42
SkyX::GPUManager::GPUP_FRAGMENT
@ GPUP_FRAGMENT
Definition: GPUManager.h:43
SkyX::GPUManager::addGroundPass
void addGroundPass(Ogre::Pass *GroundPass, const Ogre::Real &AtmosphereRadius, const Ogre::SceneBlendType &SBT=Ogre::SBT_ADD)
Add ground pass (Use for atmospheric scattering effect on the terrain)
Definition: GPUManager.cpp:41
SkyX::GPUManager::getMoonMaterialName
const Ogre::String getMoonMaterialName() const
Get moon material name.
Definition: GPUManager.h:102
SkyX::GPUManager::_notifySkydomeMaterialChanged
void _notifySkydomeMaterialChanged()
Notify skydome material changed.
Definition: GPUManager.h:115
Ogre
Definition: ExtinguishableFireAffector.cpp:35
SkyX::GPUManager::GPUManager
GPUManager(SkyX *s)
Constructor.
Definition: GPUManager.cpp:30