fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Groups / DynamicStateGenerators / testCubemapMapOnlyTravMask.cpp
blobdfd10781768cd5a5fe555a3281c42384ffbc6d5e
1 #include "OSGConfig.h"
3 #include <iostream>
5 #include "OSGGLUT.h"
6 #include "OSGNode.h"
8 #include "OSGTrackball.h"
9 #include "OSGLine.h"
10 #include "OSGPerspectiveCamera.h"
11 #include "OSGTransform.h"
12 #include "OSGComponentTransform.h"
13 #include "OSGRenderAction.h"
14 #include "OSGWindow.h"
15 #include "OSGSceneFileHandler.h"
16 #include "OSGSolidBackground.h"
17 #include "OSGSkyBackground.h"
18 #include "OSGGLUTWindow.h"
19 #include "OSGDirectionalLight.h"
20 #include "OSGSimpleGeometry.h"
21 #include "OSGSimpleMaterial.h"
22 #include "OSGCubeMapGenerator.h"
23 #include "OSGImageFileHandler.h"
24 #include "OSGVisitSubTree.h"
26 OSG::RenderActionRefPtr rentravact = NULL;
28 OSG::NodeRecPtr hdrroot = NULL;
29 OSG::NodeRecPtr root = NULL;
30 //OSG::NodeRecPtr file = NULL;
32 OSG::PerspectiveCameraRecPtr cam = NULL;
33 OSG::ViewportRecPtr vp = NULL;
34 OSG::WindowRecPtr win = NULL;
36 OSG::TransformRecPtr cam_trans = NULL;
37 OSG::TransformRecPtr scene_trans = NULL;
39 OSG::ComponentTransformRecPtr pAnimTrs[6] =
41 NULL,
42 NULL,
43 NULL,
44 NULL,
45 NULL,
46 NULL
49 OSG::Trackball tball;
51 bool move_obj = false;
53 int mouseb = 0;
54 int lastx = 0;
55 int lasty = 0;
57 OSG::Quaternion oldq;
58 OSG::Vec3f oldv;
60 static const OSG::UInt32 uiMainRenderMask = 0x0001;
61 static const OSG::UInt32 uiCubeRenderMask = 0x0002;
63 void display(void)
65 #if 0
66 Matrix m1, m2, m3;
67 Quaternion q1;
69 tball.getRotation().getValue(m3);
71 q1.setValue(m3);
73 m1.setRotate(q1);
75 m2.setTranslate( tball.getPosition() );
77 m1.mult( m2 );
79 if(move_obj == true)
81 scene_trans->editSFMatrix()->setValue( m1 );
83 else
85 cam_trans->editSFMatrix()->setValue( m1 );
87 #endif
89 cam_trans->editSFMatrix()->setValue(tball.getFullTrackballMatrix());
91 #if 0
92 fprintf(stderr, "%f %f %f\n",
93 cam_trans->getMatrix()[3][0],
94 cam_trans->getMatrix()[3][1],
95 cam_trans->getMatrix()[3][2]);
96 #endif
98 OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME);
100 for(OSG::UInt32 i = 0; i < 6; ++i)
102 if(pAnimTrs[i] != NULL)
104 pAnimTrs[i]->editRotation().setValueAsAxisDeg(
105 0.f, 1.f, 0.f,
106 t / 50.f);
110 OSG::commitChanges();
112 // fprintf(stderr, "Frame start\n");
113 // fprintf(stderr, "============================================\n");
116 win->render(rentravact);
119 void reshape( int w, int h )
121 std::cerr << "Reshape: " << w << "," << h << std::endl;
122 win->resize( w, h );
126 void animate(void)
128 glutPostRedisplay();
131 // tballall stuff
134 void motion(int x, int y)
136 OSG::Real32 w = win->getWidth(), h = win->getHeight();
139 OSG::Real32
140 a = -2. * ( lastx / w - .5 ),
141 b = -2. * ( .5 - lasty / h ),
142 c = -2. * ( x / w - .5 ),
143 d = -2. * ( .5 - y / h );
145 if ( mouseb & ( 1 << GLUT_LEFT_BUTTON ) )
147 tball.updateRotation( a, b, c, d );
149 else if ( mouseb & ( 1 << GLUT_MIDDLE_BUTTON ) )
151 tball.updatePosition( a, b, c, d );
153 else if ( mouseb & ( 1 << GLUT_RIGHT_BUTTON ) )
155 tball.updatePositionNeg( a, b, c, d );
158 lastx = x;
159 lasty = y;
162 void mouse(int button, int state, int x, int y)
164 if ( state == 0 )
166 switch ( button )
168 case GLUT_LEFT_BUTTON:
169 break;
171 case GLUT_MIDDLE_BUTTON:
172 tball.setAutoPosition(true);
173 break;
175 case GLUT_RIGHT_BUTTON:
176 tball.setAutoPositionNeg(true);
177 break;
180 mouseb |= 1 << button;
182 else if ( state == 1 )
184 switch ( button )
186 case GLUT_LEFT_BUTTON: break;
188 case GLUT_MIDDLE_BUTTON:
189 tball.setAutoPosition(false);
190 break;
192 case GLUT_RIGHT_BUTTON:
193 tball.setAutoPositionNeg(false);
194 break;
197 mouseb &= ~(1 << button);
199 lastx = x;
200 lasty = y;
203 void vis(int visible)
205 if (visible == GLUT_VISIBLE)
207 glutIdleFunc(animate);
209 else
211 glutIdleFunc(NULL);
215 void key(unsigned char key, int x, int y)
217 switch ( key )
219 case 27:
221 fprintf(stderr, "Start Destruction\n\n");
223 rentravact = NULL;
225 hdrroot = NULL;
226 root = NULL;
227 // file = NULL;
229 cam = NULL;
230 vp = NULL;
231 win = NULL;
233 cam_trans = NULL;
234 scene_trans = NULL;
236 pAnimTrs[0] = NULL;
237 pAnimTrs[1] = NULL;
238 pAnimTrs[2] = NULL;
239 pAnimTrs[3] = NULL;
240 pAnimTrs[4] = NULL;
241 pAnimTrs[5] = NULL;
244 OSG::osgExit();
245 exit(0);
246 case 'a':
247 glDisable( GL_LIGHTING );
248 std::cerr << "Lighting disabled." << std::endl;
249 break;
250 case 's':
251 glEnable( GL_LIGHTING );
252 std::cerr << "Lighting enabled." << std::endl;
253 break;
254 case 'r':
256 std::cerr << "Sending ray through " << x << "," << y << std::endl;
257 OSG::Line l;
258 cam->calcViewRay( l, x, y, *vp );
259 std::cerr << "From " << l.getPosition() << ", dir "
260 << l.getDirection()
261 << std::endl;
263 break;
265 case 'd':
266 OSG::ActionDataSlotPool::the()->dumpState();
267 OSG::StageIdPool ::the()->dumpState();
268 rentravact->dumpStore();
269 break;
270 case ' ':
272 OSG::Matrix m;
273 OSG::Quaternion q;
274 OSG::Vec3f v;
276 q = oldq;
277 v = oldv;
279 oldq = tball.getRotation();
280 oldv = tball.getPosition();
282 move_obj = ! move_obj;
283 if ( move_obj )
285 puts("moving object");
286 tball.setMode( OSG::Trackball::OSGCamera );
289 else
291 puts("moving camera");
292 tball.setMode( OSG::Trackball::OSGObject );
295 tball.setStartPosition( v, true );
296 tball.setStartRotation( q, true );
298 break;
303 OSG::NodeTransitPtr setupAnim(void)
305 OSG::NodeTransitPtr returnValue = OSG::Node::create();
307 returnValue->setCore(OSG::Group::create());
309 static const OSG::Real32 aOffsets[6][3] =
311 { -5.5, 0.0, 0.0 },
312 { 5.5, 0.0, 0.0 },
313 { 0.0, -5.5, 0.0 },
314 { 0.0, 5.5, 0.0 },
315 { 0.0, 0.0, -5.5 },
316 { 0.0, 0.0, 5.5 }
319 static const OSG::Real32 aDiffuse[6][3] =
321 { 1.f, 0.f, 0.f },
322 { 0.f, 1.f, 0.f },
323 { 0.f, 0.f, 1.f },
324 { 1.f, 1.f, 0.f },
325 { 1.f, 0.f, 1.f },
326 { 0.f, 1.f, 1.f }
329 for(OSG::UInt32 i = 0; i < 6; ++i)
331 OSG::NodeUnrecPtr pTN = OSG::Node::create();
333 pAnimTrs[i] = OSG::ComponentTransform::create();
335 OSG::GeometryUnrecPtr pGeo = OSG::makeBoxGeo(1.f, 1.f, 1.f,
336 2, 2, 2);
337 OSG::NodeUnrecPtr pGeoNode = OSG::Node::create();
339 pGeoNode->setCore(pGeo);
341 OSG::SimpleMaterialUnrecPtr pMat = OSG::SimpleMaterial::create();
343 pMat->setDiffuse(OSG::Color3f(aDiffuse[i][0],
344 aDiffuse[i][1],
345 aDiffuse[i][2]));
346 pMat->setAmbient(OSG::Color3f(aDiffuse[i][0],
347 aDiffuse[i][1],
348 aDiffuse[i][2]));
350 pGeo->setMaterial(pMat);
352 pAnimTrs[i]->editTranslation().setValues(aOffsets[i][0],
353 aOffsets[i][1],
354 aOffsets[i][2]);
356 pTN->setCore (pAnimTrs[i]);
357 pTN->addChild(pGeoNode );
359 returnValue->addChild(pTN);
362 returnValue->setTravMask(uiCubeRenderMask);
364 return returnValue;
367 int doMain (int argc, char **argv)
369 OSG::osgInit(argc,argv);
371 // GLUT init
373 glutInit(&argc, argv);
374 glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
375 glutInitWindowSize(800, 800);
378 int winid = glutCreateWindow("OpenSG");
379 glutKeyboardFunc(key);
380 glutVisibilityFunc(vis);
381 glutReshapeFunc(reshape);
382 glutDisplayFunc(display);
383 glutMouseFunc(mouse);
384 glutMotionFunc(motion);
386 glutIdleFunc(display);
388 // OSG
390 OSG::SceneFileHandler::the()->print();
392 // create the graph
394 // beacon for camera and light
395 OSG::NodeUnrecPtr b1n = OSG::Node::create();
396 OSG::GroupUnrecPtr b1 = OSG::Group::create();
398 fprintf(stderr, "Create b1n %p %d %d \n",
399 static_cast<void *>(b1n.get()),
400 b1n->getRefCount(),
401 b1n->getWeakRefCount());
403 b1n->setCore( b1 );
405 // transformation
406 OSG::NodeUnrecPtr t1n = OSG::Node::create();
407 OSG::TransformUnrecPtr t1 = OSG::Transform::create();
409 t1n->setCore (t1 );
410 t1n->addChild(b1n);
412 fprintf(stderr, "Create t1n %p %d %d \n",
413 static_cast<void *>(t1n.get()),
414 t1n->getRefCount(),
415 t1n->getWeakRefCount());
417 cam_trans = t1;
419 // light
421 OSG::NodeUnrecPtr dlight = OSG::Node::create();
422 OSG::DirectionalLightUnrecPtr dl = OSG::DirectionalLight::create();
425 dlight->setCore(dl);
427 dl->setAmbient( .3f, .3f, .3f, 1 );
428 dl->setDiffuse( .8f, .8f, .8f, .8f );
429 dl->setDirection(0,0,1);
430 dl->setBeacon( b1n);
433 fprintf(stderr, "Create dlight %p %d %d \n",
434 static_cast<void *>(dlight.get()),
435 dlight->getRefCount(),
436 dlight->getWeakRefCount());
438 hdrroot = OSG::Node::create();
440 hdrroot->editVolume().setInfinite();
441 hdrroot->editVolume().setStatic ();
443 hdrroot->setCore(OSG::Group::create());
445 // root
446 root = OSG::Node:: create();
448 OSG::GroupUnrecPtr gr1 = OSG::Group::create();
450 root->setCore(gr1);
453 hdrroot->addChild(root);
455 root->addChild(t1n );
456 root->addChild(dlight);
458 fprintf(stderr, "Create root %p %d %d \n",
459 static_cast<void *>(root.get()),
460 root->getRefCount(),
461 root->getWeakRefCount());
463 // Load the file
465 OSG::NodeUnrecPtr file = NULL;
467 if(argc > 1)
469 file = OSG::SceneFileHandler::the()->read(argv[1], NULL);
472 if(file == NULL)
474 std::cerr << "Couldn't load file, ignoring" << std::endl;
476 // file = makeBox(2.f, 2.f, 2.f, 2, 2, 2);
477 file = OSG::makeSphere(4, 2.0);
480 OSG::NodeUnrecPtr pCubeRoot = OSG::Node::create();
481 OSG::CubeMapGeneratorUnrecPtr pCubeGen = OSG::CubeMapGenerator::create();
483 pCubeRoot->addChild(file);
484 pCubeRoot->setCore(pCubeGen);
485 // pCubeRoot->setCore(Group::create());
487 OSG::NodeUnrecPtr pCubeSceneRoot = OSG::Node::create();
488 OSG::VisitSubTreeUnrecPtr pCubeVisit = OSG::VisitSubTree::create();
490 pCubeVisit->setSubTreeTravMask(uiCubeRenderMask );
491 pCubeVisit->setTravMaskMode (OSG::VisitSubTree::ReplaceTravMask);
493 pCubeSceneRoot->setCore(pCubeVisit);
494 pCubeVisit->setSubTreeRoot(root);
496 pCubeGen->setRoot (pCubeSceneRoot);
497 pCubeGen->setTextureFormat(GL_RGB32F_ARB );
498 pCubeGen->setSize (512,
499 512 );
500 pCubeGen->setTexUnit (3);
502 // Cubemap Background
503 OSG::SolidBackgroundUnrecPtr cubeBkgnd = OSG::SolidBackground::create();
505 cubeBkgnd->setColor(OSG::Color3f(0.5f, 0.3f, 0.3f));
508 pCubeGen->setBackground(cubeBkgnd);
511 OSG::NodeUnrecPtr pAnimRoot = setupAnim();
513 scene_trans = OSG::Transform::create();
514 OSG::NodeUnrecPtr sceneTrN = OSG::Node::create();
516 scene_trans->editMatrix()[3][2] = -50.f;
518 sceneTrN->setCore (scene_trans);
519 sceneTrN->addChild(pCubeRoot );
520 sceneTrN->addChild(pAnimRoot );
522 OSG::Thread::getCurrentChangeList()->commitChanges();
524 OSG::Vec3f min,max;
525 sceneTrN->updateVolume();
526 sceneTrN->getVolume().getBounds(min, max);
528 std::cout << "Volume: from " << min << " to " << max << std::endl;
531 dlight->addChild(sceneTrN);
533 // Camera
535 cam = OSG::PerspectiveCamera::create();
537 cam->setBeacon( b1n );
538 cam->setFov( OSG::osgDegree2Rad( 90 ) );
539 cam->setNear( 0.1f );
540 cam->setFar( 100000 );
543 // Background
544 OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();
546 bkgnd->setColor(OSG::Color3f(0.3f, 0.3f, 0.3f));
549 // Viewport
550 vp = OSG::Viewport::create();
552 vp->setCamera( cam );
553 vp->setBackground( bkgnd );
554 vp->setRoot( hdrroot );
555 // vp->setRoot( root );
556 vp->setSize( 0,0, 1,1 );
559 vp->setTravMask(uiMainRenderMask);
561 // Window
562 OSG::GLUTWindowUnrecPtr gwin;
564 GLint glvp[4];
566 glGetIntegerv(GL_VIEWPORT, glvp);
568 gwin = OSG::GLUTWindow::create();
570 gwin->setGlutId(winid);
571 gwin->setSize( glvp[2], glvp[3] );
573 win = gwin;
575 win->addPort( vp );
577 win->init();
580 // Action
581 rentravact = OSG::RenderAction::create();
583 rentravact->setVolumeDrawing(true);
584 // rentravact->setFrustumCulling(false);
587 // tball
588 OSG::Vec3f pos;
590 pos.setValues(min[0] + ((max[0] - min[0]) * 0.5),
591 min[1] + ((max[1] - min[1]) * 0.5),
592 max[2] + ( max[2] - min[2] ) * 1.5 );
594 float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;
596 OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
597 min[1] + (max[1] - min[1]) / 2,
598 min[2] + (max[2] - min[2]) / 2);
600 fprintf(stderr, "Startpos : %f %f %f\n", pos[0], pos[1], pos[2]);
602 tball.setMode (OSG::Trackball::OSGObject);
603 tball.setStartPosition (pos, true );
604 tball.setSum (true );
605 tball.setTranslationMode (OSG::Trackball::OSGFree );
606 tball.setTranslationScale(scale );
607 tball.setRotationCenter (tCenter );
609 fprintf(stderr, "Create b1n %p %d %d \n",
610 static_cast<void *>(b1n.get()),
611 b1n->getRefCount(),
612 b1n->getWeakRefCount());
614 fprintf(stderr, "Create t1n %p %d %d \n",
615 static_cast<void *>(t1n.get()),
616 t1n->getRefCount(),
617 t1n->getWeakRefCount());
619 fprintf(stderr, "Create dlight %p %d %d \n",
620 static_cast<void *>(dlight.get()),
621 dlight->getRefCount(),
622 dlight->getWeakRefCount());
624 fprintf(stderr, "Create hdrroot %p %d %d \n",
625 static_cast<void *>(hdrroot.get()),
626 hdrroot->getRefCount(),
627 hdrroot->getWeakRefCount());
629 fprintf(stderr, "Create root %p %d %d \n",
630 static_cast<void *>(root.get()),
631 root->getRefCount(),
632 root->getWeakRefCount());
634 return 0;
637 int main (int argc, char **argv)
639 doMain(argc, argv);
641 fprintf(stderr, "Create hdrroot %p %d %d \n",
642 static_cast<void *>(hdrroot.get()),
643 hdrroot->getRefCount(),
644 hdrroot->getWeakRefCount());
646 fprintf(stderr, "Create root %p %d %d \n",
647 static_cast<void *>(root.get()),
648 root->getRefCount(),
649 root->getWeakRefCount());
651 // run...
652 glutMainLoop();
654 return 0;