From Romano José Magacho da Silva, support for vertex attributes in vertex program.

This commit is contained in:
Robert Osfield
2003-05-07 13:13:13 +00:00
parent ba8bf1e94c
commit cda2c90315
5 changed files with 963 additions and 117 deletions

View File

@@ -127,6 +127,29 @@ class SG_EXPORT Geometry : public Drawable
const TexCoordArrayList& getTexCoordArrayList() const { return _texCoordList; }
typedef std::pair< ref_ptr<Array>, ref_ptr<IndexArray> > VertexAttribArrayPair;
typedef std::pair< GLboolean, VertexAttribArrayPair > VertexAttribNormArrayPair;
typedef std::vector< VertexAttribNormArrayPair > VertexAttribArrayList;
typedef std::vector< AttributeBinding > VertexAttribBindingList;
void setVertexAttribArray(unsigned int index,bool normalize,Array* array,AttributeBinding ab=BIND_OFF);
Array *getVertexAttribArray(unsigned int index);
const Array *getVertexAttribArray(unsigned int index) const;
bool getVertexAttribNormalize(unsigned int index, GLboolean &ret) const;
bool getVertexAttribBinding(unsigned int index, AttributeBinding& ab) const;
void setVertexAttribIndices(unsigned int index,IndexArray* array);
IndexArray* getVertexAttribIndices(unsigned int index);
const IndexArray* getVertexAttribIndices(unsigned int index) const;
unsigned int getNumVertexAttribArrays() const { return _vertexAttribList.size(); }
VertexAttribArrayList& getVertexAttribArrayList() { return _vertexAttribList; }
const VertexAttribArrayList& getVertexAttribArrayList() const { return _vertexAttribList; }
typedef std::vector< ref_ptr<PrimitiveSet> > PrimitiveSetList;
void setPrimitiveSetList(const PrimitiveSetList& primitives) { _primitives = primitives; dirtyDisplayList(); dirtyBound(); }
@@ -190,38 +213,139 @@ class SG_EXPORT Geometry : public Drawable
/** accept a PrimitiveFunctor and call its methods to tell it about the interal primitives that this Drawable has.*/
virtual void accept(PrimitiveFunctor& pf) const;
/** Extensions class which encapsulates the querring of extensions and
* associated function pointers, and provide convinience wrappers to
* check for the extensions or use the associated functions.*/
class SG_EXPORT Extensions : public osg::Referenced
{
public:
Extensions();
Extensions(const Extensions& rhs);
void lowestCommonDenominator(const Extensions& rhs);
void setupGLExtenions();
void setVertexProgramSupported(bool flag) { _isVertexProgramSupported=flag; }
bool isVertexProgramSupported() const { return _isVertexProgramSupported; }
void setSecondaryColorSupported(bool flag) { _isSecondaryColorSupported=flag; }
bool isSecondaryColorSupported() const { return _isSecondaryColorSupported; }
void setFogCoordSupported(bool flag) { _isFogCoordSupported=flag; }
bool isFogCoordSupported() const { return _isFogCoordSupported; }
void setMultiTexSupported(bool flag) { _isMultiTexSupported=flag; }
bool isMultiTexSupported() const { return _isMultiTexSupported; }
void glSecondaryColor3ubv(const GLubyte* coord) const;
void glSecondaryColor3fv(const GLfloat* coord) const;
void glFogCoordfv(const GLfloat* coord) const;
void glMultiTexCoord1f(GLenum target,GLfloat coord) const;
void glMultiTexCoord2fv(GLenum target,const GLfloat* coord) const;
void glMultiTexCoord3fv(GLenum target,const GLfloat* coord) const;
void glMultiTexCoord4fv(GLenum target,const GLfloat* coord) const;
void glVertexAttrib1s(unsigned int index, GLshort s) const;
void glVertexAttrib1f(unsigned int index, GLfloat f) const;
void glVertexAttrib2fv(unsigned int index, const GLfloat * v) const;
void glVertexAttrib3fv(unsigned int index, const GLfloat * v) const;
void glVertexAttrib4fv(unsigned int index, const GLfloat * v) const;
void glVertexAttrib4ubv(unsigned int index, const GLubyte * v) const;
void glVertexAttrib4Nubv(unsigned int index, const GLubyte * v) const;
protected:
typedef void (APIENTRY * FogCoordProc) (const GLfloat* coord);
typedef void (APIENTRY * VertexAttrib1sProc) (unsigned int index, GLshort s);
typedef void (APIENTRY * VertexAttrib1fProc) (unsigned int index, GLfloat f);
typedef void (APIENTRY * VertexAttribfvProc) (unsigned int index, const GLfloat * v);
typedef void (APIENTRY * VertexAttribubvProc) (unsigned int index, const GLubyte * v);
typedef void (APIENTRY * SecondaryColor3ubvProc) (const GLubyte* coord);
typedef void (APIENTRY * SecondaryColor3fvProc) (const GLfloat* coord);
typedef void (APIENTRY * MultiTexCoord1fProc) (GLenum target,GLfloat coord);
typedef void (APIENTRY * MultiTexCoordfvProc) (GLenum target,const GLfloat* coord);
~Extensions() {}
bool _isVertexProgramSupported;
bool _isSecondaryColorSupported;
bool _isFogCoordSupported;
bool _isMultiTexSupported;
FogCoordProc _glFogCoordfv;
SecondaryColor3ubvProc _glSecondaryColor3ubv;
SecondaryColor3fvProc _glSecondaryColor3fv;
VertexAttrib1sProc _glVertexAttrib1s;
VertexAttrib1fProc _glVertexAttrib1f;
VertexAttribfvProc _glVertexAttrib2fv;
VertexAttribfvProc _glVertexAttrib3fv;
VertexAttribfvProc _glVertexAttrib4fv;
VertexAttribubvProc _glVertexAttrib4ubv;
VertexAttribubvProc _glVertexAttrib4Nubv;
MultiTexCoord1fProc _glMultiTexCoord1f;
MultiTexCoordfvProc _glMultiTexCoord2fv;
MultiTexCoordfvProc _glMultiTexCoord3fv;
MultiTexCoordfvProc _glMultiTexCoord4fv;
};
/** Function to call to get the extension of a specified context.
* If the Exentsion object for that context has not yet been created then
* and the 'createIfNotInitalized' flag been set to false then returns NULL.
* If 'createIfNotInitalized' is true then the Extensions object is
* automatically created. However, in this case the extension object
* only be created with the graphics context associated with ContextID..*/
static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized);
/** setExtensions allows users to override the extensions across graphics contexts.
* typically used when you have different extensions supported across graphics pipes
* but need to ensure that they all use the same low common denominator extensions.*/
static void setExtensions(unsigned int contextID,Extensions* extensions);
protected:
Geometry& operator = (const Geometry&) { return *this;}
virtual ~Geometry();
PrimitiveSetList _primitives;
PrimitiveSetList _primitives;
ref_ptr<Vec3Array> _vertexArray;
ref_ptr<IndexArray> _vertexIndices;
ref_ptr<Vec3Array> _vertexArray;
ref_ptr<IndexArray> _vertexIndices;
mutable AttributeBinding _normalBinding;
ref_ptr<Vec3Array> _normalArray;
ref_ptr<IndexArray> _normalIndices;
mutable AttributeBinding _normalBinding;
ref_ptr<Vec3Array> _normalArray;
ref_ptr<IndexArray> _normalIndices;
mutable AttributeBinding _colorBinding;
ref_ptr<Array> _colorArray;
ref_ptr<IndexArray> _colorIndices;
mutable AttributeBinding _colorBinding;
ref_ptr<Array> _colorArray;
ref_ptr<IndexArray> _colorIndices;
mutable AttributeBinding _secondaryColorBinding;
ref_ptr<Array> _secondaryColorArray;
ref_ptr<IndexArray> _secondaryColorIndices;
mutable AttributeBinding _secondaryColorBinding;
ref_ptr<Array> _secondaryColorArray;
ref_ptr<IndexArray> _secondaryColorIndices;
mutable AttributeBinding _fogCoordBinding;
ref_ptr<Array> _fogCoordArray;
ref_ptr<IndexArray> _fogCoordIndices;
mutable AttributeBinding _fogCoordBinding;
ref_ptr<Array> _fogCoordArray;
ref_ptr<IndexArray> _fogCoordIndices;
TexCoordArrayList _texCoordList;
TexCoordArrayList _texCoordList;
mutable bool _fastPathComputed;
mutable bool _fastPath;
VertexAttribArrayList _vertexAttribList;
mutable VertexAttribBindingList _vertexAttribBindingList;
mutable bool _fastPathComputed;
mutable bool _fastPath;
};

