changed: gcc8 base update
[opensg.git] / Source / System / State / Shader / SHL / testSHLEarth.cpp
blob16c819614c57db6dbf8852c06fa914469eff2403
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 "OSGSimpleSHLChunk.h"
28 #include "OSGOSGSceneFileType.h"
31 // ------------------- global vars ----------------------
33 // The SimpleSceneManager to manage simple applications
34 static OSG::SimpleSceneManagerRefPtr _mgr;
35 // The scene
36 static OSG::NodeRecPtr _scene;
38 static OSG::SimpleSHLChunkRecPtr _shl = NULL;
39 static OSG::Int32 _animation = 1;
41 // forward declaration so we can have the interesting stuff upfront
42 int setupGLUT( int *argc, char *argv[] );
44 // Initialize GLUT & OpenSG and set up the scene
45 int doMain(int argc, char **argv)
47 // OSG init
48 OSG::osgInit(argc,argv);
50 // GLUT init
51 int winid = setupGLUT(&argc, argv);
53 // the connection between GLUT and OpenSG
54 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
55 gwin->setGlutId(winid);
56 gwin->setSize( 800, 800 );
57 gwin->init();
59 // Create the shader material
60 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
62 // Read the image for the normal texture
63 OSG::ImageUnrecPtr earth_map_img = OSG::Image::create();
64 if(!earth_map_img->read("Earth.jpg"))
66 fprintf(stderr, "Couldn't read texture 'Earth.jpg'\n");
67 return 1;
69 OSG::TextureObjChunkUnrecPtr tex_earth = OSG::TextureObjChunk::create();
70 OSG::TextureEnvChunkUnrecPtr tex_earth_env = OSG::TextureEnvChunk::create();
72 tex_earth->setImage(earth_map_img);
73 tex_earth->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
74 tex_earth->setMagFilter(GL_LINEAR);
75 tex_earth->setWrapS(GL_REPEAT);
76 tex_earth->setWrapT(GL_REPEAT);
78 tex_earth_env->setEnvMode(GL_MODULATE);
80 // Read the image for the normal texture
81 OSG::ImageUnrecPtr earth_night_map_img = OSG::Image::create();
82 if(!earth_night_map_img->read("EarthNight.jpg"))
84 fprintf(stderr, "Couldn't read texture 'EarthNight.jpg'\n");
85 return 1;
88 OSG::TextureObjChunkUnrecPtr tex_earth_night =
89 OSG::TextureObjChunk::create();
90 OSG::TextureEnvChunkUnrecPtr tex_earth_night_env =
91 OSG::TextureEnvChunk::create();
93 tex_earth_night->setImage(earth_night_map_img);
94 tex_earth_night->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
95 tex_earth_night->setMagFilter(GL_LINEAR);
96 tex_earth_night->setWrapS(GL_REPEAT);
97 tex_earth_night->setWrapT(GL_REPEAT);
99 tex_earth_night_env->setEnvMode(GL_MODULATE);
101 // Read the image for the normal texture
102 OSG::ImageUnrecPtr earth_clouds_map_img = OSG::Image::create();
103 if(!earth_clouds_map_img->read("EarthClouds.jpg"))
105 fprintf(stderr, "Couldn't read texture 'EarthClouds.jpg'\n");
106 return 1;
109 OSG::TextureObjChunkUnrecPtr tex_earth_clouds =
110 OSG::TextureObjChunk::create();
111 OSG::TextureEnvChunkUnrecPtr tex_earth_clouds_env =
112 OSG::TextureEnvChunk::create();
114 tex_earth_clouds->setImage(earth_clouds_map_img);
115 tex_earth_clouds->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
116 tex_earth_clouds->setMagFilter(GL_LINEAR);
117 tex_earth_clouds->setWrapS(GL_REPEAT);
118 tex_earth_clouds->setWrapT(GL_REPEAT);
120 tex_earth_clouds_env->setEnvMode(GL_MODULATE);
123 _shl = OSG::SimpleSHLChunk::create();
125 if(!_shl->readVertexProgram("Earth.vp"))
126 fprintf(stderr, "Couldn't read vertex program 'Earth.vp'\n");
127 if(!_shl->readFragmentProgram("Earth.fp"))
128 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
130 _shl->addUniformVariable("EarthDay", 0);
131 _shl->addUniformVariable("EarthNight", 1);
132 _shl->addUniformVariable("EarthCloudGloss", 2);
133 _shl->addUniformVariable("season", 0.0f);
134 _shl->addUniformVariable("cos_time_0_2PI", -0.406652f);
135 _shl->addUniformVariable("sin_time_0_2PI", -0.913583f);
136 // _shl->setUniformParameter("foo", -0.913583f);
139 cmat->addChunk(_shl);
140 cmat->addChunk(tex_earth);
141 cmat->addChunk(tex_earth_env);
142 cmat->addChunk(tex_earth_night);
143 cmat->addChunk(tex_earth_night_env);
144 cmat->addChunk(tex_earth_clouds);
145 cmat->addChunk(tex_earth_clouds_env);
148 // create root node
149 _scene = OSG::Node::create();
151 OSG::GeometryUnrecPtr geo = OSG::makeLatLongSphereGeo (100, 100, 1.0);
153 geo->setMaterial(cmat);
156 OSG::NodeUnrecPtr torus = OSG::Node::create();
158 torus->setCore(geo);
161 // add torus to scene
162 OSG::GroupUnrecPtr group = OSG::Group::create();
164 _scene->setCore(group);
165 _scene->addChild(torus);
168 // create the SimpleSceneManager helper
169 _mgr = OSG::SimpleSceneManager::create();
171 // tell the manager what to manage
172 _mgr->setWindow(gwin );
173 _mgr->setRoot(_scene);
175 // show the whole scene
176 _mgr->showAll();
178 OSG::commitChanges();
179 gwin->validateAllGLObjects();
181 return 0;
184 // Initialize GLUT & OpenSG and set up the scene
185 int main(int argc, char **argv)
187 if(doMain(argc, argv) != 0)
188 return 1;
190 // GLUT main loop
191 glutMainLoop();
193 return 0;
197 // GLUT callback functions
200 // redraw the window
201 void display(void)
203 static OSG::Real32 speed = 10000.0f;
204 static OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME);
205 static OSG::Real32 t2 = 0.0;
207 OSG::Real32 td = glutGet(GLUT_ELAPSED_TIME) - t;
209 if(td > speed)
210 t = glutGet(GLUT_ELAPSED_TIME);
212 if(_animation)
214 t2 = (2 * OSG::Pi / speed) * td;
216 _shl->updateUniformVariable("cos_time_0_2PI", OSG::osgCos(t2));
217 _shl->updateUniformVariable("sin_time_0_2PI", OSG::osgSin(t2));
220 OSG::Thread::getCurrentChangeList()->commitChanges();
222 // render scene
223 _mgr->redraw();
226 // react to size changes
227 void reshape(int w, int h)
229 _mgr->resize(w, h);
230 glutPostRedisplay();
233 // react to mouse button presses
234 void mouse(int button, int state, int x, int y)
236 if (state)
237 _mgr->mouseButtonRelease(button, x, y);
238 else
239 _mgr->mouseButtonPress(button, x, y);
241 glutPostRedisplay();
244 // react to mouse motions with pressed buttons
245 void motion(int x, int y)
247 _mgr->mouseMove(x, y);
248 glutPostRedisplay();
251 // react to keys
252 void keyboard(unsigned char k, int x, int y)
254 static OSG::Real32 season = 0.0f;
255 switch(k)
257 case 27:
258 case 'q':
260 _mgr = NULL;
261 _scene = NULL;
262 _shl = NULL;
264 OSG::osgExit();
265 exit(1);
266 break;
267 case 'w':
268 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
269 printf("wrote scene.osb.gz\n");
270 break;
271 case 's':
272 if(season < 0.435)
273 season += 0.01f;
275 _shl->updateUniformVariable("season", season);
277 break;
278 case 'S':
279 if(season > -0.435)
280 season -= 0.01f;
282 _shl->updateUniformVariable("season", season);
283 break;
284 case 'd':
285 _animation = 1 - _animation;
286 break;
288 case 'b':
289 if(!_shl->readFragmentProgram("Earth.fp"))
290 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
291 else
292 fprintf(stderr, "blue loaded\n");
293 break;
294 case 'r':
295 if(!_shl->readFragmentProgram("Earth_red.fp"))
296 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
297 else
298 fprintf(stderr, "red loaded\n");
299 break;
301 case 'A':
302 _shl->subUniformVariable("season");
303 //OSGSceneFileType::the().writeContainer(_shl, "/tmp/rem.osg");
304 break;
305 case 'a':
306 _shl->addUniformVariable("season", season);
307 //OSGSceneFileType::the().writeContainer(_shl, "/tmp/add.osg");
308 break;
312 glutPostRedisplay();
315 // setup the GLUT library which handles the windows for us
316 int setupGLUT(int *argc, char *argv[])
318 glutInit(argc, argv);
319 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
321 int winid = glutCreateWindow("OpenSG SHL Shader");
323 glutReshapeFunc(reshape);
324 glutDisplayFunc(display);
325 glutMouseFunc(mouse);
326 glutMotionFunc(motion);
327 glutKeyboardFunc(keyboard);
329 glutIdleFunc(display);
331 return winid;