opensurgsim
OsgUniform-inl.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
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 #ifndef SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
17 #define SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
18 
24 
25 
26 namespace SurgSim
27 {
28 
29 namespace Graphics
30 {
31 
37 template <typename T>
38 const T& toOsg(const T& value)
39 {
40  return value;
41 }
42 
43 template <class T>
44 OsgUniform<T>::OsgUniform(const std::string& name) :
45  UniformBase(), Uniform<T>(), OsgUniformBase(name)
46 {
47  osg::Uniform::Type osgUniformType = getOsgUniformType<T>();
48  SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
49  SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
50  m_uniform->setNumElements(1);
51 }
52 
53 template <class T>
54 void OsgUniform<T>::set(const T& value)
55 {
56  SURGSIM_ASSERT(m_uniform->set(toOsg(value))) << "Failed to set OSG uniform value!" <<
57  " Uniform: " << getName() << " value: " << value;
58  m_value = value;
59 }
60 
61 template <class T>
62 void OsgUniform<T>::set(const YAML::Node& node)
63 {
64  set(node.as<T>());
65 }
66 
67 template <class T>
68 const T& OsgUniform<T>::get() const
69 {
70  return m_value;
71 }
72 
73 template <class T>
74 const std::string OsgUniform<T>::getGlslType() const
75 {
76  return SurgSim::Graphics::getGlslType<T>();
77 }
78 
79 template <class T>
80 OsgUniform<std::vector<T>>::OsgUniform(const std::string& name, size_t numElements) :
81  UniformBase(), Uniform<std::vector<T>>(), OsgUniformBase(name)
82 {
83  osg::Uniform::Type osgUniformType = getOsgUniformType<T>();
84  SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
85  SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
86  m_value.resize(numElements);
87  m_uniform->setNumElements(numElements);
88 }
89 
90 template <class T>
91 size_t OsgUniform<std::vector<T>>::getNumElements() const
92 {
93  return m_uniform->getNumElements();
94 }
95 
96 template <class T>
97 void OsgUniform<std::vector<T>>::setElement(size_t index, const T& value)
98 {
99  SURGSIM_ASSERT(m_uniform->setElement(index, toOsg(value))) << "Failed to set OSG uniform value!" <<
100  " Uniform: " << getName() << " index: " << index << " value: " << value;
101  m_value[index] = value;
102 }
103 
104 template <class T>
105 void OsgUniform<std::vector<T>>::set(const std::vector<T>& value)
106 {
107  SURGSIM_ASSERT(value.size() == m_uniform->getNumElements()) <<
108  "Number of elements (" << value.size() << ") must match uniform's number of elements (" <<
109  m_uniform->getNumElements() << ")! Uniform: " << getName();
110  for (size_t i = 0; i < value.size(); ++i)
111  {
112  setElement(i, value[i]);
113  }
114 }
115 
116 template <class T>
117 void OsgUniform<std::vector<T>>::set(const YAML::Node& node)
118 {
119  SURGSIM_ASSERT(node.IsSequence()) << "Yaml setter called on vector uniform with non-sequence yaml node.";
120  set(node.as<std::vector<T>>());
121 }
122 
123 template <class T>
124 typename std::vector<T>::const_reference OsgUniform<std::vector<T>>::getElement(size_t index) const
125 {
126  return m_value[index];
127 }
128 
129 template <class T>
130 const std::vector<T>& OsgUniform<std::vector<T>>::get() const
131 {
132  return m_value;
133 }
134 
135 template <class T>
136 const std::string OsgUniform<std::vector<T>>::getGlslType() const
137 {
138  auto baseType = SurgSim::Graphics::getGlslType<T>();
139  return baseType + "[]";
140 }
141 
142 
143 
144 }; // namespace Graphics
145 
146 }; // namespace SurgSim
147 
148 #endif // SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
osg::ref_ptr< osg::Uniform > m_uniform
OSG uniform node.
Definition: OsgUniformBase.h:78
Conversions to and from OSG types.
Definition: MockObjects.h:47
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
This contains a series of functions to encode and decode Eigen data structures to and from YAML nodes...
virtual const T & get() const
Definition: OsgUniform-inl.h:68
Functions to get the OSG uniform type enum for a given type.
OSG implementation of graphics uniform with a value of type T.
Definition: OsgCamera.h:46
const osg::Matrix2 toOsg(const Eigen::Matrix< float, 2, 2, MOpt > &matrix)
Convert a fixed-size 2x2 matrix of floats to OSG.
Definition: OsgMatrixConversions.h:56
Base OSG implementation of graphics uniforms.
Definition: OsgUniformBase.h:41
Functions to get the OSG uniform type enum for a given type.
virtual void set(const T &value)
Sets the value of the uniform.
Definition: OsgUniform-inl.h:54
Base class for a graphics uniform with a value of type T.
Definition: Uniform.h:32
The header that provides the assertion API.
OsgUniform(const std::string &name)
Constructor.
Definition: OsgUniform-inl.h:44
const std::string & getName() const
Returns the name used in shader code to access this uniform.
Definition: OsgUniformBase.h:45
Common base class for all graphics uniforms.
Definition: UniformBase.h:33