]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/shadanim.cxx
Move SGReadFileCallback from model.cxx to public class ModelRegistry
[simgear.git] / simgear / scene / model / shadanim.cxx
1 // non fixed Opengl pipeline rendering
2 //
3 // Written by Harald JOHNSEN, started Jully 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN
6 //
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.
11 //
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.
16 //
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.
20 //
21 //
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <map>
28
29 #include <osg/Group>
30 #include <osg/Program>
31 #include <osg/Shader>
32 #include <osg/StateSet>
33 #include <osg/TextureCubeMap>
34 #include <osg/TexEnvCombine>
35 #include <osg/TexGen>
36 #include <osg/Texture1D>
37 #include <osgUtil/HighlightMapGenerator>
38
39 #include <simgear/scene/util/SGUpdateVisitor.hxx>
40 #include <simgear/threads/SGThread.hxx>
41 #include <simgear/threads/SGGuard.hxx>
42
43 #include <simgear/props/condition.hxx>
44 #include <simgear/props/props.hxx>
45
46 #include <simgear/debug/logstream.hxx>
47
48 #include "animation.hxx"
49 #include "model.hxx"
50 /*
51     <animation>
52         <type>shader</type>
53         <shader>fresnel</shader>
54         <object-name>...</object-name>
55     </animation>
56
57     <animation>
58         <type>shader</type>
59         <shader>heat-haze</shader>
60         <object-name>...</object-name>
61         <speed>...</speed>
62         <speed-prop>...</speed-prop>
63         <factor>...</factor>
64         <factor-prop>...</factor-prop>
65     </animation>
66
67     <animation>
68         <type>shader</type>
69         <shader>chrome</shader>
70         <texture>...</texture>
71         <object-name>...</object-name>
72     </animation>
73
74     <animation>
75         <type>shader</type>
76         <shader></shader>
77         <object-name>...</object-name>
78         <depth-test>false</depth-test>
79     </animation>
80
81 */
82
83
84 class SGMapGenCallback :
85   public osg::StateAttribute::Callback {
86 public:
87   virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
88   {
89     SGUpdateVisitor* updateVisitor = dynamic_cast<SGUpdateVisitor*>(nv);
90     if (!updateVisitor)
91       return;
92
93     if (distSqr(_lastLightDirection, updateVisitor->getLightDirection()) < 1e-4
94         && distSqr(_lastLightColor, updateVisitor->getAmbientLight()) < 1e-4)
95       return;
96
97     _lastLightDirection = updateVisitor->getLightDirection();
98     _lastLightColor = updateVisitor->getAmbientLight();
99
100     osg::TextureCubeMap *tcm = static_cast<osg::TextureCubeMap*>(sa);
101
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();
108
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));
122   }
123 private:
124   SGVec3f _lastLightDirection;
125   SGVec4f _lastLightColor;
126 };
127
128 static osg::TextureCubeMap*
129 getOrCreateTextureCubeMap()
130 {
131   static osg::ref_ptr<osg::TextureCubeMap> textureCubeMap;
132   if (textureCubeMap.get())
133     return textureCubeMap.get();
134
135   static SGMutex mutex;
136   SGGuard<SGMutex> locker(mutex);
137   if (textureCubeMap.get())
138     return textureCubeMap.get();
139
140   // create and setup the texture object
141   textureCubeMap = new osg::TextureCubeMap;
142
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);    
148   
149   textureCubeMap->setUpdateCallback(new SGMapGenCallback);
150
151   return textureCubeMap.get();
152 }
153
154 static void create_specular_highlights(osg::Node *node)
155 {
156   osg::StateSet *ss = node->getOrCreateStateSet();
157   
158   // create and setup the texture object
159   osg::TextureCubeMap *tcm = getOrCreateTextureCubeMap();
160   
161   // enable texturing, replacing any textures in the subgraphs
162   ss->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
163   
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);
168   
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);
177 }
178
179
180 SGShaderAnimation::SGShaderAnimation(const SGPropertyNode* configNode,
181                                      SGPropertyNode* modelRoot) :
182   SGAnimation(configNode, modelRoot)
183 {
184   const SGPropertyNode* node = configNode->getChild("texture");
185   if (node)
186     _effect_texture = SGLoadTexture2D(node->getStringValue());
187 }
188
189 namespace {
190 class ChromeLightingCallback :
191   public osg::StateAttribute::Callback {
192 public:
193   virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
194   {
195     SGUpdateVisitor* updateVisitor = dynamic_cast<SGUpdateVisitor*>(nv);
196     if (!updateVisitor)
197       return;
198     osg::TexEnvCombine *combine = dynamic_cast<osg::TexEnvCombine *>(sa);
199     if (!combine)
200         return;
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);
206   }
207 };
208     
209 typedef map<osg::ref_ptr<osg::Texture2D>, osg::ref_ptr<osg::StateSet> >
210 StateSetMap;
211 }
212
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)
217 {
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();
225     } else {
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,
233                                   GL_UNSIGNED_BYTE);
234         unsigned char* imageBytes = dummyImage->data(0, 0);
235         imageBytes[0] = 255;
236         imageBytes[1] = 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
247         // unit 0
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);
255
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);
269     
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;
278     }
279     group->setStateSet(stateSet);
280 }
281
282 osg::Group*
283 SGShaderAnimation::createAnimationGroup(osg::Group& parent)
284 {
285   osg::Group* group = new osg::Group;
286   group->setName("shader animation");
287   parent.addChild(group);
288
289   std::string shader_name = getConfig()->getStringValue("shader", "");
290 //   if( shader_name == "fresnel" || shader_name == "reflection" )
291 //     _shader_type = 1;
292 //   else if( shader_name == "heat-haze" )
293 //     _shader_type = 2;
294 //   else
295   if( shader_name == "chrome")
296 #if 0
297     create_specular_highlights(group);
298 #endif
299   create_chrome(group, _effect_texture.get());
300   return group;
301 }
302
303 #if 0
304 // static Shader *shFresnel=NULL;
305 // static GLuint texFresnel = 0;
306
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;
314
315 // static glActiveTextureProc glActiveTexturePtr = 0;
316 // static sgMat4 shadIdentMatrix;
317
318
319 // static int null_shader_callback( ssgEntity *e ) {
320 //      GLuint dlist = 0;
321 //     ssgLeaf *leaf = (ssgLeaf *) e;
322 // #ifdef _SSG_USE_DLIST
323 //     dlist = leaf->getDListIndex();
324 //     if( ! dlist ) {
325 //         leaf->makeDList();
326 //         dlist = leaf->getDListIndex();
327 //     }
328 // #endif
329 //     if( ! dlist )
330 //         return true;
331 //     ssgSimpleState *sst = ((ssgSimpleState *)leaf->getState());
332 //     if ( sst )
333 //         sst->apply();
334
335 //     SGShaderAnimation *my_shader = (SGShaderAnimation *) ( e->getUserData() );
336 //     if( ! my_shader->_depth_test )
337 //         glDisable( GL_DEPTH_TEST );
338 //     glCallList ( dlist ) ;
339 //     // restore states
340 //     if( ! my_shader->_depth_test )
341 //         glEnable( GL_DEPTH_TEST );
342
343 //     // don't draw !
344 //     return false;
345 // }
346
347 // static int heat_haze_shader_callback( ssgEntity *e ) {
348 //    if( ! ((SGShadowAnimation *)e->getUserData())->get_condition_value() )
349 //        return true;
350
351 //      GLuint dlist = 0;
352 //     ssgLeaf *leaf = (ssgLeaf *) e;
353 // #ifdef _SSG_USE_DLIST
354 //     dlist = leaf->getDListIndex();
355 //     if( ! dlist ) {
356 //         leaf->makeDList();
357 //         dlist = leaf->getDListIndex();
358 //     }
359 // #endif
360 //     if( ! dlist )
361 //         return true;
362
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;
378 //             }
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;
390 //                 else
391 //                     internalFormat = GL_RGB5_A1;
392 //             } else {
393 //                 if( alphaBits != 0 )
394 //                     internalFormat = GL_RGBA8;
395 //             }
396 //             glTexImage2D(texBackgroundTarget, 0, internalFormat, 
397 //                             texBackgroundWidth, texBackgroundHeight, 0, GL_RGB, GL_FLOAT, NULL);
398
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);
403 //         }
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 );
412 //         } else {
413 //             glCopyTexSubImage2D( texBackgroundTarget, 0, 0, 0, 
414 //                 (screen_width - texBackgroundWidth) / 2, 
415 //                 (screen_height - texBackgroundHeight) / 2, 
416 //                 texBackgroundWidth, texBackgroundHeight );
417 //         }
418 //         haveBackground = true;
419 //         glBindTexture(texBackgroundTarget, 0);
420 //         glDisable(texBackgroundTarget);
421 //     }
422 //     ssgSimpleState *sst = ((ssgSimpleState *)leaf->getState());
423 //     if ( sst )
424 //         sst->apply();
425
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 );
431 //     if(1) {
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);
437 //             glLoadIdentity();
438 //             glTranslatef( noiseDist, 0.0f, 0.0f );
439 //         glMatrixMode(GL_MODELVIEW);
440
441 //         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
442
443 //         // background texture
444 //         glActiveTexturePtr( GL_TEXTURE1_ARB );
445 //         glEnable(texBackgroundTarget);
446 //         glBindTexture(texBackgroundTarget, texBackground);
447
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);
456 //             glLoadIdentity();
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 );
462 //             } else {
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 );
466 //             }
467 //             glMultMatrixf( (GLfloat *) CameraProjM );
468 //             glMultMatrixf( (GLfloat *) CameraViewM );
469 //             glTranslatef( deltaPos, deltaPos, deltaPos );
470 //         glMatrixMode(GL_MODELVIEW);
471
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 );
484
485 //         sgVec4 enviro = {1.00f, 1.00f, 1.00f, 0.85f};
486
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);
494
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 ); 
500
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 ) ;
506
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 ); 
511
512 //         glMatrixMode(GL_TEXTURE);
513 //         glTranslatef( deltaPos*0.7f, deltaPos*1.7f, deltaPos*0.7f );
514 //         glMatrixMode(GL_MODELVIEW);
515 //         glCallList ( dlist ) ;
516
517
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);
524 //             glLoadIdentity();
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);
530 //             glLoadIdentity();
531 //         glMatrixMode(GL_MODELVIEW);
532 //         glEnable(GL_TEXTURE_2D);
533 //         glBindTexture(GL_TEXTURE_2D, 0);
534 //     }
535 //     // restore states
536 //     if( ! my_shader->_depth_test )
537 //         glEnable( GL_DEPTH_TEST );
538
539 //     glEnable( GL_LIGHTING );
540 //     glDepthMask( GL_TRUE );
541 //     if( sst )
542 //         sst->force();
543
544 //    // don't draw !
545 //     return false;
546 // }
547
548 // static int fresnel_shader_callback( ssgEntity *e ) {
549 //    if( ! ((SGShadowAnimation *)e->getUserData())->get_condition_value() )
550 //        return true;
551
552 //      GLuint dlist = 0;
553 //     ssgLeaf *leaf = (ssgLeaf *) e;
554 // #ifdef _SSG_USE_DLIST
555 //     dlist = leaf->getDListIndex();
556 //     if( ! dlist ) {
557 //         leaf->makeDList();
558 //         dlist = leaf->getDListIndex();
559 //     }
560 // #endif
561 //     if( ! dlist )
562 //         return true;
563 //     ssgSimpleState *sst = ((ssgSimpleState *)leaf->getState());
564 //     if ( sst )
565 //         sst->apply();
566
567 //     sgVec4 sunColor, ambientColor;
568 //     ssgGetLight( 0 )->getColour(GL_DIFFUSE, sunColor );
569 //     ssgGetLight( 0 )->getColour(GL_AMBIENT, ambientColor );
570
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);
576
577 //      if( true ) {
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);
610 //     }
611 //     // restore states
612 // //    glBindTexture(GL_TEXTURE_2D, 0);
613 // //    glDepthFunc(GL_LESS);
614 // //    glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
615 //    if( sst )
616 //          sst->force();
617
618 //     // don't draw !
619 //     return false;
620 // }
621
622
623
624 // static int chrome_shader_callback( ssgEntity *e ) {
625 //    if( ! ((SGShadowAnimation *)e->getUserData())->get_condition_value() )
626 //        return true;
627
628 //      GLuint dlist = 0;
629 //     ssgLeaf *leaf = (ssgLeaf *) e;
630 // #ifdef _SSG_USE_DLIST
631 //     dlist = leaf->getDListIndex();
632 //     if( ! dlist ) {
633 //         leaf->makeDList();
634 //         dlist = leaf->getDListIndex();
635 //     }
636 // #endif
637 //     if( ! dlist )
638 //         return true;
639 //     ssgSimpleState *sst = ((ssgSimpleState *)leaf->getState());
640 //     if ( sst )
641 //         sst->apply();
642
643 //     SGShaderAnimation *my_shader = (SGShaderAnimation *) ( e->getUserData() );
644 //     if( ! my_shader->_depth_test )
645 //         glDisable( GL_DEPTH_TEST );
646
647 //     GLint maskTexComponent = 3;
648 //     glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &maskTexComponent);
649
650 //     // The fake env chrome texture
651 //     glActiveTexturePtr( GL_TEXTURE1_ARB );
652 //     glEnable(GL_TEXTURE_2D);
653 //     {
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());
661
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 );
674 //         }
675 //     }
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 ); 
686
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);
690 //     } else {
691 //         // c = chrome tex
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 ); 
695
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);
699 //     }
700 //     // automatic generation of texture coordinates
701 //     // from normals
702
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 );
707
708 //     glCallList ( dlist ) ;
709
710 //     glActiveTexturePtr( GL_TEXTURE1_ARB );
711 //     glDisable( GL_TEXTURE_GEN_S );
712 //     glDisable( GL_TEXTURE_GEN_T );
713
714 //     glMatrixMode(GL_TEXTURE);
715 //         glLoadIdentity();
716 //     glMatrixMode(GL_MODELVIEW);
717
718 //     glDisable(GL_TEXTURE_2D);
719 //     glBindTexture(GL_TEXTURE_2D, 0);
720 //     glActiveTexturePtr( GL_TEXTURE0_ARB );
721
722 //     // restore states
723 //     if( ! my_shader->_depth_test )
724 //         glEnable( GL_DEPTH_TEST );
725
726 //     if( sst )
727 //         sst->force();
728
729 //    // don't draw !
730 //     return false;
731 // }
732
733 // static void init_shaders(void) {
734 //      Shader::Init();
735 //     if( false && Shader::is_VP_supported() ) {
736 //          shFresnel = new Shader("/FlightGear/data/Textures/fresnel_vp.txt", "fresnel_vp");
737 //     }
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;
747 //     }
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 );
756
757 //     sgMakeIdentMat4( shadIdentMatrix );
758
759 //      initDone = true;
760 // }
761
762 // ////////////////////////////////////////////////////////////////////////
763 // // Implementation of SGShaderAnimation
764 // ////////////////////////////////////////////////////////////////////////
765
766 SGShaderAnimation::SGShaderAnimation ( SGPropertyNode *prop_root,
767                    SGPropertyNode_ptr props )
768   : SGAnimation(props, new osg::Group),
769     _condition(0),
770     _condition_value(true),
771     _shader_type(0),
772     _param_1(props->getFloatValue("param", 1.0f)),
773     _depth_test(props->getBoolValue("depth-test", true)),
774     _factor(props->getFloatValue("factor", 1.0f)),
775     _factor_prop(0),
776     _speed(props->getFloatValue("speed", 1.0f)),
777     _speed_prop(0),
778     _textureData(0),
779     _texWidth(0),
780     _texHeight(0)
781
782 {
783     SGPropertyNode_ptr node = props->getChild("condition");
784     if (node != 0) {
785         _condition = sgReadCondition(prop_root, node);
786         _condition_value = false;
787     }
788     node = props->getChild("factor-prop");
789     if( node )
790         _factor_prop = prop_root->getNode(node->getStringValue(), true);
791     node = props->getChild("speed-prop");
792     if( node )
793         _speed_prop = prop_root->getNode(node->getStringValue(), true);
794
795     _envColor = osg::Vec4(0, 0, 0, 1);
796     node = props->getChild("texture");
797     if( node ) {
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);
802
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 );
806     }
807     string shader_name = props->getStringValue("shader");
808     if( shader_name == "fresnel" || shader_name == "reflection" )
809         _shader_type = 1;
810     else if( shader_name == "heat-haze" )
811         _shader_type = 2;
812     else if( shader_name == "chrome" && _effectTexture.valid())
813         _shader_type = 3;
814 }
815
816 void SGShaderAnimation::init()
817 {
818 //     if( ! initDone )
819 //         init_shaders();
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 );
828 //     }
829 //     else if( _shader_type == 3 )
830 //         setCallBack( getBranch(), (ssgBase *) this, chrome_shader_callback );
831 //     else
832 //         setCallBack( getBranch(), (ssgBase *) this, null_shader_callback );
833 }
834
835 SGShaderAnimation::~SGShaderAnimation()
836 {
837   delete _condition;
838   delete _textureData;
839 }
840
841 void
842 SGShaderAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
843
844   if (_condition)
845     _condition_value = _condition->test();
846   if( _factor_prop)
847     _factor = _factor_prop->getFloatValue();
848   if( _speed_prop)
849     _speed = _speed_prop->getFloatValue();
850
851   // OSGFIXME fiddle with totalTime
852   totalTime = nv->getFrameStamp()->getReferenceTime();
853
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.
857   traverse(node, nv);
858 }
859 #endif