8 #include "OSGTrackball.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] =
51 bool move_obj
= false;
66 tball
.getRotation().getValue(m3
);
72 m2
.setTranslate( tball
.getPosition() );
78 scene_trans
->editSFMatrix()->setValue( m1
);
82 cam_trans
->editSFMatrix()->setValue( m1
);
86 cam_trans
->editSFMatrix()->setValue(tball
.getFullTrackballMatrix());
89 fprintf(stderr
, "%f %f %f\n",
90 cam_trans
->getMatrix()[3][0],
91 cam_trans
->getMatrix()[3][1],
92 cam_trans
->getMatrix()[3][2]);
95 OSG::Real32 t
= glutGet(GLUT_ELAPSED_TIME
);
97 for(OSG::UInt32 i
= 0; i
< 6; ++i
)
99 if(pAnimTrs
[i
] != NULL
)
101 pAnimTrs
[i
]->editRotation().setValueAsAxisDeg(
107 OSG::commitChanges();
109 // fprintf(stderr, "Frame start\n");
110 // fprintf(stderr, "============================================\n");
113 win
->render(rentravact
);
116 void reshape( int w
, int h
)
118 std::cerr
<< "Reshape: " << w
<< "," << h
<< std::endl
;
131 void motion(int x
, int y
)
133 OSG::Real32 w
= win
->getWidth(), h
= win
->getHeight();
137 a
= -2. * ( lastx
/ w
- .5 ),
138 b
= -2. * ( .5 - lasty
/ h
),
139 c
= -2. * ( x
/ w
- .5 ),
140 d
= -2. * ( .5 - y
/ h
);
142 if ( mouseb
& ( 1 << GLUT_LEFT_BUTTON
) )
144 tball
.updateRotation( a
, b
, c
, d
);
146 else if ( mouseb
& ( 1 << GLUT_MIDDLE_BUTTON
) )
148 tball
.updatePosition( a
, b
, c
, d
);
150 else if ( mouseb
& ( 1 << GLUT_RIGHT_BUTTON
) )
152 tball
.updatePositionNeg( a
, b
, c
, d
);
159 void mouse(int button
, int state
, int x
, int y
)
165 case GLUT_LEFT_BUTTON
:
168 case GLUT_MIDDLE_BUTTON
:
169 tball
.setAutoPosition(true);
172 case GLUT_RIGHT_BUTTON
:
173 tball
.setAutoPositionNeg(true);
177 mouseb
|= 1 << button
;
179 else if ( state
== 1 )
183 case GLUT_LEFT_BUTTON
: break;
185 case GLUT_MIDDLE_BUTTON
:
186 tball
.setAutoPosition(false);
189 case GLUT_RIGHT_BUTTON
:
190 tball
.setAutoPositionNeg(false);
194 mouseb
&= ~(1 << button
);
200 void vis(int visible
)
202 if (visible
== GLUT_VISIBLE
)
204 glutIdleFunc(animate
);
212 void key(unsigned char key
, int x
, int y
)
218 fprintf(stderr
, "Start Destruction\n\n");
244 glDisable( GL_LIGHTING
);
245 std::cerr
<< "Lighting disabled." << std::endl
;
248 glEnable( GL_LIGHTING
);
249 std::cerr
<< "Lighting enabled." << std::endl
;
253 std::cerr
<< "Sending ray through " << x
<< "," << y
<< std::endl
;
255 cam
->calcViewRay( l
, x
, y
, *vp
);
256 std::cerr
<< "From " << l
.getPosition() << ", dir "
263 OSG::ActionDataSlotPool::the()->dumpState();
264 OSG::StageIdPool ::the()->dumpState();
265 rentravact
->dumpStore();
276 oldq
= tball
.getRotation();
277 oldv
= tball
.getPosition();
279 move_obj
= ! move_obj
;
282 puts("moving object");
283 tball
.setMode( OSG::Trackball::OSGCamera
);
288 puts("moving camera");
289 tball
.setMode( OSG::Trackball::OSGObject
);
292 tball
.setStartPosition( v
, true );
293 tball
.setStartRotation( q
, true );
300 OSG::NodeTransitPtr
setupAnim(void)
302 OSG::NodeTransitPtr returnValue
= OSG::Node::create();
304 returnValue
->setCore(OSG::Group::create());
306 static const OSG::Real32 aOffsets
[6][3] =
316 static const OSG::Real32 aDiffuse
[6][3] =
326 for(OSG::UInt32 i
= 0; i
< 6; ++i
)
328 OSG::NodeUnrecPtr pTN
= OSG::Node::create();
330 pAnimTrs
[i
] = OSG::ComponentTransform::create();
332 OSG::GeometryUnrecPtr pGeo
= OSG::makeBoxGeo(1.f
, 1.f
, 1.f
,
334 OSG::NodeUnrecPtr pGeoNode
= OSG::Node::create();
336 pGeoNode
->setCore(pGeo
);
338 OSG::SimpleMaterialUnrecPtr pMat
= OSG::SimpleMaterial::create();
340 pMat
->setDiffuse(OSG::Color3f(aDiffuse
[i
][0],
343 pMat
->setAmbient(OSG::Color3f(aDiffuse
[i
][0],
347 pGeo
->setMaterial(pMat
);
349 pAnimTrs
[i
]->editTranslation().setValues(aOffsets
[i
][0],
353 pTN
->setCore (pAnimTrs
[i
]);
354 pTN
->addChild(pGeoNode
);
356 returnValue
->addChild(pTN
);
363 int doMain (int argc
, char **argv
)
365 OSG::osgInit(argc
,argv
);
369 glutInit(&argc
, argv
);
370 glutInitDisplayMode( GLUT_RGBA
| GLUT_DEPTH
| GLUT_DOUBLE
);
371 glutInitWindowSize(800, 800);
374 int winid
= glutCreateWindow("OpenSG");
375 glutKeyboardFunc(key
);
376 glutVisibilityFunc(vis
);
377 glutReshapeFunc(reshape
);
378 glutDisplayFunc(display
);
379 glutMouseFunc(mouse
);
380 glutMotionFunc(motion
);
382 glutIdleFunc(display
);
386 OSG::SceneFileHandler::the()->print();
390 // beacon for camera and light
391 OSG::NodeUnrecPtr b1n
= OSG::Node::create();
392 OSG::GroupUnrecPtr b1
= OSG::Group::create();
394 fprintf(stderr
, "Create b1n %p %d %d \n",
395 static_cast<void *>(b1n
.get()),
397 b1n
->getWeakRefCount());
402 OSG::NodeUnrecPtr t1n
= OSG::Node::create();
403 OSG::TransformUnrecPtr t1
= OSG::Transform::create();
408 fprintf(stderr
, "Create t1n %p %d %d \n",
409 static_cast<void *>(t1n
.get()),
411 t1n
->getWeakRefCount());
417 OSG::NodeUnrecPtr dlight
= OSG::Node::create();
418 OSG::DirectionalLightUnrecPtr dl
= OSG::DirectionalLight::create();
423 dl
->setAmbient( .3f
, .3f
, .3f
, 1 );
424 dl
->setDiffuse( .8f
, .8f
, .8f
, .8f
);
425 dl
->setDirection(0,0,1);
429 fprintf(stderr
, "Create dlight %p %d %d \n",
430 static_cast<void *>(dlight
.get()),
431 dlight
->getRefCount(),
432 dlight
->getWeakRefCount());
434 hdrroot
= OSG::Node::create();
436 hdrroot
->editVolume().setInfinite();
437 hdrroot
->editVolume().setStatic ();
439 hdrroot
->setCore(OSG::Group::create());
442 root
= OSG::Node:: create();
444 OSG::GroupUnrecPtr gr1
= OSG::Group::create();
449 hdrroot
->addChild(root
);
451 root
->addChild(t1n
);
452 root
->addChild(dlight
);
454 fprintf(stderr
, "Create root %p %d %d \n",
455 static_cast<void *>(root
.get()),
457 root
->getWeakRefCount());
461 OSG::NodeUnrecPtr file
= NULL
;
465 file
= OSG::SceneFileHandler::the()->read(argv
[1], NULL
);
470 std::cerr
<< "Couldn't load file, ignoring" << std::endl
;
472 // file = makeBox(2.f, 2.f, 2.f, 2, 2, 2);
473 file
= OSG::makeSphere(4, 2.0);
476 OSG::NodeUnrecPtr pCubeRoot
= OSG::Node::create();
477 OSG::CubeMapGeneratorUnrecPtr pCubeGen
= OSG::CubeMapGenerator::create();
479 pCubeRoot
->addChild(file
);
480 pCubeRoot
->setCore(pCubeGen
);
481 // pCubeRoot->setCore(Group::create());
483 OSG::NodeUnrecPtr pCubeSceneRoot
= OSG::Node::create();
484 OSG::VisitSubTreeUnrecPtr pCubeVisit
= OSG::VisitSubTree::create();
486 pCubeSceneRoot
->setCore(pCubeVisit
);
487 pCubeVisit
->setSubTreeRoot(root
);
489 pCubeGen
->setRoot (pCubeSceneRoot
);
490 pCubeGen
->setTextureFormat(GL_RGB32F_ARB
);
491 pCubeGen
->setSize (512,
493 pCubeGen
->setTexUnit (3);
495 // Cubemap Background
496 OSG::SolidBackgroundUnrecPtr cubeBkgnd
= OSG::SolidBackground::create();
498 cubeBkgnd
->setColor(OSG::Color3f(0.5f
, 0.3f
, 0.3f
));
501 pCubeGen
->setBackground(cubeBkgnd
);
504 OSG::NodeUnrecPtr pAnimRoot
= setupAnim();
506 scene_trans
= OSG::Transform::create();
507 OSG::NodeUnrecPtr sceneTrN
= OSG::Node::create();
509 scene_trans
->editMatrix()[3][2] = -50.f
;
511 sceneTrN
->setCore (scene_trans
);
512 sceneTrN
->addChild(pCubeRoot
);
513 sceneTrN
->addChild(pAnimRoot
);
515 OSG::Thread::getCurrentChangeList()->commitChanges();
518 sceneTrN
->updateVolume();
519 sceneTrN
->getVolume().getBounds(min
, max
);
521 std::cout
<< "Volume: from " << min
<< " to " << max
<< std::endl
;
524 dlight
->addChild(sceneTrN
);
528 cam
= OSG::PerspectiveCamera::create();
530 cam
->setBeacon( b1n
);
531 cam
->setFov( OSG::osgDegree2Rad( 90 ) );
532 cam
->setNear( 0.1f
);
533 cam
->setFar( 100000 );
537 OSG::SolidBackgroundUnrecPtr bkgnd
= OSG::SolidBackground::create();
539 bkgnd
->setColor(OSG::Color3f(0.3f
, 0.3f
, 0.3f
));
543 vp
= OSG::Viewport::create();
545 vp
->setCamera( cam
);
546 vp
->setBackground( bkgnd
);
547 vp
->setRoot( hdrroot
);
548 // vp->setRoot( root );
549 vp
->setSize( 0,0, 1,1 );
554 OSG::GLUTWindowUnrecPtr gwin
;
558 glGetIntegerv(GL_VIEWPORT
, glvp
);
560 gwin
= OSG::GLUTWindow::create();
562 gwin
->setGlutId(winid
);
563 gwin
->setSize( glvp
[2], glvp
[3] );
573 rentravact
= OSG::RenderAction::create();
575 rentravact
->setVolumeDrawing(true);
576 // rentravact->setFrustumCulling(false);
581 pos
.setValues(min
[0] + ((max
[0] - min
[0]) * 0.5),
582 min
[1] + ((max
[1] - min
[1]) * 0.5),
583 max
[2] + ( max
[2] - min
[2] ) * 1.5 );
585 float scale
= (max
[2] - min
[2] + max
[1] - min
[1] + max
[0] - min
[0]) / 6;
587 OSG::Pnt3f
tCenter(min
[0] + (max
[0] - min
[0]) / 2,
588 min
[1] + (max
[1] - min
[1]) / 2,
589 min
[2] + (max
[2] - min
[2]) / 2);
591 fprintf(stderr
, "Startpos : %f %f %f\n", pos
[0], pos
[1], pos
[2]);
593 tball
.setMode (OSG::Trackball::OSGObject
);
594 tball
.setStartPosition (pos
, true );
595 tball
.setSum (true );
596 tball
.setTranslationMode (OSG::Trackball::OSGFree
);
597 tball
.setTranslationScale(scale
);
598 tball
.setRotationCenter (tCenter
);
600 fprintf(stderr
, "Create b1n %p %d %d \n",
601 static_cast<void *>(b1n
.get()),
603 b1n
->getWeakRefCount());
605 fprintf(stderr
, "Create t1n %p %d %d \n",
606 static_cast<void *>(t1n
.get()),
608 t1n
->getWeakRefCount());
610 fprintf(stderr
, "Create dlight %p %d %d \n",
611 static_cast<void *>(dlight
.get()),
612 dlight
->getRefCount(),
613 dlight
->getWeakRefCount());
615 fprintf(stderr
, "Create hdrroot %p %d %d \n",
616 static_cast<void *>(hdrroot
.get()),
617 hdrroot
->getRefCount(),
618 hdrroot
->getWeakRefCount());
620 fprintf(stderr
, "Create root %p %d %d \n",
621 static_cast<void *>(root
.get()),
623 root
->getWeakRefCount());
628 int main (int argc
, char **argv
)
632 fprintf(stderr
, "Create hdrroot %p %d %d \n",
633 static_cast<void *>(hdrroot
.get()),
634 hdrroot
->getRefCount(),
635 hdrroot
->getWeakRefCount());
637 fprintf(stderr
, "Create root %p %d %d \n",
638 static_cast<void *>(root
.get()),
640 root
->getWeakRefCount());