2005-04-15 05:41:28 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 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>
|
2002-06-27 18:50:19 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
void DrawArrays::draw(State&, bool) const
|
2002-06-27 18:50:19 +08:00
|
|
|
{
|
2002-06-27 21:15:34 +08:00
|
|
|
glDrawArrays(_mode,_first,_count);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
void DrawArrayLengths::draw(State&, bool) const
|
2002-07-07 22:40:41 +08:00
|
|
|
{
|
|
|
|
GLint first = _first;
|
|
|
|
for(VectorSizei::const_iterator itr=begin();
|
|
|
|
itr!=end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
glDrawArrays(_mode,first,*itr);
|
|
|
|
first += *itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2002-11-06 18:24:33 +08:00
|
|
|
for(VectorSizei::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;
|
|
|
|
for(VectorSizei::const_iterator itr=begin();
|
|
|
|
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;
|
2002-10-09 23:43:56 +08:00
|
|
|
for(VectorSizei::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()
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<_vboList.size();++i)
|
|
|
|
{
|
|
|
|
if (_vboList[i] != 0)
|
|
|
|
{
|
2005-02-09 18:39:45 +08:00
|
|
|
BufferObject::deleteBufferObject(i,_vboList[i]);
|
2004-09-22 01:26:08 +08:00
|
|
|
_vboList[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
void DrawElementsUByte::draw(State& state, bool useVertexBufferObjects) const
|
2002-06-27 21:15:34 +08:00
|
|
|
{
|
2004-06-29 23:04:38 +08:00
|
|
|
if (useVertexBufferObjects)
|
|
|
|
{
|
2005-02-09 18:39:45 +08:00
|
|
|
const BufferObject::Extensions* extensions = BufferObject::getExtensions(state.getContextID(), true);
|
2004-06-29 23:04:38 +08:00
|
|
|
|
|
|
|
GLuint& buffer = _vboList[state.getContextID()];
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
extensions->glGenBuffers(1, &buffer);
|
|
|
|
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer);
|
2004-08-18 02:56:24 +08:00
|
|
|
extensions->glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, size() * 1, &front(), GL_STATIC_DRAW_ARB);
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, 0);
|
2005-06-14 16:48:48 +08:00
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, &front());
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<_vboList.size();++i)
|
|
|
|
{
|
|
|
|
if (_vboList[i] != 0)
|
|
|
|
{
|
2005-02-09 18:39:45 +08:00
|
|
|
BufferObject::deleteBufferObject(i,_vboList[i]);
|
2004-09-22 01:26:08 +08:00
|
|
|
_vboList[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
void DrawElementsUShort::draw(State& state, bool useVertexBufferObjects) const
|
2002-06-27 21:15:34 +08:00
|
|
|
{
|
2004-06-29 23:04:38 +08:00
|
|
|
if (useVertexBufferObjects)
|
|
|
|
{
|
2005-02-09 18:39:45 +08:00
|
|
|
const BufferObject::Extensions* extensions = BufferObject::getExtensions(state.getContextID(), true);
|
2004-06-29 23:04:38 +08:00
|
|
|
|
|
|
|
GLuint& buffer = _vboList[state.getContextID()];
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
extensions->glGenBuffers(1, &buffer);
|
|
|
|
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer);
|
2004-08-18 02:56:24 +08:00
|
|
|
extensions->glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, size() * 2, &front(), GL_STATIC_DRAW_ARB);
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer);
|
|
|
|
}
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, 0);
|
2005-06-14 16:48:48 +08:00
|
|
|
|
|
|
|
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, &front());
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<_vboList.size();++i)
|
|
|
|
{
|
|
|
|
if (_vboList[i] != 0)
|
|
|
|
{
|
2005-02-09 18:39:45 +08:00
|
|
|
BufferObject::deleteBufferObject(i,_vboList[i]);
|
2004-09-22 01:26:08 +08:00
|
|
|
_vboList[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
void DrawElementsUInt::draw(State& state, bool useVertexBufferObjects) const
|
2002-06-27 21:15:34 +08:00
|
|
|
{
|
2004-06-29 23:04:38 +08:00
|
|
|
if (useVertexBufferObjects)
|
|
|
|
{
|
2005-02-09 18:39:45 +08:00
|
|
|
const BufferObject::Extensions* extensions = BufferObject::getExtensions(state.getContextID(), true);
|
2004-06-29 23:04:38 +08:00
|
|
|
|
|
|
|
GLuint& buffer = _vboList[state.getContextID()];
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
extensions->glGenBuffers(1, &buffer);
|
|
|
|
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer);
|
2004-08-18 02:56:24 +08:00
|
|
|
extensions->glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, size() * 4, &front(), GL_STATIC_DRAW_ARB);
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_INT, 0);
|
2005-06-14 16:48:48 +08:00
|
|
|
|
2004-06-29 23:04:38 +08:00
|
|
|
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_INT, &front());
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|