OsgUniform-inl.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
#define SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
#include "SurgSim/Framework/Assert.h"
#include "SurgSim/Math/MathConvert.h"
#include "SurgSim/Graphics/OsgConversions.h"
#include "SurgSim/Graphics/OsgUniformTypes.h"
namespace SurgSim
{
namespace Graphics
{
template <typename T>
const T& toOsg(const T& value)
{
return value;
}
template <class T>
OsgUniform<T>::OsgUniform(const std::string& name) :
UniformBase(), Uniform<T>(), OsgUniformBase(name)
{
osg::Uniform::Type osgUniformType = getOsgUniformType<T>();
SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
m_uniform->setNumElements(1);
}
template <class T>
void OsgUniform<T>::set(const T& value)
{
SURGSIM_ASSERT(m_uniform->set(toOsg(value))) << "Failed to set OSG uniform value!" <<
" Uniform: " << getName() << " value: " << value;
m_value = value;
}
template <class T>
void OsgUniform<T>::set(const YAML::Node& node)
{
set(node.as<T>());
}
template <class T>
const T& OsgUniform<T>::get() const
{
return m_value;
}
template <class T>
OsgUniform<std::vector<T>>::OsgUniform(const std::string& name, size_t numElements) :
UniformBase(), Uniform<std::vector<T>>(), OsgUniformBase(name)
{
osg::Uniform::Type osgUniformType = getOsgUniformType<T>();
SURGSIM_ASSERT(osgUniformType != osg::Uniform::UNDEFINED) << "Failed to get OSG uniform type!";
SURGSIM_ASSERT(m_uniform->setType(osgUniformType)) << "Failed to set OSG uniform type!";
m_value.resize(numElements);
m_uniform->setNumElements(numElements);
}
template <class T>
size_t OsgUniform<std::vector<T>>::getNumElements() const
{
return m_uniform->getNumElements();
}
template <class T>
void OsgUniform<std::vector<T>>::setElement(size_t index, const T& value)
{
SURGSIM_ASSERT(m_uniform->setElement(index, toOsg(value))) << "Failed to set OSG uniform value!" <<
" Uniform: " << getName() << " index: " << index << " value: " << value;
m_value[index] = value;
}
template <class T>
void OsgUniform<std::vector<T>>::set(const std::vector<T>& value)
{
SURGSIM_ASSERT(value.size() == m_uniform->getNumElements()) <<
"Number of elements (" << value.size() << ") must match uniform's number of elements (" <<
m_uniform->getNumElements() << ")! Uniform: " << getName();
for (size_t i = 0; i < value.size(); ++i)
{
setElement(i, value[i]);
}
}
template <class T>
void OsgUniform<std::vector<T>>::set(const YAML::Node& node)
{
SURGSIM_ASSERT(node.IsSequence()) << "Yaml setter called on vector uniform with non-sequence yaml node.";
set(node.as<std::vector<T>>());
}
template <class T>
typename std::vector<T>::const_reference OsgUniform<std::vector<T>>::getElement(size_t index) const
{
return m_value[index];
}
template <class T>
const std::vector<T>& OsgUniform<std::vector<T>>::get() const
{
return m_value;
}
}; // namespace Graphics
}; // namespace SurgSim
#endif // SURGSIM_GRAPHICS_OSGUNIFORM_INL_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Graphics::OsgUniform::m_value
T m_value
Value of the uniform.
Definition: OsgUniform.h:56
SurgSim::Graphics::OsgUniformBase::m_uniform
osg::ref_ptr< osg::Uniform > m_uniform
OSG uniform node.
Definition: OsgUniformBase.h:75
OsgConversions.h
Conversions to and from OSG types.
std
STL namespace.
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
MathConvert.h
This contains a series of functions to encode and decode Eigen data structures to and from YAML nodes...
SurgSim::Graphics::OsgUniform::get
virtual const T & get() const
Definition: OsgUniform-inl.h:67
SurgSim::Graphics::toOsg
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
OsgUniformTypes.h
Functions to get the OSG uniform type enum for a given type.
SurgSim::Graphics::OsgUniform
OSG implementation of graphics uniform with a value of type T.
Definition: OsgCamera.h:46
SurgSim::Graphics::OsgUniformBase
Base OSG implementation of graphics uniforms.
Definition: OsgUniformBase.h:41
SurgSim::Graphics::OsgUniform::set
virtual void set(const T &value)
Sets the value of the uniform.
Definition: OsgUniform-inl.h:53
SurgSim::Graphics::Uniform
Base class for a graphics uniform with a value of type T.
Definition: Uniform.h:32
Assert.h
The header that provides the assertion API.
SurgSim::Graphics::OsgUniform::OsgUniform
OsgUniform(const std::string &name)
Constructor.
Definition: OsgUniform-inl.h:43
SurgSim::Graphics::OsgUniformBase::getName
const std::string & getName() const
Returns the name used in shader code to access this uniform.
Definition: OsgUniformBase.h:45
SurgSim::Graphics::UniformBase
Common base class for all graphics uniforms.
Definition: UniformBase.h:33