| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osgUtil/GLObjectsVisitor> |
|---|
| 14 | |
|---|
| 15 | #include <osg/Drawable> |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | #include <osg/Timer> |
|---|
| 18 | |
|---|
| 19 | namespace osgUtil |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | GLObjectsVisitor::GLObjectsVisitor(Mode mode) |
|---|
| 27 | { |
|---|
| 28 | setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN); |
|---|
| 29 | |
|---|
| 30 | _mode = mode; |
|---|
| 31 | |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | void GLObjectsVisitor::apply(osg::Node& node) |
|---|
| 36 | { |
|---|
| 37 | if (node.getStateSet()) |
|---|
| 38 | { |
|---|
| 39 | apply(*(node.getStateSet())); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | traverse(node); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | void 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 | |
|---|
| 66 | void 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&COMPILE_DISPLAY_LISTS && _renderInfo.getState()) |
|---|
| 83 | { |
|---|
| 84 | drawable.compileGLObjects(_renderInfo); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | if (_mode&RELEASE_DISPLAY_LISTS) |
|---|
| 88 | { |
|---|
| 89 | drawable.releaseGLObjects(_renderInfo.getState()); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | if (_mode&SWITCH_ON_VERTEX_BUFFER_OBJECTS) |
|---|
| 93 | { |
|---|
| 94 | drawable.setUseVertexBufferObjects(true); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | if (_mode&SWITCH_OFF_VERTEX_BUFFER_OBJECTS) |
|---|
| 98 | { |
|---|
| 99 | drawable.setUseVertexBufferObjects(false); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | void 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; |
|---|
| 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 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | GLObjectsOperation::GLObjectsOperation(GLObjectsVisitor::Mode mode): |
|---|
| 165 | osg::GraphicsOperation("GLObjectOperation",false), |
|---|
| 166 | _mode(mode) |
|---|
| 167 | { |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | GLObjectsOperation::GLObjectsOperation(osg::Node* subgraph, GLObjectsVisitor::Mode mode): |
|---|
| 171 | osg::GraphicsOperation("GLObjectOperation",false), |
|---|
| 172 | _subgraph(subgraph), |
|---|
| 173 | _mode(mode) |
|---|
| 174 | { |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | void GLObjectsOperation::operator () (osg::GraphicsContext* context) |
|---|
| 178 | { |
|---|
| 179 | GLObjectsVisitor glObjectsVisitor(_mode); |
|---|
| 180 | |
|---|
| 181 | context->getState()->initializeExtensionProcs(); |
|---|
| 182 | |
|---|
| 183 | glObjectsVisitor.setState(context->getState()); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 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 | |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | } |
|---|