H3D API  2.4.1
H3DGeneratedShaderNode.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __H3DGENERATEDSHADERNODE_H__
30 #define __H3DGENERATEDSHADERNODE_H__
31 
32 #include <H3D/ComposedShader.h>
33 #include <H3D/X3DTexture2DNode.h>
34 #include <H3D/SFMatrix4f.h>
35 
36 namespace H3D {
37 
112  class H3DAPI_API H3DGeneratedShaderNode :
113  public ComposedShader {
114  public:
115 
117  struct RebuildShader :
118  public TypedField< SFBool, void, AnyNumber< Field > > {
119 
120  virtual void update() {
122  static_cast< H3DGeneratedShaderNode * >( getOwner() );
123  vn->buildShader();
124  }
125  };
126 
128  H3DGeneratedShaderNode( Inst< DisplayList > _displayList = 0,
129  Inst< SFNode > _metadata = 0,
130  Inst< SFBool > _isSelected = 0,
131  Inst< SFBool > _isValid = 0,
132  Inst< SFBool > _activate = 0,
133  Inst< SFString > _language = 0,
134  Inst< MFShaderPart > _parts = 0,
135  Inst< SFBool > _suppressUniformWarnings = 0,
136  Inst< MFString > _fragmentShaderString = 0,
137  Inst< MFString > _vertexShaderString = 0 );
138 
140  virtual void buildShader();
141 
144  virtual void preRender();
145 
151  H3DUniquePtr< MFString > fragmentShaderString;
152 
158  H3DUniquePtr< MFString > vertexShaderString;
159 
162 
163  // protected:
164 
168  H3DUniquePtr< RebuildShader > rebuildShader;
169 
183  virtual string addUniformFields( ComposedShader *shader );
184 
187  virtual string getFunctionShaderString() { return ""; }
188 
192  string name;
193 
195  string type;
196 
199  string calculation;
200 
202  VaryingVariable( const string &_name,
203  const string &_type,
204  const string &_calculation ):
205  name( _name ),
206  type( _type ),
207  calculation( _calculation ) {
208  }
209 
210  };
211 
213  struct Attribute {
215  string name;
216 
218  string type;
219 
221  Attribute( const string &_name,
222  const string &_type ) :
223  name( _name ),
224  type( _type ) {}
225  };
226 
230  virtual void getVaryingVariables( vector< VaryingVariable > &/*variables*/ ) {}
231 
235  virtual void getAttributes( vector< Attribute > &/*attributes*/ ) {}
236 
241  virtual string getFinalFragmentShaderOutputString();
242 
248  virtual string getVertexShaderString();
249 
256  virtual string getFragmentShaderString() { return ""; }
257 
266  virtual string uniqueShaderName( const string &base_name );
267 
283  static string addUniformToFragmentShader( ComposedShader *shader,
284  const string &name,
285  const string &glsl_type,
286  const Field::AccessType &access,
287  Field *field,
288  int array_size = -1,
289  bool delete_unadded_field = true );
290 
291  // Duplicates the input field and routes it to the output.
292  // Use this to add the render shader's H3DUniquePtr<field> to the shader.
293  template<class T>
294  T* copyAndRouteField(T *input) {
295  T* f = new T;
296  input->route( f );
297  return f;
298  }
299  template<class T>
300  T* copyAndRouteField(const H3DUniquePtr<T> &input) {
301  T* f = new T;
302  input->route( f );
303  return f;
304  }
305 
306  string shader_id;
307 
311  virtual string getFragmentShaderHeader() { return ""; }
312 
316  virtual string getVertexShaderHeader() { return ""; }
317 
318  private:
319  static unsigned int instance_count;
320  };
321 }
322 
323 #endif
Header file for ComposedShader, X3D scene-graph node.
Contains the SFMatrix4f field class.
Header file for X3DTexture2DNode, X3D scene-graph node.
The ComposedShader node defines a shader where the individual source files are not individually progr...
Definition: ComposedShader.h:77
The Field class.
Definition: Field.h:46
AccessType
The different access types that a field can have.
Definition: Field.h:53
The H3DGeneratedShaderNode node is the base class for nodes that generates GLSL shaders for use in pl...
Definition: H3DGeneratedShaderNode.h:113
virtual string getVertexShaderHeader()
Return a string that is placed at the beginning of a vertex shader to specify minimum required versio...
Definition: H3DGeneratedShaderNode.h:316
H3DUniquePtr< MFString > vertexShaderString
Contains the generated vertex shader code.
Definition: H3DGeneratedShaderNode.h:158
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: H3DGeneratedShaderNode.h:161
H3DUniquePtr< MFString > fragmentShaderString
Contains the generated fragment shader code.
Definition: H3DGeneratedShaderNode.h:151
virtual string getFunctionShaderString()
Get a string with GLSL function definitions to be used by the generated shader.
Definition: H3DGeneratedShaderNode.h:187
virtual void buildShader()
Rebuilds the generated shader, causing a full rebuild and relink of the shader.
Definition: H3DGeneratedShaderNode.cpp:121
H3DUniquePtr< RebuildShader > rebuildShader
Field used to update the shader code when an event is received.
Definition: H3DGeneratedShaderNode.h:168
virtual void getVaryingVariables(vector< VaryingVariable > &)
Get the varying variables used by the shader generator.
Definition: H3DGeneratedShaderNode.h:230
virtual void getAttributes(vector< Attribute > &)
Get the attribues used by the shader generator.
Definition: H3DGeneratedShaderNode.h:235
virtual string getFragmentShaderHeader()
Return a string that is placed at the beginning of a fragment shader to specify minimum required vers...
Definition: H3DGeneratedShaderNode.h:311
virtual string getFragmentShaderString()
Returns the shader code for the fragment shader.
Definition: H3DGeneratedShaderNode.h:256
A template modifier class for adding type checking on the routes to any Field class.
Definition: TypedField.h:84
H3D API namespace.
Definition: Anchor.h:38
Struct used to define an attribute in the shader.
Definition: H3DGeneratedShaderNode.h:213
string name
The name of the attribute.
Definition: H3DGeneratedShaderNode.h:215
Attribute(const string &_name, const string &_type)
Constructor.
Definition: H3DGeneratedShaderNode.h:221
string type
The glsl type of the variable(int, float, etc).
Definition: H3DGeneratedShaderNode.h:218
Specialized field that rebuilds the shader when it updates.
Definition: H3DGeneratedShaderNode.h:118
virtual void update()
Make the field up to date given that an event has occured.
Definition: H3DGeneratedShaderNode.h:120
Struct used to define a varying variable in the shader.
Definition: H3DGeneratedShaderNode.h:190
string name
The name of the variable.
Definition: H3DGeneratedShaderNode.h:192
string calculation
String defining glsl code to set the value of the varying variable.
Definition: H3DGeneratedShaderNode.h:199
VaryingVariable(const string &_name, const string &_type, const string &_calculation)
Constructor.
Definition: H3DGeneratedShaderNode.h:202
string type
The glsl type of the variable(int, mat3, sampler2D etc).
Definition: H3DGeneratedShaderNode.h:195
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194