OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FragmentProgram.h
Go to the documentation of this file.
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 
14 #ifndef OSG_FRAGMENTPROGRAM
15 #define OSG_FRAGMENTPROGRAM 1
16 
17 #include <osg/StateAttribute>
18 #include <osg/Vec4>
19 #include <osg/Matrix>
20 #include <osg/buffered_value>
21 
22 #include <map>
23 #include <string>
24 
25 // if not defined by gl.h use the definition found in:
26 // http://oss.sgi.com/projects/ogl-sample/registry/ARB/fragment_program.txt
27 #ifndef GL_ARB_fragment_program
28 #define GL_FRAGMENT_PROGRAM_ARB 0x8804
29 #define GL_PROGRAM_FORMAT_ASCII_ARB 0x8875
30 #define GL_PROGRAM_LENGTH_ARB 0x8627
31 #define GL_PROGRAM_FORMAT_ARB 0x8876
32 #define GL_PROGRAM_BINDING_ARB 0x8677
33 #define GL_PROGRAM_INSTRUCTIONS_ARB 0x88A0
34 #define GL_MAX_PROGRAM_INSTRUCTIONS_ARB 0x88A1
35 #define GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A2
36 #define GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A3
37 #define GL_PROGRAM_TEMPORARIES_ARB 0x88A4
38 #define GL_MAX_PROGRAM_TEMPORARIES_ARB 0x88A5
39 #define GL_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A6
40 #define GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A7
41 #define GL_PROGRAM_PARAMETERS_ARB 0x88A8
42 #define GL_MAX_PROGRAM_PARAMETERS_ARB 0x88A9
43 #define GL_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AA
44 #define GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AB
45 #define GL_PROGRAM_ATTRIBS_ARB 0x88AC
46 #define GL_MAX_PROGRAM_ATTRIBS_ARB 0x88AD
47 #define GL_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AE
48 #define GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AF
49 #define GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB 0x88B4
50 #define GL_MAX_PROGRAM_ENV_PARAMETERS_ARB 0x88B5
51 #define GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB 0x88B6
52 #define GL_PROGRAM_ALU_INSTRUCTIONS_ARB 0x8805
53 #define GL_PROGRAM_TEX_INSTRUCTIONS_ARB 0x8806
54 #define GL_PROGRAM_TEX_INDIRECTIONS_ARB 0x8807
55 #define GL_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB 0x8808
56 #define GL_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB 0x8809
57 #define GL_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB 0x880A
58 #define GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB 0x880B
59 #define GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB 0x880C
60 #define GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB 0x880D
61 #define GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB 0x880E
62 #define GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB 0x880F
63 #define GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB 0x8810
64 #define GL_PROGRAM_STRING_ARB 0x8628
65 #define GL_PROGRAM_ERROR_POSITION_ARB 0x864B
66 #define GL_CURRENT_MATRIX_ARB 0x8641
67 #define GL_TRANSPOSE_CURRENT_MATRIX_ARB 0x88B7
68 #define GL_CURRENT_MATRIX_STACK_DEPTH_ARB 0x8640
69 #define GL_MAX_PROGRAM_MATRICES_ARB 0x862F
70 #define GL_MAX_PROGRAM_MATRIX_STACK_DEPTH_ARB 0x862E
71 #define GL_MAX_TEXTURE_COORDS_ARB 0x8871
72 #define GL_MAX_TEXTURE_IMAGE_UNITS_ARB 0x8872
73 #define GL_PROGRAM_ERROR_STRING_ARB 0x8874
74 #define GL_MATRIX0_ARB 0x88C0
75 #define GL_MATRIX1_ARB 0x88C1
76 #define GL_MATRIX2_ARB 0x88C2
77 #define GL_MATRIX3_ARB 0x88C3
78 #define GL_MATRIX4_ARB 0x88C4
79 #define GL_MATRIX5_ARB 0x88C5
80 #define GL_MATRIX6_ARB 0x88C6
81 #define GL_MATRIX7_ARB 0x88C7
82 #define GL_MATRIX8_ARB 0x88C8
83 #define GL_MATRIX9_ARB 0x88C9
84 #define GL_MATRIX10_ARB 0x88CA
85 #define GL_MATRIX11_ARB 0x88CB
86 #define GL_MATRIX12_ARB 0x88CC
87 #define GL_MATRIX13_ARB 0x88CD
88 #define GL_MATRIX14_ARB 0x88CE
89 #define GL_MATRIX15_ARB 0x88CF
90 #define GL_MATRIX16_ARB 0x88D0
91 #define GL_MATRIX17_ARB 0x88D1
92 #define GL_MATRIX18_ARB 0x88D2
93 #define GL_MATRIX19_ARB 0x88D3
94 #define GL_MATRIX20_ARB 0x88D4
95 #define GL_MATRIX21_ARB 0x88D5
96 #define GL_MATRIX22_ARB 0x88D6
97 #define GL_MATRIX23_ARB 0x88D7
98 #define GL_MATRIX24_ARB 0x88D8
99 #define GL_MATRIX25_ARB 0x88D9
100 #define GL_MATRIX26_ARB 0x88DA
101 #define GL_MATRIX27_ARB 0x88DB
102 #define GL_MATRIX28_ARB 0x88DC
103 #define GL_MATRIX29_ARB 0x88DD
104 #define GL_MATRIX30_ARB 0x88DE
105 #define GL_MATRIX31_ARB 0x88DF
106 
107 #endif
108 
109 
110 namespace osg {
111 
112 
113 
116 {
117  public:
118 
119  FragmentProgram();
120 
123 
124  META_StateAttribute(osg, FragmentProgram, FRAGMENTPROGRAM);
125 
127  virtual int compare(const osg::StateAttribute& sa) const
128  {
129  // check the types are equal and then create the rhs variable
130  // used by the COMPARE_StateAttribute_Parameter macros below.
132 
133  // compare each parameter in turn against the rhs.
134  COMPARE_StateAttribute_Parameter(_fragmentProgram)
135 
136  return 0; // passed all the above comparison macros, must be equal.
137  }
138 
139  virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
140  {
142  return true;
143  }
144 
145  // data access methods.
146 
148  inline GLuint& getFragmentProgramID(unsigned int contextID) const
149  {
150  return _fragmentProgramIDList[contextID];
151  }
152 
153 
155  inline void setFragmentProgram( const char* program )
156  {
157  _fragmentProgram = program;
158  dirtyFragmentProgramObject();
159  }
160 
162  inline void setFragmentProgram( const std::string& program )
163  {
164  _fragmentProgram = program;
165  dirtyFragmentProgramObject();
166  }
167 
169  inline const std::string& getFragmentProgram() const { return _fragmentProgram; }
170 
172  inline void setProgramLocalParameter(const GLuint index, const Vec4& p)
173  {
174  _programLocalParameters[index] = p;
175  }
176 
177  typedef std::map<GLuint,Vec4> LocalParamList;
178 
180  inline void setLocalParameters(const LocalParamList& lpl) { _programLocalParameters = lpl; }
181 
183  inline LocalParamList& getLocalParameters() { return _programLocalParameters; }
184 
186  inline const LocalParamList& getLocalParameters() const { return _programLocalParameters; }
187 
189  inline void setMatrix(const GLenum mode, const Matrix& matrix)
190  {
191  _matrixList[mode] = matrix;
192  }
193 
194  typedef std::map<GLenum,Matrix> MatrixList;
195 
197  inline void setMatrices(const MatrixList& matrices) { _matrixList = matrices; }
198 
200  inline MatrixList& getMatrices() { return _matrixList; }
201 
203  inline const MatrixList& getMatrices() const { return _matrixList; }
204 
205 
207  void dirtyFragmentProgramObject();
208 
213  static void deleteFragmentProgramObject(unsigned int contextID,GLuint handle);
214 
217  static void flushDeletedFragmentProgramObjects(unsigned int contextID,double currentTime, double& availableTime);
218 
223  static void discardDeletedFragmentProgramObjects(unsigned int contextID);
224 
225  virtual void apply(State& state) const;
226 
227  virtual void compileGLObjects(State& state) const { apply(state); }
228 
230  virtual void resizeGLObjectBuffers(unsigned int maxSize);
231 
235  virtual void releaseGLObjects(State* state=0) const;
236 
237  protected:
238 
239 
240  virtual ~FragmentProgram();
241 
243  mutable FragmentProgramIDList _fragmentProgramIDList;
244 
245  std::string _fragmentProgram;
246 
247  LocalParamList _programLocalParameters;
248  MatrixList _matrixList;
249 };
250 
251 }
252 
253 #endif
254 
void setMatrix(const GLenum mode, const Matrix &matrix)
#define OSG_EXPORT
Definition: Export.h:43
MatrixList & getMatrices()
LocalParamList & getLocalParameters()
GLuint & getFragmentProgramID(unsigned int contextID) const
#define COMPARE_StateAttribute_Types(TYPE, rhs_attribute)
void setLocalParameters(const LocalParamList &lpl)
FragmentProgramIDList _fragmentProgramIDList
#define GL_FRAGMENT_PROGRAM_ARB
virtual void usesMode(GLMode mode)=0
virtual bool getModeUsage(StateAttribute::ModeUsage &usage) const
void setFragmentProgram(const std::string &program)
std::map< GLenum, Matrix > MatrixList
buffered_value< GLuint > FragmentProgramIDList
std::map< GLuint, Vec4 > LocalParamList
const MatrixList & getMatrices() const
#define META_StateAttribute(library, name, type)
void setProgramLocalParameter(const GLuint index, const Vec4 &p)
const std::string & getFragmentProgram() const
Definition: AlphaFunc.h:19
#define COMPARE_StateAttribute_Parameter(parameter)
virtual void compileGLObjects(State &state) const
LocalParamList _programLocalParameters
const LocalParamList & getLocalParameters() const
std::string _fragmentProgram
virtual int compare(const osg::StateAttribute &sa) const
void setMatrices(const MatrixList &matrices)
void setFragmentProgram(const char *program)