1 // OpenSG example: testSHL
3 // Demonstrates the use of the SHLChunk
9 #include "OSGSimpleGeometry.h"
11 #include "OSGGLUTWindow.h"
12 #include "OSGSimpleSceneManager.h"
13 #include "OSGAction.h"
14 #include "OSGSceneFileHandler.h"
15 #include "OSGBaseFunctions.h"
19 #include "OSGTransform.h"
20 #include "OSGPointLight.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
;
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
)
52 OSG::osgInit(argc
,argv
);
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 );
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");
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");
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");
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
);
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();
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
195 // Initialize GLUT & OpenSG and set up the scene
196 int main(int argc
, char **argv
)
198 if(doMain(argc
, argv
) != 0)
208 // GLUT callback functions
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
;
221 t
= glutGet(GLUT_ELAPSED_TIME
);
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();
237 // react to size changes
238 void reshape(int w
, int h
)
244 // react to mouse button presses
245 void mouse(int button
, int state
, int x
, int y
)
248 _mgr
->mouseButtonRelease(button
, x
, y
);
250 _mgr
->mouseButtonPress(button
, x
, y
);
255 // react to mouse motions with pressed buttons
256 void motion(int x
, int y
)
258 _mgr
->mouseMove(x
, y
);
263 void keyboard(unsigned char k
, int x
, int y
)
265 static OSG::Real32 season
= 0.0f
;
282 _mgr
->getWindow()->getShaderCache()->dump();
286 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
287 printf("wrote scene.osb.gz\n");
293 _shl_vp
->updateUniformVariable("season", season
);
300 _shl_vp
->updateUniformVariable("season", season
);
303 _animation
= 1 - _animation
;
307 if(!_shl_fp
->readProgram("Earth.fp"))
308 fprintf(stderr
, "Couldn't read fragment program 'Earth.fp'\n");
310 fprintf(stderr
, "blue loaded\n");
313 if(!_shl_fp
->readProgram("Earth_red.fp"))
314 fprintf(stderr
, "Couldn't read fragment program 'Earth.fp'\n");
316 fprintf(stderr
, "red loaded\n");
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");
334 _shl
->subFragmentShader(0);
335 _shl
->addFragmentShader(_shl_fp
);
336 fprintf(stderr
, "blue loaded\n");
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");
355 _shl
->subFragmentShader(0);
356 _shl
->addFragmentShader(_shl_fp
);
357 fprintf(stderr
, "red loaded\n");
362 _shl_vp
->subUniformVariable("season");
363 //OSGSceneFileType::the().writeContainer(_shl, "/tmp/rem.osg");
366 _shl_vp
->addUniformVariable("season", season
);
367 //OSGSceneFileType::the().writeContainer(_shl, "/tmp/add.osg");
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
);