changed: gcc8 base update
[opensg.git] / Source / System / State / Shader / SHL / testSHLEarth_compat.cpp
blob92397c2ca05509cafd22af1b67a0163d0d5962ec
1 // OpenSG example: testSHL
2 //
3 // Demonstrates the use of the SHLChunk
4 // Earth Shader Demo
7 // Headers
8 #include "OSGGLUT.h"
9 #include "OSGConfig.h"
11 #ifdef OSG_1_COMPAT
13 #include "OSGSimpleGeometry.h"
14 #include "OSGGLUT.h"
15 #include "OSGGLUTWindow.h"
16 #include "OSGSimpleSceneManager.h"
17 #include "OSGAction.h"
18 #include "OSGSceneFileHandler.h"
19 #include "OSGBaseFunctions.h"
21 #include "OSGNode.h"
22 #include "OSGGroup.h"
23 #include "OSGTransform.h"
24 #include "OSGPointLight.h"
26 #include "OSGImage.h"
27 #include "OSGChunkMaterial.h"
28 #include "OSGMaterialChunk.h"
29 #include "OSGTextureObjChunk.h"
30 #include "OSGTextureEnvChunk.h"
31 #include "OSGSHLChunk.h"
34 // ------------------- global vars ----------------------
36 // The SimpleSceneManager to manage simple applications
37 static OSG::SimpleSceneManagerRefPtr _mgr;
38 // The scene
39 static OSG::NodeRecPtr _scene;
41 static OSG::SHLChunkRecPtr _shl = NULL;
42 static OSG::Int32 _animation = 1;
44 // forward declaration so we can have the interesting stuff upfront
45 int setupGLUT( int *argc, char *argv[] );
47 // Initialize GLUT & OpenSG and set up the scene
48 int doMain(int argc, char **argv)
50 // OSG init
51 OSG::osgInit(argc,argv);
53 // GLUT init
54 int winid = setupGLUT(&argc, argv);
56 // the connection between GLUT and OpenSG
57 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
58 gwin->setGlutId(winid);
59 gwin->setSize( 800, 800 );
60 gwin->init();
62 // Create the shader material
63 OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create();
65 // Read the image for the normal texture
66 OSG::ImageUnrecPtr earth_map_img = OSG::Image::create();
67 if(!earth_map_img->read("Earth.jpg"))
69 fprintf(stderr, "Couldn't read texture 'Earth.jpg'\n");
70 return 1;
72 OSG::TextureObjChunkUnrecPtr tex_earth = OSG::TextureObjChunk::create();
73 OSG::TextureEnvChunkUnrecPtr tex_earth_env = OSG::TextureEnvChunk::create();
75 tex_earth->setImage(earth_map_img);
76 tex_earth->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
77 tex_earth->setMagFilter(GL_LINEAR);
78 tex_earth->setWrapS(GL_REPEAT);
79 tex_earth->setWrapT(GL_REPEAT);
81 tex_earth_env->setEnvMode(GL_MODULATE);
83 // Read the image for the normal texture
84 OSG::ImageUnrecPtr earth_night_map_img = OSG::Image::create();
85 if(!earth_night_map_img->read("EarthNight.jpg"))
87 fprintf(stderr, "Couldn't read texture 'EarthNight.jpg'\n");
88 return 1;
91 OSG::TextureObjChunkUnrecPtr tex_earth_night =
92 OSG::TextureObjChunk::create();
93 OSG::TextureEnvChunkUnrecPtr tex_earth_night_env =
94 OSG::TextureEnvChunk::create();
96 tex_earth_night->setImage(earth_night_map_img);
97 tex_earth_night->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
98 tex_earth_night->setMagFilter(GL_LINEAR);
99 tex_earth_night->setWrapS(GL_REPEAT);
100 tex_earth_night->setWrapT(GL_REPEAT);
102 tex_earth_night_env->setEnvMode(GL_MODULATE);
104 // Read the image for the normal texture
105 OSG::ImageUnrecPtr earth_clouds_map_img = OSG::Image::create();
106 if(!earth_clouds_map_img->read("EarthClouds.jpg"))
108 fprintf(stderr, "Couldn't read texture 'EarthClouds.jpg'\n");
109 return 1;
112 OSG::TextureObjChunkUnrecPtr tex_earth_clouds =
113 OSG::TextureObjChunk::create();
114 OSG::TextureEnvChunkUnrecPtr tex_earth_clouds_env =
115 OSG::TextureEnvChunk::create();
117 tex_earth_clouds->setImage(earth_clouds_map_img);
118 tex_earth_clouds->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
119 tex_earth_clouds->setMagFilter(GL_LINEAR);
120 tex_earth_clouds->setWrapS(GL_REPEAT);
121 tex_earth_clouds->setWrapT(GL_REPEAT);
123 tex_earth_clouds_env->setEnvMode(GL_MODULATE);
126 _shl = OSG::SHLChunk::create();
128 if(!_shl->readVertexProgram("Earth.vp"))
129 fprintf(stderr, "Couldn't read vertex program 'Earth.vp'\n");
130 if(!_shl->readFragmentProgram("Earth.fp"))
131 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
133 _shl->setUniformParameter("EarthDay", 0);
134 _shl->setUniformParameter("EarthNight", 1);
135 _shl->setUniformParameter("EarthCloudGloss", 2);
136 _shl->setUniformParameter("season", 0.0f);
137 _shl->setUniformParameter("cos_time_0_2PI", -0.406652f);
138 _shl->setUniformParameter("sin_time_0_2PI", -0.913583f);
139 // _shl->setUniformParameter("foo", -0.913583f);
142 cmat->addChunk(_shl);
143 cmat->addChunk(tex_earth);
144 cmat->addChunk(tex_earth_env);
145 cmat->addChunk(tex_earth_night);
146 cmat->addChunk(tex_earth_night_env);
147 cmat->addChunk(tex_earth_clouds);
148 cmat->addChunk(tex_earth_clouds_env);
151 // create root node
152 _scene = OSG::Node::create();
154 OSG::GeometryUnrecPtr geo = OSG::makeLatLongSphereGeo (100, 100, 1.0);
156 geo->setMaterial(cmat);
159 OSG::NodeUnrecPtr torus = OSG::Node::create();
161 torus->setCore(geo);
164 // add torus to scene
165 OSG::GroupUnrecPtr group = OSG::Group::create();
167 _scene->setCore(group);
168 _scene->addChild(torus);
171 // create the SimpleSceneManager helper
172 _mgr = OSG::SimpleSceneManager::create();
174 // tell the manager what to manage
175 _mgr->setWindow(gwin );
176 _mgr->setRoot(_scene);
178 // show the whole scene
179 _mgr->showAll();
182 OSG::commitChanges();
183 gwin->validateAllGLObjects();
185 return 0;
188 // Initialize GLUT & OpenSG and set up the scene
189 int main(int argc, char **argv)
191 if(doMain(argc, argv) != 0)
192 return 1;
194 // GLUT main loop
195 glutMainLoop();
197 return 0;
201 // GLUT callback functions
204 // redraw the window
205 void display(void)
207 static OSG::Real32 speed = 10000.0f;
208 static OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME);
209 static OSG::Real32 t2 = 0.0;
211 OSG::Real32 td = glutGet(GLUT_ELAPSED_TIME) - t;
213 if(td > speed)
214 t = glutGet(GLUT_ELAPSED_TIME);
216 if(_animation)
218 t2 = (2 * OSG::Pi / speed) * td;
220 _shl->setUniformParameter("cos_time_0_2PI", OSG::osgCos(t2));
221 _shl->setUniformParameter("sin_time_0_2PI", OSG::osgSin(t2));
224 OSG::Thread::getCurrentChangeList()->commitChanges();
226 // render scene
227 _mgr->redraw();
230 // react to size changes
231 void reshape(int w, int h)
233 _mgr->resize(w, h);
234 glutPostRedisplay();
237 // react to mouse button presses
238 void mouse(int button, int state, int x, int y)
240 if (state)
241 _mgr->mouseButtonRelease(button, x, y);
242 else
243 _mgr->mouseButtonPress(button, x, y);
245 glutPostRedisplay();
248 // react to mouse motions with pressed buttons
249 void motion(int x, int y)
251 _mgr->mouseMove(x, y);
252 glutPostRedisplay();
255 // react to keys
256 void keyboard(unsigned char k, int x, int y)
258 static OSG::Real32 season = 0.0f;
259 switch(k)
261 case 27:
262 case 'q':
264 _mgr = NULL;
265 _scene = NULL;
266 _shl = NULL;
268 OSG::osgExit();
269 exit(1);
270 break;
271 case 'w':
272 OSG::SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
273 printf("wrote scene.osb.gz\n");
274 break;
275 case 's':
276 if(season < 0.435)
277 season += 0.01;
279 _shl->setUniformParameter("season", season);
281 break;
282 case 'S':
283 if(season > -0.435)
284 season -= 0.01;
286 _shl->setUniformParameter("season", season);
287 break;
288 case 'a':
289 _animation = 1 - _animation;
290 break;
292 case 'b':
293 if(!_shl->readFragmentProgram("Earth.fp"))
294 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
295 else
296 fprintf(stderr, "blue loaded\n");
297 break;
298 case 'r':
299 if(!_shl->readFragmentProgram("Earth_red.fp"))
300 fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
301 else
302 fprintf(stderr, "red loaded\n");
303 break;
306 glutPostRedisplay();
309 // setup the GLUT library which handles the windows for us
310 int setupGLUT(int *argc, char *argv[])
312 glutInit(argc, argv);
313 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
315 int winid = glutCreateWindow("OpenSG SHL Shader");
317 glutReshapeFunc(reshape);
318 glutDisplayFunc(display);
319 glutMouseFunc(mouse);
320 glutMotionFunc(motion);
321 glutKeyboardFunc(keyboard);
323 glutIdleFunc(display);
325 return winid;
328 #else
330 int main(int argc, char **argv)
332 return 0;
335 #endif