root/OpenSceneGraph/trunk/src/osg/PrimitiveSet.cpp @ 10600

Revision 10600, 7.4 kB (checked in by robert, 5 years ago)

Introduced new BufferObject? design + implementation in preperation of implementing a pool system for buffer objects

  • 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 <osg/PrimitiveSet>
14#include <osg/BufferObject>
15#include <osg/State>
16#include <osg/Notify>
17
18using namespace osg;
19
20unsigned 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
38void 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
44void DrawArrays::accept(PrimitiveFunctor& functor) const
45{
46    functor.drawArrays(_mode,_first,_count);
47}
48
49void DrawArrays::accept(PrimitiveIndexFunctor& functor) const
50{
51    functor.drawArrays(_mode,_first,_count);
52}
53
54unsigned 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
72void 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
84void 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
96void 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
108unsigned 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
120DrawElementsUByte::~DrawElementsUByte()
121{
122    releaseGLObjects();
123}
124
125void DrawElementsUByte::draw(State& state, bool useVertexBufferObjects) const 
126{
127    if (useVertexBufferObjects)
128    {
129        GLBufferObject* ebo = getOrCreateGLBufferObject(state.getContextID());
130        state.bindElementBufferObject(ebo);
131        if (ebo)
132        {
133            if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_BYTE, (const GLvoid *)(ebo->getOffset(getBufferIndex())), _numInstances);
134            else glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, (const GLvoid *)(ebo->getOffset(getBufferIndex())));
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
149void DrawElementsUByte::accept(PrimitiveFunctor& functor) const
150{
151    if (!empty()) functor.drawElements(_mode,size(),&front());
152}
153
154void DrawElementsUByte::accept(PrimitiveIndexFunctor& functor) const
155{
156    if (!empty()) functor.drawElements(_mode,size(),&front());
157}
158
159void DrawElementsUByte::offsetIndices(int offset)
160{
161    for(iterator itr=begin();
162        itr!=end();
163        ++itr)
164    {
165        *itr += offset;
166    }
167}
168
169
170DrawElementsUShort::~DrawElementsUShort()
171{
172    releaseGLObjects();
173}
174
175void DrawElementsUShort::draw(State& state, bool useVertexBufferObjects) const 
176{
177    if (useVertexBufferObjects)
178    {
179        GLBufferObject* ebo = getOrCreateGLBufferObject(state.getContextID());
180        state.bindElementBufferObject(ebo);
181        if (ebo)
182        {
183            if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_SHORT, (const GLvoid *)(ebo->getOffset(getBufferIndex())), _numInstances);
184            else glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, (const GLvoid *)(ebo->getOffset(getBufferIndex())));
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
199void DrawElementsUShort::accept(PrimitiveFunctor& functor) const
200{
201    if (!empty()) functor.drawElements(_mode,size(),&front());
202}
203
204void DrawElementsUShort::accept(PrimitiveIndexFunctor& functor) const
205{
206    if (!empty()) functor.drawElements(_mode,size(),&front());
207}
208
209void DrawElementsUShort::offsetIndices(int offset)
210{
211    for(iterator itr=begin();
212        itr!=end();
213        ++itr)
214    {
215        *itr += offset;
216    }
217}
218
219
220DrawElementsUInt::~DrawElementsUInt()
221{
222    releaseGLObjects();
223}
224
225void DrawElementsUInt::draw(State& state, bool useVertexBufferObjects) const 
226{
227    if (useVertexBufferObjects)
228    {
229        GLBufferObject* ebo = getOrCreateGLBufferObject(state.getContextID());
230        state.bindElementBufferObject(ebo);
231        if (ebo)
232        {
233            if (_numInstances>=1) state.glDrawElementsInstanced(_mode, size(), GL_UNSIGNED_INT, (const GLvoid *)(ebo->getOffset(getBufferIndex())), _numInstances);
234            else glDrawElements(_mode, size(), GL_UNSIGNED_INT, (const GLvoid *)(ebo->getOffset(getBufferIndex())));
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
249void DrawElementsUInt::accept(PrimitiveFunctor& functor) const
250{
251    if (!empty()) functor.drawElements(_mode,size(),&front());
252}
253
254void DrawElementsUInt::accept(PrimitiveIndexFunctor& functor) const
255{
256    if (!empty()) functor.drawElements(_mode,size(),&front());
257}
258
259void DrawElementsUInt::offsetIndices(int offset)
260{
261    for(iterator itr=begin();
262        itr!=end();
263        ++itr)
264    {
265        *itr += offset;
266    }
267}
Note: See TracBrowser for help on using the browser.