1 // non fixed Opengl pipeline rendering
3 // Written by Harald JOHNSEN, started Jully 2005.
5 // Copyright (C) 2005 Harald JOHNSEN
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 # include <simgear_config.h>
30 #include <osg/Program>
32 #include <osg/StateSet>
33 #include <osg/TextureCubeMap>
34 #include <osg/TexEnvCombine>
36 #include <osg/Texture1D>
37 #include <osgUtil/HighlightMapGenerator>
39 #include <simgear/scene/util/SGUpdateVisitor.hxx>
40 #include <simgear/threads/SGThread.hxx>
41 #include <simgear/threads/SGGuard.hxx>
43 #include <simgear/props/condition.hxx>
44 #include <simgear/props/props.hxx>
46 #include <simgear/debug/logstream.hxx>
48 #include "animation.hxx"
53 <shader>fresnel</shader>
54 <object-name>...</object-name>
59 <shader>heat-haze</shader>
60 <object-name>...</object-name>
62 <speed-prop>...</speed-prop>
64 <factor-prop>...</factor-prop>
69 <shader>chrome</shader>
70 <texture>...</texture>
71 <object-name>...</object-name>
77 <object-name>...</object-name>
78 <depth-test>false</depth-test>
84 class SGMapGenCallback :
85 public osg::StateAttribute::Callback {
87 virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
89 SGUpdateVisitor* updateVisitor = dynamic_cast<SGUpdateVisitor*>(nv);
93 if (distSqr(_lastLightDirection, updateVisitor->getLightDirection()) < 1e-4
94 && distSqr(_lastLightColor, updateVisitor->getAmbientLight()) < 1e-4)
97 _lastLightDirection = updateVisitor->getLightDirection();
98 _lastLightColor = updateVisitor->getAmbientLight();
100 osg::TextureCubeMap *tcm = static_cast<osg::TextureCubeMap*>(sa);
102 // FIXME: need an update or callback ...
103 // generate the six highlight map images (light direction = [1, 1, -1])
104 osg::ref_ptr<osgUtil::HighlightMapGenerator> mapgen;
105 mapgen = new osgUtil::HighlightMapGenerator(_lastLightDirection.osg(),
106 _lastLightColor.osg(), 5);
107 mapgen->generateMap();
109 // assign the six images to the texture object
110 tcm->setImage(osg::TextureCubeMap::POSITIVE_X,
111 mapgen->getImage(osg::TextureCubeMap::POSITIVE_X));
112 tcm->setImage(osg::TextureCubeMap::NEGATIVE_X,
113 mapgen->getImage(osg::TextureCubeMap::NEGATIVE_X));
114 tcm->setImage(osg::TextureCubeMap::POSITIVE_Y,
115 mapgen->getImage(osg::TextureCubeMap::POSITIVE_Y));
116 tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y,
117 mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Y));
118 tcm->setImage(osg::TextureCubeMap::POSITIVE_Z,
119 mapgen->getImage(osg::TextureCubeMap::POSITIVE_Z));
120 tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z,
121 mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Z));
124 SGVec3f _lastLightDirection;
125 SGVec4f _lastLightColor;
128 static osg::TextureCubeMap*
129 getOrCreateTextureCubeMap()
131 static osg::ref_ptr<osg::TextureCubeMap> textureCubeMap;
132 if (textureCubeMap.get())
133 return textureCubeMap.get();
135 static SGMutex mutex;
136 SGGuard<SGMutex> locker(mutex);
137 if (textureCubeMap.get())
138 return textureCubeMap.get();
140 // create and setup the texture object
141 textureCubeMap = new osg::TextureCubeMap;
143 textureCubeMap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
144 textureCubeMap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
145 textureCubeMap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP);
146 textureCubeMap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
147 textureCubeMap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
149 textureCubeMap->setUpdateCallback(new SGMapGenCallback);
151 return textureCubeMap.get();
154 static void create_specular_highlights(osg::Node *node)
156 osg::StateSet *ss = node->getOrCreateStateSet();
158 // create and setup the texture object
159 osg::TextureCubeMap *tcm = getOrCreateTextureCubeMap();
161 // enable texturing, replacing any textures in the subgraphs
162 ss->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
164 // texture coordinate generation
165 osg::TexGen *tg = new osg::TexGen;
166 tg->setMode(osg::TexGen::REFLECTION_MAP);
167 ss->setTextureAttributeAndModes(0, tg, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
169 // use TexEnvCombine to add the highlights to the original lighting
170 osg::TexEnvCombine *te = new osg::TexEnvCombine;
171 te->setCombine_RGB(osg::TexEnvCombine::ADD);
172 te->setSource0_RGB(osg::TexEnvCombine::TEXTURE);
173 te->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
174 te->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
175 te->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
176 ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
180 SGShaderAnimation::SGShaderAnimation(const SGPropertyNode* configNode,
181 SGPropertyNode* modelRoot) :
182 SGAnimation(configNode, modelRoot)
184 const SGPropertyNode* node = configNode->getChild("texture");
186 _effect_texture = SGLoadTexture2D(node->getStringValue());
190 class ChromeLightingCallback :
191 public osg::StateAttribute::Callback {
193 virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
195 SGUpdateVisitor* updateVisitor = dynamic_cast<SGUpdateVisitor*>(nv);
198 osg::TexEnvCombine *combine = dynamic_cast<osg::TexEnvCombine *>(sa);
201 // An approximation for light reflected back by chrome.
202 osg::Vec4 globalColor = (updateVisitor->getAmbientLight().osg() * .4f
203 + updateVisitor->getDiffuseLight().osg());
204 globalColor.a() = 1.0f;
205 combine->setConstantColor(globalColor);
209 typedef map<osg::ref_ptr<osg::Texture2D>, osg::ref_ptr<osg::StateSet> >
213 // The chrome effect is mixed by the alpha channel of the texture
214 // on the model, which will be attached to a node lower in the scene
215 // graph: 0 -> completely chrome, 1 -> completely model texture.
216 static void create_chrome(osg::Group* group, osg::Texture2D* texture)
218 static SGMutex mutex;
219 SGGuard<SGMutex> locker(mutex);
220 static StateSetMap chromeMap;
221 osg::StateSet *stateSet;
222 StateSetMap::iterator iterator = chromeMap.find(texture);
223 if (iterator != chromeMap.end()) {
224 stateSet = iterator->second.get();
226 stateSet = new osg::StateSet;
227 // If the model doesn't have any texture, we need to have one
228 // activated so that we don't need a seperate combiner
229 // attribute for the non-textured case. This texture will be
230 // shadowed by any attached to the model.
231 osg::Image *dummyImage = new osg::Image;
232 dummyImage->allocateImage(1, 1, 1, GL_LUMINANCE_ALPHA,
234 unsigned char* imageBytes = dummyImage->data(0, 0);
237 osg::Texture2D* dummyTexture = new osg::Texture2D;
238 dummyTexture->setImage(dummyImage);
239 dummyTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
240 dummyTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
241 stateSet->setTextureAttributeAndModes(0, dummyTexture,
242 osg::StateAttribute::ON);
243 osg::TexEnvCombine* combine0 = new osg::TexEnvCombine;
244 osg::TexEnvCombine* combine1 = new osg::TexEnvCombine;
245 osg::TexGen* texGen = new osg::TexGen;
246 // Mix the environmental light color and the chrome map on texture
248 combine0->setCombine_RGB(osg::TexEnvCombine::MODULATE);
249 combine0->setSource0_RGB(osg::TexEnvCombine::CONSTANT);
250 combine0->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
251 combine0->setSource1_RGB(osg::TexEnvCombine::TEXTURE1);
252 combine0->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
253 combine0->setDataVariance(osg::Object::DYNAMIC);
254 combine0->setUpdateCallback(new ChromeLightingCallback);
256 // Interpolate the colored chrome map with the texture on the
257 // model, using the model texture's alpha.
258 combine1->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE);
259 combine1->setSource0_RGB(osg::TexEnvCombine::TEXTURE0);
260 combine1->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
261 combine1->setSource1_RGB(osg::TexEnvCombine::PREVIOUS);
262 combine1->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
263 combine1->setSource2_RGB(osg::TexEnvCombine::TEXTURE0);
264 combine1->setOperand2_RGB(osg::TexEnvCombine::SRC_ALPHA);
265 // Are these used for anything?
266 combine1->setCombine_Alpha(osg::TexEnvCombine::REPLACE);
267 combine1->setSource0_Alpha(osg::TexEnvCombine::TEXTURE1);
268 combine1->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA);
270 texGen->setMode(osg::TexGen::SPHERE_MAP);
271 stateSet->setTextureAttribute(0, combine0);
272 stateSet->setTextureAttribute(1, combine1);
273 stateSet->setTextureAttributeAndModes(1, texture,
274 osg::StateAttribute::ON);
275 stateSet->setTextureAttributeAndModes(1, texGen,
276 osg::StateAttribute::ON);
277 chromeMap[texture] = stateSet;
279 group->setStateSet(stateSet);
283 SGShaderAnimation::createAnimationGroup(osg::Group& parent)
285 osg::Group* group = new osg::Group;
286 group->setName("shader animation");
287 parent.addChild(group);
289 std::string shader_name = getConfig()->getStringValue("shader", "");
290 // if( shader_name == "fresnel" || shader_name == "reflection" )
292 // else if( shader_name == "heat-haze" )
295 if( shader_name == "chrome")
297 create_specular_highlights(group);
299 create_chrome(group, _effect_texture.get());
304 // static Shader *shFresnel=NULL;
305 // static GLuint texFresnel = 0;
307 // static GLuint texBackground = 0;
308 // static int texBackgroundWidth = 1024, texBackgroundHeight = 1024;
309 // static GLenum texBackgroundTarget = GL_TEXTURE_2D;
310 // static bool isRectangleTextureSupported = false;
311 // static bool istexBackgroundRectangle = false;
312 // static bool initDone = false;
313 static bool haveBackground = false;
315 // static glActiveTextureProc glActiveTexturePtr = 0;
316 // static sgMat4 shadIdentMatrix;
319 // static int null_shader_callback( ssgEntity *e ) {
321 // ssgLeaf *leaf = (ssgLeaf *) e;
322 // #ifdef _SSG_USE_DLIST
323 // dlist = leaf->getDListIndex();
325 // leaf->makeDList();
326 // dlist = leaf->getDListIndex();
331 // ssgSimpleState *sst = ((ssgSimpleState *)leaf->getState());
335 // SGShaderAnimation *my_shader = (SGShaderAnimation *) ( e->getUserData() );
336 // if( ! my_shader->_depth_test )
337 // glDisable( GL_DEPTH_TEST );
338 // glCallList ( dlist ) ;
340 // if( ! my_shader->_depth_test )
341 // glEnable( GL_DEPTH_TEST );
347 // static int heat_haze_shader_callback( ssgEntity *e ) {
348 // if( ! ((SGShadowAnimation *)e->getUserData())->get_condition_value() )
352 // ssgLeaf *leaf = (ssgLeaf *) e;
353 // #ifdef _SSG_USE_DLIST
354 // dlist = leaf->getDListIndex();
356 // leaf->makeDList();
357 // dlist = leaf->getDListIndex();
363 // GLint viewport[4];
364 // glGetIntegerv( GL_VIEWPORT, viewport );
365 // const int screen_width = viewport[2];
366 // const int screen_height = viewport[3];
367 // if( ! haveBackground ) {
368 // // store the backbuffer in a texture
369 // if( ! texBackground ) {
370 // // allocate our texture here so we don't waste memory if no model use that effect
371 // // check if we need a rectangle texture and if the card support it
372 // if( (screen_width > 1024 || screen_height > 1024) && isRectangleTextureSupported ) {
373 // // Note that the 3 (same) extensions use the same enumerants
374 // texBackgroundTarget = GL_TEXTURE_RECTANGLE_NV;
375 // istexBackgroundRectangle = true;
376 // texBackgroundWidth = screen_width;
377 // texBackgroundHeight = screen_height;
379 // glGenTextures(1, &texBackground);
380 // glEnable(texBackgroundTarget);
381 // glBindTexture(texBackgroundTarget, texBackground);
382 // // trying to match the backbuffer pixel format
383 // GLint internalFormat = GL_RGB8;
384 // GLint colorBits = 0, alphaBits = 0;
385 // glGetIntegerv( GL_BLUE_BITS, &colorBits );
386 // glGetIntegerv( GL_ALPHA_BITS, &alphaBits );
387 // if(colorBits == 5) {
388 // if( alphaBits == 0 )
389 // internalFormat = GL_RGB5;
391 // internalFormat = GL_RGB5_A1;
393 // if( alphaBits != 0 )
394 // internalFormat = GL_RGBA8;
396 // glTexImage2D(texBackgroundTarget, 0, internalFormat,
397 // texBackgroundWidth, texBackgroundHeight, 0, GL_RGB, GL_FLOAT, NULL);
399 // glTexParameteri(texBackgroundTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
400 // glTexParameteri(texBackgroundTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
401 // glTexParameteri(texBackgroundTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
402 // glTexParameteri(texBackgroundTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
404 // glEnable(texBackgroundTarget);
405 // glBindTexture(texBackgroundTarget, texBackground);
406 // // center of texture = center of screen
407 // // obviously we don't have the whole screen if screen_width > texBackgroundWidth
408 // // if rectangle textures are not supported, this give some artifacts on the borders
409 // if( istexBackgroundRectangle ) {
410 // glCopyTexSubImage2D( texBackgroundTarget, 0, 0, 0,
411 // 0, 0, texBackgroundWidth, texBackgroundHeight );
413 // glCopyTexSubImage2D( texBackgroundTarget, 0, 0, 0,
414 // (screen_width - texBackgroundWidth) / 2,
415 // (screen_height - texBackgroundHeight) / 2,
416 // texBackgroundWidth, texBackgroundHeight );
418 // haveBackground = true;
419 // glBindTexture(texBackgroundTarget, 0);
420 // glDisable(texBackgroundTarget);
422 // ssgSimpleState *sst = ((ssgSimpleState *)leaf->getState());
426 // SGShaderAnimation *my_shader = (SGShaderAnimation *) ( e->getUserData() );
427 // if( ! my_shader->_depth_test )
428 // glDisable( GL_DEPTH_TEST );
429 // glDepthMask( GL_FALSE );
430 // glDisable( GL_LIGHTING );
432 // // noise texture, tex coord from the model translated by a time factor
433 // glActiveTexturePtr( GL_TEXTURE0_ARB );
434 // glEnable(GL_TEXTURE_2D);
435 // const float noiseDist = fmod(- totalTime * my_shader->_factor * my_shader->_speed, 4.0);
436 // glMatrixMode(GL_TEXTURE);
438 // glTranslatef( noiseDist, 0.0f, 0.0f );
439 // glMatrixMode(GL_MODELVIEW);
441 // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
443 // // background texture
444 // glActiveTexturePtr( GL_TEXTURE1_ARB );
445 // glEnable(texBackgroundTarget);
446 // glBindTexture(texBackgroundTarget, texBackground);
448 // // automatic generation of texture coordinates
449 // // map to screen space
450 // sgMat4 CameraProjM, CameraViewM;
451 // glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat *) CameraProjM);
452 // glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *) CameraViewM);
453 // // const float dummy_scale = 1.0f; //0.95f;
454 // const float deltaPos = 0.05f;
455 // glMatrixMode(GL_TEXTURE);
457 // if( istexBackgroundRectangle ) {
458 // // coords go from 0.0 to n, not from 0.0 to 1.0
459 // glTranslatef( texBackgroundWidth * 0.5f, texBackgroundHeight * 0.5f, 0.0f );
460 // glScalef( texBackgroundWidth * 0.5f,
461 // texBackgroundHeight * 0.5f, 1.0f );
463 // glTranslatef( 0.5f, 0.5f, 0.0f );
464 // glScalef( float( screen_width ) / float( texBackgroundWidth ) * 0.5f,
465 // float( screen_height ) / float( texBackgroundHeight ) * 0.5f, 1.0f );
467 // glMultMatrixf( (GLfloat *) CameraProjM );
468 // glMultMatrixf( (GLfloat *) CameraViewM );
469 // glTranslatef( deltaPos, deltaPos, deltaPos );
470 // glMatrixMode(GL_MODELVIEW);
472 // glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
473 // glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
474 // glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
475 // glTexGeni( GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
476 // glTexGenfv( GL_S, GL_EYE_PLANE, shadIdentMatrix[0] );
477 // glTexGenfv( GL_T, GL_EYE_PLANE, shadIdentMatrix[1] );
478 // glTexGenfv( GL_R, GL_EYE_PLANE, shadIdentMatrix[2] );
479 // glTexGenfv( GL_Q, GL_EYE_PLANE, shadIdentMatrix[3] );
480 // glEnable( GL_TEXTURE_GEN_S );
481 // glEnable( GL_TEXTURE_GEN_T );
482 // glEnable( GL_TEXTURE_GEN_R );
483 // glEnable( GL_TEXTURE_GEN_Q );
485 // sgVec4 enviro = {1.00f, 1.00f, 1.00f, 0.85f};
487 // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
488 // glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE );
489 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
490 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR );
491 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_CONSTANT_ARB );
492 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR );
493 // glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, enviro);
495 // glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);
496 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE0_ARB);
497 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
498 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PRIMARY_COLOR_ARB );
499 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA );
501 // glCallList ( dlist ) ;
502 // glMatrixMode(GL_TEXTURE);
503 // glTranslatef( - deltaPos*2.0f, -deltaPos*2.5f, -deltaPos*2.0f );
504 // glMatrixMode(GL_MODELVIEW);
505 // glCallList ( dlist ) ;
507 // // alter colors only on last rendering
508 // // sgVec4 fLight = {0.93f, 0.93f, 1.00f, 0.85f};
509 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR_ARB );
510 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR );
512 // glMatrixMode(GL_TEXTURE);
513 // glTranslatef( deltaPos*0.7f, deltaPos*1.7f, deltaPos*0.7f );
514 // glMatrixMode(GL_MODELVIEW);
515 // glCallList ( dlist ) ;
518 // glActiveTexturePtr( GL_TEXTURE1_ARB );
519 // glDisable( GL_TEXTURE_GEN_S );
520 // glDisable( GL_TEXTURE_GEN_T );
521 // glDisable( GL_TEXTURE_GEN_R );
522 // glDisable( GL_TEXTURE_GEN_Q );
523 // glMatrixMode(GL_TEXTURE);
525 // glMatrixMode(GL_MODELVIEW);
526 // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
527 // glDisable(texBackgroundTarget);
528 // glActiveTexturePtr( GL_TEXTURE0_ARB );
529 // glMatrixMode(GL_TEXTURE);
531 // glMatrixMode(GL_MODELVIEW);
532 // glEnable(GL_TEXTURE_2D);
533 // glBindTexture(GL_TEXTURE_2D, 0);
536 // if( ! my_shader->_depth_test )
537 // glEnable( GL_DEPTH_TEST );
539 // glEnable( GL_LIGHTING );
540 // glDepthMask( GL_TRUE );
548 // static int fresnel_shader_callback( ssgEntity *e ) {
549 // if( ! ((SGShadowAnimation *)e->getUserData())->get_condition_value() )
553 // ssgLeaf *leaf = (ssgLeaf *) e;
554 // #ifdef _SSG_USE_DLIST
555 // dlist = leaf->getDListIndex();
557 // leaf->makeDList();
558 // dlist = leaf->getDListIndex();
563 // ssgSimpleState *sst = ((ssgSimpleState *)leaf->getState());
567 // sgVec4 sunColor, ambientColor;
568 // ssgGetLight( 0 )->getColour(GL_DIFFUSE, sunColor );
569 // ssgGetLight( 0 )->getColour(GL_AMBIENT, ambientColor );
571 // // SGShaderAnimation *my_shader = (SGShaderAnimation *) ( e->getUserData() );
572 // glEnable(GL_BLEND);
573 // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
574 // glEnable(GL_ALPHA_TEST);
575 // glAlphaFunc(GL_GREATER, 0.0f);
578 // // sgVec4 R = {0.5,0.0,0.0,0.0};
579 // sgVec4 enviro = {1.0,0.0,0.0,1.0};
580 // // sgCopyVec4( enviro, sunColor );
581 // glActiveTexturePtr( GL_TEXTURE0_ARB );
582 // glEnable(GL_TEXTURE_2D);
583 // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
584 // glActiveTexturePtr( GL_TEXTURE1_ARB );
585 // glDisable(GL_TEXTURE_2D);
586 // glEnable(GL_TEXTURE_1D);
587 // glBindTexture(GL_TEXTURE_1D, texFresnel);
588 // // c = a0 * a2 + a1 * (1-a2)
589 // // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
590 // // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
591 // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
592 // glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE_ARB );
593 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_CONSTANT_ARB );
594 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR );
595 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
596 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR );
597 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE );
598 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_COLOR );
599 // glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, enviro);
600 // shFresnel->enable();
601 // shFresnel->bind();
602 // glCallList ( dlist ) ;
603 // shFresnel->disable();
604 // glActiveTexturePtr( GL_TEXTURE1_ARB );
605 // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
606 // glDisable(GL_TEXTURE_1D);
607 // glActiveTexturePtr( GL_TEXTURE0_ARB );
608 // glDisable(GL_TEXTURE_1D);
609 // glEnable(GL_TEXTURE_2D);
612 // // glBindTexture(GL_TEXTURE_2D, 0);
613 // // glDepthFunc(GL_LESS);
614 // // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
624 // static int chrome_shader_callback( ssgEntity *e ) {
625 // if( ! ((SGShadowAnimation *)e->getUserData())->get_condition_value() )
629 // ssgLeaf *leaf = (ssgLeaf *) e;
630 // #ifdef _SSG_USE_DLIST
631 // dlist = leaf->getDListIndex();
633 // leaf->makeDList();
634 // dlist = leaf->getDListIndex();
639 // ssgSimpleState *sst = ((ssgSimpleState *)leaf->getState());
643 // SGShaderAnimation *my_shader = (SGShaderAnimation *) ( e->getUserData() );
644 // if( ! my_shader->_depth_test )
645 // glDisable( GL_DEPTH_TEST );
647 // GLint maskTexComponent = 3;
648 // glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &maskTexComponent);
650 // // The fake env chrome texture
651 // glActiveTexturePtr( GL_TEXTURE1_ARB );
652 // glEnable(GL_TEXTURE_2D);
654 // // No lighting is computed in spherical mapping mode because the environment
655 // // is supposed to be allready lighted. We must reshade our environment texture.
656 // sgVec4 sunColor, ambientColor, envColor;
657 // ssgGetLight( 0 )->getColour(GL_DIFFUSE, sunColor );
658 // ssgGetLight( 0 )->getColour(GL_AMBIENT, ambientColor );
659 // sgAddScaledVec3( envColor, ambientColor, sunColor, 0.4f);
660 // glBindTexture(GL_TEXTURE_2D, my_shader->_effectTexture->getHandle());
662 // sgVec3 delta_light;
663 // sgSubVec3(delta_light, envColor, my_shader->_envColor);
664 // if( (fabs(delta_light[0]) + fabs(delta_light[1]) + fabs(delta_light[2])) > 0.05f ) {
665 // sgCopyVec3( my_shader->_envColor, envColor );
666 // // reload the texture data and let the driver reshade it for us
667 // glPixelTransferf( GL_RED_SCALE, envColor[0] );
668 // glPixelTransferf( GL_GREEN_SCALE, envColor[1] );
669 // glPixelTransferf( GL_BLUE_SCALE, envColor[2] );
670 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, my_shader->_texWidth, my_shader->_texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, my_shader->_textureData);
671 // glPixelTransferf( GL_RED_SCALE, 1.0f );
672 // glPixelTransferf( GL_GREEN_SCALE, 1.0f );
673 // glPixelTransferf( GL_BLUE_SCALE, 1.0f );
676 // if( maskTexComponent == 4 ) {
677 // // c = lerp(model tex, chrome tex, model tex alpha)
678 // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
679 // glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE_ARB );
680 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PREVIOUS_ARB );
681 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR );
682 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE );
683 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR );
684 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_PREVIOUS_ARB );
685 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA );
687 // glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
688 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
689 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
692 // glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
693 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
694 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR );
696 // glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
697 // glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
698 // glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
700 // // automatic generation of texture coordinates
703 // glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
704 // glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
705 // glEnable( GL_TEXTURE_GEN_S );
706 // glEnable( GL_TEXTURE_GEN_T );
708 // glCallList ( dlist ) ;
710 // glActiveTexturePtr( GL_TEXTURE1_ARB );
711 // glDisable( GL_TEXTURE_GEN_S );
712 // glDisable( GL_TEXTURE_GEN_T );
714 // glMatrixMode(GL_TEXTURE);
716 // glMatrixMode(GL_MODELVIEW);
718 // glDisable(GL_TEXTURE_2D);
719 // glBindTexture(GL_TEXTURE_2D, 0);
720 // glActiveTexturePtr( GL_TEXTURE0_ARB );
723 // if( ! my_shader->_depth_test )
724 // glEnable( GL_DEPTH_TEST );
733 // static void init_shaders(void) {
735 // if( false && Shader::is_VP_supported() ) {
736 // shFresnel = new Shader("/FlightGear/data/Textures/fresnel_vp.txt", "fresnel_vp");
738 // glActiveTexturePtr = (glActiveTextureProc) SGLookupFunction("glActiveTextureARB");
739 // const int fresnelSize = 512;
740 // unsigned char imageFresnel[ fresnelSize * 3 ];
741 // for(int i = 0; i < fresnelSize; i++) {
742 // const float R0 = 0.2f;
743 // float NdotV = float( i ) / float( fresnelSize );
744 // float f = R0 + (1.0f-R0)*pow(1.0f - NdotV, 5);
745 // unsigned char ff = (unsigned char) (f * 255.0);
746 // imageFresnel[i*3+0] = imageFresnel[i*3+1] = imageFresnel[i*3+2] = ff;
748 // glGenTextures( 1, &texFresnel );
749 // glBindTexture(GL_TEXTURE_1D, texFresnel );
750 // glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
751 // glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
752 // glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
753 // glTexParameteri(GL_TEXTURE_1D, GL_GENERATE_MIPMAP_SGIS, true);
754 // glTexImage1D(GL_TEXTURE_1D, 0, 3, fresnelSize, 0, GL_RGB, GL_UNSIGNED_BYTE, imageFresnel);
755 // glBindTexture(GL_TEXTURE_1D, 0 );
757 // sgMakeIdentMat4( shadIdentMatrix );
762 // ////////////////////////////////////////////////////////////////////////
763 // // Implementation of SGShaderAnimation
764 // ////////////////////////////////////////////////////////////////////////
766 SGShaderAnimation::SGShaderAnimation ( SGPropertyNode *prop_root,
767 SGPropertyNode_ptr props )
768 : SGAnimation(props, new osg::Group),
770 _condition_value(true),
772 _param_1(props->getFloatValue("param", 1.0f)),
773 _depth_test(props->getBoolValue("depth-test", true)),
774 _factor(props->getFloatValue("factor", 1.0f)),
776 _speed(props->getFloatValue("speed", 1.0f)),
783 SGPropertyNode_ptr node = props->getChild("condition");
785 _condition = sgReadCondition(prop_root, node);
786 _condition_value = false;
788 node = props->getChild("factor-prop");
790 _factor_prop = prop_root->getNode(node->getStringValue(), true);
791 node = props->getChild("speed-prop");
793 _speed_prop = prop_root->getNode(node->getStringValue(), true);
795 _envColor = osg::Vec4(0, 0, 0, 1);
796 node = props->getChild("texture");
798 _effectTexture = SGLoadTexture2D(node->getStringValue());
799 // glBindTexture(GL_TEXTURE_2D, _effectTexture->getHandle() );
800 // glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &_texWidth);
801 // glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &_texHeight);
803 // _textureData = new unsigned char[_texWidth * _texHeight * 3];
804 // glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, _textureData);
805 // glBindTexture(GL_TEXTURE_2D, 0 );
807 string shader_name = props->getStringValue("shader");
808 if( shader_name == "fresnel" || shader_name == "reflection" )
810 else if( shader_name == "heat-haze" )
812 else if( shader_name == "chrome" && _effectTexture.valid())
816 void SGShaderAnimation::init()
820 // if( _shader_type == 1 && Shader::is_VP_supported() && shFresnel)
821 // setCallBack( getBranch(), (ssgBase *) this, fresnel_shader_callback );
822 // else if( _shader_type == 2 ) {
823 // // this is the same extension with different names
824 // isRectangleTextureSupported = SGIsOpenGLExtensionSupported("GL_EXT_texture_rectangle") ||
825 // SGIsOpenGLExtensionSupported("GL_ARB_texture_rectangle") ||
826 // SGIsOpenGLExtensionSupported("GL_NV_texture_rectangle");
827 // setCallBack( getBranch(), (ssgBase *) this, heat_haze_shader_callback );
829 // else if( _shader_type == 3 )
830 // setCallBack( getBranch(), (ssgBase *) this, chrome_shader_callback );
832 // setCallBack( getBranch(), (ssgBase *) this, null_shader_callback );
835 SGShaderAnimation::~SGShaderAnimation()
842 SGShaderAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
845 _condition_value = _condition->test();
847 _factor = _factor_prop->getFloatValue();
849 _speed = _speed_prop->getFloatValue();
851 // OSGFIXME fiddle with totalTime
852 totalTime = nv->getFrameStamp()->getReferenceTime();
854 // note, callback is responsible for scenegraph traversal so
855 // should always include call traverse(node,nv) to ensure
856 // that the rest of cullbacks and the scene graph are traversed.