OpenCOVER
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
coVRShader.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 #ifndef CO_VR_SHADER_H
9 #define CO_VR_SHADER_H
10 
24 #include <util/coExport.h>
25 #include <list>
26 #include <string>
27 #include <osg/Shader>
28 #include <osg/Program>
29 #include <osg/Texture>
30 #include <osg/Drawable>
31 
32 namespace covise
33 {
34 class TokenBuffer;
35 }
36 
37 namespace osg
38 {
39 class Node;
40 class Geode;
41 };
42 namespace opencover
43 {
44 class coVRShader;
45 
46 class COVEREXPORT coVRUniform
47 {
48 
49 private:
50  const coVRShader *shader;
51  std::string name;
52  std::string type;
53  std::string value;
54  std::string min;
55  std::string max;
56  std::string textureFile;
57  std::string cubeMapFiles[6];
58  std::string wrapMode;
59  bool overwrite;
60  bool unique;
61 
62 public:
63  const std::string &getName() const
64  {
65  return name;
66  }
67  osg::Texture::WrapMode getWrapMode() const;
68  const std::string &getType() const
69  {
70  return type;
71  }
72  const std::string &getValue() const
73  {
74  return value;
75  }
76  const std::string &getMin() const
77  {
78  return min;
79  }
80  const std::string &getMax() const
81  {
82  return max;
83  }
84  const std::string &getTextureFileName() const
85  {
86  return textureFile;
87  }
88  const std::string *getCubeMapFiles() const
89  {
90  return cubeMapFiles;
91  }
92  void setMin(const std::string &m)
93  {
94  min = m;
95  }
96  void setMax(const std::string &m)
97  {
98  max = m;
99  }
100  void setOverwrite(bool o)
101  {
102  overwrite = o;
103  }
104  void setUnique(bool u)
105  {
106  unique = u;
107  }
108  void setValue(const char *value);
109  void setValue(osg::Matrixd m);
110  void setValue(osg::Matrixf m);
111  void setValue(float f);
112  void setValue(osg::Vec3 v);
113  void setValue(osg::Vec4 v);
114  void setWrapMode(std::string wm);
115  void setTexture(const char *textureFile, int texNum = 0);
116  bool doOverwrite() const
117  {
118  return overwrite;
119  }
120  bool isUnique() const
121  {
122  return unique;
123  }
124  osg::ref_ptr<osg::Uniform> uniform;
125  osg::ref_ptr<osg::Texture> texture;
126 
127  coVRUniform(const coVRShader *shader, const std::string &name, const std::string &type, const std::string &value);
128  virtual ~coVRUniform();
129 };
130 
131 class COVEREXPORT coVRAttribute
132 {
133 
134 private:
135  std::string name;
136  std::string type;
137  std::string value;
138 
139 public:
140  const std::string &getName()
141  {
142  return name;
143  }
144  const std::string &getType()
145  {
146  return type;
147  }
148  const std::string &getValue()
149  {
150  return value;
151  }
152  coVRAttribute(const std::string &name, const std::string &type, const std::string &value);
153  virtual ~coVRAttribute();
154 };
155 
156 class COVEREXPORT coVRShaderInstance
157 {
158 
159 private:
160  std::list<osg::ref_ptr<osg::Uniform> > uniforms;
161  osg::Drawable *myDrawable;
162 
163 public:
164  coVRShaderInstance(osg::Drawable *d);
165  virtual ~coVRShaderInstance();
166  void addUniform(const osg::Uniform &u);
167  std::list<osg::ref_ptr<osg::Uniform> > &getUniforms()
168  {
169  return uniforms;
170  };
171  osg::Uniform *getUniform(const std::string &name);
172 };
173 
174 class COVEREXPORT coVRShader
175 {
176  friend class coVRShaderList;
177 
178 private:
179  std::string name;
180  std::string fileName;
181  std::string dir;
182  bool wasCloned;
183  std::list<coVRUniform *> uniforms;
184  std::list<coVRAttribute *> attributes;
185  std::list<coVRShaderInstance *> instances;
186  osg::ref_ptr<osg::Shader> fragmentShader;
187  osg::ref_ptr<osg::Shader> geometryShader;
188  osg::ref_ptr<osg::Shader> vertexShader;
189  osg::ref_ptr<osg::Shader> tessControlShader;
190  osg::ref_ptr<osg::Shader> tessEvalShader;
191  osg::ref_ptr<osg::Program> program;
192  bool transparent; // the shader is transparent regardless of the users wishes
193  bool opaque; // the shader is opaque regardless of the users wishes
194  int geomParams[3];
195  int cullFace;
196 
197 public:
198  std::string findAsset(const std::string &path) const;
199  const std::string &getName()
200  {
201  return name;
202  }
204  {
205  return transparent;
206  };
207  std::list<coVRUniform *> &getUniforms()
208  {
209  return uniforms;
210  };
211  osg::ref_ptr<osg::Shader> &getFragmentShader()
212  {
213  return fragmentShader;
214  };
215  osg::ref_ptr<osg::Shader> &getGeometryShader()
216  {
217  return geometryShader;
218  };
219  osg::ref_ptr<osg::Shader> &getVertexShader()
220  {
221  return vertexShader;
222  };
223  osg::ref_ptr<osg::Shader> &getTessControlShader()
224  {
225  return tessControlShader;
226  };
227  osg::ref_ptr<osg::Shader> &getTessEvalShader()
228  {
229  return tessEvalShader;
230  };
231  osg::ref_ptr<osg::Program> &getProgram()
232  {
233  return program;
234  };
236  {
237  return geomParams[0];
238  };
240  {
241  return geomParams[1];
242  };
244  {
245  return geomParams[2];
246  };
247  bool isClone()
248  {
249  return wasCloned;
250  }
251  coVRShader(const std::string &name, const std::string &d);
252  coVRShader(const coVRShader &other);
253  void setData(covise::TokenBuffer &tb);
254  void setMatrixUniform(const std::string &name, osg::Matrixd m);
255  void setMatrixUniform(const std::string &name, osg::Matrixf m);
256  void setFloatUniform(const std::string &name, float f);
257  void setVec3Uniform(const std::string &name, osg::Vec3 v);
258  void setVec4Uniform(const std::string &name, osg::Vec4 v);
259  void setNumVertices(int);
260  void setInputType(int);
261  void setOutputType(int);
262  osg::Uniform *getUniform(const std::string &name);
263  // void remove(osg::Node *);
264  coVRShaderInstance *apply(osg::Node *);
265  void apply(osg::StateSet *);
266  coVRShaderInstance *apply(osg::Geode *geode, osg::Drawable *drawable);
267  void setUniformesFromAttribute(const char *uniformValues);
268 
269  void storeMaterial();
270  void loadMaterial();
271 
272  virtual ~coVRShader();
273 };
274 
275 class COVEREXPORT coVRShaderList : public std::list<coVRShader *>
276 {
277 private:
278  static coVRShaderList *s_instance;
279  coVRShaderList();
280  void loadMaterials();
281  osg::ref_ptr<osg::Uniform> timeUniform;
282  osg::ref_ptr<osg::Uniform> timeStepUniform;
283  osg::ref_ptr<osg::Uniform> lightMatrix;
284  osg::ref_ptr<osg::Uniform> projectionMatrix; //neue Projektionsmatrix
285  osg::ref_ptr<osg::Uniform> viewMatrix;
286  osg::ref_ptr<osg::Uniform> durationUniform;
287  osg::ref_ptr<osg::Uniform> viewportWidthUniform;
288  osg::ref_ptr<osg::Uniform> viewportHeightUniform;
289  osg::ref_ptr<osg::Uniform> stereoUniform; // 0 = LEFT, 1 = RIGHT
290  void applyParams(coVRShader *shader, std::map<std::string, std::string> *params);
291 public:
292  ~coVRShaderList();
293  coVRShader *get(const std::string &name, std::map<std::string, std::string> *params = NULL);
294  coVRShader *getUnique(const std::string &n, std::map<std::string, std::string> *params = NULL);
295  coVRShader *add(const std::string &name, std::string &dirName);
296  static coVRShaderList *instance();
297  void setData(covise::TokenBuffer &tb);
298  osg::Uniform *getTime();
299  osg::Uniform *getTimeStep();
300  osg::Uniform *getLight();
301  osg::Uniform *getProjection(); // neue Projektionsmatrix
302  osg::Uniform *getView();
303  osg::Uniform *getDuration();
304  osg::Uniform *getViewportWidth();
305  osg::Uniform *getViewportHeight();
306  osg::Uniform *getStereo();
307  void update();
308 
309  void init();
310  void remove(osg::Node *);
311 };
312 
313 class COVEREXPORT ShaderNode : public osg::Drawable
314 {
315 
316 public:
318  {
319  Left = 128,
320  Right = 256
321  };
322  ShaderNode(StereoView v);
323  virtual ~ShaderNode();
325  virtual void drawImplementation(osg::RenderInfo &renderInfo) const;
328  virtual osg::Object *cloneType() const;
329 
332  virtual osg::Object *clone(const osg::CopyOp &) const;
334 
335 private:
336 };
337 
338 
339 
351 class coTangentSpaceGenerator : public osg::Referenced {
352 public:
354  coTangentSpaceGenerator(const coTangentSpaceGenerator &copy, const osg::CopyOp &copyop = osg::CopyOp::SHALLOW_COPY);
355 
356  void generate(osg::Geometry *geo);
357 
358  inline osg::Vec4Array *getTangentArray() { return T_.get(); }
359  inline const osg::Vec4Array *getTangentArray() const { return T_.get(); }
360  inline void setTangentArray(osg::Vec4Array *array) { T_ = array; }
361 
362  inline osg::Vec4Array *getNormalArray() { return N_.get(); }
363  inline const osg::Vec4Array *getNormalArray() const { return N_.get(); }
364  inline void setNormalArray(osg::Vec4Array *array) { N_ = array; }
365 
366  inline osg::Vec4Array *getBinormalArray() { return B_.get(); }
367  inline const osg::Vec4Array *getBinormalArray() const { return B_.get(); }
368  inline void setBinormalArray(osg::Vec4Array *array) { B_ = array; }
369 
370  inline osg::IndexArray *getIndices() { return indices_.get(); }
371 
372 protected:
373 
376 
377  void compute(osg::PrimitiveSet *pset,
378  const osg::Array *vx,
379  const osg::Array *nx,
380  int iA, int iB, int iC);
381 
382  osg::ref_ptr<osg::Vec4Array> T_;
383  osg::ref_ptr<osg::Vec4Array> B_;
384  osg::ref_ptr<osg::Vec4Array> N_;
385  osg::ref_ptr<osg::UIntArray> indices_;
386 };
387 
388 
389 }
390 #endif
static ShaderNode * theNode
Definition: coVRShader.h:324
const std::string & getMin() const
Definition: coVRShader.h:76
void setTangentArray(osg::Vec4Array *array)
Definition: coVRShader.h:360
const std::string * getCubeMapFiles() const
Definition: coVRShader.h:88
void setUnique(bool u)
Definition: coVRShader.h:104
osg::ref_ptr< osg::Shader > & getFragmentShader()
Definition: coVRShader.h:211
std::list< coVRUniform * > & getUniforms()
Definition: coVRShader.h:207
osg::ref_ptr< osg::Vec4Array > T_
Definition: coVRShader.h:382
osg::ref_ptr< osg::Vec4Array > N_
Definition: coVRShader.h:384
void generate(osg::Geometry *geo)
void setOverwrite(bool o)
Definition: coVRShader.h:100
StereoView view
Definition: coVRShader.h:333
const osg::Vec4Array * getNormalArray() const
Definition: coVRShader.h:363
const std::string & getMax() const
Definition: coVRShader.h:80
coTangentSpaceGenerator & operator=(const coTangentSpaceGenerator &)
Definition: coVRShader.h:375
const std::string & getName()
Definition: coVRShader.h:140
const std::string & getValue()
Definition: coVRShader.h:148
bool isUnique() const
Definition: coVRShader.h:120
Definition: coVRShader.h:131
Definition: coVRShader.h:174
const std::string & getTextureFileName() const
Definition: coVRShader.h:84
const osg::Vec4Array * getBinormalArray() const
Definition: coVRShader.h:367
virtual ~coTangentSpaceGenerator()
Definition: coVRShader.h:374
void setMax(const std::string &m)
Definition: coVRShader.h:96
const osg::Vec4Array * getTangentArray() const
Definition: coVRShader.h:359
Definition: MultiChannelDrawer.h:30
StereoView
Definition: coVRShader.h:317
int getInputType()
Definition: coVRShader.h:239
const std::string & getType()
Definition: coVRShader.h:144
osg::Vec4Array * getTangentArray()
Definition: coVRShader.h:358
osg::ref_ptr< osg::Program > & getProgram()
Definition: coVRShader.h:231
Definition: MultiChannelDrawer.h:31
osg::ref_ptr< osg::Shader > & getVertexShader()
Definition: coVRShader.h:219
osg::Vec4Array * getNormalArray()
Definition: coVRShader.h:362
const std::string & getType() const
Definition: coVRShader.h:68
void setBinormalArray(osg::Vec4Array *array)
Definition: coVRShader.h:368
std::list< osg::ref_ptr< osg::Uniform > > & getUniforms()
Definition: coVRShader.h:167
osg::ref_ptr< osg::Shader > & getTessControlShader()
Definition: coVRShader.h:223
void compute(osg::PrimitiveSet *pset, const osg::Array *vx, const osg::Array *nx, int iA, int iB, int iC)
bool isClone()
Definition: coVRShader.h:247
osg::ref_ptr< osg::Uniform > uniform
Definition: coVRShader.h:124
osg::ref_ptr< osg::Shader > & getTessEvalShader()
Definition: coVRShader.h:227
Definition: coVRShader.h:275
osg::ref_ptr< osg::Texture > texture
Definition: coVRShader.h:125
int getNumVertices()
Definition: coVRShader.h:235
osg::ref_ptr< osg::Vec4Array > B_
Definition: coVRShader.h:383
osg::ref_ptr< osg::Shader > & getGeometryShader()
Definition: coVRShader.h:215
osg::IndexArray * getIndices()
Definition: coVRShader.h:370
bool doOverwrite() const
Definition: coVRShader.h:116
osg::ref_ptr< osg::UIntArray > indices_
Definition: coVRShader.h:385
const std::string & getValue() const
Definition: coVRShader.h:72
const std::string & getName()
Definition: coVRShader.h:199
bool isTransparent()
Definition: coVRShader.h:203
void setNormalArray(osg::Vec4Array *array)
Definition: coVRShader.h:364
Definition: coVRShader.h:46
Definition: coVRShader.h:313
const std::string & getName() const
Definition: coVRShader.h:63
osg::Vec4Array * getBinormalArray()
Definition: coVRShader.h:366
Definition: coVRShader.h:351
int getOutputType()
Definition: coVRShader.h:243
Definition: coVRShader.h:156
void setMin(const std::string &m)
Definition: coVRShader.h:92