fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / Chunks / testSHLEarth_shaderprog.cpp
blobc1de0d8ee6b653097a867f3e63b5998308807380
1 // OpenSG example: testSHL
2 //
3 // Demonstrates the use of the SHLChunk
4 // Earth Shader Demo
6 // Headers
7 #include "OSGGLUT.h"
8 #include "OSGConfig.h"
9 #include "OSGSimpleGeometry.h"
10 #include "OSGGLUT.h"
11 #include "OSGGLUTWindow.h"
12 #include "OSGSimpleSceneManager.h"
13 #include "OSGAction.h"
14 #include "OSGSceneFileHandler.h"
15 #include "OSGBaseFunctions.h"
17 #include "OSGNode.h"
18 #include "OSGGroup.h"
19 #include "OSGTransform.h"
20 #include "OSGPointLight.h"
22 #include "OSGImage.h"
23 #include "OSGChunkMaterial.h"
24 #include "OSGMaterialChunk.h"
25 #include "OSGTextureObjChunk.h"
26 #include "OSGTextureEnvChunk.h"
27 #include "OSGShaderProgramChunk.h"
28 #include "OSGOSGSceneFileType.h"
29 #include "OSGShaderCache.h"
32 // ------------------- global vars ----------------------
34 // The SimpleSceneManager to manage simple applications
35 static OSG::SimpleSceneManagerRefPtr _mgr;
36 // The scene
37 static OSG::NodeRecPtr _scene;
39 static OSG::ShaderProgramChunkRecPtr _shl = NULL;
40 static OSG::ShaderProgramRecPtr _shl_vp = NULL;
41 static OSG::ShaderProgramRecPtr _shl_fp = NULL;
43 static OSG::Int32 _animation = 1;
45 // forward declaration so we can have the interesting stuff upfront
46 int setupGLUT( int *argc, char *argv[] );
48 // Initialize GLUT & OpenSG and set up the scene
49 int doMain(int argc, char **argv)
51 // OSG init
52 OSG::osgInit(argc,argv);
54 // GLUT init
55 int winid = setupGLUT(&argc, argv);
57 // the connection between GLUT and OpenSG
58 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
59 gwin->setGlutId(winid);
60 gwin->setSize( 800, 800 );
61 gwin->init();
63 // Create the shader material
64 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
66 // Read the image for the normal texture
67 OSG::ImageUnrecPtr earth_map_img = OSG::Image::create();
68 if(!earth_map_img->read("Earth.jpg"))
70 fprintf(stderr, "Couldn't read texture 'Earth.jpg'\n");
71 return 1;
73 OSG::TextureObjChunkUnrecPtr tex_earth = OSG::TextureObjChunk::create();
74 OSG::TextureEnvChunkUnrecPtr tex_earth_env = OSG::TextureEnvChunk::create();
76 tex_earth->setImage(earth_map_img);
77 tex_earth->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
78 tex_earth->setMagFilter(GL_LINEAR);
79 tex_earth->setWrapS(GL_REPEAT);
80 tex_earth->setWrapT(GL_REPEAT);
82 tex_earth_env->setEnvMode(GL_MODULATE);
84 // Read the image for the normal texture
85 OSG::ImageUnrecPtr earth_night_map_img = OSG::Image::create();
86 if(!earth_night_map_img->read("EarthNight.jpg"))
88 fprintf(stderr, "Couldn't read texture 'EarthNight.jpg'\n");
89 return 1;
92 OSG::TextureObjChunkUnrecPtr tex_earth_night =
93 OSG::TextureObjChunk::create();
94 OSG::TextureEnvChunkUnrecPtr tex_earth_night_env =
95 OSG::TextureEnvChunk::create();
97 tex_earth_night->setImage(earth_night_map_img);
98 tex_earth_night->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
99 tex_earth_night->setMagFilter(GL_LINEAR);
100 tex_earth_night->setWrapS(GL_REPEAT);
101 tex_earth_night->setWrapT(GL_REPEAT);
103 tex_earth_night_env->setEnvMode(GL_MODULATE);
105 // Read the image for the normal texture
106 OSG::ImageUnrecPtr earth_clouds_map_img = OSG::Image::create();
107 if(!earth_clouds_map_img->read("EarthClouds.jpg"))
109 fprintf(stderr, "Couldn't read texture 'EarthClouds.jpg'\n");
110 return 1;
113 OSG::TextureObjChunkUnrecPtr tex_earth_clouds =
114 OSG::TextureObjChunk::create();
115 OSG::TextureEnvChunkUnrecPtr tex_earth_clouds_env =
116 OSG::TextureEnvChunk::create();
118 tex_earth_clouds->setImage(earth_clouds_map_img);
119 tex_earth_clouds->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
120 tex_earth_clouds->setMagFilter(GL_LINEAR);
121 tex_earth_clouds->setWrapS(GL_REPEAT);
122 tex_earth_clouds->setWrapT(GL_REPEAT);
124 tex_earth_clouds_env->setEnvMode(GL_MODULATE);
127 _shl = OSG::ShaderProgramChunk::create();
129 _shl_vp = OSG::ShaderProgram::create();
130 _shl_fp = OSG::ShaderProgram::create();
132 if(!_shl_vp->readProgram("Earth.vp"))
133 fprintf(stderr, "Couldn't read vertex program 'Earth.vp'\n");
134 if(!_shl_fp->readProgram("Earth.fp"))
135 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
137 _shl_vp->setShaderType(GL_VERTEX_SHADER);
138 _shl_fp->setShaderType(GL_FRAGMENT_SHADER);
140 _shl->addVertexShader (_shl_vp);
141 _shl->addFragmentShader(_shl_fp);
143 _shl_fp->addUniformVariable("EarthDay", 0);
144 _shl_fp->addUniformVariable("EarthNight", 1);
145 _shl_fp->addUniformVariable("EarthCloudGloss", 2);
147 _shl_vp->addUniformVariable("season", 0.0f);
148 _shl_vp->addUniformVariable("cos_time_0_2PI", -0.406652f);
149 _shl_vp->addUniformVariable("sin_time_0_2PI", -0.913583f);
150 // _shl->setUniformParameter("foo", -0.913583f);
153 cmat->addChunk(_shl);
154 cmat->addChunk(tex_earth);
155 cmat->addChunk(tex_earth_env);
156 cmat->addChunk(tex_earth_night);
157 cmat->addChunk(tex_earth_night_env);
158 cmat->addChunk(tex_earth_clouds);
159 cmat->addChunk(tex_earth_clouds_env);
162 // create root node
163 _scene = OSG::Node::create();
165 OSG::GeometryUnrecPtr geo = OSG::makeLatLongSphereGeo (100, 100, 1.0);
167 geo->setMaterial(cmat);
170 OSG::NodeUnrecPtr torus = OSG::Node::create();
172 torus->setCore(geo);
175 // add torus to scene
176 OSG::GroupUnrecPtr group = OSG::Group::create();
178 _scene->setCore(group);
179 _scene->addChild(torus);
182 // create the SimpleSceneManager helper
183 _mgr = OSG::SimpleSceneManager::create();
185 // tell the manager what to manage
186 _mgr->setWindow(gwin );
187 _mgr->setRoot(_scene);
189 // show the whole scene
190 _mgr->showAll();
192 return 0;
195 // Initialize GLUT & OpenSG and set up the scene
196 int main(int argc, char **argv)
198 if(doMain(argc, argv) != 0)
199 return 1;
201 // GLUT main loop
202 glutMainLoop();
204 return 0;
208 // GLUT callback functions
211 // redraw the window
212 void display(void)
214 static OSG::Real32 speed = 10000.0f;
215 static OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME);
216 static OSG::Real32 t2 = 0.0;
218 OSG::Real32 td = glutGet(GLUT_ELAPSED_TIME) - t;
220 if(td > speed)
221 t = glutGet(GLUT_ELAPSED_TIME);
223 if(_animation)
225 t2 = (2 * OSG::Pi / speed) * td;
227 _shl_vp->updateUniformVariable("cos_time_0_2PI", OSG::osgCos(t2));
228 _shl_vp->updateUniformVariable("sin_time_0_2PI", OSG::osgSin(t2));
231 OSG::Thread::getCurrentChangeList()->commitChanges();
233 // render scene
234 _mgr->redraw();
237 // react to size changes
238 void reshape(int w, int h)
240 _mgr->resize(w, h);
241 glutPostRedisplay();
244 // react to mouse button presses
245 void mouse(int button, int state, int x, int y)
247 if (state)
248 _mgr->mouseButtonRelease(button, x, y);
249 else
250 _mgr->mouseButtonPress(button, x, y);
252 glutPostRedisplay();
255 // react to mouse motions with pressed buttons
256 void motion(int x, int y)
258 _mgr->mouseMove(x, y);
259 glutPostRedisplay();
262 // react to keys
263 void keyboard(unsigned char k, int x, int y)
265 static OSG::Real32 season = 0.0f;
266 switch(k)
268 case 27:
269 case 'q':
271 _mgr = NULL;
272 _scene = NULL;
273 _shl = NULL;
274 _shl_vp = NULL;
275 _shl_fp = NULL;
277 OSG::osgExit();
278 exit(1);
279 break;
281 case 'c':
282 _mgr->getWindow()->getShaderCache()->dump();
283 break;
285 case 'w':
286 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
287 printf("wrote scene.osb.gz\n");
288 break;
289 case 's':
290 if(season < 0.435)
291 season += 0.01f;
293 _shl_vp->updateUniformVariable("season", season);
295 break;
296 case 'S':
297 if(season > -0.435)
298 season -= 0.01f;
300 _shl_vp->updateUniformVariable("season", season);
301 break;
302 case 'd':
303 _animation = 1 - _animation;
304 break;
306 case 'b':
307 if(!_shl_fp->readProgram("Earth.fp"))
308 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
309 else
310 fprintf(stderr, "blue loaded\n");
311 break;
312 case 'r':
313 if(!_shl_fp->readProgram("Earth_red.fp"))
314 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
315 else
316 fprintf(stderr, "red loaded\n");
317 break;
319 case 'B':
320 _shl_fp = OSG::ShaderProgram::create();
322 _shl_fp->setShaderType(GL_FRAGMENT_SHADER);
324 _shl_fp->addUniformVariable("EarthDay", 0);
325 _shl_fp->addUniformVariable("EarthNight", 1);
326 _shl_fp->addUniformVariable("EarthCloudGloss", 2);
328 if(!_shl_fp->readProgram("Earth.fp"))
330 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
332 else
334 _shl->subFragmentShader(0);
335 _shl->addFragmentShader(_shl_fp);
336 fprintf(stderr, "blue loaded\n");
338 break;
340 case 'R':
341 _shl_fp = OSG::ShaderProgram::create();
343 _shl_fp->setShaderType(GL_FRAGMENT_SHADER);
345 _shl_fp->addUniformVariable("EarthDay", 0);
346 _shl_fp->addUniformVariable("EarthNight", 1);
347 _shl_fp->addUniformVariable("EarthCloudGloss", 2);
349 if(!_shl_fp->readProgram("Earth_red.fp"))
351 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
353 else
355 _shl->subFragmentShader(0);
356 _shl->addFragmentShader(_shl_fp);
357 fprintf(stderr, "red loaded\n");
359 break;
361 case 'A':
362 _shl_vp->subUniformVariable("season");
363 //OSGSceneFileType::the().writeContainer(_shl, "/tmp/rem.osg");
364 break;
365 case 'a':
366 _shl_vp->addUniformVariable("season", season);
367 //OSGSceneFileType::the().writeContainer(_shl, "/tmp/add.osg");
368 break;
372 glutPostRedisplay();
375 // setup the GLUT library which handles the windows for us
376 int setupGLUT(int *argc, char *argv[])
378 glutInit(argc, argv);
379 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
381 int winid = glutCreateWindow("OpenSG SHL Shader");
383 glutReshapeFunc(reshape);
384 glutDisplayFunc(display);
385 glutMouseFunc(mouse);
386 glutMotionFunc(motion);
387 glutKeyboardFunc(keyboard);
389 glutIdleFunc(display);
391 return winid;