VertexProgram 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_VERTEXPROGRAM
  14. #define OSG_VERTEXPROGRAM 1
  15. #include <osg/StateAttribute>
  16. #include <osg/Vec4>
  17. #include <osg/Matrix>
  18. #include <osg/buffered_value>
  19. #include <map>
  20. #include <string>
  21. // if not defined by gl.h use the definition found in:
  22. // http://oss.sgi.com/projects/ogl-sample/registry/ARB/vertex_program.txt
  23. #ifndef GL_ARB_vertex_program
  24. #define GL_VERTEX_PROGRAM_ARB 0x8620
  25. #define GL_VERTEX_PROGRAM_POINT_SIZE_ARB 0x8642
  26. #define GL_VERTEX_PROGRAM_TWO_SIDE_ARB 0x8643
  27. #define GL_COLOR_SUM_ARB 0x8458
  28. #define GL_PROGRAM_FORMAT_ASCII_ARB 0x8875
  29. #define GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB 0x8622
  30. #define GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB 0x8623
  31. #define GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB 0x8624
  32. #define GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB 0x8625
  33. #define GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB 0x886A
  34. #define GL_CURRENT_VERTEX_ATTRIB_ARB 0x8626
  35. #define GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB 0x8645
  36. #define GL_PROGRAM_LENGTH_ARB 0x8627
  37. #define GL_PROGRAM_FORMAT_ARB 0x8876
  38. #define GL_PROGRAM_BINDING_ARB 0x8677
  39. #define GL_PROGRAM_INSTRUCTIONS_ARB 0x88A0
  40. #define GL_MAX_PROGRAM_INSTRUCTIONS_ARB 0x88A1
  41. #define GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A2
  42. #define GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A3
  43. #define GL_PROGRAM_TEMPORARIES_ARB 0x88A4
  44. #define GL_MAX_PROGRAM_TEMPORARIES_ARB 0x88A5
  45. #define GL_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A6
  46. #define GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A7
  47. #define GL_PROGRAM_PARAMETERS_ARB 0x88A8
  48. #define GL_MAX_PROGRAM_PARAMETERS_ARB 0x88A9
  49. #define GL_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AA
  50. #define GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AB
  51. #define GL_PROGRAM_ATTRIBS_ARB 0x88AC
  52. #define GL_MAX_PROGRAM_ATTRIBS_ARB 0x88AD
  53. #define GL_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AE
  54. #define GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AF
  55. #define GL_PROGRAM_ADDRESS_REGISTERS_ARB 0x88B0
  56. #define GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB 0x88B1
  57. #define GL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB 0x88B2
  58. #define GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB 0x88B3
  59. #define GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB 0x88B4
  60. #define GL_MAX_PROGRAM_ENV_PARAMETERS_ARB 0x88B5
  61. #define GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB 0x88B6
  62. #define GL_PROGRAM_STRING_ARB 0x8628
  63. #define GL_PROGRAM_ERROR_POSITION_ARB 0x864B
  64. #define GL_CURRENT_MATRIX_ARB 0x8641
  65. #define GL_TRANSPOSE_CURRENT_MATRIX_ARB 0x88B7
  66. #define GL_CURRENT_MATRIX_STACK_DEPTH_ARB 0x8640
  67. #define GL_MAX_VERTEX_ATTRIBS_ARB 0x8869
  68. #define GL_MAX_PROGRAM_MATRICES_ARB 0x862F
  69. #define GL_MAX_PROGRAM_MATRIX_STACK_DEPTH_ARB 0x862E
  70. #define GL_PROGRAM_ERROR_STRING_ARB 0x8874
  71. #define GL_MATRIX0_ARB 0x88C0
  72. #define GL_MATRIX1_ARB 0x88C1
  73. #define GL_MATRIX2_ARB 0x88C2
  74. #define GL_MATRIX3_ARB 0x88C3
  75. #define GL_MATRIX4_ARB 0x88C4
  76. #define GL_MATRIX5_ARB 0x88C5
  77. #define GL_MATRIX6_ARB 0x88C6
  78. #define GL_MATRIX7_ARB 0x88C7
  79. #define GL_MATRIX8_ARB 0x88C8
  80. #define GL_MATRIX9_ARB 0x88C9
  81. #define GL_MATRIX10_ARB 0x88CA
  82. #define GL_MATRIX11_ARB 0x88CB
  83. #define GL_MATRIX12_ARB 0x88CC
  84. #define GL_MATRIX13_ARB 0x88CD
  85. #define GL_MATRIX14_ARB 0x88CE
  86. #define GL_MATRIX15_ARB 0x88CF
  87. #define GL_MATRIX16_ARB 0x88D0
  88. #define GL_MATRIX17_ARB 0x88D1
  89. #define GL_MATRIX18_ARB 0x88D2
  90. #define GL_MATRIX19_ARB 0x88D3
  91. #define GL_MATRIX20_ARB 0x88D4
  92. #define GL_MATRIX21_ARB 0x88D5
  93. #define GL_MATRIX22_ARB 0x88D6
  94. #define GL_MATRIX23_ARB 0x88D7
  95. #define GL_MATRIX24_ARB 0x88D8
  96. #define GL_MATRIX25_ARB 0x88D9
  97. #define GL_MATRIX26_ARB 0x88DA
  98. #define GL_MATRIX27_ARB 0x88DB
  99. #define GL_MATRIX28_ARB 0x88DC
  100. #define GL_MATRIX29_ARB 0x88DD
  101. #define GL_MATRIX30_ARB 0x88DE
  102. #define GL_MATRIX31_ARB 0x88DF
  103. #endif
  104. namespace osg {
  105. /** VertexProgram - encapsulates the OpenGL ARB vertex program state. */
  106. class OSG_EXPORT VertexProgram : public StateAttribute
  107. {
  108. public:
  109. VertexProgram();
  110. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  111. VertexProgram(const VertexProgram& vp,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  112. META_StateAttribute(osg, VertexProgram, VERTEXPROGRAM);
  113. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  114. virtual int compare(const osg::StateAttribute& sa) const
  115. {
  116. // check the types are equal and then create the rhs variable
  117. // used by the COMPARE_StateAttribute_Parameter macros below.
  118. COMPARE_StateAttribute_Types(VertexProgram,sa)
  119. // compare each parameter in turn against the rhs.
  120. COMPARE_StateAttribute_Parameter(_vertexProgram)
  121. return 0; // passed all the above comparison macros, must be equal.
  122. }
  123. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  124. {
  125. usage.usesMode(GL_VERTEX_PROGRAM_ARB);
  126. return true;
  127. }
  128. // data access methods.
  129. /** Get the handle to the vertex program ID for the current context. */
  130. inline GLuint& getVertexProgramID(unsigned int contextID) const
  131. {
  132. return _vertexProgramIDList[contextID];
  133. }
  134. /** Set the vertex program using a C style string. */
  135. inline void setVertexProgram( const char* program )
  136. {
  137. _vertexProgram = program;
  138. dirtyVertexProgramObject();
  139. }
  140. /** Set the vertex program using C++ style string. */
  141. inline void setVertexProgram( const std::string& program )
  142. {
  143. _vertexProgram = program;
  144. dirtyVertexProgramObject();
  145. }
  146. /** Get the vertex program. */
  147. inline const std::string& getVertexProgram() const { return _vertexProgram; }
  148. /** Set Program Parameters */
  149. inline void setProgramLocalParameter(const GLuint index, const Vec4& p)
  150. {
  151. _programLocalParameters[index] = p;
  152. }
  153. typedef std::map<GLuint,Vec4> LocalParamList;
  154. /** Set list of Program Parameters */
  155. inline void setLocalParameters(const LocalParamList& lpl) { _programLocalParameters = lpl; }
  156. /** Get list of Program Parameters */
  157. inline LocalParamList& getLocalParameters() { return _programLocalParameters; }
  158. /** Get const list of Program Parameters */
  159. inline const LocalParamList& getLocalParameters() const { return _programLocalParameters; }
  160. /** Matrix */
  161. inline void setMatrix(const GLenum mode, const Matrix& matrix)
  162. {
  163. _matrixList[mode] = matrix;
  164. }
  165. typedef std::map<GLenum,Matrix> MatrixList;
  166. /** Set list of Matrices */
  167. inline void setMatrices(const MatrixList& matrices) { _matrixList = matrices; }
  168. /** Get list of Matrices */
  169. inline MatrixList& getMatrices() { return _matrixList; }
  170. /** Get list of Matrices */
  171. inline const MatrixList& getMatrices() const { return _matrixList; }
  172. /** Force a recompile on next apply() of associated OpenGL vertex program objects. */
  173. void dirtyVertexProgramObject();
  174. virtual void apply(State& state) const;
  175. virtual void compileGLObjects(State& state) const { apply(state); }
  176. /** Resize any per context GLObject buffers to specified size. */
  177. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  178. /** Release any OpenGL objects in specified graphics context if State
  179. * object is passed, otherwise release OpenGL objects for all graphics contexts if
  180. * State object pointer is NULL.
  181. */
  182. virtual void releaseGLObjects(State* state=0) const;
  183. protected:
  184. virtual ~VertexProgram();
  185. typedef buffered_value<GLuint> VertexProgramIDList;
  186. mutable VertexProgramIDList _vertexProgramIDList;
  187. std::string _vertexProgram;
  188. LocalParamList _programLocalParameters;
  189. MatrixList _matrixList;
  190. };
  191. }
  192. #endif