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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
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)
|
|
|
|
{
|
|
|
|
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;
|
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();
|
|
|
|
}
|
|
|
|
|
2007-01-04 22:11:51 +08:00
|
|
|
void DrawElementsUByte::resizeGLObjectBuffers(unsigned int maxSize)
|
|
|
|
{
|
|
|
|
_vboList.resize(maxSize);
|
|
|
|
}
|
|
|
|
|
2005-11-25 20:31:04 +08:00
|
|
|
void DrawElementsUByte::releaseGLObjects(State* state) const
|
|
|
|
{
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
unsigned int contextID = state->getContextID();
|
|
|
|
if (_vboList[contextID]._objectID != 0)
|
|
|
|
{
|
|
|
|
BufferObject::deleteBufferObject(contextID,_vboList[contextID]._objectID);
|
|
|
|
_vboList[contextID]._objectID = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2004-09-22 01:26:08 +08:00
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
for(unsigned int i=0;i<_vboList.size();++i)
|
2004-09-22 01:26:08 +08:00
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
if (_vboList[i]._objectID != 0)
|
|
|
|
{
|
|
|
|
BufferObject::deleteBufferObject(i,_vboList[i]._objectID);
|
|
|
|
_vboList[i]._objectID = 0;
|
|
|
|
}
|
2004-09-22 01:26:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2007-05-01 14:28:20 +08:00
|
|
|
const ElementBufferObject* ebo = getElementBufferObject();
|
|
|
|
state.bindElementBufferObject(ebo);
|
|
|
|
if (ebo)
|
|
|
|
{
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, ebo->getOffset(getElementBufferObjectIndex()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, &front());
|
|
|
|
}
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
releaseGLObjects();
|
|
|
|
}
|
|
|
|
|
2007-01-04 22:11:51 +08:00
|
|
|
void DrawElementsUShort::resizeGLObjectBuffers(unsigned int maxSize)
|
|
|
|
{
|
|
|
|
_vboList.resize(maxSize);
|
|
|
|
}
|
|
|
|
|
2005-11-25 20:31:04 +08:00
|
|
|
void DrawElementsUShort::releaseGLObjects(State* state) const
|
|
|
|
{
|
|
|
|
if (state)
|
2004-09-22 01:26:08 +08:00
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
unsigned int contextID = state->getContextID();
|
|
|
|
if (_vboList[contextID]._objectID != 0)
|
2004-09-22 01:26:08 +08:00
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
BufferObject::deleteBufferObject(contextID,_vboList[contextID]._objectID);
|
|
|
|
_vboList[contextID]._objectID = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<_vboList.size();++i)
|
|
|
|
{
|
|
|
|
if (_vboList[i]._objectID != 0)
|
|
|
|
{
|
|
|
|
BufferObject::deleteBufferObject(i,_vboList[i]._objectID);
|
|
|
|
_vboList[i]._objectID = 0;
|
|
|
|
}
|
2004-09-22 01:26:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2007-05-01 14:28:20 +08:00
|
|
|
const ElementBufferObject* ebo = getElementBufferObject();
|
|
|
|
state.bindElementBufferObject(ebo);
|
|
|
|
if (ebo)
|
|
|
|
{
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, ebo->getOffset(getElementBufferObjectIndex()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, &front());
|
|
|
|
}
|
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()
|
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
releaseGLObjects();
|
|
|
|
}
|
|
|
|
|
2007-01-04 22:11:51 +08:00
|
|
|
void DrawElementsUInt::resizeGLObjectBuffers(unsigned int maxSize)
|
|
|
|
{
|
|
|
|
_vboList.resize(maxSize);
|
|
|
|
}
|
|
|
|
|
2005-11-25 20:31:04 +08:00
|
|
|
void DrawElementsUInt::releaseGLObjects(State* state) const
|
|
|
|
{
|
|
|
|
if (state)
|
2004-09-22 01:26:08 +08:00
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
unsigned int contextID = state->getContextID();
|
|
|
|
if (_vboList[contextID]._objectID != 0)
|
2004-09-22 01:26:08 +08:00
|
|
|
{
|
2005-11-25 20:31:04 +08:00
|
|
|
BufferObject::deleteBufferObject(contextID,_vboList[contextID]._objectID);
|
|
|
|
_vboList[contextID]._objectID = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<_vboList.size();++i)
|
|
|
|
{
|
|
|
|
if (_vboList[i]._objectID != 0)
|
|
|
|
{
|
|
|
|
BufferObject::deleteBufferObject(i,_vboList[i]._objectID);
|
|
|
|
_vboList[i]._objectID = 0;
|
|
|
|
}
|
2004-09-22 01:26:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2007-05-01 14:28:20 +08:00
|
|
|
const ElementBufferObject* ebo = getElementBufferObject();
|
|
|
|
state.bindElementBufferObject(ebo);
|
|
|
|
if (ebo)
|
|
|
|
{
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_INT, ebo->getOffset(getElementBufferObjectIndex()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDrawElements(_mode, size(), GL_UNSIGNED_INT, &front());
|
|
|
|
}
|
2004-06-29 23:04:38 +08:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|