| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#include <OpenSG/OSGGLUT.h> |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
#include <OpenSG/OSGConfig.h> |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
#include <OpenSG/OSGSimpleGeometry.h> |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
#include <OpenSG/OSGGLUTWindow.h> |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
#include <OpenSG/OSGSimpleSceneManager.h> |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
#include <OpenSG/OSGMultiDisplayWindow.h> |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
#include <OpenSG/OSGSceneFileHandler.h> |
|---|
| 47 |
|
|---|
| 48 |
#include <OpenSG/OSGImage.h> |
|---|
| 49 |
#include <OpenSG/OSGChunkMaterial.h> |
|---|
| 50 |
#include <OpenSG/OSGMaterialChunk.h> |
|---|
| 51 |
#include <OpenSG/OSGTextureObjChunk.h> |
|---|
| 52 |
#include <OpenSG/OSGTextureEnvChunk.h> |
|---|
| 53 |
#include <OpenSG/OSGSHLChunk.h> |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
OSG_USING_NAMESPACE |
|---|
| 57 |
|
|---|
| 58 |
using namespace std; |
|---|
| 59 |
|
|---|
| 60 |
SimpleSceneManager *mgr; |
|---|
| 61 |
|
|---|
| 62 |
static SHLChunkPtr _shl = NullFC; |
|---|
| 63 |
static Int32 _animation = 1; |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
int setupGLUT( int *argc, char *argv[] ); |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
NodePtr createScene(void) |
|---|
| 70 |
{ |
|---|
| 71 |
NodePtr _scene; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
ChunkMaterialPtr cmat = ChunkMaterial::create(); |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
ImagePtr earth_map_img = Image::create(); |
|---|
| 78 |
if(!earth_map_img->read("Earth.jpg")) |
|---|
| 79 |
{ |
|---|
| 80 |
fprintf(stderr, "Couldn't read texture 'Earth.jpg'\n"); |
|---|
| 81 |
return NullFC; |
|---|
| 82 |
} |
|---|
| 83 |
TextureObjChunkPtr tex_earth = TextureObjChunk::create(); |
|---|
| 84 |
TextureEnvChunkPtr tex_earth_env = TextureEnvChunk::create(); |
|---|
| 85 |
|
|---|
| 86 |
tex_earth->setImage(earth_map_img); |
|---|
| 87 |
tex_earth->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); |
|---|
| 88 |
tex_earth->setMagFilter(GL_LINEAR); |
|---|
| 89 |
tex_earth->setWrapS(GL_REPEAT); |
|---|
| 90 |
tex_earth->setWrapT(GL_REPEAT); |
|---|
| 91 |
|
|---|
| 92 |
tex_earth_env->setEnvMode(GL_MODULATE); |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
ImagePtr earth_night_map_img = Image::create(); |
|---|
| 96 |
if(!earth_night_map_img->read("EarthNight.jpg")) |
|---|
| 97 |
{ |
|---|
| 98 |
fprintf(stderr, "Couldn't read texture 'EarthNight.jpg'\n"); |
|---|
| 99 |
return NullFC; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
TextureObjChunkPtr tex_earth_night = TextureObjChunk::create(); |
|---|
| 103 |
TextureEnvChunkPtr tex_earth_night_env = TextureEnvChunk::create(); |
|---|
| 104 |
|
|---|
| 105 |
tex_earth_night->setImage(earth_night_map_img); |
|---|
| 106 |
tex_earth_night->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); |
|---|
| 107 |
tex_earth_night->setMagFilter(GL_LINEAR); |
|---|
| 108 |
tex_earth_night->setWrapS(GL_REPEAT); |
|---|
| 109 |
tex_earth_night->setWrapT(GL_REPEAT); |
|---|
| 110 |
|
|---|
| 111 |
tex_earth_night_env->setEnvMode(GL_MODULATE); |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
ImagePtr earth_clouds_map_img = Image::create(); |
|---|
| 115 |
if(!earth_clouds_map_img->read("EarthClouds.jpg")) |
|---|
| 116 |
{ |
|---|
| 117 |
fprintf(stderr, "Couldn't read texture 'EarthClouds.jpg'\n"); |
|---|
| 118 |
return NullFC; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
TextureObjChunkPtr tex_earth_clouds = TextureObjChunk::create(); |
|---|
| 122 |
TextureEnvChunkPtr tex_earth_clouds_env = TextureEnvChunk::create(); |
|---|
| 123 |
|
|---|
| 124 |
tex_earth_clouds->setImage(earth_clouds_map_img); |
|---|
| 125 |
tex_earth_clouds->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); |
|---|
| 126 |
tex_earth_clouds->setMagFilter(GL_LINEAR); |
|---|
| 127 |
tex_earth_clouds->setWrapS(GL_REPEAT); |
|---|
| 128 |
tex_earth_clouds->setWrapT(GL_REPEAT); |
|---|
| 129 |
|
|---|
| 130 |
tex_earth_clouds_env->setEnvMode(GL_MODULATE); |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
_shl = SHLChunk::create(); |
|---|
| 134 |
|
|---|
| 135 |
if(!_shl->readVertexProgram("Earth.vp")) |
|---|
| 136 |
fprintf(stderr, "Couldn't read vertex program 'Earth.vp'\n"); |
|---|
| 137 |
if(!_shl->readFragmentProgram("Earth.fp")) |
|---|
| 138 |
fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n"); |
|---|
| 139 |
|
|---|
| 140 |
_shl->setUniformParameter("EarthDay", 0); |
|---|
| 141 |
_shl->setUniformParameter("EarthNight", 1); |
|---|
| 142 |
_shl->setUniformParameter("EarthCloudGloss", 2); |
|---|
| 143 |
_shl->setUniformParameter("season", 0.0f); |
|---|
| 144 |
_shl->setUniformParameter("cos_time_0_2PI", -0.406652f); |
|---|
| 145 |
_shl->setUniformParameter("sin_time_0_2PI", -0.913583f); |
|---|
| 146 |
_shl->setUniformParameter("foo", -0.913583f); |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
cmat->addChunk(_shl); |
|---|
| 150 |
cmat->addChunk(tex_earth); |
|---|
| 151 |
cmat->addChunk(tex_earth_env); |
|---|
| 152 |
cmat->addChunk(tex_earth_night); |
|---|
| 153 |
cmat->addChunk(tex_earth_night_env); |
|---|
| 154 |
cmat->addChunk(tex_earth_clouds); |
|---|
| 155 |
cmat->addChunk(tex_earth_clouds_env); |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
_scene = Node::create(); |
|---|
| 160 |
|
|---|
| 161 |
GeometryPtr geo = makeLatLongSphereGeo (100, 100, 1.0); |
|---|
| 162 |
|
|---|
| 163 |
geo->setMaterial(cmat); |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
NodePtr torus = Node::create(); |
|---|
| 167 |
|
|---|
| 168 |
torus->setCore(geo); |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
GroupPtr group = Group::create(); |
|---|
| 173 |
|
|---|
| 174 |
_scene->setCore(group); |
|---|
| 175 |
_scene->addChild(torus); |
|---|
| 176 |
|
|---|
| 177 |
return _scene; |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
int main(int argc, char **argv) |
|---|
| 182 |
{ |
|---|
| 183 |
char *opt; |
|---|
| 184 |
NodePtr scene=NullFC; |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
osgInit(argc,argv); |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
int winid = setupGLUT(&argc, argv); |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
MultiDisplayWindowPtr mwin= MultiDisplayWindow::create(); |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
for(int a=1 ; a<argc ; ++a) |
|---|
| 197 |
{ |
|---|
| 198 |
if(argv[a][0] == '-') |
|---|
| 199 |
{ |
|---|
| 200 |
switch(argv[a][1]) |
|---|
| 201 |
{ |
|---|
| 202 |
case 'm': mwin->setConnectionType("Multicast"); |
|---|
| 203 |
cout << "Connection type set to Multicast" << endl; |
|---|
| 204 |
break; |
|---|
| 205 |
case 'p': mwin->setConnectionType("SockPipeline"); |
|---|
| 206 |
cout << "Connection type set to SockPipeline" << endl; |
|---|
| 207 |
break; |
|---|
| 208 |
case 'i': opt = argv[a][2] ? argv[a]+2 : argv[++a]; |
|---|
| 209 |
if(opt != argv[argc]) |
|---|
| 210 |
mwin->setConnectionInterface(opt); |
|---|
| 211 |
break; |
|---|
| 212 |
case 'a': opt = argv[a][2] ? argv[a]+2 : argv[++a]; |
|---|
| 213 |
if(opt != argv[argc]) |
|---|
| 214 |
mwin->setServiceAddress(opt); |
|---|
| 215 |
break; |
|---|
| 216 |
case 'f': opt = argv[a][2] ? argv[a]+2 : argv[++a]; |
|---|
| 217 |
if(opt != argv[argc]) |
|---|
| 218 |
scene = SceneFileHandler::the()->read( |
|---|
| 219 |
opt,0); |
|---|
| 220 |
break; |
|---|
| 221 |
case 'x': opt = argv[a][2] ? argv[a]+2 : argv[++a]; |
|---|
| 222 |
if(opt != argv[argc]) |
|---|
| 223 |
mwin->setHServers(atoi(opt)); |
|---|
| 224 |
break; |
|---|
| 225 |
case 'y': opt = argv[a][2] ? argv[a]+2 : argv[++a]; |
|---|
| 226 |
if(opt != argv[argc]) |
|---|
| 227 |
mwin->setVServers(atoi(opt)); |
|---|
| 228 |
break; |
|---|
| 229 |
default: std::cout << argv[0] |
|---|
| 230 |
<< " -m" |
|---|
| 231 |
<< " -p" |
|---|
| 232 |
<< " -i interface" |
|---|
| 233 |
<< " -f file" |
|---|
| 234 |
<< " -x horizontal server cnt" |
|---|
| 235 |
<< " -y vertical server cnt" |
|---|
| 236 |
<< endLog; |
|---|
| 237 |
return 0; |
|---|
| 238 |
} |
|---|
| 239 |
} |
|---|
| 240 |
else |
|---|
| 241 |
{ |
|---|
| 242 |
printf("%s\n",argv[a]); |
|---|
| 243 |
mwin->editServers().push_back(argv[a]); |
|---|
| 244 |
} |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
mwin->setSize(300,300); |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
if(scene == NullFC) |
|---|
| 252 |
scene = createScene(); |
|---|
| 253 |
|
|---|
| 254 |
if(scene == NullFC) |
|---|
| 255 |
scene = makeTorus(.5, 2, 16, 16); |
|---|
| 256 |
|
|---|
| 257 |
commitChanges(); |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
mgr = new SimpleSceneManager; |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
mgr->setWindow(mwin ); |
|---|
| 264 |
mgr->setRoot (scene); |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
mgr->showAll(); |
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
mwin->init(); |
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
glutMainLoop(); |
|---|
| 274 |
|
|---|
| 275 |
return 0; |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
void display(void) |
|---|
| 284 |
{ |
|---|
| 285 |
static Real32 speed = 10000.0f; |
|---|
| 286 |
static Real32 t = glutGet(GLUT_ELAPSED_TIME); |
|---|
| 287 |
static Real32 t2 = 0.0; |
|---|
| 288 |
|
|---|
| 289 |
Real32 td = glutGet(GLUT_ELAPSED_TIME) - t; |
|---|
| 290 |
|
|---|
| 291 |
if(td > speed) |
|---|
| 292 |
t = glutGet(GLUT_ELAPSED_TIME); |
|---|
| 293 |
|
|---|
| 294 |
if(_animation) |
|---|
| 295 |
{ |
|---|
| 296 |
t2 = (2 * OSG::Pi / speed) * td; |
|---|
| 297 |
|
|---|
| 298 |
_shl->setUniformParameter("cos_time_0_2PI", osgCos(t2)); |
|---|
| 299 |
_shl->setUniformParameter("sin_time_0_2PI", osgSin(t2)); |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
Thread::getCurrentChangeList()->commitChanges(); |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
mgr->redraw(); |
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
OSG::Thread::getCurrentChangeList()->clear(); |
|---|
| 310 |
|
|---|
| 311 |
glClear(GL_COLOR_BUFFER_BIT); |
|---|
| 312 |
glutSwapBuffers(); |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
void reshape(int w, int h) |
|---|
| 317 |
{ |
|---|
| 318 |
glutPostRedisplay(); |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
void mouse(int button, int state, int x, int y) |
|---|
| 323 |
{ |
|---|
| 324 |
if (state) |
|---|
| 325 |
mgr->mouseButtonRelease(button, x, y); |
|---|
| 326 |
else |
|---|
| 327 |
mgr->mouseButtonPress(button, x, y); |
|---|
| 328 |
glutPostRedisplay(); |
|---|
| 329 |
} |
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
void motion(int x, int y) |
|---|
| 333 |
{ |
|---|
| 334 |
mgr->mouseMove(x, y); |
|---|
| 335 |
glutPostRedisplay(); |
|---|
| 336 |
} |
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
void keyboard(unsigned char k, int x, int y) |
|---|
| 340 |
{ |
|---|
| 341 |
static Real32 season = 0.0f; |
|---|
| 342 |
|
|---|
| 343 |
switch(k) |
|---|
| 344 |
{ |
|---|
| 345 |
case 27: |
|---|
| 346 |
{ |
|---|
| 347 |
OSG::osgExit(); |
|---|
| 348 |
exit(0); |
|---|
| 349 |
} |
|---|
| 350 |
break; |
|---|
| 351 |
case 's': |
|---|
| 352 |
if(season < 0.435) |
|---|
| 353 |
season += 0.01; |
|---|
| 354 |
|
|---|
| 355 |
_shl->setUniformParameter("season", season); |
|---|
| 356 |
|
|---|
| 357 |
break; |
|---|
| 358 |
case 'S': |
|---|
| 359 |
if(season > -0.435) |
|---|
| 360 |
season -= 0.01; |
|---|
| 361 |
|
|---|
| 362 |
_shl->setUniformParameter("season", season); |
|---|
| 363 |
break; |
|---|
| 364 |
case 'a': |
|---|
| 365 |
_animation = 1 - _animation; |
|---|
| 366 |
break; |
|---|
| 367 |
} |
|---|
| 368 |
} |
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
int setupGLUT(int *argc, char *argv[]) |
|---|
| 372 |
{ |
|---|
| 373 |
glutInit(argc, argv); |
|---|
| 374 |
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); |
|---|
| 375 |
|
|---|
| 376 |
int winid = glutCreateWindow("OpenSG"); |
|---|
| 377 |
|
|---|
| 378 |
glutReshapeFunc(reshape); |
|---|
| 379 |
glutDisplayFunc(display); |
|---|
| 380 |
glutMouseFunc(mouse); |
|---|
| 381 |
glutMotionFunc(motion); |
|---|
| 382 |
glutKeyboardFunc(keyboard); |
|---|
| 383 |
|
|---|
| 384 |
glutIdleFunc(display); |
|---|
| 385 |
|
|---|
| 386 |
return winid; |
|---|
| 387 |
} |
|---|