1 // OpenSG example: testSHL
3 // Demonstrates the use of the SHLChunk
13 #include "OSGSimpleGeometry.h"
15 #include "OSGGLUTWindow.h"
16 #include "OSGSimpleSceneManager.h"
17 #include "OSGAction.h"
18 #include "OSGSceneFileHandler.h"
19 #include "OSGBaseFunctions.h"
23 #include "OSGTransform.h"
24 #include "OSGPointLight.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
;
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
)
51 OSG::osgInit(argc
,argv
);
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 );
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");
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");
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");
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
);
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();
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
182 OSG::commitChanges();
183 gwin
->validateAllGLObjects();
188 // Initialize GLUT & OpenSG and set up the scene
189 int main(int argc
, char **argv
)
191 if(doMain(argc
, argv
) != 0)
201 // GLUT callback functions
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
;
214 t
= glutGet(GLUT_ELAPSED_TIME
);
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();
230 // react to size changes
231 void reshape(int w
, int h
)
237 // react to mouse button presses
238 void mouse(int button
, int state
, int x
, int y
)
241 _mgr
->mouseButtonRelease(button
, x
, y
);
243 _mgr
->mouseButtonPress(button
, x
, y
);
248 // react to mouse motions with pressed buttons
249 void motion(int x
, int y
)
251 _mgr
->mouseMove(x
, y
);
256 void keyboard(unsigned char k
, int x
, int y
)
258 static OSG::Real32 season
= 0.0f
;
272 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
273 printf("wrote scene.osb.gz\n");
279 _shl
->setUniformParameter("season", season
);
286 _shl
->setUniformParameter("season", season
);
289 _animation
= 1 - _animation
;
293 if(!_shl
->readFragmentProgram("Earth.fp"))
294 fprintf(stderr
, "Couldn't read fragment program 'Earth.fp'\n");
296 fprintf(stderr
, "blue loaded\n");
299 if(!_shl
->readFragmentProgram("Earth_red.fp"))
300 fprintf(stderr
, "Couldn't read fragment program 'Earth.fp'\n");
302 fprintf(stderr
, "red loaded\n");
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
);
330 int main(int argc
, char **argv
)