2003-01-22 00:45:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2003-01-05 04:45:53 +08:00
|
|
|
|
|
|
|
#ifndef OSG_VERTEXPROGRAM
|
|
|
|
#define OSG_VERTEXPROGRAM 1
|
|
|
|
|
|
|
|
#include <osg/StateAttribute>
|
|
|
|
#include <osg/Vec4>
|
|
|
|
#include <osg/Matrix>
|
2003-04-10 20:11:40 +08:00
|
|
|
#include <osg/buffered_value>
|
2003-01-05 04:45:53 +08:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
// if not defined by gl.h use the definition found in:
|
|
|
|
// http://oss.sgi.com/projects/ogl-sample/registry/ARB/vertex_program.txt
|
|
|
|
#ifndef GL_ARB_vertex_program
|
|
|
|
#define GL_VERTEX_PROGRAM_ARB 0x8620
|
|
|
|
#define GL_VERTEX_PROGRAM_POINT_SIZE_ARB 0x8642
|
|
|
|
#define GL_VERTEX_PROGRAM_TWO_SIDE_ARB 0x8643
|
|
|
|
#define GL_COLOR_SUM_ARB 0x8458
|
|
|
|
#define GL_PROGRAM_FORMAT_ASCII_ARB 0x8875
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB 0x8622
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB 0x8623
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB 0x8624
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB 0x8625
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB 0x886A
|
|
|
|
#define GL_CURRENT_VERTEX_ATTRIB_ARB 0x8626
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB 0x8645
|
|
|
|
#define GL_PROGRAM_LENGTH_ARB 0x8627
|
|
|
|
#define GL_PROGRAM_FORMAT_ARB 0x8876
|
|
|
|
#define GL_PROGRAM_BINDING_ARB 0x8677
|
|
|
|
#define GL_PROGRAM_INSTRUCTIONS_ARB 0x88A0
|
|
|
|
#define GL_MAX_PROGRAM_INSTRUCTIONS_ARB 0x88A1
|
|
|
|
#define GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A2
|
|
|
|
#define GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A3
|
|
|
|
#define GL_PROGRAM_TEMPORARIES_ARB 0x88A4
|
|
|
|
#define GL_MAX_PROGRAM_TEMPORARIES_ARB 0x88A5
|
|
|
|
#define GL_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A6
|
|
|
|
#define GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A7
|
|
|
|
#define GL_PROGRAM_PARAMETERS_ARB 0x88A8
|
|
|
|
#define GL_MAX_PROGRAM_PARAMETERS_ARB 0x88A9
|
|
|
|
#define GL_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AA
|
|
|
|
#define GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AB
|
|
|
|
#define GL_PROGRAM_ATTRIBS_ARB 0x88AC
|
|
|
|
#define GL_MAX_PROGRAM_ATTRIBS_ARB 0x88AD
|
|
|
|
#define GL_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AE
|
|
|
|
#define GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AF
|
|
|
|
#define GL_PROGRAM_ADDRESS_REGISTERS_ARB 0x88B0
|
|
|
|
#define GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB 0x88B1
|
|
|
|
#define GL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB 0x88B2
|
|
|
|
#define GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB 0x88B3
|
|
|
|
#define GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB 0x88B4
|
|
|
|
#define GL_MAX_PROGRAM_ENV_PARAMETERS_ARB 0x88B5
|
|
|
|
#define GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB 0x88B6
|
|
|
|
#define GL_PROGRAM_STRING_ARB 0x8628
|
|
|
|
#define GL_PROGRAM_ERROR_POSITION_ARB 0x864B
|
|
|
|
#define GL_CURRENT_MATRIX_ARB 0x8641
|
|
|
|
#define GL_TRANSPOSE_CURRENT_MATRIX_ARB 0x88B7
|
|
|
|
#define GL_CURRENT_MATRIX_STACK_DEPTH_ARB 0x8640
|
|
|
|
#define GL_MAX_VERTEX_ATTRIBS_ARB 0x8869
|
|
|
|
#define GL_MAX_PROGRAM_MATRICES_ARB 0x862F
|
|
|
|
#define GL_MAX_PROGRAM_MATRIX_STACK_DEPTH_ARB 0x862E
|
|
|
|
#define GL_PROGRAM_ERROR_STRING_ARB 0x8874
|
|
|
|
#define GL_MATRIX0_ARB 0x88C0
|
|
|
|
#define GL_MATRIX1_ARB 0x88C1
|
|
|
|
#define GL_MATRIX2_ARB 0x88C2
|
|
|
|
#define GL_MATRIX3_ARB 0x88C3
|
|
|
|
#define GL_MATRIX4_ARB 0x88C4
|
|
|
|
#define GL_MATRIX5_ARB 0x88C5
|
|
|
|
#define GL_MATRIX6_ARB 0x88C6
|
|
|
|
#define GL_MATRIX7_ARB 0x88C7
|
|
|
|
#define GL_MATRIX8_ARB 0x88C8
|
|
|
|
#define GL_MATRIX9_ARB 0x88C9
|
|
|
|
#define GL_MATRIX10_ARB 0x88CA
|
|
|
|
#define GL_MATRIX11_ARB 0x88CB
|
|
|
|
#define GL_MATRIX12_ARB 0x88CC
|
|
|
|
#define GL_MATRIX13_ARB 0x88CD
|
|
|
|
#define GL_MATRIX14_ARB 0x88CE
|
|
|
|
#define GL_MATRIX15_ARB 0x88CF
|
|
|
|
#define GL_MATRIX16_ARB 0x88D0
|
|
|
|
#define GL_MATRIX17_ARB 0x88D1
|
|
|
|
#define GL_MATRIX18_ARB 0x88D2
|
|
|
|
#define GL_MATRIX19_ARB 0x88D3
|
|
|
|
#define GL_MATRIX20_ARB 0x88D4
|
|
|
|
#define GL_MATRIX21_ARB 0x88D5
|
|
|
|
#define GL_MATRIX22_ARB 0x88D6
|
|
|
|
#define GL_MATRIX23_ARB 0x88D7
|
|
|
|
#define GL_MATRIX24_ARB 0x88D8
|
|
|
|
#define GL_MATRIX25_ARB 0x88D9
|
|
|
|
#define GL_MATRIX26_ARB 0x88DA
|
|
|
|
#define GL_MATRIX27_ARB 0x88DB
|
|
|
|
#define GL_MATRIX28_ARB 0x88DC
|
|
|
|
#define GL_MATRIX29_ARB 0x88DD
|
|
|
|
#define GL_MATRIX30_ARB 0x88DE
|
|
|
|
#define GL_MATRIX31_ARB 0x88DF
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** VertexProgram - encapsulates the OpenGL ARB vertex program state. */
|
2003-01-05 04:45:53 +08:00
|
|
|
class SG_EXPORT VertexProgram : public StateAttribute
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
VertexProgram();
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
|
2003-01-05 04:45:53 +08:00
|
|
|
VertexProgram(const VertexProgram& vp,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_StateAttribute(osg, VertexProgram, VERTEXPROGRAM);
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
|
2003-01-05 04:45:53 +08:00
|
|
|
virtual int compare(const osg::StateAttribute& sa) const
|
|
|
|
{
|
|
|
|
// check the types are equal and then create the rhs variable
|
|
|
|
// used by the COMPARE_StateAttribute_Paramter macro's below.
|
|
|
|
COMPARE_StateAttribute_Types(VertexProgram,sa)
|
|
|
|
|
|
|
|
// compare each paramter in turn against the rhs.
|
2003-04-10 20:11:40 +08:00
|
|
|
COMPARE_StateAttribute_Parameter(_vertexProgram)
|
2003-01-05 04:45:53 +08:00
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
return 0; // passed all the above comparison macros, must be equal.
|
2003-01-05 04:45:53 +08:00
|
|
|
}
|
|
|
|
|
2004-03-03 23:38:22 +08:00
|
|
|
virtual bool getModeUsage(ModeUsage& usage) const
|
2003-01-05 04:45:53 +08:00
|
|
|
{
|
2004-03-03 23:38:22 +08:00
|
|
|
usage.usesMode(GL_VERTEX_PROGRAM_ARB);
|
|
|
|
return true;
|
2003-01-05 04:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// data access methods.
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Get the handle to the vertex program ID for the current context. */
|
2003-04-10 20:11:40 +08:00
|
|
|
inline GLuint& getVertexProgramID(unsigned int contextID) const
|
|
|
|
{
|
|
|
|
return _vertexProgramIDList[contextID];
|
|
|
|
}
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Set the vertex program using C++ style string. */
|
2003-05-21 20:15:45 +08:00
|
|
|
inline void setVertexProgram( const std::string& program )
|
|
|
|
{
|
|
|
|
_vertexProgram = program;
|
|
|
|
dirtyVertexProgramObject();
|
|
|
|
}
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Set the vertex program using a C style string. */
|
2003-04-11 17:54:27 +08:00
|
|
|
inline void setVertexProgram( const char* program )
|
|
|
|
{
|
|
|
|
_vertexProgram = program;
|
|
|
|
dirtyVertexProgramObject();
|
|
|
|
}
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Get the vertex program. */
|
2003-01-05 04:45:53 +08:00
|
|
|
inline const std::string& getVertexProgram() const { return _vertexProgram; }
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Set program parameters. */
|
2003-01-05 04:45:53 +08:00
|
|
|
inline void setProgramLocalParameter(const GLuint index, const Vec4& p)
|
2003-01-10 17:25:42 +08:00
|
|
|
{
|
2003-01-05 04:45:53 +08:00
|
|
|
_programLocalParameters[index] = p;
|
2003-01-10 17:25:42 +08:00
|
|
|
}
|
2003-01-05 04:45:53 +08:00
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Set matrix. */
|
2003-01-05 04:45:53 +08:00
|
|
|
inline void setMatrix(const GLenum mode, const Matrix& matrix)
|
|
|
|
{
|
2003-01-10 17:25:42 +08:00
|
|
|
_matrixList[mode] = matrix;
|
2003-01-05 04:45:53 +08:00
|
|
|
}
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Force a recompile on next apply() of associated OpenGL vertex program objects. */
|
2003-04-11 17:54:27 +08:00
|
|
|
void dirtyVertexProgramObject();
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Use deleteVertexProgramObject instead of glDeletePrograms to allow
|
2003-04-11 17:54:27 +08:00
|
|
|
* OpenGL Vertex Program objects to cached until they can be deleted
|
|
|
|
* by the OpenGL context in which they were created, specified
|
2004-09-16 03:10:15 +08:00
|
|
|
* by contextID.
|
|
|
|
*/
|
2003-04-11 17:54:27 +08:00
|
|
|
static void deleteVertexProgramObject(unsigned int contextID,GLuint handle);
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Flush all the cached vertex programs which need to be deleted
|
|
|
|
* in the OpenGL context related to contextID.
|
|
|
|
*/
|
2003-07-16 05:19:03 +08:00
|
|
|
static void flushDeletedVertexProgramObjects(unsigned int contextID,double currentTime, double& availableTime);
|
2003-04-11 17:54:27 +08:00
|
|
|
|
2003-01-05 04:45:53 +08:00
|
|
|
virtual void apply(State& state) const;
|
|
|
|
|
2004-07-20 13:37:59 +08:00
|
|
|
virtual void compileGLObjects(State& state) const { apply(state); }
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Release any OpenGL objects in specified graphics context if State
|
|
|
|
* object is passed, otherwise release OpenGL objexts for all graphics contexts if
|
|
|
|
* State object pointer is NULL.
|
|
|
|
*/
|
2004-07-20 13:37:59 +08:00
|
|
|
virtual void releaseGLObjects(State* state=0) const;
|
2003-04-11 03:32:32 +08:00
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** Extensions class which encapsulates the querying of extensions and
|
|
|
|
* associated function pointers, and provide convenience wrappers to
|
|
|
|
* check for the extensions or use the associated functions.
|
|
|
|
*/
|
2003-04-11 03:32:32 +08:00
|
|
|
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 glBindProgram(GLenum target, GLuint id) const;
|
|
|
|
void glGenPrograms(GLsizei n, GLuint *programs) const;
|
2003-04-11 17:54:27 +08:00
|
|
|
void glDeletePrograms(GLsizei n, GLuint *programs) const;
|
2003-04-11 03:32:32 +08:00
|
|
|
void glProgramString(GLenum target, GLenum format, GLsizei len, const void *string) const;
|
|
|
|
void glProgramLocalParameter4fv(GLenum target, GLuint index, const GLfloat *params) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
~Extensions() {}
|
|
|
|
|
|
|
|
bool _isVertexProgramSupported;
|
|
|
|
|
|
|
|
void* _glBindProgram;
|
|
|
|
void* _glGenPrograms;
|
2003-04-11 17:54:27 +08:00
|
|
|
void *_glDeletePrograms;
|
2003-04-11 03:32:32 +08:00
|
|
|
void* _glProgramString;
|
|
|
|
void* _glProgramLocalParameter4fv;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Function to call to get the extension of a specified context.
|
2004-09-16 03:10:15 +08:00
|
|
|
* If the Exentsion object for that context has not yet been created
|
2003-04-11 03:32:32 +08:00
|
|
|
* and the 'createIfNotInitalized' flag been set to false then returns NULL.
|
2004-09-16 03:10:15 +08:00
|
|
|
* If 'createIfNotInitalized' is true then the Extensions object is
|
|
|
|
* automatically created. However, in this case the extension object
|
|
|
|
* will only be created with the graphics context associated with ContextID.
|
|
|
|
*/
|
2003-04-11 03:32:32 +08:00
|
|
|
static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized);
|
|
|
|
|
2004-09-16 03:10:15 +08:00
|
|
|
/** The setExtensions method 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.
|
|
|
|
*/
|
2003-04-11 03:32:32 +08:00
|
|
|
static void setExtensions(unsigned int contextID,Extensions* extensions);
|
|
|
|
|
|
|
|
|
2003-01-05 04:45:53 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
|
|
virtual ~VertexProgram();
|
|
|
|
|
2003-04-10 20:11:40 +08:00
|
|
|
typedef buffered_value<GLuint> VertexProgramIDList;
|
|
|
|
mutable VertexProgramIDList _vertexProgramIDList;
|
|
|
|
|
2003-01-05 04:45:53 +08:00
|
|
|
std::string _vertexProgram;
|
|
|
|
|
|
|
|
typedef std::map<GLuint,Vec4> LocalParamList;
|
|
|
|
LocalParamList _programLocalParameters;
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
typedef std::map<GLenum,Matrix> MatrixList;
|
2003-01-05 04:45:53 +08:00
|
|
|
MatrixList _matrixList;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|