| 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(POLYGON): return 1; |
|---|
| 34 | } |
|---|
| 35 | return 0; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | void DrawArrays::draw(State& state, bool) const |
|---|
| 39 | { |
|---|
| 40 | if (_numInstances>=1) state.glDrawArraysInstanced(_mode,_first,_count, _numInstances); |
|---|
| 41 | else glDrawArrays(_mode,_first,_count); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void DrawArrays::accept(PrimitiveFunctor& functor) const |
|---|
| 45 | { |
|---|
| 46 | functor.drawArrays(_mode,_first,_count); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | void DrawArrays::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 50 | { |
|---|
| 51 | functor.drawArrays(_mode,_first,_count); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | unsigned int DrawArrayLengths::getNumPrimitives() const |
|---|
| 55 | { |
|---|
| 56 | switch(_mode) |
|---|
| 57 | { |
|---|
| 58 | case(POINTS): return getNumIndices(); |
|---|
| 59 | case(LINES): return getNumIndices()/2; |
|---|
| 60 | case(TRIANGLES): return getNumIndices()/3; |
|---|
| 61 | case(QUADS): return getNumIndices()/4; |
|---|
| 62 | case(LINE_STRIP): |
|---|
| 63 | case(LINE_LOOP): |
|---|
| 64 | case(TRIANGLE_STRIP): |
|---|
| 65 | case(TRIANGLE_FAN): |
|---|
| 66 | case(QUAD_STRIP): |
|---|
| 67 | case(POLYGON): return size(); |
|---|
| 68 | } |
|---|
| 69 | return 0; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void DrawArrayLengths::draw(State&, bool) const |
|---|
| 73 | { |
|---|
| 74 | GLint first = _first; |
|---|
| 75 | for(vector_type::const_iterator itr=begin(); |
|---|
| 76 | itr!=end(); |
|---|
| 77 | ++itr) |
|---|
| 78 | { |
|---|
| 79 | glDrawArrays(_mode,first,*itr); |
|---|
| 80 | first += *itr; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void DrawArrayLengths::accept(PrimitiveFunctor& functor) const |
|---|
| 85 | { |
|---|
| 86 | GLint first = _first; |
|---|
| 87 | for(vector_type::const_iterator itr=begin(); |
|---|
| 88 | itr!=end(); |
|---|
| 89 | ++itr) |
|---|
| 90 | { |
|---|
| 91 | functor.drawArrays(_mode,first,*itr); |
|---|
| 92 | first += *itr; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void DrawArrayLengths::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 97 | { |
|---|
| 98 | GLint first = _first; |
|---|
| 99 | for(vector_type::const_iterator itr=begin(); |
|---|
| 100 | itr!=end(); |
|---|
| 101 | ++itr) |
|---|
| 102 | { |
|---|
| 103 | functor.drawArrays(_mode,first,*itr); |
|---|
| 104 | first += *itr; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | unsigned int DrawArrayLengths::getNumIndices() const |
|---|
| 109 | { |
|---|
| 110 | unsigned int count = 0; |
|---|
| 111 | for(vector_type::const_iterator itr=begin(); |
|---|
| 112 | itr!=end(); |
|---|
| 113 | ++itr) |
|---|
| 114 | { |
|---|
| 115 | count += *itr; |
|---|
| 116 | } |
|---|
| 117 | return count; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | DrawElementsUByte::~DrawElementsUByte() |
|---|
| 121 | { |
|---|
| 122 | releaseGLObjects(); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | void DrawElementsUByte::draw(State& state, bool useVertexBufferObjects) const |
|---|
| 126 | { |
|---|
| 127 | if (useVertexBufferObjects) |
|---|
| 128 | { |
|---|
| 129 | const ElementBufferObject* ebo = getElementBufferObject(); |
|---|
| 130 | state.bindElementBufferObject(ebo); |
|---|
| 131 | if (ebo) |
|---|
| 132 | { |
|---|
| 133 | if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_BYTE, getElementBufferObjectOffset(), _numInstances); |
|---|
| 134 | else glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, getElementBufferObjectOffset()); |
|---|
| 135 | } |
|---|
| 136 | else |
|---|
| 137 | { |
|---|
| 138 | if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_BYTE, &front(), _numInstances); |
|---|
| 139 | else glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, &front()); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | else |
|---|
| 143 | { |
|---|
| 144 | if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_BYTE, &front(), _numInstances); |
|---|
| 145 | else glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, &front()); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | void DrawElementsUByte::accept(PrimitiveFunctor& functor) const |
|---|
| 150 | { |
|---|
| 151 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void DrawElementsUByte::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 155 | { |
|---|
| 156 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | void DrawElementsUByte::offsetIndices(int offset) |
|---|
| 160 | { |
|---|
| 161 | for(iterator itr=begin(); |
|---|
| 162 | itr!=end(); |
|---|
| 163 | ++itr) |
|---|
| 164 | { |
|---|
| 165 | *itr += offset; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | DrawElementsUShort::~DrawElementsUShort() |
|---|
| 171 | { |
|---|
| 172 | releaseGLObjects(); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | void DrawElementsUShort::draw(State& state, bool useVertexBufferObjects) const |
|---|
| 176 | { |
|---|
| 177 | if (useVertexBufferObjects) |
|---|
| 178 | { |
|---|
| 179 | const ElementBufferObject* ebo = getElementBufferObject(); |
|---|
| 180 | state.bindElementBufferObject(ebo); |
|---|
| 181 | if (ebo) |
|---|
| 182 | { |
|---|
| 183 | if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_SHORT, getElementBufferObjectOffset(), _numInstances); |
|---|
| 184 | else glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, getElementBufferObjectOffset()); |
|---|
| 185 | } |
|---|
| 186 | else |
|---|
| 187 | { |
|---|
| 188 | if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_SHORT, &front(), _numInstances); |
|---|
| 189 | else glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, &front()); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | else |
|---|
| 193 | { |
|---|
| 194 | if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_SHORT, &front(), _numInstances); |
|---|
| 195 | else glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, &front()); |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | void DrawElementsUShort::accept(PrimitiveFunctor& functor) const |
|---|
| 200 | { |
|---|
| 201 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | void DrawElementsUShort::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 205 | { |
|---|
| 206 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | void DrawElementsUShort::offsetIndices(int offset) |
|---|
| 210 | { |
|---|
| 211 | for(iterator itr=begin(); |
|---|
| 212 | itr!=end(); |
|---|
| 213 | ++itr) |
|---|
| 214 | { |
|---|
| 215 | *itr += offset; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | DrawElementsUInt::~DrawElementsUInt() |
|---|
| 221 | { |
|---|
| 222 | releaseGLObjects(); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | void DrawElementsUInt::draw(State& state, bool useVertexBufferObjects) const |
|---|
| 226 | { |
|---|
| 227 | if (useVertexBufferObjects) |
|---|
| 228 | { |
|---|
| 229 | const ElementBufferObject* ebo = getElementBufferObject(); |
|---|
| 230 | state.bindElementBufferObject(ebo); |
|---|
| 231 | if (ebo) |
|---|
| 232 | { |
|---|
| 233 | if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_INT, getElementBufferObjectOffset(), _numInstances); |
|---|
| 234 | else glDrawElements(_mode, size(), GL_UNSIGNED_INT, getElementBufferObjectOffset()); |
|---|
| 235 | } |
|---|
| 236 | else |
|---|
| 237 | { |
|---|
| 238 | if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_INT, &front(), _numInstances); |
|---|
| 239 | else glDrawElements(_mode, size(), GL_UNSIGNED_INT, &front()); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | else |
|---|
| 243 | { |
|---|
| 244 | if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_INT, &front(), _numInstances); |
|---|
| 245 | else glDrawElements(_mode, size(), GL_UNSIGNED_INT, &front()); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | void DrawElementsUInt::accept(PrimitiveFunctor& functor) const |
|---|
| 250 | { |
|---|
| 251 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | void DrawElementsUInt::accept(PrimitiveIndexFunctor& functor) const |
|---|
| 255 | { |
|---|
| 256 | if (!empty()) functor.drawElements(_mode,size(),&front()); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | void DrawElementsUInt::offsetIndices(int offset) |
|---|
| 260 | { |
|---|
| 261 | for(iterator itr=begin(); |
|---|
| 262 | itr!=end(); |
|---|
| 263 | ++itr) |
|---|
| 264 | { |
|---|
| 265 | *itr += offset; |
|---|
| 266 | } |
|---|
| 267 | } |
|---|