All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LightMaterialGenerator.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef _LIGHTMATERIALGENERATOR_HH_
18 #define _LIGHTMATERIALGENERATOR_HH_
19 
20 #include <OgreStringConverter.h>
21 #include <OgreException.h>
22 #include <OgreMaterialManager.h>
23 
24 #include <OgrePass.h>
25 #include <OgreTechnique.h>
26 
27 #include <OgreHighLevelGpuProgram.h>
28 #include <OgreHighLevelGpuProgramManager.h>
29 
30 #include <string>
31 
35 #include "gazebo/util/system.hh"
36 
37 namespace gazebo
38 {
39  namespace rendering
40  {
41  template<typename techniquePolicy>
43 
44  template<typename techniquePolicy>
46  public MaterialGenerator, techniquePolicy
47  {
49  public: enum MaterialID
50  {
51  MI_POINT = 0x01,
52  MI_SPOTLIGHT = 0x02,
53  MI_DIRECTIONAL = 0x04,
54  MI_ATTENUATED = 0x08,
55  MI_SPECULAR = 0x10,
56  MI_SHADOW_CASTER = 0x20
57  };
58 
60  {
61  vsMask = 0x00000004;
62  fsMask = 0x0000003F;
65 
66  this->schemeName.clear();
67  materialBaseName = this->GetMaterialPrefix() + "/LightMaterial/";
69  materialBaseName);
70  }
71 
72  public: virtual ~LightMaterialGenerator()
73  {}
74  };
75 
76  template<typename techniquePolicy>
77  class GAZEBO_VISIBLE LightMaterialGeneratorGLSL :
78  public MaterialGenerator::Impl, public techniquePolicy
79  {
80  public: typedef MaterialGenerator::Perm Perm;
81 
83  public: LightMaterialGeneratorGLSL(const std::string &_baseName)
84  : baseName(_baseName) {}
85 
87  public: virtual ~LightMaterialGeneratorGLSL() {}
88 
90  public: virtual Ogre::GpuProgramPtr GenerateVertexShader(
91  Perm _permutation)
92  {
93  std::string programName = "DeferredShading/";
94 
95  if (_permutation &
97  programName += "vs";
98  else
99  programName += "LightMaterial_vs";
100 
101  Ogre::GpuProgramPtr ptr =
102  Ogre::HighLevelGpuProgramManager::getSingleton().getByName(
103  programName);
104 
105  if (ptr.isNull())
106  gzthrow("Null pointer");
107 
108  return ptr;
109  }
111  /* public: virtual Ogre::GpuProgramPtr GenerateFragmentShader(
112  Perm _permutation)
113  {
115  if (this->masterSource.empty())
116  {
117  Ogre::DataStreamPtr ptrMasterSource =
118  Ogre::ResourceGroupManager::getSingleton().openResource(
119  //NEW "deferred_rendering/deferred_shading/LightMaterial_ps.cg",
120  "DeferredShading/post/LightMaterial_ps.cg",
121  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
122 
123  if (ptrMasterSource.isNull())
124  gzthrow("Null Pointer\n");
125 
126  this->masterSource = ptrMasterSource->getAsString();
127  }
128 
129  if (this->masterSource.empty())
130  gzthrow("Empty string");
131 
132  // Create name
133  std::string name = this->baseName +
134  Ogre::StringConverter::toString(_permutation) + "_ps";
135 
136  // Create shader object
137  Ogre::HighLevelGpuProgramPtr ptrProgram =
138  Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(
139  name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
140  "cg", Ogre::GPT_FRAGMENT_PROGRAM);
141 
142  ptrProgram->setSource(this->masterSource);
143  ptrProgram->setParameter("entry_point","main");
144  ptrProgram->setParameter("profiles","ps_3_0 arbfp1");
145 
146  // set up the preprocessor defines
147  // Important to do this before any call to get parameters,
148  // i.e. before the program gets loaded
149  ptrProgram->setParameter("compile_arguments",
150  this->GetPPDefines(_permutation));
151  this->SetUpBaseParameters(ptrProgram->getDefaultParameters());
152 
153  return Ogre::GpuProgramPtr(ptrProgram);
154  }*/
156  public: virtual Ogre::GpuProgramPtr GenerateFragmentShader(
157  Perm _permutation)
158  {
160  if (this->masterSource.empty())
161  {
162  Ogre::DataStreamPtr ptrMasterSource =
163  Ogre::ResourceGroupManager::getSingleton().openResource(
164  "deferred_rendering/deferred_shading/light_material_ps.glsl",
165  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
166 
167  if (ptrMasterSource.isNull())
168  gzthrow("Null Pointer\n");
169 
170  this->masterSource = ptrMasterSource->getAsString();
171  }
172 
173  if (this->masterSource.empty())
174  gzthrow("Empty string");
175 
176  // Create name
177  std::string name = this->baseName +
178  Ogre::StringConverter::toString(_permutation) + "_ps";
179 
180  // Create shader object
181  Ogre::HighLevelGpuProgramPtr ptrProgram =
182  Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(
183  name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
184  "glsl", Ogre::GPT_FRAGMENT_PROGRAM);
185 
186  ptrProgram->setSource(this->masterSource);
187 
188  // set up the preprocessor defines
189  // Important to do this before any call to get parameters,
190  // i.e. before the program gets loaded
191  // ptrProgram->setParameter("compile_arguments",
192  // this->GetPPDefines(_permutation));
193  ptrProgram->setParameter("preprocessor_defines",
194  this->GetPPDefines(_permutation));
195 
196  this->SetUpBaseParameters(ptrProgram->getDefaultParameters());
197 
198  ptrProgram->getDefaultParameters()->setNamedConstant(
199  "tex0", static_cast<int>(0));
200 
201  // TODO: This requires a test for Deferred Shading. Deferred
202  // Lighting does not use this
203  if (ptrProgram->getDefaultParameters()->_findNamedConstantDefinition(
204  "tex1"))
205  {
206  ptrProgram->getDefaultParameters()->setNamedConstant(
207  "tex1", static_cast<int>(1));
208  }
209 
210  if (_permutation &
212  {
213  ptrProgram->getDefaultParameters()->setNamedConstant(
214  "shadowTex", static_cast<int>(1));
215  }
216 
217  return Ogre::GpuProgramPtr(ptrProgram);
218  }
219 
221  public: virtual Ogre::MaterialPtr GenerateTemplateMaterial(
222  Perm _permutation)
223  {
224  std::string materialName = this->baseName;
225 
226  if (_permutation &
228  materialName += "Quad";
229  else
230  materialName += "Geometry";
231 
232  if (_permutation &
234  materialName += "Shadow";
235 
236  return Ogre::MaterialManager::getSingleton().getByName(
237  materialName);
238  }
239 
240  /*Ogre::String GetPPDefines(Perm permutation)
241  {
242  Ogre::String strPPD;
243 
244  //Get the type of light
245  Ogre::String lightType;
246  if (permutation & LightMaterialGenerator<techniquePolicy>::MI_POINT)
247  {
248  lightType = "POINT";
249  }
250  else if (permutation & LightMaterialGenerator<techniquePolicy>::MI_SPOTLIGHT)
251  {
252  lightType = "SPOT";
253  }
254  else if (permutation & LightMaterialGenerator<techniquePolicy>::MI_DIRECTIONAL)
255  {
256  lightType = "DIRECTIONAL";
257  }
258  else
259  {
260  assert(false && "Permutation must have a light type");
261  }
262  strPPD += "-DLIGHT_TYPE=LIGHT_" + lightType + " ";
263 
264  //Optional parameters
265  if (permutation & LightMaterialGenerator<techniquePolicy>::MI_SPECULAR)
266  {
267  strPPD += "-DIS_SPECULAR ";
268  }
269  if (permutation & LightMaterialGenerator<techniquePolicy>::MI_ATTENUATED)
270  {
271  strPPD += "-DIS_ATTENUATED ";
272  }
273  if (permutation & LightMaterialGenerator<techniquePolicy>::MI_SHADOW_CASTER)
274  {
275  strPPD += "-DIS_SHADOW_CASTER ";
276  }
277  if(this->UseMaterialProperties()){
278  strPPD += "-DUSE_MAT_PROPERTIES ";
279  }
280 
281  return strPPD;
282  }*/
284  // Utility method
285  protected: std::string GetPPDefines(Perm _permutation)
286  {
287  std::string strPPD;
288 
289  // Get the type of light
290  std::string lightType;
291  if (_permutation &
293  {
294  lightType = "POINT";
295  }
296  else if (_permutation &
298  {
299  lightType = "SPOT";
300  }
301  else if (_permutation &
303  {
304  lightType = "DIRECTIONAL";
305  }
306  else
307  gzthrow("Permutation must have a light type");
308 
309  strPPD += "LIGHT_TYPE=LIGHT_" + lightType + ";";
310 
311  // Optional parameters
312  if (_permutation &
314  {
315  strPPD += "IS_SPECULAR=1;";
316  }
317 
318  if (_permutation &
320  {
321  strPPD += "IS_ATTENUATED=1;";
322  }
323 
324  if (_permutation &
326  {
327  strPPD += "IS_SHADOW_CASTER=1;";
328  }
329 
330  if (this->UseMaterialProperties())
331  strPPD += "USE_MAT_PROPERTIES=1;";
332 
333  return strPPD;
334  }
335 
337  protected: void SetUpBaseParameters(
338  const Ogre::GpuProgramParametersSharedPtr &_params)
339  {
340  if (_params.isNull())
341  gzthrow("Params is null");
342 
343  struct AutoParamPair
344  {
345  std::string name;
346  Ogre::GpuProgramParameters::AutoConstantType type;
347  };
348 
349  // A list of auto params that might be present in the
350  // shaders generated
351  static const AutoParamPair AUTO_PARAMS[] =
352  {
353  {"vpWidth",
354  Ogre::GpuProgramParameters::ACT_VIEWPORT_WIDTH},
355  {"vpHeight",
356  Ogre::GpuProgramParameters::ACT_VIEWPORT_HEIGHT},
357  {"worldView",
358  Ogre::GpuProgramParameters::ACT_WORLDVIEW_MATRIX},
359  {"invProj",
360  Ogre::GpuProgramParameters::ACT_INVERSE_PROJECTION_MATRIX},
361  {"invView",
362  Ogre::GpuProgramParameters::ACT_INVERSE_VIEW_MATRIX},
363  {"flip",
364  Ogre::GpuProgramParameters::ACT_RENDER_TARGET_FLIPPING},
365  {"lightDiffuseColor",
366  Ogre::GpuProgramParameters::ACT_LIGHT_DIFFUSE_COLOUR},
367  {"lightSpecularColor",
368  Ogre::GpuProgramParameters::ACT_LIGHT_SPECULAR_COLOUR},
369  {"lightFalloff",
370  Ogre::GpuProgramParameters::ACT_LIGHT_ATTENUATION},
371  {"lightPos",
372  Ogre::GpuProgramParameters::ACT_LIGHT_POSITION_VIEW_SPACE},
373  {"lightDir",
374  Ogre::GpuProgramParameters::ACT_LIGHT_DIRECTION_VIEW_SPACE},
375  {"spotParams",
376  Ogre::GpuProgramParameters::ACT_SPOTLIGHT_PARAMS},
377  {"farClipDistance",
378  Ogre::GpuProgramParameters::ACT_FAR_CLIP_DISTANCE},
379  {"shadowViewProjMat",
380  Ogre::GpuProgramParameters::ACT_TEXTURE_VIEWPROJ_MATRIX}
381  };
382 
383  int numParams = sizeof(AUTO_PARAMS) / sizeof(AutoParamPair);
384 
385  for (int i = 0; i < numParams; ++i)
386  {
387  if (_params->_findNamedConstantDefinition(AUTO_PARAMS[i].name))
388  {
389  _params->setNamedAutoConstant(
390  AUTO_PARAMS[i].name, AUTO_PARAMS[i].type);
391  }
392  }
393  }
394 
395  protected: std::string baseName;
396  protected: std::string masterSource;
397  };
398  }
399 }
400 #endif
std::string masterSource
Definition: LightMaterialGenerator.hh:396
std::string baseName
Definition: LightMaterialGenerator.hh:395
Definition: LightMaterialGenerator.hh:53
LightMaterialGenerator()
Definition: LightMaterialGenerator.hh:59
virtual Ogre::GpuProgramPtr GenerateVertexShader(Perm _permutation)
Definition: LightMaterialGenerator.hh:90
Definition: LightMaterialGenerator.hh:42
void SetUpBaseParameters(const Ogre::GpuProgramParametersSharedPtr &_params)
Definition: LightMaterialGenerator.hh:337
virtual Ogre::GpuProgramPtr GenerateFragmentShader(Perm _permutation)
Definition: LightMaterialGenerator.hh:156
#define gzthrow(msg)
This macro logs an error to the throw stream and throws an exception that contains the file name and ...
Definition: Exception.hh:39
MaterialGenerator::Perm Perm
Definition: LightMaterialGenerator.hh:80
std::string GetPPDefines(Perm _permutation)
Definition: LightMaterialGenerator.hh:285
Definition: LightMaterialGenerator.hh:45
LightMaterialGeneratorGLSL(const std::string &_baseName)
Definition: LightMaterialGenerator.hh:83
Caching, on-the-fly material generator.
Definition: MaterialGenerator.hh:39
virtual Ogre::MaterialPtr GenerateTemplateMaterial(Perm _permutation)
Definition: LightMaterialGenerator.hh:221
Definition: LightMaterialGenerator.hh:56
MaterialID
Permutation of light materials.
Definition: LightMaterialGenerator.hh:49
virtual ~LightMaterialGenerator()
Definition: LightMaterialGenerator.hh:72
virtual ~LightMaterialGeneratorGLSL()
Definition: LightMaterialGenerator.hh:87
#define GAZEBO_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system.hh:48
uint32_t Perm
Bitfield used to signify a material permutations.
Definition: MaterialGenerator.hh:42