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;
60 static const OSG::UInt32 uiMainRenderMask
= 0x0001;
61 static const OSG::UInt32 uiCubeRenderMask
= 0x0002;
69 tball
.getRotation().getValue(m3
);
75 m2
.setTranslate( tball
.getPosition() );
81 scene_trans
->editSFMatrix()->setValue( m1
);
85 cam_trans
->editSFMatrix()->setValue( m1
);
89 cam_trans
->editSFMatrix()->setValue(tball
.getFullTrackballMatrix());
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]);
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(
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
;
134 void motion(int x
, int y
)
136 OSG::Real32 w
= win
->getWidth(), h
= win
->getHeight();
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
);
162 void mouse(int button
, int state
, int x
, int y
)
168 case GLUT_LEFT_BUTTON
:
171 case GLUT_MIDDLE_BUTTON
:
172 tball
.setAutoPosition(true);
175 case GLUT_RIGHT_BUTTON
:
176 tball
.setAutoPositionNeg(true);
180 mouseb
|= 1 << button
;
182 else if ( state
== 1 )
186 case GLUT_LEFT_BUTTON
: break;
188 case GLUT_MIDDLE_BUTTON
:
189 tball
.setAutoPosition(false);
192 case GLUT_RIGHT_BUTTON
:
193 tball
.setAutoPositionNeg(false);
197 mouseb
&= ~(1 << button
);
203 void vis(int visible
)
205 if (visible
== GLUT_VISIBLE
)
207 glutIdleFunc(animate
);
215 void key(unsigned char key
, int x
, int y
)
221 fprintf(stderr
, "Start Destruction\n\n");
247 glDisable( GL_LIGHTING
);
248 std::cerr
<< "Lighting disabled." << std::endl
;
251 glEnable( GL_LIGHTING
);
252 std::cerr
<< "Lighting enabled." << std::endl
;
256 std::cerr
<< "Sending ray through " << x
<< "," << y
<< std::endl
;
258 cam
->calcViewRay( l
, x
, y
, *vp
);
259 std::cerr
<< "From " << l
.getPosition() << ", dir "
266 OSG::ActionDataSlotPool::the()->dumpState();
267 OSG::StageIdPool ::the()->dumpState();
268 rentravact
->dumpStore();
279 oldq
= tball
.getRotation();
280 oldv
= tball
.getPosition();
282 move_obj
= ! move_obj
;
285 puts("moving object");
286 tball
.setMode( OSG::Trackball::OSGCamera
);
291 puts("moving camera");
292 tball
.setMode( OSG::Trackball::OSGObject
);
295 tball
.setStartPosition( v
, true );
296 tball
.setStartRotation( q
, true );
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] =
319 static const OSG::Real32 aDiffuse
[6][3] =
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
,
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],
346 pMat
->setAmbient(OSG::Color3f(aDiffuse
[i
][0],
350 pGeo
->setMaterial(pMat
);
352 pAnimTrs
[i
]->editTranslation().setValues(aOffsets
[i
][0],
356 pTN
->setCore (pAnimTrs
[i
]);
357 pTN
->addChild(pGeoNode
);
359 returnValue
->addChild(pTN
);
362 returnValue
->setTravMask(uiCubeRenderMask
);
367 int doMain (int argc
, char **argv
)
369 OSG::osgInit(argc
,argv
);
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
);
390 OSG::SceneFileHandler::the()->print();
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()),
401 b1n
->getWeakRefCount());
406 OSG::NodeUnrecPtr t1n
= OSG::Node::create();
407 OSG::TransformUnrecPtr t1
= OSG::Transform::create();
412 fprintf(stderr
, "Create t1n %p %d %d \n",
413 static_cast<void *>(t1n
.get()),
415 t1n
->getWeakRefCount());
421 OSG::NodeUnrecPtr dlight
= OSG::Node::create();
422 OSG::DirectionalLightUnrecPtr dl
= OSG::DirectionalLight::create();
427 dl
->setAmbient( .3f
, .3f
, .3f
, 1 );
428 dl
->setDiffuse( .8f
, .8f
, .8f
, .8f
);
429 dl
->setDirection(0,0,1);
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());
446 root
= OSG::Node:: create();
448 OSG::GroupUnrecPtr gr1
= OSG::Group::create();
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()),
461 root
->getWeakRefCount());
465 OSG::NodeUnrecPtr file
= NULL
;
469 file
= OSG::SceneFileHandler::the()->read(argv
[1], 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,
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();
525 sceneTrN
->updateVolume();
526 sceneTrN
->getVolume().getBounds(min
, max
);
528 std::cout
<< "Volume: from " << min
<< " to " << max
<< std::endl
;
531 dlight
->addChild(sceneTrN
);
535 cam
= OSG::PerspectiveCamera::create();
537 cam
->setBeacon( b1n
);
538 cam
->setFov( OSG::osgDegree2Rad( 90 ) );
539 cam
->setNear( 0.1f
);
540 cam
->setFar( 100000 );
544 OSG::SolidBackgroundUnrecPtr bkgnd
= OSG::SolidBackground::create();
546 bkgnd
->setColor(OSG::Color3f(0.3f
, 0.3f
, 0.3f
));
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
);
562 OSG::GLUTWindowUnrecPtr gwin
;
566 glGetIntegerv(GL_VIEWPORT
, glvp
);
568 gwin
= OSG::GLUTWindow::create();
570 gwin
->setGlutId(winid
);
571 gwin
->setSize( glvp
[2], glvp
[3] );
581 rentravact
= OSG::RenderAction::create();
583 rentravact
->setVolumeDrawing(true);
584 // rentravact->setFrustumCulling(false);
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()),
612 b1n
->getWeakRefCount());
614 fprintf(stderr
, "Create t1n %p %d %d \n",
615 static_cast<void *>(t1n
.get()),
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()),
632 root
->getWeakRefCount());
637 int main (int argc
, char **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()),
649 root
->getWeakRefCount());