View File

@@ -17,6 +17,7 @@
#include <osg/Export>
#include <osg/StateSet>
#include <osg/Matrix>
#include <osg/GLExtensions>
#include <osg/FrameStamp>
#include <osg/DisplaySettings>
@@ -35,7 +36,7 @@ namespace osg {
#ifdef GL_FOG_COORDINATE_ARRAY_EXT
#define GL_FOG_COORDINATE_ARRAY GL_FOG_COORDINATE_ARRAY_EXT
#else
#define GL_FOG_COORDINATE_ARRAY 0x8457
#define GL_FOG_COORDINATE_ARRAY 0x8457
#endif
#endif
@@ -43,7 +44,7 @@ namespace osg {
#ifdef GL_SECONDARY_COLOR_ARRAY_EXT
#define GL_SECONDARY_COLOR_ARRAY GL_SECONDARY_COLOR_ARRAY_EXT
#else
#define GL_SECONDARY_COLOR_ARRAY 0x845E
#define GL_SECONDARY_COLOR_ARRAY 0x845E
#endif
#endif
@@ -449,8 +450,8 @@ class SG_EXPORT State : public Referenced
/** wrapper around glEnableClientState(GL_TEXTURE_COORD_ARRAY);glTexCoordPointer(..);
* note, only updates values that change.*/
inline void setTexCoordPointer( unsigned int unit,
GLint size, GLenum type,
GLsizei stride, const GLvoid *ptr )
GLint size, GLenum type,
GLsizei stride, const GLvoid *ptr )
{
if (setClientActiveTextureUnit(unit))
{
@@ -470,7 +471,7 @@ class SG_EXPORT State : public Referenced
eap._dirty = false;
}
}
/** wrapper around glDisableClientState(GL_TEXTURE_COORD_ARRAY);
* note, only updates values that change.*/
inline void disableTexCoordPointer( unsigned int unit )
@@ -537,6 +538,96 @@ class SG_EXPORT State : public Referenced
bool setActiveTextureUnit( unsigned int unit );
typedef void (APIENTRY * VertexAttribPointerProc) (unsigned int, GLint, GLenum, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
typedef void (APIENTRY * EnableVertexAttribProc) (unsigned int);
typedef void (APIENTRY * DisableVertexAttribProc) (unsigned int);
/** wrapper around glEnableVertexAttribArrayARB(index);glVertexAttribPointerARB(..);
* note, only updates values that change.*/
inline void setVertexAttribPointer( unsigned int index,
GLint size, GLenum type, GLboolean normalized,
GLsizei stride, const GLvoid *ptr )
{
static VertexAttribPointerProc s_glVertexAttribPointer =
(VertexAttribPointerProc) osg::getGLExtensionFuncPtr("glVertexAttribPointer","glVertexAttribPointerARB");
static EnableVertexAttribProc s_glEnableVertexAttribArray =
(EnableVertexAttribProc) osg::getGLExtensionFuncPtr("glEnableVertexAttribArray","glEnableVertexAttribArrayARB");
if( s_glVertexAttribPointer )
{
if ( index >= _vertexAttribArrayList.size()) _vertexAttribArrayList.resize(index+1);
EnabledArrayPair& eap = _vertexAttribArrayList[index];
if (!eap._enabled || eap._dirty)
{
eap._enabled = true;
s_glEnableVertexAttribArray( index );
}
if (eap._pointer != ptr || eap._normalized!=normalized || eap._dirty)
{
s_glVertexAttribPointer( index, size, type, normalized, stride, ptr );
eap._pointer = ptr;
eap._normalized = normalized;
}
eap._dirty = false;
}
}
/** wrapper around DisableVertexAttribArrayARB(index);
* note, only updates values that change.*/
inline void disableVertexAttribPointer( unsigned int index )
{
static DisableVertexAttribProc s_glDisableVertexAttribArray =
(DisableVertexAttribProc) osg::getGLExtensionFuncPtr("glDisableVertexAttribArray","glDisableVertexAttribArrayARB");
if (s_glDisableVertexAttribArray)
{
if ( index >= _vertexAttribArrayList.size()) _vertexAttribArrayList.resize(index+1);
EnabledArrayPair& eap = _vertexAttribArrayList[index];
if (eap._enabled || eap._dirty)
{
eap._enabled = false;
eap._dirty = false;
s_glDisableVertexAttribArray( index );
}
}
}
inline void disableVertexAttribPointersAboveAndIncluding( unsigned int index )
{
static DisableVertexAttribProc s_glDisableVertexAttribArray =
(DisableVertexAttribProc) osg::getGLExtensionFuncPtr("glDisableVertexAttribArray","glDisableVertexAttribArrayARB");
if (s_glDisableVertexAttribArray)
{
while (index<_vertexAttribArrayList.size())
{
EnabledArrayPair& eap = _vertexAttribArrayList[index];
if (eap._enabled || eap._dirty)
{
eap._enabled = false;
eap._dirty = false;
s_glDisableVertexAttribArray( index );
}
++index;
}
}
}
inline void dirtyVertexAttribPointersAboveAndIncluding( unsigned int index )
{
while (index<_vertexAttribArrayList.size())
{
EnabledArrayPair& eap = _vertexAttribArrayList[index];
eap._pointer = 0;
eap._dirty = true;
++index;
}
}
/** Set the current OpenGL context uniqueID.
Note, it is the application developers responsibility to
set up unique ID for each OpenGL context. This value is
@@ -712,27 +803,30 @@ class SG_EXPORT State : public Referenced
struct EnabledArrayPair
{
EnabledArrayPair():_dirty(true),_enabled(false),_pointer(0) {}
EnabledArrayPair(const EnabledArrayPair& eap):_dirty(eap._dirty), _enabled(eap._enabled),_pointer(eap._pointer) {}
EnabledArrayPair& operator = (const EnabledArrayPair& eap) { _dirty=eap._dirty; _enabled=eap._enabled; _pointer=eap._pointer; return *this; }
EnabledArrayPair():_dirty(true),_enabled(false),_normalized(0),_pointer(0) {}
EnabledArrayPair(const EnabledArrayPair& eap):_dirty(eap._dirty), _enabled(eap._enabled),_normalized(eap._normalized),_pointer(eap._pointer) {}
EnabledArrayPair& operator = (const EnabledArrayPair& eap) { _dirty=eap._dirty; _enabled=eap._enabled; _normalized=eap._normalized;_pointer=eap._pointer; return *this; }
bool _dirty;
bool _enabled;
GLboolean _normalized;
const GLvoid* _pointer;
};
typedef std::vector<EnabledArrayPair> EnabledTexCoordArrayList;
typedef std::vector<EnabledArrayPair> EnabledVertexAttribArrayList;
EnabledArrayPair _vertexArray;
EnabledArrayPair _normalArray;
EnabledArrayPair _colorArray;
EnabledArrayPair _secondaryColorArray;
EnabledArrayPair _indexArray;
EnabledArrayPair _fogArray;
EnabledTexCoordArrayList _texCoordArrayList;
unsigned int _currentActiveTextureUnit;
unsigned int _currentClientActiveTextureUnit;
EnabledArrayPair _vertexArray;
EnabledArrayPair _normalArray;
EnabledArrayPair _colorArray;
EnabledArrayPair _secondaryColorArray;
EnabledArrayPair _indexArray;
EnabledArrayPair _fogArray;
EnabledTexCoordArrayList _texCoordArrayList;
EnabledVertexAttribArrayList _vertexAttribArrayList;
unsigned int _currentActiveTextureUnit;
unsigned int _currentClientActiveTextureUnit;
inline ModeMap& getOrCreateTextureModeMap(unsigned int unit)
{

File diff suppressed because it is too large Load Diff

View File

@@ -10,7 +10,6 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/GLExtensions>
#include <osg/State>
#include <osg/Notify>
#include <osg/GLU>
@@ -202,7 +201,7 @@ void State::captureCurrentState(StateSet& stateset) const
// template<class T>
// T mymax(const T& a,const T& b)
// {
// return (((a) > (b)) ? (a) : (b));
// return (((a) > (b)) ? (a) : (b));
// }
void State::apply(const StateSet* dstate)
@@ -491,6 +490,7 @@ void State::disableAllVertexArrays()
{
disableVertexPointer();
disableTexCoordPointersAboveAndIncluding(0);
disableVertexAttribPointersAboveAndIncluding(0);
disableColorPointer();
disableFogCoordPointer();
disableIndexPointer();
@@ -502,6 +502,7 @@ void State::dirtyAllVertexArrays()
{
dirtyVertexPointer();
dirtyTexCoordPointersAboveAndIncluding(0);
dirtyVertexAttribPointersAboveAndIncluding(0);
dirtyColorPointer();
dirtyFogCoordPointer();
dirtyIndexPointer();

View File

@@ -119,7 +119,7 @@ void VertexProgram::apply(State& state) const
}
else if (!_vertexProgram.empty())
{
::glGetError(); // Reset Error flags.
glGetError(); // Reset Error flags.
extensions->glGenPrograms( 1, &vertexProgramId );
extensions->glBindProgram( GL_VERTEX_PROGRAM_ARB, vertexProgramId );
extensions->glProgramString( GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
@@ -127,10 +127,10 @@ void VertexProgram::apply(State& state) const
// Check for errors
GLint errorposition;
::glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorposition);
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorposition);
if (errorposition != -1)
{
notify(osg::FATAL) << "VertexProgram: " << ::glGetString(GL_PROGRAM_ERROR_STRING_ARB) << std::endl;
notify(osg::FATAL) << "VertexProgram: " << glGetString(GL_PROGRAM_ERROR_STRING_ARB) << std::endl;
std::string::size_type start = _vertexProgram.rfind('\n', errorposition);
std::string::size_type stop = _vertexProgram.find('\n', errorposition);
@@ -161,10 +161,10 @@ void VertexProgram::apply(State& state) const
itr!=_matrixList.end();
++itr)
{
::glMatrixMode((*itr).first);
::glLoadMatrixf((*itr).second.ptr());
glMatrixMode((*itr).first);
glLoadMatrixf((*itr).second.ptr());
}
::glMatrixMode(GL_MODELVIEW); // restore matrix mode
glMatrixMode(GL_MODELVIEW); // restore matrix mode
}
}