| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/PrimitiveSet> |
|---|
| 14 | #include <osg/BufferObject> |
|---|
| 15 | #include <osg/State> |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | |
|---|
| 18 | using namespace osg; |
|---|
| 19 | |
|---|
| 20 | unsigned int PrimitiveSet::getNumPrimitives() const |
|---|
| 21 | { |
|---|
| 22 | switch(_mode) |
|---|
| 23 | { |
|---|
| 24 | case(POINTS): return getNumIndices(); |
|---|
| 25 | case(LINES): return getNumIndices()/2; |
|---|
| 26 | case(TRIANGLES): return getNumIndices()/3; |
|---|
| 27 | case(QUADS): return getNumIndices()/4; |
|---|
| 28 | case(LINE_STRIP): |
|---|
| 29 | case(LINE_LOOP): |
|---|
| 30 | case(TRIANGLE_STRIP): |
|---|
| 31 | case(TRIANGLE_FAN): |
|---|
| 32 | case(QUAD_STRIP): |
|---|
| 33 | case(PATCHES): |
|---|
| 34 | case(POLYGON): return 1; |
|---|
| 35 | } |
|---|
| 36 | return 0; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | void DrawArrays::draw(State& state, bool) const |
|---|
| 40 | { |
|---|
| 41 | #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) |
|---|
| 42 | GLenum mode = _mode; |
|---|
| 43 | if (_mode==GL_QUADS) |
|---|
| 44 | { |
|---|
| 45 | state.drawQuads(_first, _count, _numInstances); |
|---|
| 46 | return; |
|---|
| 47 | } |
|---|
| 48 | else if (mode==GL_POLYGON) |
|---|
| 49 | { |
|---|
| 50 | mode = GL_TRIANGLE_FAN; |
|---|
| 51 | } |
|---|
| 52 | else if (mode==GL_QUAD_STRIP) |
|---|
| 53 | { |
|---|
| 54 | mode = GL_TRIANGLE_STRIP; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | if (_numInstances>=1) state.glDrawArraysInstanced(mode,_first,_count, _numInstances); |
|---|
| 58 | else glDrawArrays(mode,_first,_count); |
|---|
| 59 | #else |
|---|
| 60 | if (_numInstances>=1) state.glDrawArraysInstanced(_mode,_first,_count, _numInstances); |
|---|
| 61 | else glDrawArrays(_mode,_first,_count); |
|---|
| 62 | #endif |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | void DrawArrays::accept(PrimitiveFunctor& functor) const |
|---|
| 66 | { |
|---|
| 67 | functor.drawArrays(_mode,_first,_count); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | void DrawArrays::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 71 | { |
|---|
| 72 | functor.drawArrays(_mode,_first,_count); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | unsigned int DrawArrayLengths::getNumPrimitives() const |
|---|
| 76 | { |
|---|
| 77 | switch(_mode) |
|---|
| 78 | { |
|---|
| 79 | case(POINTS): return getNumIndices(); |
|---|
| 80 | case(LINES): return getNumIndices()/2; |
|---|
| 81 | case(TRIANGLES): return getNumIndices()/3; |
|---|
| 82 | case(QUADS): return getNumIndices()/4; |
|---|
| 83 | case(LINE_STRIP): |
|---|
| 84 | case(LINE_LOOP): |
|---|
| 85 | case(TRIANGLE_STRIP): |
|---|
| 86 | case(TRIANGLE_FAN): |
|---|
| 87 | case(QUAD_STRIP): |
|---|
| 88 | case(PATCHES): |
|---|
| 89 | case(POLYGON): return size(); |
|---|
| 90 | } |
|---|
| 91 | return 0; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void DrawArrayLengths::draw(State& state, bool) const |
|---|
| 95 | { |
|---|
| 96 | GLenum mode = _mode; |
|---|
| 97 | #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) |
|---|
| 98 | if (_mode==GL_QUADS) |
|---|
| 99 | { |
|---|
| 100 | GLint first = _first; |
|---|
| 101 | for(vector_type::const_iterator itr=begin(); |
|---|
| 102 | itr!=end(); |
|---|
| 103 | ++itr) |
|---|
| 104 | { |
|---|
| 105 | state.drawQuads(first, *itr, _numInstances); |
|---|
| 106 | first += *itr; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | return; |
|---|
| 110 | } |
|---|
| 111 | if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN; |
|---|
| 112 | if (mode==GL_QUAD_STRIP) mode = GL_TRIANGLE_STRIP; |
|---|
| 113 | #endif |
|---|
| 114 | |
|---|
| 115 | GLint first = _first; |
|---|
| 116 | for(vector_type::const_iterator itr=begin(); |
|---|
| 117 | itr!=end(); |
|---|
| 118 | ++itr) |
|---|
| 119 | { |
|---|
| 120 | glDrawArrays(mode,first,*itr); |
|---|
| 121 | first += *itr; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | void DrawArrayLengths::accept(PrimitiveFunctor& functor) const |
|---|
| 127 | { |
|---|
| 128 | GLint first = _first; |
|---|
| 129 | for(vector_type::const_iterator itr=begin(); |
|---|
| 130 | itr!=end(); |
|---|
| 131 | ++itr) |
|---|
| 132 | { |
|---|
| 133 | functor.drawArrays(_mode,first,*itr); |
|---|
| 134 | first += *itr; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | void DrawArrayLengths::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 139 | { |
|---|
| 140 | GLint first = _first; |
|---|
| 141 | for(vector_type::const_iterator itr=begin(); |
|---|
| 142 | itr!=end(); |
|---|
| 143 | ++itr) |
|---|
| 144 | { |
|---|
| 145 | functor.drawArrays(_mode,first,*itr); |
|---|
| 146 | first += *itr; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | unsigned int DrawArrayLengths::getNumIndices() const |
|---|
| 151 | { |
|---|
| 152 | unsigned int count = 0; |
|---|
| 153 | for(vector_type::const_iterator itr=begin(); |
|---|
| 154 | itr!=end(); |
|---|
| 155 | ++itr) |
|---|
| 156 | { |
|---|
| 157 | count += *itr; |
|---|
| 158 | } |
|---|
| 159 | return count; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | DrawElementsUByte::~DrawElementsUByte() |
|---|
| 163 | { |
|---|
| 164 | releaseGLObjects(); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void DrawElementsUByte::draw(State& state, bool useVertexBufferObjects) const |
|---|
| 168 | { |
|---|
| 169 | GLenum mode = _mode; |
|---|
| 170 | #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) |
|---|
| 171 | if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN; |
|---|
| 172 | if (mode==GL_QUAD_STRIP) mode = GL_TRIANGLE_STRIP; |
|---|
| 173 | #endif |
|---|
| 174 | |
|---|
| 175 | if (useVertexBufferObjects) |
|---|
| 176 | { |
|---|
| 177 | GLBufferObject* ebo = getOrCreateGLBufferObject(state.getContextID()); |
|---|
| 178 | state.bindElementBufferObject(ebo); |
|---|
| 179 | if (ebo) |
|---|
| 180 | { |
|---|
| 181 | if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_BYTE, (const GLvoid *)(ebo->getOffset(getBufferIndex())), _numInstances); |
|---|
| 182 | else glDrawElements(mode, size(), GL_UNSIGNED_BYTE, (const GLvoid *)(ebo->getOffset(getBufferIndex()))); |
|---|
| 183 | } |
|---|
| 184 | else |
|---|
| 185 | { |
|---|
| 186 | if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_BYTE, &front(), _numInstances); |
|---|
| 187 | else glDrawElements(mode, size(), GL_UNSIGNED_BYTE, &front()); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | else |
|---|
| 191 | { |
|---|
| 192 | if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_BYTE, &front(), _numInstances); |
|---|
| 193 | else glDrawElements(mode, size(), GL_UNSIGNED_BYTE, &front()); |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | void DrawElementsUByte::accept(PrimitiveFunctor& functor) const |
|---|
| 198 | { |
|---|
| 199 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | void DrawElementsUByte::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 203 | { |
|---|
| 204 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | void DrawElementsUByte::offsetIndices(int offset) |
|---|
| 208 | { |
|---|
| 209 | for(iterator itr=begin(); |
|---|
| 210 | itr!=end(); |
|---|
| 211 | ++itr) |
|---|
| 212 | { |
|---|
| 213 | *itr += offset; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | DrawElementsUShort::~DrawElementsUShort() |
|---|
| 219 | { |
|---|
| 220 | releaseGLObjects(); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | void DrawElementsUShort::draw(State& state, bool useVertexBufferObjects) const |
|---|
| 224 | { |
|---|
| 225 | GLenum mode = _mode; |
|---|
| 226 | #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) |
|---|
| 227 | if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN; |
|---|
| 228 | if (mode==GL_QUAD_STRIP) mode = GL_TRIANGLE_STRIP; |
|---|
| 229 | #endif |
|---|
| 230 | |
|---|
| 231 | if (useVertexBufferObjects) |
|---|
| 232 | { |
|---|
| 233 | GLBufferObject* ebo = getOrCreateGLBufferObject(state.getContextID()); |
|---|
| 234 | state.bindElementBufferObject(ebo); |
|---|
| 235 | if (ebo) |
|---|
| 236 | { |
|---|
| 237 | if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_SHORT, (const GLvoid *)(ebo->getOffset(getBufferIndex())), _numInstances); |
|---|
| 238 | else glDrawElements(mode, size(), GL_UNSIGNED_SHORT, (const GLvoid *)(ebo->getOffset(getBufferIndex()))); |
|---|
| 239 | } |
|---|
| 240 | else |
|---|
| 241 | { |
|---|
| 242 | if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_SHORT, &front(), _numInstances); |
|---|
| 243 | else glDrawElements(mode, size(), GL_UNSIGNED_SHORT, &front()); |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | else |
|---|
| 247 | { |
|---|
| 248 | if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_SHORT, &front(), _numInstances); |
|---|
| 249 | else glDrawElements(mode, size(), GL_UNSIGNED_SHORT, &front()); |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | void DrawElementsUShort::accept(PrimitiveFunctor& functor) const |
|---|
| 254 | { |
|---|
| 255 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | void DrawElementsUShort::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 259 | { |
|---|
| 260 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | void DrawElementsUShort::offsetIndices(int offset) |
|---|
| 264 | { |
|---|
| 265 | for(iterator itr=begin(); |
|---|
| 266 | itr!=end(); |
|---|
| 267 | ++itr) |
|---|
| 268 | { |
|---|
| 269 | *itr += offset; |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | DrawElementsUInt::~DrawElementsUInt() |
|---|
| 275 | { |
|---|
| 276 | releaseGLObjects(); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | void DrawElementsUInt::draw(State& state, bool useVertexBufferObjects) const |
|---|
| 280 | { |
|---|
| 281 | GLenum mode = _mode; |
|---|
| 282 | #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) |
|---|
| 283 | if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN; |
|---|
| 284 | if (mode==GL_QUAD_STRIP) mode = GL_TRIANGLE_STRIP; |
|---|
| 285 | #endif |
|---|
| 286 | |
|---|
| 287 | if (useVertexBufferObjects) |
|---|
| 288 | { |
|---|
| 289 | GLBufferObject* ebo = getOrCreateGLBufferObject(state.getContextID()); |
|---|
| 290 | state.bindElementBufferObject(ebo); |
|---|
| 291 | if (ebo) |
|---|
| 292 | { |
|---|
| 293 | if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_INT, (const GLvoid *)(ebo->getOffset(getBufferIndex())), _numInstances); |
|---|
| 294 | else glDrawElements(mode, size(), GL_UNSIGNED_INT, (const GLvoid *)(ebo->getOffset(getBufferIndex()))); |
|---|
| 295 | } |
|---|
| 296 | else |
|---|
| 297 | { |
|---|
| 298 | if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_INT, &front(), _numInstances); |
|---|
| 299 | else glDrawElements(mode, size(), GL_UNSIGNED_INT, &front()); |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | else |
|---|
| 303 | { |
|---|
| 304 | if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_INT, &front(), _numInstances); |
|---|
| 305 | else glDrawElements(mode, size(), GL_UNSIGNED_INT, &front()); |
|---|
| 306 | } |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | void DrawElementsUInt::accept(PrimitiveFunctor& functor) const |
|---|
| 310 | { |
|---|
| 311 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | void DrawElementsUInt::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 315 | { |
|---|
| 316 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | void DrawElementsUInt::offsetIndices(int offset) |
|---|
| 320 | { |
|---|
| 321 | for(iterator itr=begin(); |
|---|
| 322 | itr!=end(); |
|---|
| 323 | ++itr) |
|---|
| 324 | { |
|---|
| 325 | *itr += offset; |
|---|
| 326 | } |
|---|
| 327 | } |
|---|