All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OsgTextureUniform-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_OSGTEXTUREUNIFORM_INL_H
17 #define SURGSIM_GRAPHICS_OSGTEXTUREUNIFORM_INL_H
18 
27 
28 namespace SurgSim
29 {
30 
31 namespace Graphics
32 {
33 
34 template <class T>
35 OsgTextureUniform<T>::OsgTextureUniform(const std::string& name) :
36  UniformBase(), Uniform<std::shared_ptr<T>>(), OsgUniformBase(name), m_unit(-1), m_minimumTextureUnit(0)
37 {
39  MinimumTextureUnit, getMinimumTextureUnit, setMinimumTextureUnit);
40 
41  osg::Uniform::Type osgUniformType = getOsgUniformType<std::shared_ptr<T>>();
42  SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
43  SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
44  m_uniform->setNumElements(1);
45 }
46 
47 template <class T>
48 void OsgTextureUniform<T>::set(const std::shared_ptr<T>& value)
49 {
50  m_texture = value;
51  if (m_stateset != nullptr)
52  {
53  m_stateset->setTextureAttributeAndModes(m_unit, m_texture->getOsgTexture(), osg::StateAttribute::ON);
54  }
55 }
56 
57 template <class T>
58 const std::shared_ptr<T>& OsgTextureUniform<T>::get() const
59 {
60  return m_texture;
61 }
62 
63 template <class T>
64 void OsgTextureUniform<T>::addToStateSet(osg::StateSet* stateSet)
65 {
66  SURGSIM_ASSERT(m_stateset == nullptr) << "Unexpected addToStateSet for OsgTextureUniform.";
67 
68  const osg::StateSet::TextureAttributeList& textures = stateSet->getTextureAttributeList();
69 
70  // Grab the smallest unit that is equal or higher than m_minimumTextureUnit
71  // and search through allocated units for free ones
72  int availableUnit = m_minimumTextureUnit;
73  if (textures.size() > m_minimumTextureUnit)
74  {
75  for (auto it = textures.begin() + m_minimumTextureUnit; it != textures.end(); ++it)
76  {
77  if (it->empty())
78  {
79  break;
80  }
81  availableUnit++;
82  }
83  }
84 
85  m_unit = availableUnit;
86 
87  SURGSIM_ASSERT(m_texture != nullptr) << "Tried to add this uniform without a valid Texture";
88  stateSet->setTextureAttributeAndModes(m_unit, m_texture->getOsgTexture(),
89  osg::StateAttribute::ON);
90  SURGSIM_ASSERT(m_uniform->set(static_cast<int>(m_unit))) << "Failed to set OSG texture uniform unit!" <<
91  " Uniform: " << getName() << " unit: " << m_unit;
92  stateSet->addUniform(m_uniform);
93  m_stateset = stateSet;
94 }
95 
96 template <class T>
97 void OsgTextureUniform<T>::removeFromStateSet(osg::StateSet* stateSet)
98 {
99  SURGSIM_ASSERT(m_stateset != stateSet) << "Unexpected Remove for OsgTextureUniform";
100  stateSet->removeTextureAttribute(m_unit, m_texture->getOsgTexture());
101  stateSet->removeUniform(m_uniform);
102  m_stateset = nullptr;
103 }
104 
105 template <class T>
107 {
108  SURGSIM_ASSERT(m_unit == -1) << "Can't set minimumTextureUnit after the unit has been assigned.";
109  m_minimumTextureUnit = unit;
110 }
111 
112 template <class T>
114 {
115  return m_minimumTextureUnit;
116 }
117 }; // namespace Graphics
118 
119 }; // namespace SurgSim
120 
121 #endif // SURGSIM_GRAPHICS_OSGTEXTUREUNIFORM_INL_H
osg::ref_ptr< osg::Uniform > m_uniform
OSG uniform node.
Definition: OsgUniformBase.h:65
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
virtual void set(const std::shared_ptr< T > &value)
Sets the value of the uniform.
Definition: OsgTextureUniform-inl.h:48
Functions to get the OSG uniform type enum for a given type.
Base OSG implementation of graphics uniforms.
Definition: OsgUniformBase.h:36
virtual void removeFromStateSet(osg::StateSet *stateSet)
Removes this uniform from the OSG state set.
Definition: OsgTextureUniform-inl.h:97
OsgTextureUniform(const std::string &name)
Constructor.
Definition: OsgTextureUniform-inl.h:35
void setMinimumTextureUnit(size_t unit)
This is the texture unit from where the search for a free texture unit will start.
Definition: OsgTextureUniform-inl.h:106
Base class for a graphics uniform with a value of type T.
Definition: Uniform.h:32
The header that provides the assertion API.
virtual const std::shared_ptr< T > & get() const
Returns the value of the uniform.
Definition: OsgTextureUniform-inl.h:58
OSG implementation of graphics uniform with a texture value.
Definition: OsgTextureUniform.h:30
virtual void addToStateSet(osg::StateSet *stateSet)
Adds this uniform to the OSG state set.
Definition: OsgTextureUniform-inl.h:64
#define SURGSIM_ADD_RW_PROPERTY(class, type, property, getter, setter)
A macro to register getter and setter for a property that is readable and writeable, order of getter and setter agrees with 'RW'.
Definition: Accessible.h:198
size_t getMinimumTextureUnit() const
Definition: OsgTextureUniform-inl.h:113
Common base class for all graphics uniforms.
Definition: UniformBase.h:33