| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
#include <OpenSG/OSGGLUT.h> |
|---|
| 8 |
#include <OpenSG/OSGConfig.h> |
|---|
| 9 |
#include <OpenSG/OSGSimpleGeometry.h> |
|---|
| 10 |
#include <OpenSG/OSGGLUTWindow.h> |
|---|
| 11 |
#include <OpenSG/OSGSimpleSceneManager.h> |
|---|
| 12 |
#include <OpenSG/OSGTransform.h> |
|---|
| 13 |
#include <OpenSG/OSGGroup.h> |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
#include <OpenSG/OSGDirectionalLight.h> |
|---|
| 17 |
#include <OpenSG/OSGPointLight.h> |
|---|
| 18 |
#include <OpenSG/OSGSpotLight.h> |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
OSG_USING_NAMESPACE |
|---|
| 22 |
|
|---|
| 23 |
UInt32 nlights = 6; |
|---|
| 24 |
|
|---|
| 25 |
TransformPtr lightBeacons[8]; |
|---|
| 26 |
LightPtr lights [8]; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
SimpleSceneManager *mgr; |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
int setupGLUT( int *argc, char *argv[] ); |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
void makeMatrix(Real32 t, Matrix &result) |
|---|
| 36 |
{ |
|---|
| 37 |
Matrix m; |
|---|
| 38 |
|
|---|
| 39 |
result.setTransform(Quaternion(Vec3f(0,0,1), -Pi / 2)); |
|---|
| 40 |
|
|---|
| 41 |
m.setTransform(Vec3f(1, 0, 0)); |
|---|
| 42 |
result.multLeft(m); |
|---|
| 43 |
|
|---|
| 44 |
m.setTransform(Quaternion(Vec3f(0,1,0), t / 1000.f)); |
|---|
| 45 |
result.multLeft(m); |
|---|
| 46 |
|
|---|
| 47 |
m.setTransform(Vec3f(2, 0, 0)); |
|---|
| 48 |
result.multLeft(m); |
|---|
| 49 |
|
|---|
| 50 |
m.setTransform(Quaternion(Vec3f(0,0,1), t / 3000.f)); |
|---|
| 51 |
result.multLeft(m); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
void display( void ) |
|---|
| 56 |
{ |
|---|
| 57 |
|
|---|
| 58 |
Real32 t = glutGet(GLUT_ELAPSED_TIME ); |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
for(UInt16 i = 0; i < nlights; ++i) |
|---|
| 62 |
{ |
|---|
| 63 |
Matrix m; |
|---|
| 64 |
|
|---|
| 65 |
makeMatrix(t - 2000 * i, m); |
|---|
| 66 |
|
|---|
| 67 |
lightBeacons[i]->setMatrix(m); |
|---|
| 68 |
} |
|---|
| 69 |
commitChanges(); |
|---|
| 70 |
|
|---|
| 71 |
mgr->redraw(); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
int main(int argc, char **argv) |
|---|
| 76 |
{ |
|---|
| 77 |
|
|---|
| 78 |
osgInit(argc,argv); |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
int winid = setupGLUT(&argc, argv); |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
if(argc > 1) |
|---|
| 85 |
{ |
|---|
| 86 |
if(sscanf(argv[1], "%d", &nlights) != 1) |
|---|
| 87 |
{ |
|---|
| 88 |
FWARNING(("Number of lights '%s' not understood.\n", argv[1])); |
|---|
| 89 |
nlights = 3; |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
GLUTWindowPtr gwin= GLUTWindow::create(); |
|---|
| 95 |
gwin->setGlutId(winid); |
|---|
| 96 |
gwin->init(); |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
NodePtr scene = Node::create(); |
|---|
| 151 |
GroupPtr group = Group::create(); |
|---|
| 152 |
scene->setCore(group); |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
NodePtr lit_scene = makeTorus(.5, 2, 32, 64); |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
NodePtr lastnode = lit_scene; |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
Color3f colors[] = { Color3f(1,0,0), Color3f(0,1,0), Color3f(0,0,1), |
|---|
| 166 |
Color3f(1,1,0), Color3f(0,1,1), Color3f(1,0,1), |
|---|
| 167 |
Color3f(1,1,1), Color3f(1,1,1) |
|---|
| 168 |
}; |
|---|
| 169 |
if(nlights > 8) |
|---|
| 170 |
{ |
|---|
| 171 |
FWARNING(("Currently only 8 lights supported\n")); |
|---|
| 172 |
nlights = 8; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
Real32 scale = osgMax(1., 1.5 / nlights); |
|---|
| 177 |
|
|---|
| 178 |
for(UInt16 i = 0; i < nlights; ++i) |
|---|
| 179 |
{ |
|---|
| 180 |
|
|---|
| 181 |
NodePtr light = Node::create(); |
|---|
| 182 |
LightPtr light_core; |
|---|
| 183 |
NodePtr geo_node; |
|---|
| 184 |
|
|---|
| 185 |
switch((i % 3) + 0) |
|---|
| 186 |
{ |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
case 0: |
|---|
| 199 |
{ |
|---|
| 200 |
PointLightPtr l = PointLight::create(); |
|---|
| 201 |
|
|---|
| 202 |
l->setPosition (0, 0, 0); |
|---|
| 203 |
l->setConstantAttenuation (1); |
|---|
| 204 |
l->setLinearAttenuation (0); |
|---|
| 205 |
l->setQuadraticAttenuation (3); |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
geo_node = makeLatLongSphere(8, 8, 0.1); |
|---|
| 209 |
|
|---|
| 210 |
GeometryPtr geo = dynamic_cast<GeometryPtr>(geo_node->getCore()); |
|---|
| 211 |
|
|---|
| 212 |
SimpleMaterialPtr sm = SimpleMaterial::create(); |
|---|
| 213 |
|
|---|
| 214 |
sm->setLit(false); |
|---|
| 215 |
sm->setDiffuse(Color3f( colors[i][0], |
|---|
| 216 |
colors[i][1], |
|---|
| 217 |
colors[i][2] )); |
|---|
| 218 |
|
|---|
| 219 |
geo->setMaterial(sm); |
|---|
| 220 |
|
|---|
| 221 |
light_core = l; |
|---|
| 222 |
} |
|---|
| 223 |
break; |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
case 1: |
|---|
| 237 |
{ |
|---|
| 238 |
DirectionalLightPtr l = DirectionalLight::create(); |
|---|
| 239 |
|
|---|
| 240 |
l->setDirection(0, 0, 1); |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
geo_node = makeCylinder(.1, .03, 8, true, true, true); |
|---|
| 244 |
|
|---|
| 245 |
GeometryPtr geo = dynamic_cast<GeometryPtr>(geo_node->getCore()); |
|---|
| 246 |
|
|---|
| 247 |
SimpleMaterialPtr sm = SimpleMaterial::create(); |
|---|
| 248 |
|
|---|
| 249 |
sm->setLit(false); |
|---|
| 250 |
sm->setDiffuse(Color3f( colors[i][0], |
|---|
| 251 |
colors[i][1], |
|---|
| 252 |
colors[i][2] )); |
|---|
| 253 |
|
|---|
| 254 |
geo->setMaterial(sm); |
|---|
| 255 |
|
|---|
| 256 |
light_core = l; |
|---|
| 257 |
} |
|---|
| 258 |
break; |
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
case 2: |
|---|
| 269 |
{ |
|---|
| 270 |
SpotLightPtr l = SpotLight::create(); |
|---|
| 271 |
|
|---|
| 272 |
l->setPosition (Pnt3f(0, 0, 0)); |
|---|
| 273 |
l->setDirection (Vec3f(0, -1, 0)); |
|---|
| 274 |
l->setSpotExponent (2); |
|---|
| 275 |
l->setSpotCutOff (osgDegree2Rad(45)); |
|---|
| 276 |
l->setConstantAttenuation (1); |
|---|
| 277 |
l->setLinearAttenuation (0); |
|---|
| 278 |
l->setQuadraticAttenuation (3); |
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
geo_node = makeCone(.2, .2, 8, true, true); |
|---|
| 282 |
|
|---|
| 283 |
GeometryPtr geo = dynamic_cast<GeometryPtr>(geo_node->getCore()); |
|---|
| 284 |
|
|---|
| 285 |
SimpleMaterialPtr sm = SimpleMaterial::create(); |
|---|
| 286 |
|
|---|
| 287 |
sm->setLit(false); |
|---|
| 288 |
sm->setDiffuse(Color3f( colors[i][0], |
|---|
| 289 |
colors[i][1], |
|---|
| 290 |
colors[i][2] )); |
|---|
| 291 |
|
|---|
| 292 |
geo->setMaterial(sm); |
|---|
| 293 |
|
|---|
| 294 |
light_core = l; |
|---|
| 295 |
} |
|---|
| 296 |
break; |
|---|
| 297 |
} |
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
NodePtr beacon = Node::create(); |
|---|
| 301 |
TransformPtr beacon_core = Transform::create(); |
|---|
| 302 |
|
|---|
| 303 |
lightBeacons[i] = beacon_core; |
|---|
| 304 |
|
|---|
| 305 |
beacon->setCore(beacon_core); |
|---|
| 306 |
beacon->addChild(geo_node); |
|---|
| 307 |
|
|---|
| 308 |
scene->addChild(beacon); |
|---|
| 309 |
|
|---|
| 310 |
light_core->setAmbient (colors[i][0] / scale, |
|---|
| 311 |
colors[i][1] / scale, |
|---|
| 312 |
colors[i][2] / scale, |
|---|
| 313 |
1); |
|---|
| 314 |
light_core->setDiffuse (colors[i][0] / scale, |
|---|
| 315 |
colors[i][1] / scale, |
|---|
| 316 |
colors[i][2] / scale, |
|---|
| 317 |
1); |
|---|
| 318 |
light_core->setSpecular (1 / scale, |
|---|
| 319 |
1 / scale, |
|---|
| 320 |
1 / scale, |
|---|
| 321 |
1); |
|---|
| 322 |
light_core->setBeacon (beacon); |
|---|
| 323 |
|
|---|
| 324 |
light->setCore(light_core); |
|---|
| 325 |
light->addChild(lastnode); |
|---|
| 326 |
|
|---|
| 327 |
lights[i] = light_core; |
|---|
| 328 |
lastnode = light; |
|---|
| 329 |
} |
|---|
| 330 |
|
|---|
| 331 |
scene->addChild(lastnode); |
|---|
| 332 |
|
|---|
| 333 |
commitChanges(); |
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
mgr = new SimpleSceneManager; |
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
mgr->setWindow(gwin ); |
|---|
| 340 |
mgr->setRoot (scene); |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
mgr->setHeadlight(false); |
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
mgr->showAll(); |
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
glutMainLoop(); |
|---|
| 350 |
|
|---|
| 351 |
return 0; |
|---|
| 352 |
} |
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
void reshape(int w, int h) |
|---|
| 360 |
{ |
|---|
| 361 |
mgr->resize(w, h); |
|---|
| 362 |
glutPostRedisplay(); |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
void mouse(int button, int state, int x, int y) |
|---|
| 367 |
{ |
|---|
| 368 |
if (state) |
|---|
| 369 |
mgr->mouseButtonRelease(button, x, y); |
|---|
| 370 |
else |
|---|
| 371 |
mgr->mouseButtonPress(button, x, y); |
|---|
| 372 |
|
|---|
| 373 |
glutPostRedisplay(); |
|---|
| 374 |
} |
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
void motion(int x, int y) |
|---|
| 378 |
{ |
|---|
| 379 |
mgr->mouseMove(x, y); |
|---|
| 380 |
glutPostRedisplay(); |
|---|
| 381 |
} |
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
void keyboard(unsigned char k, int x, int y) |
|---|
| 385 |
{ |
|---|
| 386 |
switch(k) |
|---|
| 387 |
{ |
|---|
| 388 |
case 27: |
|---|
| 389 |
{ |
|---|
| 390 |
osgExit(); |
|---|
| 391 |
exit(1); |
|---|
| 392 |
} |
|---|
| 393 |
break; |
|---|
| 394 |
|
|---|
| 395 |
case 'a': |
|---|
| 396 |
{ |
|---|
| 397 |
for(UInt16 i = 0; i < nlights; ++i) |
|---|
| 398 |
{ |
|---|
| 399 |
lights[i]->setOn(true); |
|---|
| 400 |
} |
|---|
| 401 |
} |
|---|
| 402 |
break; |
|---|
| 403 |
|
|---|
| 404 |
case 's': |
|---|
| 405 |
{ |
|---|
| 406 |
for(UInt16 i = 0; i < nlights; ++i) |
|---|
| 407 |
{ |
|---|
| 408 |
if(lights[i]->getTypeId() != SpotLight::getClassTypeId()) |
|---|
| 409 |
{ |
|---|
| 410 |
lights[i]->setOn(false); |
|---|
| 411 |
} |
|---|
| 412 |
else |
|---|
| 413 |
{ |
|---|
| 414 |
lights[i]->setOn(true); |
|---|
| 415 |
} |
|---|
| 416 |
} |
|---|
| 417 |
commitChanges(); |
|---|
| 418 |
} |
|---|
| 419 |
break; |
|---|
| 420 |
|
|---|
| 421 |
case 'd': |
|---|
| 422 |
{ |
|---|
| 423 |
for(UInt16 i = 0; i < nlights; ++i) |
|---|
| 424 |
{ |
|---|
| 425 |
if(lights[i]->getTypeId() != DirectionalLight::getClassTypeId()) |
|---|
| 426 |
{ |
|---|
| 427 |
lights[i]->setOn(false); |
|---|
| 428 |
} |
|---|
| 429 |
else |
|---|
| 430 |
{ |
|---|
| 431 |
lights[i]->setOn(true); |
|---|
| 432 |
} |
|---|
| 433 |
} |
|---|
| 434 |
commitChanges(); |
|---|
| 435 |
} |
|---|
| 436 |
break; |
|---|
| 437 |
|
|---|
| 438 |
case 'p': |
|---|
| 439 |
{ |
|---|
| 440 |
for(UInt16 i = 0; i < nlights; ++i) |
|---|
| 441 |
{ |
|---|
| 442 |
if(lights[i]->getTypeId() != PointLight::getClassTypeId()) |
|---|
| 443 |
{ |
|---|
| 444 |
lights[i]->setOn(false); |
|---|
| 445 |
} |
|---|
| 446 |
else |
|---|
| 447 |
{ |
|---|
| 448 |
lights[i]->setOn(true); |
|---|
| 449 |
} |
|---|
| 450 |
} |
|---|
| 451 |
commitChanges(); |
|---|
| 452 |
} |
|---|
| 453 |
break; |
|---|
| 454 |
|
|---|
| 455 |
case 'S': |
|---|
| 456 |
{ |
|---|
| 457 |
mgr->setStatistics(!mgr->getStatistics()); |
|---|
| 458 |
} |
|---|
| 459 |
break; |
|---|
| 460 |
} |
|---|
| 461 |
} |
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
int setupGLUT(int *argc, char *argv[]) |
|---|
| 465 |
{ |
|---|
| 466 |
glutInit(argc, argv); |
|---|
| 467 |
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); |
|---|
| 468 |
|
|---|
| 469 |
int winid = glutCreateWindow("OpenSG"); |
|---|
| 470 |
|
|---|
| 471 |
glutReshapeFunc(reshape); |
|---|
| 472 |
glutDisplayFunc(display); |
|---|
| 473 |
glutMouseFunc(mouse); |
|---|
| 474 |
glutMotionFunc(motion); |
|---|
| 475 |
glutKeyboardFunc(keyboard); |
|---|
| 476 |
|
|---|
| 477 |
|
|---|
| 478 |
glutIdleFunc(display); |
|---|
| 479 |
|
|---|
| 480 |
return winid; |
|---|
| 481 |
} |
|---|