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 "OSGSimpleSHLChunk.h"
28 #include "OSGOSGSceneFileType.h"
31 // ------------------- global vars ----------------------
33 // The SimpleSceneManager to manage simple applications
34 static OSG::SimpleSceneManagerRefPtr _mgr
;
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
)
48 OSG::osgInit(argc
,argv
);
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 );
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");
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");
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");
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
);
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();
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
178 OSG::commitChanges();
179 gwin
->validateAllGLObjects();
184 // Initialize GLUT & OpenSG and set up the scene
185 int main(int argc
, char **argv
)
187 if(doMain(argc
, argv
) != 0)
197 // GLUT callback functions
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
;
210 t
= glutGet(GLUT_ELAPSED_TIME
);
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();
226 // react to size changes
227 void reshape(int w
, int h
)
233 // react to mouse button presses
234 void mouse(int button
, int state
, int x
, int y
)
237 _mgr
->mouseButtonRelease(button
, x
, y
);
239 _mgr
->mouseButtonPress(button
, x
, y
);
244 // react to mouse motions with pressed buttons
245 void motion(int x
, int y
)
247 _mgr
->mouseMove(x
, y
);
252 void keyboard(unsigned char k
, int x
, int y
)
254 static OSG::Real32 season
= 0.0f
;
268 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
269 printf("wrote scene.osb.gz\n");
275 _shl
->updateUniformVariable("season", season
);
282 _shl
->updateUniformVariable("season", season
);
285 _animation
= 1 - _animation
;
289 if(!_shl
->readFragmentProgram("Earth.fp"))
290 fprintf(stderr
, "Couldn't read fragment program 'Earth.fp'\n");
292 fprintf(stderr
, "blue loaded\n");
295 if(!_shl
->readFragmentProgram("Earth_red.fp"))
296 fprintf(stderr
, "Couldn't read fragment program 'Earth.fp'\n");
298 fprintf(stderr
, "red loaded\n");
302 _shl
->subUniformVariable("season");
303 //OSGSceneFileType::the().writeContainer(_shl, "/tmp/rem.osg");
306 _shl
->addUniformVariable("season", season
);
307 //OSGSceneFileType::the().writeContainer(_shl, "/tmp/add.osg");
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
);