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

Revision 13041, 5.4 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • 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        (drawable.getUseDisplayList() || drawable.getUseVertexBufferObjects()))
94    {
95        drawable.compileGLObjects(_renderInfo);
96    }
97
98    if (_mode&RELEASE_DISPLAY_LISTS)
99    {
100        drawable.releaseGLObjects(_renderInfo.getState());
101    }
102}
103
104void GLObjectsVisitor::apply(osg::StateSet& stateset)
105{
106    if (_stateSetAppliedSet.count(&stateset)!=0) return;
107
108    _stateSetAppliedSet.insert(&stateset);
109
110    if (_mode & COMPILE_STATE_ATTRIBUTES && _renderInfo.getState())
111    {
112        stateset.compileGLObjects(*_renderInfo.getState());
113
114        osg::Program* program = dynamic_cast<osg::Program*>(stateset.getAttribute(osg::StateAttribute::PROGRAM));
115        if (program) {
116            if( program->isFixedFunction() )
117                _lastCompiledProgram = NULL; // It does not make sense to apply uniforms on fixed pipe
118            else
119                _lastCompiledProgram = program;
120        }
121
122        if (_lastCompiledProgram.valid() && !stateset.getUniformList().empty())
123        {
124            osg::Program::PerContextProgram* pcp = _lastCompiledProgram->getPCP(_renderInfo.getState()->getContextID());
125            if (pcp)
126            {
127                pcp->useProgram();
128
129                _renderInfo.getState()->setLastAppliedProgramObject(pcp);
130
131                osg::StateSet::UniformList& ul = stateset.getUniformList();
132                for(osg::StateSet::UniformList::iterator itr = ul.begin();
133                    itr != ul.end();
134                    ++itr)
135                {
136                    pcp->apply(*(itr->second.first));
137                }
138            }
139        }
140        else if(_renderInfo.getState()->getLastAppliedProgramObject()){
141
142            osg::GL2Extensions* extensions = osg::GL2Extensions::Get(_renderInfo.getState()->getContextID(), true);
143            extensions->glUseProgram(0);
144            _renderInfo.getState()->setLastAppliedProgramObject(0);
145        }
146
147    }
148
149    if (_mode & RELEASE_STATE_ATTRIBUTES)
150    {
151        stateset.releaseGLObjects(_renderInfo.getState());
152    }
153
154    if (_mode & CHECK_BLACK_LISTED_MODES)
155    {
156        stateset.checkValidityOfAssociatedModes(*_renderInfo.getState());
157    }
158}
159
160/////////////////////////////////////////////////////////////////
161//
162// GLObjectsVisitor
163//
164
165GLObjectsOperation::GLObjectsOperation(GLObjectsVisitor::Mode mode):
166    osg::GraphicsOperation("GLObjectOperation",false),
167    _mode(mode)
168{
169}
170
171GLObjectsOperation::GLObjectsOperation(osg::Node* subgraph, GLObjectsVisitor::Mode mode):
172    osg::GraphicsOperation("GLObjectOperation",false),
173    _subgraph(subgraph),
174    _mode(mode)
175{
176}
177
178void GLObjectsOperation::operator () (osg::GraphicsContext* context)
179{
180    GLObjectsVisitor glObjectsVisitor(_mode);
181
182    context->getState()->initializeExtensionProcs();
183
184    glObjectsVisitor.setState(context->getState());
185
186    // OSG_NOTICE<<"GLObjectsOperation::before <<<<<<<<<<<"<<std::endl;
187    if (_subgraph.valid())
188    {
189        _subgraph->accept(glObjectsVisitor);
190    }
191    else
192    {
193        for(osg::GraphicsContext::Cameras::iterator itr = context->getCameras().begin();
194            itr != context->getCameras().end();
195            ++itr)
196        {
197            (*itr)->accept(glObjectsVisitor);
198        }
199    }
200    // OSG_NOTICE<<"GLObjectsOperation::after >>>>>>>>>>> "<<std::endl;
201}
202
203
204} // end of namespace osgUtil
Note: See TracBrowser for help on using the browser.