OpenCOVER
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
32namespace covise
33{
34class TokenBuffer;
35}
36
37namespace osg
38{
39class Node;
40class Geode;
41}
42namespace opencover
43{
44class coVRShader;
45
46class COVEREXPORT coVRUniform
47{
48
49private:
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
62public:
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(bool b);
112 void setValue(float f);
113 void setValue(osg::Vec3 v);
114 void setValue(osg::Vec4 v);
115 void setWrapMode(std::string wm);
116 void setTexture(const char *textureFile, int texNum = 0);
117 bool doOverwrite() const
118 {
119 return overwrite;
120 }
121 bool isUnique() const
122 {
123 return unique;
124 }
125 osg::ref_ptr<osg::Uniform> uniform;
126 osg::ref_ptr<osg::Texture> texture;
127
128 coVRUniform(const coVRShader *shader, const std::string &name, const std::string &type, const std::string &value);
129 virtual ~coVRUniform();
130};
131
132class COVEREXPORT coVRAttribute
133{
134
135private:
136 std::string name;
137 std::string type;
138 std::string value;
139
140public:
141 const std::string &getName()
142 {
143 return name;
144 }
145 const std::string &getType()
146 {
147 return type;
148 }
149 const std::string &getValue()
150 {
151 return value;
152 }
153 coVRAttribute(const std::string &name, const std::string &type, const std::string &value);
154 virtual ~coVRAttribute();
155};
156
157class COVEREXPORT coVRShaderInstance
158{
159
160private:
161 std::list<osg::ref_ptr<osg::Uniform> > uniforms;
162 osg::Drawable *myDrawable;
163
164public:
165 coVRShaderInstance(osg::Drawable *d);
167 void addUniform(const osg::Uniform &u);
168 std::list<osg::ref_ptr<osg::Uniform> > &getUniforms()
169 {
170 return uniforms;
171 };
172 osg::Uniform *getUniform(const std::string &name);
173};
174
175class COVEREXPORT coVRShader
176{
177 friend class coVRShaderList;
178
179private:
180 std::string name;
181 std::string fileName;
182 std::string dir;
183 bool wasCloned;
184 std::list<coVRUniform *> uniforms;
185 std::list<coVRAttribute *> attributes;
186 std::list<coVRShaderInstance *> instances;
187 osg::ref_ptr<osg::Shader> fragmentShader;
188 osg::ref_ptr<osg::Shader> geometryShader;
189 osg::ref_ptr<osg::Shader> vertexShader;
190 osg::ref_ptr<osg::Shader> tessControlShader;
191 osg::ref_ptr<osg::Shader> tessEvalShader;
192 osg::ref_ptr<osg::Program> program;
193 bool transparent; // the shader is transparent regardless of the users wishes
194 bool opaque; // the shader is opaque regardless of the users wishes
195 int geomParams[3];
196 int cullFace;
197
198public:
199 std::string findAsset(const std::string &path) const;
200 const std::string &getName()
201 {
202 return name;
203 }
205 {
206 return transparent;
207 };
208 std::list<coVRUniform *> &getUniforms()
209 {
210 return uniforms;
211 };
212 osg::ref_ptr<osg::Shader> &getFragmentShader()
213 {
214 return fragmentShader;
215 };
216 osg::ref_ptr<osg::Shader> &getGeometryShader()
217 {
218 return geometryShader;
219 };
220 osg::ref_ptr<osg::Shader> &getVertexShader()
221 {
222 return vertexShader;
223 };
224 osg::ref_ptr<osg::Shader> &getTessControlShader()
225 {
226 return tessControlShader;
227 };
228 osg::ref_ptr<osg::Shader> &getTessEvalShader()
229 {
230 return tessEvalShader;
231 };
232 osg::ref_ptr<osg::Program> &getProgram()
233 {
234 return program;
235 };
237 {
238 return geomParams[0];
239 };
241 {
242 return geomParams[1];
243 };
245 {
246 return geomParams[2];
247 };
248 bool isClone()
249 {
250 return wasCloned;
251 }
252 coVRShader(const std::string &name, const std::string &d);
253 coVRShader(const coVRShader &other);
254 void setData(covise::TokenBuffer &tb);
255 void setMatrixUniform(const std::string &name, osg::Matrixd m);
256 void setMatrixUniform(const std::string &name, osg::Matrixf m);
257 void setBoolUniform(const std::string &name, bool b);
258 void setFloatUniform(const std::string &name, float f);
259 void setVec3Uniform(const std::string &name, osg::Vec3 v);
260 void setVec4Uniform(const std::string &name, osg::Vec4 v);
261 void setNumVertices(int);
262 void setInputType(int);
263 void setOutputType(int);
264 osg::Uniform *getUniform(const std::string &name);
265 // void remove(osg::Node *);
267 void apply(osg::StateSet *);
268 coVRShaderInstance *apply(osg::Geode *geode, osg::Drawable *drawable);
269 void setUniformesFromAttribute(const char *uniformValues);
270
273
274 virtual ~coVRShader();
275};
276
277class COVEREXPORT coVRShaderList : public std::list<coVRShader *>
278{
279private:
280 static coVRShaderList *s_instance;
282 void loadMaterials();
283 std::vector<osg::ref_ptr<osg::Uniform>> lightEnabled;
284 osg::ref_ptr<osg::Uniform> timeUniform;
285 osg::ref_ptr<osg::Uniform> timeStepUniform;
286 osg::ref_ptr<osg::Uniform> lightMatrix;
287 osg::ref_ptr<osg::Uniform> projectionMatrix; //neue Projektionsmatrix
288 osg::ref_ptr<osg::Uniform> viewMatrix;
289 osg::ref_ptr<osg::Uniform> durationUniform;
290 osg::ref_ptr<osg::Uniform> viewportWidthUniform;
291 osg::ref_ptr<osg::Uniform> viewportHeightUniform;
292 osg::ref_ptr<osg::Uniform> stereoUniform; // 0 = LEFT, 1 = RIGHT
293 void applyParams(coVRShader *shader, std::map<std::string, std::string> *params);
294public:
296 coVRShader *get(const std::string &name, std::map<std::string, std::string> *params = NULL);
297 coVRShader *getUnique(const std::string &n, std::map<std::string, std::string> *params = NULL);
298 coVRShader *add(const std::string &name, std::string &dirName);
300 void setData(covise::TokenBuffer &tb);
301 osg::Uniform *getLightEnabled(size_t lightnum);
302 osg::Uniform *getTime();
303 osg::Uniform *getTimeStep();
304 osg::Uniform *getLight();
305 osg::Uniform *getProjection(); // neue Projektionsmatrix
306 osg::Uniform *getView();
307 osg::Uniform *getDuration();
308 osg::Uniform *getViewportWidth();
309 osg::Uniform *getViewportHeight();
310 osg::Uniform *getStereo();
311 void update();
312
313 void init();
314 void remove(osg::Node *);
315};
316
317class COVEREXPORT ShaderNode : public osg::Drawable
318{
319
320public:
322 {
323 Left = 128,
324 Right = 256
325 };
327 virtual ~ShaderNode();
329 virtual void drawImplementation(osg::RenderInfo &renderInfo) const;
332 virtual osg::Object *cloneType() const;
333
336 virtual osg::Object *clone(const osg::CopyOp &) const;
338
339private:
340};
341
342
343
355class coTangentSpaceGenerator : public osg::Referenced {
356public:
358 coTangentSpaceGenerator(const coTangentSpaceGenerator &copy, const osg::CopyOp &copyop = osg::CopyOp::SHALLOW_COPY);
359
360 void generate(osg::Geometry *geo);
361
362 inline osg::Vec4Array *getTangentArray() { return T_.get(); }
363 inline const osg::Vec4Array *getTangentArray() const { return T_.get(); }
364 inline void setTangentArray(osg::Vec4Array *array) { T_ = array; }
365
366 inline osg::Vec4Array *getNormalArray() { return N_.get(); }
367 inline const osg::Vec4Array *getNormalArray() const { return N_.get(); }
368 inline void setNormalArray(osg::Vec4Array *array) { N_ = array; }
369
370 inline osg::Vec4Array *getBinormalArray() { return B_.get(); }
371 inline const osg::Vec4Array *getBinormalArray() const { return B_.get(); }
372 inline void setBinormalArray(osg::Vec4Array *array) { B_ = array; }
373
374 inline osg::IndexArray *getIndices() { return indices_.get(); }
375
376protected:
377
380
381 void compute(osg::PrimitiveSet *pset,
382 const osg::Array *vx,
383 const osg::Array *nx,
384 int iA, int iB, int iC);
385
386 osg::ref_ptr<osg::Vec4Array> T_;
387 osg::ref_ptr<osg::Vec4Array> B_;
388 osg::ref_ptr<osg::Vec4Array> N_;
389 osg::ref_ptr<osg::UIntArray> indices_;
390};
391
392
393}
394#endif
Definition: ARToolKit.h:33
@ Right
Definition: MultiChannelDrawer.h:31
@ Left
Definition: MultiChannelDrawer.h:30
Definition: coClusterStat.h:29
Definition: coTabletUI.h:51
Definition: coVRShader.h:47
coVRUniform(const coVRShader *shader, const std::string &name, const std::string &type, const std::string &value)
void setValue(osg::Vec4 v)
osg::ref_ptr< osg::Texture > texture
Definition: coVRShader.h:126
void setValue(osg::Vec3 v)
const std::string & getMin() const
Definition: coVRShader.h:76
void setValue(float f)
osg::ref_ptr< osg::Uniform > uniform
Definition: coVRShader.h:125
void setMin(const std::string &m)
Definition: coVRShader.h:92
bool doOverwrite() const
Definition: coVRShader.h:117
void setOverwrite(bool o)
Definition: coVRShader.h:100
const std::string & getValue() const
Definition: coVRShader.h:72
void setUnique(bool u)
Definition: coVRShader.h:104
void setValue(osg::Matrixd m)
void setValue(const char *value)
osg::Texture::WrapMode getWrapMode() const
void setValue(osg::Matrixf m)
const std::string & getTextureFileName() const
Definition: coVRShader.h:84
void setWrapMode(std::string wm)
const std::string & getType() const
Definition: coVRShader.h:68
const std::string * getCubeMapFiles() const
Definition: coVRShader.h:88
const std::string & getMax() const
Definition: coVRShader.h:80
void setTexture(const char *textureFile, int texNum=0)
const std::string & getName() const
Definition: coVRShader.h:63
void setMax(const std::string &m)
Definition: coVRShader.h:96
bool isUnique() const
Definition: coVRShader.h:121
Definition: coVRShader.h:133
const std::string & getName()
Definition: coVRShader.h:141
coVRAttribute(const std::string &name, const std::string &type, const std::string &value)
const std::string & getType()
Definition: coVRShader.h:145
const std::string & getValue()
Definition: coVRShader.h:149
Definition: coVRShader.h:158
void addUniform(const osg::Uniform &u)
std::list< osg::ref_ptr< osg::Uniform > > & getUniforms()
Definition: coVRShader.h:168
osg::Uniform * getUniform(const std::string &name)
coVRShaderInstance(osg::Drawable *d)
Definition: coVRShader.h:176
coVRShaderInstance * apply(osg::Geode *geode, osg::Drawable *drawable)
void setVec3Uniform(const std::string &name, osg::Vec3 v)
void setBoolUniform(const std::string &name, bool b)
osg::ref_ptr< osg::Shader > & getTessEvalShader()
Definition: coVRShader.h:228
void setMatrixUniform(const std::string &name, osg::Matrixd m)
coVRShaderInstance * apply(osg::Node *)
int getNumVertices()
Definition: coVRShader.h:236
osg::ref_ptr< osg::Shader > & getGeometryShader()
Definition: coVRShader.h:216
void setMatrixUniform(const std::string &name, osg::Matrixf m)
std::string findAsset(const std::string &path) const
int getInputType()
Definition: coVRShader.h:240
osg::ref_ptr< osg::Program > & getProgram()
Definition: coVRShader.h:232
bool isTransparent()
Definition: coVRShader.h:204
void setVec4Uniform(const std::string &name, osg::Vec4 v)
std::list< coVRUniform * > & getUniforms()
Definition: coVRShader.h:208
osg::Uniform * getUniform(const std::string &name)
void setData(covise::TokenBuffer &tb)
const std::string & getName()
Definition: coVRShader.h:200
osg::ref_ptr< osg::Shader > & getVertexShader()
Definition: coVRShader.h:220
void setFloatUniform(const std::string &name, float f)
coVRShader(const coVRShader &other)
bool isClone()
Definition: coVRShader.h:248
void apply(osg::StateSet *)
coVRShader(const std::string &name, const std::string &d)
int getOutputType()
Definition: coVRShader.h:244
osg::ref_ptr< osg::Shader > & getTessControlShader()
Definition: coVRShader.h:224
void setUniformesFromAttribute(const char *uniformValues)
osg::ref_ptr< osg::Shader > & getFragmentShader()
Definition: coVRShader.h:212
Definition: coVRShader.h:278
coVRShader * getUnique(const std::string &n, std::map< std::string, std::string > *params=NULL)
osg::Uniform * getTimeStep()
void remove(osg::Node *)
osg::Uniform * getLightEnabled(size_t lightnum)
osg::Uniform * getStereo()
osg::Uniform * getProjection()
coVRShader * add(const std::string &name, std::string &dirName)
osg::Uniform * getViewportWidth()
static coVRShaderList * instance()
void setData(covise::TokenBuffer &tb)
osg::Uniform * getDuration()
coVRShader * get(const std::string &name, std::map< std::string, std::string > *params=NULL)
osg::Uniform * getViewportHeight()
osg::Uniform * getView()
osg::Uniform * getLight()
osg::Uniform * getTime()
Definition: coVRShader.h:318
virtual osg::Object * cloneType() const
static ShaderNode * theNode
Definition: coVRShader.h:328
ShaderNode(StereoView v)
virtual void drawImplementation(osg::RenderInfo &renderInfo) const
StereoView
Definition: coVRShader.h:322
virtual osg::Object * clone(const osg::CopyOp &) const
StereoView view
Definition: coVRShader.h:337
Definition: coVRShader.h:355
void generate(osg::Geometry *geo)
void setNormalArray(osg::Vec4Array *array)
Definition: coVRShader.h:368
const osg::Vec4Array * getBinormalArray() const
Definition: coVRShader.h:371
const osg::Vec4Array * getTangentArray() const
Definition: coVRShader.h:363
osg::Vec4Array * getTangentArray()
Definition: coVRShader.h:362
osg::Vec4Array * getNormalArray()
Definition: coVRShader.h:366
void setBinormalArray(osg::Vec4Array *array)
Definition: coVRShader.h:372
osg::ref_ptr< osg::Vec4Array > T_
Definition: coVRShader.h:386
const osg::Vec4Array * getNormalArray() const
Definition: coVRShader.h:367
void compute(osg::PrimitiveSet *pset, const osg::Array *vx, const osg::Array *nx, int iA, int iB, int iC)
osg::IndexArray * getIndices()
Definition: coVRShader.h:374
osg::ref_ptr< osg::Vec4Array > B_
Definition: coVRShader.h:387
void setTangentArray(osg::Vec4Array *array)
Definition: coVRShader.h:364
coTangentSpaceGenerator & operator=(const coTangentSpaceGenerator &)
Definition: coVRShader.h:379
osg::ref_ptr< osg::Vec4Array > N_
Definition: coVRShader.h:388
coTangentSpaceGenerator(const coTangentSpaceGenerator &copy, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
virtual ~coTangentSpaceGenerator()
Definition: coVRShader.h:378
osg::ref_ptr< osg::UIntArray > indices_
Definition: coVRShader.h:389
osg::Vec4Array * getBinormalArray()
Definition: coVRShader.h:370