OsgUniform-inl.h
Go to the documentation of this file.
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 
22 
23 namespace SurgSim
24 {
25 
26 namespace Graphics
27 {
28 
34 template <typename T>
35 const T& toOsg(const T& value)
36 {
37  return value;
38 }
39 
40 template <class T>
41 OsgUniform<T>::OsgUniform(const std::string& name) :
42  UniformBase(), Uniform<T>(), OsgUniformBase(name)
43 {
44  osg::Uniform::Type osgUniformType = getOsgUniformType<T>();
45  SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
46  SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
47  m_uniform->setNumElements(1);
48 }
49 
50 template <class T>
51 void OsgUniform<T>::set(const T& value)
52 {
53  SURGSIM_ASSERT(m_uniform->set(toOsg(value))) << "Failed to set OSG uniform value!" <<
54  " Uniform: " << getName() << " value: " << value;
55  m_value = value;
56 }
57 
58 template <class T>
59 const T& OsgUniform<T>::get() const
60 {
61  return m_value;
62 }
63 
64 template <class T>
65 OsgUniform<std::vector<T>>::OsgUniform(const std::string& name, size_t numElements) :
66  UniformBase(), Uniform<std::vector<T>>(), OsgUniformBase(name)
67 {
68  osg::Uniform::Type osgUniformType = getOsgUniformType<T>();
69  SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
70  SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
71  m_value.resize(numElements);
72  m_uniform->setNumElements(numElements);
73 }
74 
75 template <class T>
76 size_t OsgUniform<std::vector<T>>::getNumElements() const
77 {
78  return m_uniform->getNumElements();
79 }
80 
81 template <class T>
82 void OsgUniform<std::vector<T>>::setElement(size_t index, const T& value)
83 {
84  SURGSIM_ASSERT(m_uniform->setElement(index, toOsg(value))) << "Failed to set OSG uniform value!" <<
85  " Uniform: " << getName() << " index: " << index << " value: " << value;
86  m_value[index] = value;
87 }
88 
89 template <class T>
90 void OsgUniform<std::vector<T>>::set(const std::vector<T>& value)
91 {
92  SURGSIM_ASSERT(value.size() == m_uniform->getNumElements()) <<
93  "Number of elements (" << value.size() << ") must match uniform's number of elements (" <<
94  m_uniform->getNumElements() << ")! Uniform: " << getName();
95  for (size_t i = 0; i < value.size(); ++i)
96  {
97  setElement(i, value[i]);
98  }
99 }
100 
101 template <class T>
102 typename std::vector<T>::const_reference OsgUniform<std::vector<T>>::getElement(size_t index) const
103 {
104  return m_value[index];
105 }
106 
107 template <class T>
108 const std::vector<T>& OsgUniform<std::vector<T>>::get() const
109 {
110  return m_value;
111 }
112 
113 }; // namespace Graphics
114 
115 }; // namespace SurgSim
116 
117 #endif // SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
Definition: DriveElementFromInputBehavior.cpp:27
T m_value
Value of the uniform.
Definition: OsgUniform.h:46
osg::ref_ptr< osg::Uniform > m_uniform
OSG uniform node.
Definition: OsgUniformBase.h:65
Conversions to and from OSG types.
STL namespace.
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
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
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
virtual const T & get() const
Returns the value of the uniform.
Definition: OsgUniform-inl.h:59
Base OSG implementation of graphics uniforms.
Definition: OsgUniformBase.h:36
virtual void set(const T &value)
Sets the value of the uniform.
Definition: OsgUniform-inl.h:51
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:41
Common base class for all graphics uniforms.
Definition: UniformBase.h:33