root/OpenSceneGraph/trunk/src/osgUtil/GLObjectsVisitor.cpp @ 10859

Revision 10859, 5.4 kB (checked in by robert, 5 years ago)

Re-order the setting of the VBO's

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <osgUtil/GLObjectsVisitor>
14
15#include <osg/Drawable>
16#include <osg/Notify>
17#include <osg/Timer>
18
19namespace osgUtil
20{
21
22/////////////////////////////////////////////////////////////////
23//
24// GLObjectsVisitor
25//
26GLObjectsVisitor::GLObjectsVisitor(Mode mode)
27{
28    setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN);
29
30    _mode = mode;
31
32}
33
34
35void GLObjectsVisitor::apply(osg::Node& node)
36{
37    if (node.getStateSet())
38    {
39        apply(*(node.getStateSet()));
40    }
41
42    traverse(node);
43}
44
45void GLObjectsVisitor::apply(osg::Geode& node)
46{
47    if (node.getStateSet())
48    {
49        apply(*(node.getStateSet()));
50    }
51
52    for(unsigned int i=0;i<node.getNumDrawables();++i)
53    {
54        osg::Drawable* drawable = node.getDrawable(i);
55        if (drawable)
56        {
57            apply(*drawable);
58            if (drawable->getStateSet())
59            {
60                apply(*(drawable->getStateSet()));
61            }
62        }
63    }
64}
65
66void GLObjectsVisitor::apply(osg::Drawable& drawable)
67{
68    if (_drawablesAppliedSet.count(&drawable)!=0) return;
69   
70    _drawablesAppliedSet.insert(&drawable);
71
72    if (_mode&SWITCH_OFF_DISPLAY_LISTS)
73    {
74        drawable.setUseDisplayList(false);
75    }
76
77    if (_mode&SWITCH_ON_DISPLAY_LISTS)
78    {
79        drawable.setUseDisplayList(true);
80    }
81
82    if (_mode&SWITCH_ON_VERTEX_BUFFER_OBJECTS)
83    {
84        drawable.setUseVertexBufferObjects(true);
85    }
86
87    if (_mode&SWITCH_OFF_VERTEX_BUFFER_OBJECTS)
88    {
89        drawable.setUseVertexBufferObjects(false);
90    }
91
92    if (_mode&COMPILE_DISPLAY_LISTS && _renderInfo.getState())
93    {
94        drawable.compileGLObjects(_renderInfo);
95    }
96
97    if (_mode&RELEASE_DISPLAY_LISTS)
98    {
99        drawable.releaseGLObjects(_renderInfo.getState());
100    }
101}
102
103void GLObjectsVisitor::apply(osg::StateSet& stateset)
104{
105    if (_stateSetAppliedSet.count(&stateset)!=0) return;
106   
107    _stateSetAppliedSet.insert(&stateset);
108
109    if (_mode & COMPILE_STATE_ATTRIBUTES && _renderInfo.getState())
110    {
111        stateset.compileGLObjects(*_renderInfo.getState());
112       
113        osg::Program* program = dynamic_cast<osg::Program*>(stateset.getAttribute(osg::StateAttribute::PROGRAM));
114        if (program) {
115            if( program->isFixedFunction() )
116                _lastCompiledProgram = NULL; // It does not make sense to apply uniforms on fixed pipe
117            else 
118                _lastCompiledProgram = program;
119        }
120
121        if (_lastCompiledProgram.valid() && !stateset.getUniformList().empty())
122        {
123            osg::Program::PerContextProgram* pcp = _lastCompiledProgram->getPCP(_renderInfo.getState()->getContextID());
124            if (pcp)
125            {
126                pcp->useProgram();
127               
128                _renderInfo.getState()->setLastAppliedProgramObject(pcp);
129           
130                osg::StateSet::UniformList& ul = stateset.getUniformList();
131                for(osg::StateSet::UniformList::iterator itr = ul.begin();
132                    itr != ul.end();
133                    ++itr)
134                {
135                    pcp->apply(*(itr->second.first));
136                }
137            }
138        }
139        else if(_renderInfo.getState()->getLastAppliedProgramObject()){
140                           
141            osg::GL2Extensions* extensions = osg::GL2Extensions::Get(_renderInfo.getState()->getContextID(), true);
142            extensions->glUseProgram(0);
143            _renderInfo.getState()->setLastAppliedProgramObject(0);
144        }
145       
146    }
147
148    if (_mode & RELEASE_STATE_ATTRIBUTES)
149    {
150        stateset.releaseGLObjects(_renderInfo.getState());
151    }
152   
153    if (_mode & CHECK_BLACK_LISTED_MODES)
154    {
155        stateset.checkValidityOfAssociatedModes(*_renderInfo.getState());
156    }
157}
158
159/////////////////////////////////////////////////////////////////
160//
161// GLObjectsVisitor
162//
163
164GLObjectsOperation::GLObjectsOperation(GLObjectsVisitor::Mode mode):
165    osg::GraphicsOperation("GLObjectOperation",false),
166    _mode(mode)
167{
168}
169
170GLObjectsOperation::GLObjectsOperation(osg::Node* subgraph, GLObjectsVisitor::Mode mode):
171    osg::GraphicsOperation("GLObjectOperation",false),
172    _subgraph(subgraph),
173    _mode(mode)
174{
175}
176
177void GLObjectsOperation::operator () (osg::GraphicsContext* context)
178{
179    GLObjectsVisitor glObjectsVisitor(_mode);
180   
181    context->getState()->initializeExtensionProcs();
182
183    glObjectsVisitor.setState(context->getState());
184   
185    // osg::notify(osg::NOTICE)<<"GLObjectsOperation::before <<<<<<<<<<<"<<std::endl;
186    if (_subgraph.valid())
187    {
188        _subgraph->accept(glObjectsVisitor);
189    }
190    else
191    {
192        for(osg::GraphicsContext::Cameras::iterator itr = context->getCameras().begin();
193            itr != context->getCameras().end();
194            ++itr)
195        {
196            (*itr)->accept(glObjectsVisitor);
197        }
198    }
199    // osg::notify(osg::NOTICE)<<"GLObjectsOperation::after >>>>>>>>>>> "<<std::endl;
200}
201
202
203} // end of namespace osgUtil
Note: See TracBrowser for help on using the browser.