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

Revision 13041, 9.1 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 <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(PATCHES):
34        case(POLYGON): return 1;
35    }
36    return 0;
37}
38
39void 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
65void DrawArrays::accept(PrimitiveFunctor& functor) const
66{
67    functor.drawArrays(_mode,_first,_count);
68}
69
70void DrawArrays::accept(PrimitiveIndexFunctor& functor) const
71{
72    functor.drawArrays(_mode,_first,_count);
73}
74
75unsigned 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
94void 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
126void 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
138void 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
150unsigned 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
162DrawElementsUByte::~DrawElementsUByte()
163{
164    releaseGLObjects();
165}
166
167void 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
197void DrawElementsUByte::accept(PrimitiveFunctor& functor) const
198{
199    if (!empty()) functor.drawElements(_mode,size(),&front());
200}
201
202void DrawElementsUByte::accept(PrimitiveIndexFunctor& functor) const
203{
204    if (!empty()) functor.drawElements(_mode,size(),&front());
205}
206
207void DrawElementsUByte::offsetIndices(int offset)
208{
209    for(iterator itr=begin();
210        itr!=end();
211        ++itr)
212    {
213        *itr += offset;
214    }
215}
216
217
218DrawElementsUShort::~DrawElementsUShort()
219{
220    releaseGLObjects();
221}
222
223void 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
253void DrawElementsUShort::accept(PrimitiveFunctor& functor) const
254{
255    if (!empty()) functor.drawElements(_mode,size(),&front());
256}
257
258void DrawElementsUShort::accept(PrimitiveIndexFunctor& functor) const
259{
260    if (!empty()) functor.drawElements(_mode,size(),&front());
261}
262
263void DrawElementsUShort::offsetIndices(int offset)
264{
265    for(iterator itr=begin();
266        itr!=end();
267        ++itr)
268    {
269        *itr += offset;
270    }
271}
272
273
274DrawElementsUInt::~DrawElementsUInt()
275{
276    releaseGLObjects();
277}
278
279void 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
309void DrawElementsUInt::accept(PrimitiveFunctor& functor) const
310{
311    if (!empty()) functor.drawElements(_mode,size(),&front());
312}
313
314void DrawElementsUInt::accept(PrimitiveIndexFunctor& functor) const
315{
316    if (!empty()) functor.drawElements(_mode,size(),&front());
317}
318
319void DrawElementsUInt::offsetIndices(int offset)
320{
321    for(iterator itr=begin();
322        itr!=end();
323        ++itr)
324    {
325        *itr += offset;
326    }
327}
Note: See TracBrowser for help on using the browser.