7 #include "OSGFieldContainerFactory.h"
9 #include "OSGQuaternion.h"
10 #include "OSGMatrix.h"
11 #include "OSGMatrixUtility.h"
12 #include "OSGBoxVolume.h"
16 #include "OSGThread.h"
17 #include "OSGTransform.h"
18 #include "OSGAction.h"
19 #include "OSGRenderAction.h"
20 #include "OSGSimpleGeometry.h"
21 #include "OSGSceneFileHandler.h"
22 #include "OSGChunkOverrideGroup.h"
23 #include "OSGPolygonChunk.h"
24 #include "OSGDirectionalLight.h"
26 #include "OSGViewport.h"
27 #include "OSGCamera.h"
28 #include "OSGWindow.h"
29 #include "OSGGLUTWindow.h"
30 #include "OSGCamera.h"
31 #include "OSGPerspectiveCamera.h"
32 #include "OSGSolidBackground.h"
33 //#include "OSGUniformBackground.h"
34 #include "OSGOSGWriter.h"
35 #include "OSGChangeList.h"
36 #include "OSGIOStream.h"
37 #include "OSGGeoFunctions.h"
38 #include "OSGGraphOp.h"
39 #include "OSGGraphOpFactory.h"
40 #include "OSGMultiCore.h"
41 #include "OSGFrameHandler.h"
42 #include "OSGWebInterface.h"
44 #include "OSGTrackball.h"
46 OSG::RenderActionRefPtr rentravact
;
50 //OSG::NodeRecPtr file;
52 OSG::PerspectiveCameraRecPtr cam
;
53 OSG::ViewportRecPtr vp
;
54 OSG::WindowRecPtr win
;
56 OSG::TransformRecPtr cam_trans
;
57 OSG::TransformRecPtr scene_trans
;
59 OSG::PolygonChunkRecPtr pPoly
;
60 bool bPolyActive
= false;
61 OSG::ChunkOverrideGroupRecPtr pCOver
;
63 OSG::GLUTWindowUnrecPtr gwin
;
67 bool move_obj
= false;
75 OSG::WebInterfaceRefPtr webInterface
;
80 OSG::Matrix m1
, m2
, m3
;
83 tball
.getRotation().getValue(m3
);
89 // std::cout << "TBROT" << std::endl << tball.getRotation() << endl;
90 // std::cout << "M3" << std::endl << m3 << std::endl;
91 // std::cout << "Q1" << std::endl << q1 << std::endl;
92 // std::cout << "M1" << std::endl << m1 << std::endl;
94 // m1.setRotate( tball.getRotation() );
95 m2
.setTranslate( tball
.getPosition() );
97 //std::cout << "Pos: " << tball.getPosition() << ", Rot: " << tball.getRotation() << std::endl;
99 // std::cout << tball.getRotation() << std::endl;
103 // std::cerr << m1 << std::endl;
105 m1
= tball
.getFullTrackballMatrix();
109 scene_trans
->editSFMatrix()->setValue( m1
);
113 cam_trans
->editSFMatrix()->setValue( m1
);
116 OSG::FrameHandler::the()->frame();
118 OSG::commitChanges();
120 win
->render(rentravact
);
122 webInterface
->waitRequest(0.01);
123 webInterface
->handleRequests();
125 // win->render(renact);
127 // std::cerr << "------------- AR START --------------" << std::endl;
129 // Thread::getCurrentChangeList()->dump();
132 void reshape( int w
, int h
)
134 std::cerr
<< "Reshape: " << w
<< "," << h
<< std::endl
;
151 OSG::Real32 w
= win
->getWidth(), h
= win
->getHeight();
154 OSG::Real32 a
= -2. * ( lastx
/ w
- .5 ),
155 b
= -2. * ( .5 - lasty
/ h
),
156 c
= -2. * ( x
/ w
- .5 ),
157 d
= -2. * ( .5 - y
/ h
);
159 if ( mouseb
& ( 1 << GLUT_LEFT_BUTTON
) )
161 tball
.updateRotation( a
, b
, c
, d
);
163 else if ( mouseb
& ( 1 << GLUT_MIDDLE_BUTTON
) )
165 tball
.updatePosition( a
, b
, c
, d
);
167 else if ( mouseb
& ( 1 << GLUT_RIGHT_BUTTON
) )
169 tball
.updatePositionNeg( a
, b
, c
, d
);
176 mouse(int button
, int state
, int x
, int y
)
182 case GLUT_LEFT_BUTTON
: break;
183 case GLUT_MIDDLE_BUTTON
:tball
.setAutoPosition(true);
185 case GLUT_RIGHT_BUTTON
: tball
.setAutoPositionNeg(true);
188 mouseb
|= 1 << button
;
190 else if ( state
== 1 )
194 case GLUT_LEFT_BUTTON
: break;
195 case GLUT_MIDDLE_BUTTON
:tball
.setAutoPosition(false);
197 case GLUT_RIGHT_BUTTON
: tball
.setAutoPositionNeg(false);
200 mouseb
&= ~(1 << button
);
209 if (visible
== GLUT_VISIBLE
)
211 glutIdleFunc(animate
);
219 void key(unsigned char key
, int x
, int y
)
224 OSG::FrameHandler::the()->shutdown();
244 rentravact
->setVolumeDrawing(!rentravact
->getVolumeDrawing());
246 glDisable( GL_LIGHTING
);
247 std::cerr
<< "Lighting disabled." << std::endl
;
250 glEnable( GL_LIGHTING
);
251 std::cerr
<< "Lighting enabled." << std::endl
;
254 pPoly
->setFrontMode(GL_POINT
);
255 pPoly
->setBackMode(GL_POINT
);
256 std::cerr
<< "PolygonMode: Point." << std::endl
;
259 pPoly
->setFrontMode(GL_LINE
);
260 pPoly
->setBackMode(GL_LINE
);
261 std::cerr
<< "PolygonMode: Line." << std::endl
;
264 pPoly
->setFrontMode(GL_FILL
);
265 pPoly
->setBackMode(GL_FILL
);
266 std::cerr
<< "PolygonMode: Fill." << std::endl
;
270 if(bPolyActive
== true)
272 OSG_ASSERT(pCOver
->subChunk(pPoly
) == true);
277 OSG_ASSERT(pCOver
->addChunk(pPoly
) == true);
284 std::cerr
<< "Sending ray through " << x
<< "," << y
<< std::endl
;
286 cam
->calcViewRay( l
, x
, y
, *vp
);
287 std::cerr
<< "From " << l
.getPosition() << ", dir "
302 oldq
= tball
.getRotation();
303 oldv
= tball
.getPosition();
305 move_obj
= ! move_obj
;
308 puts("moving object");
309 // m = scene_trans->getSFMatrix()->getValue();
310 tball
.setMode( OSG::Trackball::OSGCamera
);
315 puts("moving camera");
316 // m = cam_trans->getSFMatrix()->getValue();
317 tball
.setMode( OSG::Trackball::OSGObject
);
321 tball
.setStartPosition( v
, true );
322 tball
.setStartRotation( q
, true );
324 // std::cout << q << std::endl;
325 // std::cout << v << std::endl;
327 // std::cout << " " << m[3][0] << " " << m[3][1] << " " << m[3][2] << std::endl;
335 int init(int argc
, char **argv
)
337 OSG::osgInit(argc
,argv
);
339 OSG::setVBOUsageOnPropertyProtos(true);
343 glutInit(&argc
, argv
);
344 glutInitDisplayMode( GLUT_RGBA
| GLUT_DEPTH
| GLUT_DOUBLE
);
345 int winid
= glutCreateWindow("OpenSG");
346 glutKeyboardFunc(key
);
347 glutVisibilityFunc(vis
);
348 glutReshapeFunc(reshape
);
349 glutDisplayFunc(display
);
350 glutMouseFunc(mouse
);
351 glutMotionFunc(motion
);
353 glutIdleFunc(display
);
355 // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
357 // glEnable( GL_DEPTH_TEST );
358 // glEnable( GL_LIGHTING );
359 // glEnable( GL_LIGHT0 );
360 // glFrontFace(GL_CW);
361 // glEnable(GL_CULL_FACE);
365 OSG::SceneFileHandler::the()->print();
369 // beacon for camera and light
370 OSG::NodeUnrecPtr b1n
= OSG::Node::create();
371 OSG::GroupUnrecPtr b1
= OSG::Group::create();
376 OSG::NodeUnrecPtr t1n
= OSG::Node::create();
377 OSG::TransformUnrecPtr t1
= OSG::Transform::create();
380 t1n
->addChild( b1n
);
386 OSG::NodeUnrecPtr dlight
= OSG::Node::create();
387 OSG::DirectionalLightUnrecPtr dl
= OSG::DirectionalLight::create();
389 dlight
->setCore( dl
);
390 // dlight->setCore( Group::create() );
392 dl
->setAmbient( .0, .0, .0, 1 );
393 dl
->setDiffuse( .8f
, .8f
, .8f
, 1.f
);
394 dl
->setDirection(0,0,1);
398 root
= OSG::Node::create();
399 OSG::GroupUnrecPtr gr1
= OSG::Group::create();
401 root
->setCore( gr1
);
402 root
->addChild( t1n
);
403 root
->addChild( dlight
);
407 OSG::NodeUnrecPtr file
= NULL
;
410 file
= OSG::SceneFileHandler::the()->read(argv
[1], NULL
, NULL
);
414 std::cerr
<< "Couldn't load file, ignoring" << std::endl
;
416 file
= OSG::makeSphere(4, 2.0);
420 OSG::Thread::getCurrentChangeList()->commitChanges();
423 OSG::GeometryPtr pGeo
= cast_dynamic
<OSG::GeometryPtr
>(file
->getCore());
425 if(pGeo
== NULL
&& file
->getNChildren() != 0)
427 pGeo
= cast_dynamic
<OSG::GeometryPtr
>(file
->getChild(0)->getCore());
432 fprintf(stderr
, "no geo\n");
436 OSG::GraphOpRefPtr op
= OSG::GraphOpFactory::the()->create("Stripe");
438 // op->traverse(file);
439 // createOptimizedPrimitives(pGeo);
440 // createSharedIndex(pGeo);
444 file
->updateVolume();
447 const char *outFileName
= "/tmp/foo1.osg";
449 OSG::IndentFileOutStream
outFileStream(outFileName
);
453 std::cerr
<< "Can not open output stream to file: "
454 << outFileName
<< std::endl
;
458 std::cerr
<< "STARTING PRINTOUT:" << std::endl
;
459 OSG::OSGWriter
writer( outFileStream
, 4 );
461 writer
.write( file
);
463 outFileStream
.close();
465 OSG::SceneFileHandler::the()->write(file
, "/tmp/foo.osb");
473 file
->getVolume().getBounds( min
, max
);
476 std::cout
<< "Volume: from " << min
<< " to " << max
<< std::endl
;
479 // NodePtr pChunkOverNode = Node::create();
482 // pChunkOverNode->setCore(pCOver);
483 // pChunkOverNode->addChild(file);
485 OSG::MultiCoreUnrecPtr pMCore
= OSG::MultiCore::create();
487 pCOver
= OSG::ChunkOverrideGroup::create();
488 scene_trans
= OSG::Transform::create();
490 pMCore
->addCore(scene_trans
);
491 pMCore
->addCore(pCOver
);
493 OSG::NodeUnrecPtr sceneTrN
= OSG::Node::create();
495 sceneTrN
->setCore(pMCore
);
496 sceneTrN
->addChild(file
);
499 dlight
->addChild(sceneTrN
);
501 std::cerr
<< "Tree: " << std::endl
;
506 cam
= OSG::PerspectiveCamera::create();
508 cam
->setBeacon( b1n
);
509 cam
->setFov( OSG::osgDegree2Rad( 90 ) );
510 cam
->setNear( 0.1f
);
511 cam
->setFar( 10000 );
514 OSG::SolidBackgroundUnrecPtr bkgnd
= OSG::SolidBackground::create();
516 bkgnd
->setColor(OSG::Color3f(0.1,0.1,0.1));
520 vp
= OSG::Viewport::create();
522 vp
->setCamera( cam
);
523 vp
->setBackground( bkgnd
);
525 vp
->setSize( 0,0, 1,1 );
530 std::cout
<< "GLUT winid: " << winid
<< std::endl
;
535 glGetIntegerv( GL_VIEWPORT
, glvp
);
537 gwin
= OSG::GLUTWindow::create();
538 gwin
->setGlutId(winid
);
539 gwin
->setSize( glvp
[2], glvp
[3] );
549 rentravact
= OSG::RenderAction::create();
550 // renact->setFrustumCulling(false);
552 rentravact
->setCorrectTwoSidedLighting(true);
557 pos
.setValues(min
[0] + ((max
[0] - min
[0]) * 0.5),
558 min
[1] + ((max
[1] - min
[1]) * 0.5),
559 max
[2] + ( max
[2] - min
[2] ) * 1.5 );
561 float scale
= (max
[2] - min
[2] + max
[1] - min
[1] + max
[0] - min
[0]) / 6;
563 OSG::Pnt3f
tCenter(min
[0] + (max
[0] - min
[0]) / 2,
564 min
[1] + (max
[1] - min
[1]) / 2,
565 min
[2] + (max
[2] - min
[2]) / 2);
567 fprintf(stderr
, "Startpos : %f %f %f\n", pos
[0], pos
[1], pos
[2]);
569 tball
.setMode( OSG::Trackball::OSGObject
);
570 tball
.setStartPosition( pos
, true );
571 tball
.setSum( true );
572 tball
.setTranslationMode( OSG::Trackball::OSGFree
);
573 tball
.setTranslationScale(scale
);
574 tball
.setRotationCenter(tCenter
);
576 fprintf(stderr
, "%d\n",
577 OSG::MFUnrecNodePtr ::getClassType().getId());
578 fprintf(stderr
, "%d\n",
579 OSG::MFUnrecFieldContainerPtr::getClassType().getId());
581 // MFNodePtr ::getClassType().dump();
582 // MFFieldContainerPtr::getClassType().dump();
586 pPoly
= OSG::PolygonChunk::create();
588 pCOver
->subChunk(pPoly
);
591 GroupNodePtr pGr
= GroupNodePtr::create();
596 fprintf(stderr
, "A\n");
602 fprintf(stderr
, "B\n");
608 fprintf(stderr
, "C\n");
617 OSG::FrameHandler::the()->init();
620 webInterface
= OSG::WebInterface::create();
621 webInterface
->setRoot(root
);
626 int main (int argc
, char **argv
)