From Michael Platings, introduced osg::State::applyModelViewMaitrx(const osg::Matrix& matrix) method and associated osg::State::_modelViewCache to enable osgText::Text3D to be refactored in away that avoids creating/destroying matrices

This commit is contained in:
Robert Osfield
2010-06-01 13:33:58 +00:00
parent 0cb839280f
commit 4a047f2a82
3 changed files with 45 additions and 27 deletions

View File

@@ -191,6 +191,7 @@ class OSG_EXPORT State : public Referenced, public Observer
inline const osg::Matrix& getProjectionMatrix() const { return *_projection; }
void applyModelViewMatrix(const osg::RefMatrix* matrix);
void applyModelViewMatrix(const osg::Matrix&);
const osg::Matrix& getModelViewMatrix() const { return *_modelView; }
@@ -1344,6 +1345,7 @@ class OSG_EXPORT State : public Referenced, public Observer
ref_ptr<const RefMatrix> _initialViewMatrix;
ref_ptr<const RefMatrix> _projection;
ref_ptr<const RefMatrix> _modelView;
ref_ptr<RefMatrix> _modelViewCache;
bool _useModelViewAndProjectionUniforms;
ref_ptr<Uniform> _modelViewMatrixUniform;
@@ -1647,6 +1649,8 @@ class OSG_EXPORT State : public Referenced, public Observer
bool getLastAppliedMode(const ModeMap& modeMap,StateAttribute::GLMode mode) const;
const StateAttribute* getLastAppliedAttribute(const AttributeMap& attributeMap,StateAttribute::Type type, unsigned int member) const;
void loadModelViewMatrix();
mutable bool _isSecondaryColorSupportResolved;
mutable bool _isSecondaryColorSupported;

View File

@@ -45,6 +45,7 @@ State::State():
_initialViewMatrix = _identity;
_projection = _identity;
_modelView = _identity;
_modelViewCache = new osg::RefMatrix;
#if !defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)
_useModelViewAndProjectionUniforms = true;
@@ -1296,6 +1297,19 @@ void State::applyProjectionMatrix(const osg::RefMatrix* matrix)
}
}
void State::loadModelViewMatrix()
{
if (_useModelViewAndProjectionUniforms)
{
if (_modelViewMatrixUniform.valid()) _modelViewMatrixUniform->set(*_modelView);
updateModelViewAndProjectionMatrixUniforms();
}
#ifdef OSG_GL_MATRICES_AVAILABLE
glLoadMatrix(_modelView->ptr());
#endif
}
void State::applyModelViewMatrix(const osg::RefMatrix* matrix)
{
if (_modelView!=matrix)
@@ -1309,18 +1323,18 @@ void State::applyModelViewMatrix(const osg::RefMatrix* matrix)
_modelView=_identity;
}
if (_useModelViewAndProjectionUniforms)
{
if (_modelViewMatrixUniform.valid()) _modelViewMatrixUniform->set(*_modelView);
updateModelViewAndProjectionMatrixUniforms();
}
#ifdef OSG_GL_MATRICES_AVAILABLE
glLoadMatrix(_modelView->ptr());
#endif
loadModelViewMatrix();
}
}
void State::applyModelViewMatrix(const osg::Matrix& matrix)
{
_modelViewCache->set(matrix);
_modelView = _modelViewCache;
loadModelViewMatrix();
}
#include <osg/io_utils>
void State::updateModelViewAndProjectionMatrixUniforms()

View File

@@ -505,16 +505,16 @@ void Text3D::drawImplementation(osg::RenderInfo& renderInfo) const
// ** save the previous modelview matrix
osg::ref_ptr<osg::RefMatrix> previous(new osg::RefMatrix(state.getModelViewMatrix()));
osg::Matrix previous(state.getModelViewMatrix());
// ** get the modelview for this context
osg::ref_ptr<osg::RefMatrix> modelview(new osg::RefMatrix(_autoTransformCache[contextID]._matrix));
osg::Matrix modelview(_autoTransformCache[contextID]._matrix);
// ** mult previous by the modelview for this context
modelview->postMult(*previous.get());
modelview.postMult(previous);
// ** apply this new modelview matrix
state.applyModelViewMatrix(modelview.get());
state.applyModelViewMatrix(modelview);
osg::GLBeginEndAdapter& gl = (state.getGLBeginEndAdapter());
@@ -603,7 +603,7 @@ void Text3D::drawImplementation(osg::RenderInfo& renderInfo) const
// restore the previous modelview matrix
state.applyModelViewMatrix(previous.get());
state.applyModelViewMatrix(previous);
}
void Text3D::renderPerGlyph(osg::State & state) const
@@ -619,9 +619,9 @@ void Text3D::renderPerGlyph(osg::State & state) const
for (it = itLine->begin(); it!=end; ++it)
{
osg::ref_ptr<osg::RefMatrix> matrix = new osg::RefMatrix(original_modelview);
matrix->preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
state.applyModelViewMatrix(matrix.get());
osg::Matrix matrix(original_modelview);
matrix.preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
state.applyModelViewMatrix(matrix);
// ** apply the vertex array
state.setVertexPointer(it->_glyph->getVertexArray());
@@ -670,9 +670,9 @@ void Text3D::renderPerFace(osg::State & state) const
LineRenderInfo::const_iterator it, end = itLine->end();
for (it = itLine->begin(); it!=end; ++it)
{
osg::ref_ptr<osg::RefMatrix> matrix = new osg::RefMatrix(original_modelview);
matrix->preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
state.applyModelViewMatrix(matrix.get());
osg::Matrix matrix(original_modelview);
matrix.preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
state.applyModelViewMatrix(matrix);
state.setVertexPointer(it->_glyph->getVertexArray());
@@ -693,9 +693,9 @@ void Text3D::renderPerFace(osg::State & state) const
LineRenderInfo::const_iterator it, end = itLine->end();
for (it = itLine->begin(); it!=end; ++it)
{
osg::ref_ptr<osg::RefMatrix> matrix = new osg::RefMatrix(original_modelview);
matrix->preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
state.applyModelViewMatrix(matrix.get());
osg::Matrix matrix(original_modelview);
matrix.preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
state.applyModelViewMatrix(matrix);
state.setVertexPointer(it->_glyph->getVertexArray());
state.setNormalPointer(it->_glyph->getNormalArray());
@@ -707,7 +707,7 @@ void Text3D::renderPerFace(osg::State & state) const
}
}
}
state.disableNormalPointer();
// ** render all back face of the text
state.Normal(0.0f,0.0f,-1.0f);
@@ -718,9 +718,9 @@ void Text3D::renderPerFace(osg::State & state) const
LineRenderInfo::const_iterator it, end = itLine->end();
for (it = itLine->begin(); it!=end; ++it)
{
osg::ref_ptr<osg::RefMatrix> matrix = new osg::RefMatrix(state.getModelViewMatrix());
matrix->preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
state.applyModelViewMatrix(matrix.get());
osg::Matrix matrix(original_modelview);
matrix.preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
state.applyModelViewMatrix(matrix);
state.setVertexPointer(it->_glyph->getVertexArray());