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 
19 #include <osg/PointSprite>
20 
29 
30 namespace SurgSim
31 {
32 
33 namespace Graphics
34 {
35 
36 template <class T>
38  UniformBase(), Uniform<std::shared_ptr<T>>(), OsgUniformBase(name), m_unit(-1), m_minimumTextureUnit(0)
39 {
41  MinimumTextureUnit, getMinimumTextureUnit, setMinimumTextureUnit);
42 
43  osg::Uniform::Type osgUniformType = getOsgUniformType<std::shared_ptr<T>>();
44  SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
45  SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
46  m_uniform->setNumElements(1);
47 }
48 
49 template <class T>
50 void OsgTextureUniform<T>::set(const std::shared_ptr<T>& value)
51 {
52  m_texture = value;
53  if (m_stateset != nullptr)
54  {
55  m_stateset->setTextureAttributeAndModes(m_unit, m_texture->getOsgTexture(), osg::StateAttribute::ON);
56  }
57 }
58 
59 template <class T>
60 void OsgTextureUniform<T>::set(const YAML::Node& value)
61 {
62  m_unit = value.as<int>();
63 }
64 
65 template <class T>
66 const std::shared_ptr<T>& OsgTextureUniform<T>::get() const
67 {
68  return m_texture;
69 }
70 
71 template <class T>
72 void OsgTextureUniform<T>::addToStateSet(osg::StateSet* stateSet)
73 {
74  SURGSIM_ASSERT(m_stateset == nullptr) << "Unexpected addToStateSet for OsgTextureUniform " << getName() << ".";
75 
76  const osg::StateSet::TextureAttributeList& textures = stateSet->getTextureAttributeList();
77 
78  // Grab the smallest unit that is equal or higher than m_minimumTextureUnit
79  // and search through allocated units for free ones
80  int availableUnit = m_minimumTextureUnit;
81  if (textures.size() > m_minimumTextureUnit)
82  {
83  for (auto it = textures.begin() + m_minimumTextureUnit; it != textures.end(); ++it)
84  {
85  if (it->empty())
86  {
87  break;
88  }
89  availableUnit++;
90  }
91  }
92 
93  m_unit = availableUnit;
94 
95  SURGSIM_ASSERT(m_texture != nullptr) << "Tried to add uniform " << getName() << " without a valid Texture";
96  if(m_texture->isPointSprite())
97  {
98  osg::PointSprite* sprite = new osg::PointSprite();
99  stateSet->setTextureAttributeAndModes(m_unit, sprite, osg::StateAttribute::ON);
100  stateSet->setMode(GL_VERTEX_PROGRAM_POINT_SIZE, osg::StateAttribute::ON);
101  }
102  stateSet->setTextureAttributeAndModes(m_unit, m_texture->getOsgTexture(),
103  osg::StateAttribute::ON);
104  SURGSIM_ASSERT(m_uniform->set(static_cast<int>(m_unit))) << "Failed to set OSG texture uniform unit!" <<
105  " Uniform: " << getName() << " unit: " << m_unit;
106  stateSet->addUniform(m_uniform);
107  m_stateset = stateSet;
108 }
109 
110 template <class T>
111 void OsgTextureUniform<T>::removeFromStateSet(osg::StateSet* stateSet)
112 {
113  SURGSIM_ASSERT(m_stateset != stateSet) << "Unexpected Remove for OsgTextureUniform";
114  stateSet->removeTextureAttribute(m_unit, m_texture->getOsgTexture());
115  stateSet->removeUniform(m_uniform);
116  m_stateset = nullptr;
117 }
118 
119 template <class T>
121 {
122  SURGSIM_ASSERT(m_unit == -1) << "Can't set minimumTextureUnit after the unit has been assigned.";
123  m_minimumTextureUnit = unit;
124 }
125 
126 template <class T>
128 {
129  return m_minimumTextureUnit;
130 }
131 }; // namespace Graphics
132 
133 }; // namespace SurgSim
134 
135 #endif // SURGSIM_GRAPHICS_OSGTEXTUREUNIFORM_INL_H
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
SURGSIM_ADD_RW_PROPERTY
#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,...
Definition: Accessible.h:213
SurgSim::Graphics::OsgUniformBase
Base OSG implementation of graphics uniforms.
Definition: OsgUniformBase.h:41
SurgSim::Graphics::OsgTextureUniform::OsgTextureUniform
OsgTextureUniform(const std::string &name)
Constructor.
Definition: OsgTextureUniform-inl.h:37
Assert.h
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
OsgTexture2d.h
OsgTexture.h
set
set(SURGSIM_COLLISION_SOURCES BoxCapsuleContact.cpp BoxDoubleSidedPlaneContact.cpp BoxPlaneContact.cpp BoxSphereContact.cpp CapsuleSphereContact.cpp CollisionPair.cpp CompoundShapeContact.cpp ContactCalculation.cpp ContactFilter.cpp DefaultContactCalculation.cpp ElementContactFilter.cpp OctreeCapsuleContact.cpp OctreeContact.cpp OctreeDoubleSidedPlaneContact.cpp OctreePlaneContact.cpp OctreeSphereContact.cpp Representation.cpp SegmentMeshTriangleMeshContact.cpp SegmentSegmentCcdIntervalCheck.cpp SegmentSegmentCcdMovingContact.cpp SegmentSegmentCcdStaticContact.cpp SegmentSelfContact.cpp ShapeCollisionRepresentation.cpp SphereDoubleSidedPlaneContact.cpp SpherePlaneContact.cpp SphereSphereContact.cpp TriangleMeshParticlesContact.cpp TriangleMeshPlaneContact.cpp TriangleMeshSurfaceMeshContact.cpp TriangleMeshTriangleMeshContact.cpp) set(SURGSIM_COLLISION_HEADERS BoxCapsuleContact.h BoxDoubleSidedPlaneContact.h BoxPlaneContact.h BoxSphereContact.h CapsuleSphereContact.h CcdDcdCollision.h CollisionPair.h CompoundShapeContact.h ContactCalculation.h ContactFilter.h DefaultContactCalculation.h ElementContactFilter.h OctreeCapsuleContact.h OctreeContact.h OctreeDoubleSidedPlaneContact.h OctreePlaneContact.h OctreeSphereContact.h Representation.h SegmentMeshTriangleMeshContact.h SegmentSegmentCcdIntervalCheck.h SegmentSegmentCcdMovingContact.h SegmentSegmentCcdStaticContact.h SegmentSelfContact.h ShapeCollisionRepresentation.h ShapeShapeContactCalculation.h SphereDoubleSidedPlaneContact.h SpherePlaneContact.h SphereSphereContact.h TriangleMeshParticlesContact.h TriangleMeshPlaneContact.h TriangleMeshSurfaceMeshContact.h TriangleMeshTriangleMeshContact.h) surgsim_create_library_header(Collision.h "$
Definition: CMakeLists.txt:16
SurgSim::Graphics::UniformBase
Common base class for all graphics uniforms.
Definition: UniformBase.h:33
OsgUniformTypes.h
OsgTextureRectangle.h
OsgTexture1d.h
string
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38
OsgTextureCubeMap.h
SurgSim::Graphics::OsgTextureUniform
OSG implementation of graphics uniform with a texture value.
Definition: OsgTextureUniform.h:30
SurgSim::Graphics::Uniform
Base class for a graphics uniform with a value of type T.
Definition: Uniform.h:32
OsgTexture3d.h