Gazebo.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 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 
18 #include <array>
19 #include <iostream>
20 #include <iterator>
21 #include <string>
22 
23 #include <gazebo/gazebo.hh>
24 #include <gazebo/common/common.hh>
25 #include <gazebo/rendering/rendering.hh>
26 
28 
29 namespace gazebo
30 {
31  namespace rendering
32  {
34  Visual& _visual,
35  const std::string &_paramName,
36  const std::string &_shaderType,
37  const std::string &_value)
38  {
39  // currently only vertex and fragment shaders are supported
40  if (_shaderType != "vertex" && _shaderType != "fragment")
41  {
42  gzerr << "Shader type: '" << _shaderType << "' is not supported"
43  << std::endl;
44  return;
45  }
46 
47  // set the parameter based name and type defined in material script
48  // and shaders
49  auto setNamedParam = [](Ogre::GpuProgramParametersSharedPtr _params,
50  const std::string &_name, const std::string &_v)
51  {
52  auto paramDef = _params->_findNamedConstantDefinition(_name);
53  if (!paramDef)
54  return;
55 
56  switch (paramDef->constType)
57  {
58  case Ogre::GCT_INT1:
59  {
60  int value = Ogre::StringConverter::parseInt(_v);
61  _params->setNamedConstant(_name, value);
62  break;
63  }
64  case Ogre::GCT_FLOAT1:
65  {
66  Ogre::Real value = Ogre::StringConverter::parseReal(_v);
67  _params->setNamedConstant(_name, value);
68  break;
69  }
70  #if (OGRE_VERSION >= ((1 << 16) | (9 << 8) | 0))
71  case Ogre::GCT_INT2:
72  case Ogre::GCT_FLOAT2:
73  {
74  Ogre::Vector2 value = Ogre::StringConverter::parseVector2(_v);
75  _params->setNamedConstant(_name, value);
76  break;
77  }
78  #endif
79  case Ogre::GCT_INT3:
80  case Ogre::GCT_FLOAT3:
81  {
82  Ogre::Vector3 value = Ogre::StringConverter::parseVector3(_v);
83  _params->setNamedConstant(_name, value);
84  break;
85  }
86  case Ogre::GCT_INT4:
87  case Ogre::GCT_FLOAT4:
88  {
89  Ogre::Vector4 value = Ogre::StringConverter::parseVector4(_v);
90  _params->setNamedConstant(_name, value);
91  break;
92  }
93  case Ogre::GCT_MATRIX_4X4:
94  {
95  Ogre::Matrix4 value = Ogre::StringConverter::parseMatrix4(_v);
96  _params->setNamedConstant(_name, value);
97  break;
98  }
99  default:
100  break;
101  }
102  };
103 
104  // loop through material techniques and passes to find the param
105  Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().getByName(
106  _visual.GetMaterialName());
107  if (mat.isNull())
108  {
109  gzerr << "Failed to find material: '" << _visual.GetMaterialName()
110  << std::endl;
111  return;
112  }
113  for (unsigned int i = 0; i < mat->getNumTechniques(); ++i)
114  {
115  Ogre::Technique *technique = mat->getTechnique(i);
116  if (!technique)
117  continue;
118  for (unsigned int j = 0; j < technique->getNumPasses(); ++j)
119  {
120  Ogre::Pass *pass = technique->getPass(j);
121  if (!pass)
122  continue;
123 
124  // check if pass is programmable, ie if they are using shaders
125  if (!pass->isProgrammable())
126  continue;
127 
128  if (_shaderType == "vertex" && pass->hasVertexProgram())
129  {
130  setNamedParam(pass->getVertexProgramParameters(), \
131  _paramName, _value);
132  }
133  else if (_shaderType == "fragment" && pass->hasFragmentProgram())
134  {
135  setNamedParam(pass->getFragmentProgramParameters(), \
136  _paramName, _value);
137  }
138  else
139  {
140  gzerr << "Failed to retrieve shaders for material: '"
141  << _visual.GetMaterialName() << "', technique: '"
142  << technique->getName() << "', pass: '"
143  << pass->getName() << "'"
144  << std::endl;
145  continue;
146  }
147  }
148  }
149  }
150  }
151 }
Support for methods not available in legacy versions of Gazebo.
void SetMaterialShaderParam(Visual &_visual, const std::string &_paramName, const std::string &_shaderType, const std::string &_value)
Set a shader program parameter associated to this visual&#39;s material.
Definition: Gazebo.cc:33


wave_gazebo_plugins
Author(s): Rhys Mainwaring
autogenerated on Thu May 7 2020 03:54:44