2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-09-20 22:51:59 +08:00
|
|
|
#include <osg/PrimitiveSet>
|
2005-02-09 18:39:45 +08:00
|
|
|
#include <osg/BufferObject>
|
2005-04-11 23:17:24 +08:00
|
|
|
#include <osg/State>
|
2005-11-25 20:31:04 +08:00
|
|
|
#include <osg/Notify>
|
2002-06-27 18:50:19 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2005-12-03 08:03:31 +08:00
|
|
|
unsigned int PrimitiveSet::getNumPrimitives() const
|
|
|
|
{
|
|
|
|
switch(_mode)
|
|
|
|
{
|
|
|
|
case(POINTS): return getNumIndices();
|
|
|
|
case(LINES): return getNumIndices()/2;
|
|
|
|
case(TRIANGLES): return getNumIndices()/3;
|
|
|
|
case(QUADS): return getNumIndices()/4;
|
|
|
|
case(LINE_STRIP):
|
|
|
|
case(LINE_LOOP):
|
|
|
|
case(TRIANGLE_STRIP):
|
|
|
|
case(TRIANGLE_FAN):
|
|
|
|
case(QUAD_STRIP):
|
|
|
|
case(POLYGON): return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-06 22:55:49 +08:00
|
|
|
void DrawArrays::draw(State& state, bool) const
|
2002-06-27 18:50:19 +08:00
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
#if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE)
|
|
|
|
GLenum mode = _mode;
|
|
|
|
if (_mode==GL_QUADS)
|
|
|
|
{
|
|
|
|
state.drawQuads(_first, _count, _numInstances);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (mode==GL_POLYGON)
|
|
|
|
{
|
|
|
|
mode = GL_TRIANGLE_FAN;
|
|
|
|
}
|
|
|
|
else if (mode==GL_QUAD_STRIP)
|
|
|
|
{
|
|
|
|
mode = GL_TRIANGLE_STRIP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_numInstances>=1) state.glDrawArraysInstanced(mode,_first,_count, _numInstances);
|
|
|
|
else glDrawArrays(mode,_first,_count);
|
|
|
|
#else
|
2009-01-06 22:55:49 +08:00
|
|
|
if (_numInstances>=1) state.glDrawArraysInstanced(_mode,_first,_count, _numInstances);
|
|
|
|
else glDrawArrays(_mode,_first,_count);
|
2009-11-12 22:35:44 +08:00
|
|
|
#endif
|
2002-06-27 21:15:34 +08:00
|
|
|
}
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawArrays::accept(PrimitiveFunctor& functor) const
|
2002-06-27 21:15:34 +08:00
|
|
|
{
|
|
|
|
functor.drawArrays(_mode,_first,_count);
|
|
|
|
}
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawArrays::accept(PrimitiveIndexFunctor& functor) const
|
2004-03-15 05:54:17 +08:00
|
|
|
{
|
|
|
|
functor.drawArrays(_mode,_first,_count);
|
|
|
|
}
|
|
|
|
|
2005-12-03 08:03:31 +08:00
|
|
|
unsigned int DrawArrayLengths::getNumPrimitives() const
|
|
|
|
{
|
|
|
|
switch(_mode)
|
|
|
|
{
|
|
|
|
case(POINTS): return getNumIndices();
|
|
|
|
case(LINES): return getNumIndices()/2;
|
|
|
|
case(TRIANGLES): return getNumIndices()/3;
|
|
|
|
case(QUADS): return getNumIndices()/4;
|
|
|
|
case(LINE_STRIP):
|
|
|
|
case(LINE_LOOP):
|
|
|
|
case(TRIANGLE_STRIP):
|
|
|
|
case(TRIANGLE_FAN):
|
|
|
|
case(QUAD_STRIP):
|
|
|
|
case(POLYGON): return size();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-12 22:35:44 +08:00
|
|
|
void DrawArrayLengths::draw(State& state, bool) const
|
2002-07-07 22:40:41 +08:00
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
GLenum mode = _mode;
|
|
|
|
#if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE)
|
|
|
|
if (_mode==GL_QUADS)
|
|
|
|
{
|
|
|
|
GLint first = _first;
|
|
|
|
for(vector_type::const_iterator itr=begin();
|
|
|
|
itr!=end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
state.drawQuads(first, *itr, _numInstances);
|
|
|
|
first += *itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN;
|
|
|
|
if (mode==GL_QUAD_STRIP) mode = GL_TRIANGLE_STRIP;
|
|
|
|
#endif
|
|
|
|
|
2002-07-07 22:40:41 +08:00
|
|
|
GLint first = _first;
|
2005-07-21 03:42:59 +08:00
|
|
|
for(vector_type::const_iterator itr=begin();
|
2002-07-07 22:40:41 +08:00
|
|
|
itr!=end();
|
|
|
|
++itr)
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
glDrawArrays(mode,first,*itr);
|
2002-07-07 22:40:41 +08:00
|
|
|
first += *itr;
|
|
|
|
}
|
2009-11-12 22:35:44 +08:00
|
|
|
|
2002-07-07 22:40:41 +08:00
|
|
|
}
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawArrayLengths::accept(PrimitiveFunctor& functor) const
|
2002-07-07 22:40:41 +08:00
|
|
|
{
|
|
|
|
GLint first = _first;
|
2005-07-21 03:42:59 +08:00
|
|
|
for(vector_type::const_iterator itr=begin();
|
2002-07-07 22:40:41 +08:00
|
|
|
itr!=end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
functor.drawArrays(_mode,first,*itr);
|
|
|
|
first += *itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawArrayLengths::accept(PrimitiveIndexFunctor& functor) const
|
2004-03-15 05:54:17 +08:00
|
|
|
{
|
|
|
|
GLint first = _first;
|
2005-07-21 03:42:59 +08:00
|
|
|
for(vector_type::const_iterator itr=begin();
|
2004-03-15 05:54:17 +08:00
|
|
|
itr!=end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
functor.drawArrays(_mode,first,*itr);
|
|
|
|
first += *itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-09 23:43:56 +08:00
|
|
|
unsigned int DrawArrayLengths::getNumIndices() const
|
2002-09-04 04:12:29 +08:00
|
|
|
{
|
|
|
|
unsigned int count = 0;
|
2005-07-21 03:42:59 +08:00
|
|
|
for(vector_type::const_iterator itr=begin();
|
2002-09-04 04:12:29 +08:00
|
|
|
itr!=end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
count += *itr;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2002-07-07 22:40:41 +08:00
|
|
|
|
2004-09-22 01:26:08 +08:00
|
|
|
DrawElementsUByte::~DrawElementsUByte()
|
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
releaseGLObjects();
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
void DrawElementsUByte::draw(State& state, bool useVertexBufferObjects) const
|
2002-06-27 21:15:34 +08:00
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
GLenum mode = _mode;
|
|
|
|
#if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE)
|
|
|
|
if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN;
|
|
|
|
if (mode==GL_QUAD_STRIP) mode = GL_TRIANGLE_STRIP;
|
|
|
|
#endif
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
if (useVertexBufferObjects)
|
|
|
|
{
|
2009-10-02 04:19:42 +08:00
|
|
|
GLBufferObject* ebo = getOrCreateGLBufferObject(state.getContextID());
|
2007-05-01 14:28:20 +08:00
|
|
|
state.bindElementBufferObject(ebo);
|
|
|
|
if (ebo)
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_BYTE, (const GLvoid *)(ebo->getOffset(getBufferIndex())), _numInstances);
|
|
|
|
else glDrawElements(mode, size(), GL_UNSIGNED_BYTE, (const GLvoid *)(ebo->getOffset(getBufferIndex())));
|
2007-05-01 14:28:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_BYTE, &front(), _numInstances);
|
|
|
|
else glDrawElements(mode, size(), GL_UNSIGNED_BYTE, &front());
|
2007-05-01 14:28:20 +08:00
|
|
|
}
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_BYTE, &front(), _numInstances);
|
|
|
|
else glDrawElements(mode, size(), GL_UNSIGNED_BYTE, &front());
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
2002-06-27 21:15:34 +08:00
|
|
|
}
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawElementsUByte::accept(PrimitiveFunctor& functor) const
|
2002-06-27 21:15:34 +08:00
|
|
|
{
|
|
|
|
if (!empty()) functor.drawElements(_mode,size(),&front());
|
|
|
|
}
|
2002-06-27 18:50:19 +08:00
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawElementsUByte::accept(PrimitiveIndexFunctor& functor) const
|
2004-03-15 05:54:17 +08:00
|
|
|
{
|
|
|
|
if (!empty()) functor.drawElements(_mode,size(),&front());
|
|
|
|
}
|
|
|
|
|
2002-07-18 08:53:03 +08:00
|
|
|
void DrawElementsUByte::offsetIndices(int offset)
|
|
|
|
{
|
|
|
|
for(iterator itr=begin();
|
|
|
|
itr!=end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr += offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-27 21:15:34 +08:00
|
|
|
|
2004-09-22 01:26:08 +08:00
|
|
|
DrawElementsUShort::~DrawElementsUShort()
|
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
releaseGLObjects();
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
void DrawElementsUShort::draw(State& state, bool useVertexBufferObjects) const
|
2002-06-27 21:15:34 +08:00
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
GLenum mode = _mode;
|
|
|
|
#if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE)
|
|
|
|
if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN;
|
|
|
|
if (mode==GL_QUAD_STRIP) mode = GL_TRIANGLE_STRIP;
|
|
|
|
#endif
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
if (useVertexBufferObjects)
|
|
|
|
{
|
2009-10-02 04:19:42 +08:00
|
|
|
GLBufferObject* ebo = getOrCreateGLBufferObject(state.getContextID());
|
2007-05-01 14:28:20 +08:00
|
|
|
state.bindElementBufferObject(ebo);
|
|
|
|
if (ebo)
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_SHORT, (const GLvoid *)(ebo->getOffset(getBufferIndex())), _numInstances);
|
|
|
|
else glDrawElements(mode, size(), GL_UNSIGNED_SHORT, (const GLvoid *)(ebo->getOffset(getBufferIndex())));
|
2007-05-01 14:28:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_SHORT, &front(), _numInstances);
|
|
|
|
else glDrawElements(mode, size(), GL_UNSIGNED_SHORT, &front());
|
2007-05-01 14:28:20 +08:00
|
|
|
}
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_SHORT, &front(), _numInstances);
|
|
|
|
else glDrawElements(mode, size(), GL_UNSIGNED_SHORT, &front());
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
2002-06-27 21:15:34 +08:00
|
|
|
}
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawElementsUShort::accept(PrimitiveFunctor& functor) const
|
2002-06-27 18:50:19 +08:00
|
|
|
{
|
2002-06-27 21:15:34 +08:00
|
|
|
if (!empty()) functor.drawElements(_mode,size(),&front());
|
2002-06-27 18:50:19 +08:00
|
|
|
}
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawElementsUShort::accept(PrimitiveIndexFunctor& functor) const
|
2004-03-15 05:54:17 +08:00
|
|
|
{
|
|
|
|
if (!empty()) functor.drawElements(_mode,size(),&front());
|
|
|
|
}
|
|
|
|
|
2002-07-18 08:53:03 +08:00
|
|
|
void DrawElementsUShort::offsetIndices(int offset)
|
|
|
|
{
|
|
|
|
for(iterator itr=begin();
|
|
|
|
itr!=end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr += offset;
|
|
|
|
}
|
|
|
|
}
|
2002-06-27 21:15:34 +08:00
|
|
|
|
|
|
|
|
2004-09-22 01:26:08 +08:00
|
|
|
DrawElementsUInt::~DrawElementsUInt()
|
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
releaseGLObjects();
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
void DrawElementsUInt::draw(State& state, bool useVertexBufferObjects) const
|
2002-06-27 21:15:34 +08:00
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
GLenum mode = _mode;
|
|
|
|
#if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE)
|
|
|
|
if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN;
|
|
|
|
if (mode==GL_QUAD_STRIP) mode = GL_TRIANGLE_STRIP;
|
|
|
|
#endif
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
if (useVertexBufferObjects)
|
|
|
|
{
|
2009-10-02 04:19:42 +08:00
|
|
|
GLBufferObject* ebo = getOrCreateGLBufferObject(state.getContextID());
|
2007-05-01 14:28:20 +08:00
|
|
|
state.bindElementBufferObject(ebo);
|
|
|
|
if (ebo)
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_INT, (const GLvoid *)(ebo->getOffset(getBufferIndex())), _numInstances);
|
|
|
|
else glDrawElements(mode, size(), GL_UNSIGNED_INT, (const GLvoid *)(ebo->getOffset(getBufferIndex())));
|
2007-05-01 14:28:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_INT, &front(), _numInstances);
|
|
|
|
else glDrawElements(mode, size(), GL_UNSIGNED_INT, &front());
|
2007-05-01 14:28:20 +08:00
|
|
|
}
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-12 22:35:44 +08:00
|
|
|
if (_numInstances>=1) state.glDrawElementsInstanced(mode, size(), GL_UNSIGNED_INT, &front(), _numInstances);
|
|
|
|
else glDrawElements(mode, size(), GL_UNSIGNED_INT, &front());
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
2002-06-27 21:15:34 +08:00
|
|
|
}
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawElementsUInt::accept(PrimitiveFunctor& functor) const
|
2002-06-27 21:15:34 +08:00
|
|
|
{
|
|
|
|
if (!empty()) functor.drawElements(_mode,size(),&front());
|
|
|
|
}
|
2002-07-18 08:53:03 +08:00
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
void DrawElementsUInt::accept(PrimitiveIndexFunctor& functor) const
|
2004-03-15 05:54:17 +08:00
|
|
|
{
|
|
|
|
if (!empty()) functor.drawElements(_mode,size(),&front());
|
|
|
|
}
|
|
|
|
|
2002-07-18 08:53:03 +08:00
|
|
|
void DrawElementsUInt::offsetIndices(int offset)
|
|
|
|
{
|
|
|
|
for(iterator itr=begin();
|
|
|
|
itr!=end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr += offset;
|
|
|
|
}
|
|
|
|
}
|