OpenSceneGraph/src/osgUtil/GLObjectsVisitor.cpp

157 lines
4.1 KiB
C++
Raw Normal View History

2006-07-18 23:21:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgUtil/GLObjectsVisitor>
#include <osg/Drawable>
#include <osg/Notify>
2001-01-11 00:32:10 +08:00
using namespace osg;
using namespace osgUtil;
GLObjectsVisitor::GLObjectsVisitor(Mode mode)
2001-01-11 00:32:10 +08:00
{
setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN);
_mode = mode;
2001-01-11 00:32:10 +08:00
}
void GLObjectsVisitor::apply(osg::Node& node)
{
if (node.getStateSet())
{
apply(*(node.getStateSet()));
}
traverse(node);
}
void GLObjectsVisitor::apply(osg::Geode& node)
2001-01-11 00:32:10 +08:00
{
if (node.getStateSet())
2001-01-11 00:32:10 +08:00
{
apply(*(node.getStateSet()));
}
for(unsigned int i=0;i<node.getNumDrawables();++i)
{
Drawable* drawable = node.getDrawable(i);
if (drawable)
2001-01-11 00:32:10 +08:00
{
apply(*drawable);
if (drawable->getStateSet())
2001-01-11 00:32:10 +08:00
{
apply(*(drawable->getStateSet()));
2001-01-11 00:32:10 +08:00
}
}
}
}
void GLObjectsVisitor::apply(osg::Drawable& drawable)
{
if (_drawablesAppliedSet.count(&drawable)!=0) return;
_drawablesAppliedSet.insert(&drawable);
if (_mode&SWITCH_OFF_DISPLAY_LISTS)
{
drawable.setUseDisplayList(false);
}
if (_mode&SWITCH_ON_DISPLAY_LISTS)
{
drawable.setUseDisplayList(true);
}
2006-11-14 20:51:31 +08:00
if (_mode&COMPILE_DISPLAY_LISTS && _renderInfo.getState())
{
2006-11-14 20:51:31 +08:00
drawable.compileGLObjects(_renderInfo);
}
if (_mode&RELEASE_DISPLAY_LISTS)
{
2006-11-14 20:51:31 +08:00
drawable.releaseGLObjects(_renderInfo.getState());
}
if (_mode&SWITCH_ON_VERTEX_BUFFER_OBJECTS)
{
drawable.setUseVertexBufferObjects(true);
}
if (_mode&SWITCH_OFF_VERTEX_BUFFER_OBJECTS)
{
drawable.setUseVertexBufferObjects(false);
}
}
void GLObjectsVisitor::apply(osg::StateSet& stateset)
{
if (_stateSetAppliedSet.count(&stateset)!=0) return;
_stateSetAppliedSet.insert(&stateset);
2006-11-14 20:51:31 +08:00
if (_mode & COMPILE_STATE_ATTRIBUTES && _renderInfo.getState())
{
2006-11-14 20:51:31 +08:00
stateset.compileGLObjects(*_renderInfo.getState());
osg::Program* program = dynamic_cast<osg::Program*>(stateset.getAttribute(osg::StateAttribute::PROGRAM));
if (program) _lastCompiledProgram = program;
if (_lastCompiledProgram.valid() && !stateset.getUniformList().empty())
{
osg::Program::PerContextProgram* pcp = _lastCompiledProgram->getPCP(_renderInfo.getState()->getContextID());
if (pcp)
{
pcp->useProgram();
_renderInfo.getState()->setLastAppliedProgramObject(pcp);
osg::StateSet::UniformList& ul = stateset.getUniformList();
for(osg::StateSet::UniformList::iterator itr = ul.begin();
itr != ul.end();
++itr)
{
pcp->apply(*(itr->second.first));
}
}
}
}
if (_mode & RELEASE_STATE_ATTRIBUTES)
{
2006-11-14 20:51:31 +08:00
stateset.releaseGLObjects(_renderInfo.getState());
2001-01-11 00:32:10 +08:00
}
if (_mode & CHECK_BLACK_LISTED_MODES)
{
2006-11-14 20:51:31 +08:00
stateset.checkValidityOfAssociatedModes(*_renderInfo.getState());
}
2001-01-11 00:32:10 +08:00
}
GLObjectsOperation::GLObjectsOperation(osg::Node* subgraph, GLObjectsVisitor::Mode mode):
osg::GraphicsOperation("GLObjectOperation",false),
_subgraph(subgraph),
_mode(mode)
{
}
void GLObjectsOperation::operator () (osg::GraphicsContext* context)
{
GLObjectsVisitor glObjectsVisitor(_mode);
glObjectsVisitor.setState(context->getState());
_subgraph->accept(glObjectsVisitor);
}