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"
43 #include "OSGTrackball.h"
45 OSG::RenderActionRefPtr rentravact
= NULL
;
49 //OSG::NodeRecPtr file;
51 OSG::PerspectiveCameraRecPtr cam
;
52 OSG::ViewportRecPtr vp
;
53 OSG::WindowRecPtr win
;
55 OSG::TransformRecPtr cam_trans
;
56 OSG::TransformRecPtr scene_trans
;
58 OSG::PolygonChunkRecPtr pPoly
;
59 bool bPolyActive
= false;
60 OSG::ChunkOverrideGroupRecPtr pCOver
;
62 OSG::GLUTWindowUnrecPtr gwin
;
66 bool move_obj
= false;
77 OSG::Matrix m1
, m2
, m3
;
80 tball
.getRotation().getValue(m3
);
86 // std::cout << "TBROT" << std::endl << tball.getRotation() << endl;
87 // std::cout << "M3" << std::endl << m3 << std::endl;
88 // std::cout << "Q1" << std::endl << q1 << std::endl;
89 // std::cout << "M1" << std::endl << m1 << std::endl;
91 // m1.setRotate( tball.getRotation() );
92 m2
.setTranslate( tball
.getPosition() );
94 //std::cout << "Pos: " << tball.getPosition() << ", Rot: " << tball.getRotation() << std::endl;
96 // std::cout << tball.getRotation() << std::endl;
100 // std::cerr << m1 << std::endl;
102 m1
= tball
.getFullTrackballMatrix();
106 scene_trans
->editSFMatrix()->setValue( m1
);
110 cam_trans
->editSFMatrix()->setValue( m1
);
113 OSG::FrameHandler::the()->frame();
115 OSG::commitChanges();
117 win
->render(rentravact
);
119 // win->render(renact);
121 // std::cerr << "------------- AR START --------------" << std::endl;
123 // Thread::getCurrentChangeList()->dump();
126 void reshape( int w
, int h
)
128 std::cerr
<< "Reshape: " << w
<< "," << h
<< std::endl
;
145 OSG::Real32 w
= win
->getWidth(), h
= win
->getHeight();
148 OSG::Real32 a
= -2. * ( lastx
/ w
- .5 ),
149 b
= -2. * ( .5 - lasty
/ h
),
150 c
= -2. * ( x
/ w
- .5 ),
151 d
= -2. * ( .5 - y
/ h
);
153 if ( mouseb
& ( 1 << GLUT_LEFT_BUTTON
) )
155 tball
.updateRotation( a
, b
, c
, d
);
157 else if ( mouseb
& ( 1 << GLUT_MIDDLE_BUTTON
) )
159 tball
.updatePosition( a
, b
, c
, d
);
161 else if ( mouseb
& ( 1 << GLUT_RIGHT_BUTTON
) )
163 tball
.updatePositionNeg( a
, b
, c
, d
);
170 mouse(int button
, int state
, int x
, int y
)
176 case GLUT_LEFT_BUTTON
: break;
177 case GLUT_MIDDLE_BUTTON
:tball
.setAutoPosition(true);
179 case GLUT_RIGHT_BUTTON
: tball
.setAutoPositionNeg(true);
182 mouseb
|= 1 << button
;
184 else if ( state
== 1 )
188 case GLUT_LEFT_BUTTON
: break;
189 case GLUT_MIDDLE_BUTTON
:tball
.setAutoPosition(false);
191 case GLUT_RIGHT_BUTTON
: tball
.setAutoPositionNeg(false);
194 mouseb
&= ~(1 << button
);
203 if (visible
== GLUT_VISIBLE
)
205 glutIdleFunc(animate
);
213 void key(unsigned char key
, int x
, int y
)
218 OSG::FrameHandler::the()->shutdown();
237 rentravact
->setVolumeDrawing(!rentravact
->getVolumeDrawing());
240 glDisable( GL_LIGHTING
);
241 std::cerr
<< "Lighting disabled." << std::endl
;
244 glEnable( GL_LIGHTING
);
245 std::cerr
<< "Lighting enabled." << std::endl
;
248 pPoly
->setFrontMode(GL_POINT
);
249 pPoly
->setBackMode(GL_POINT
);
250 std::cerr
<< "PolygonMode: Point." << std::endl
;
253 pPoly
->setFrontMode(GL_LINE
);
254 pPoly
->setBackMode(GL_LINE
);
255 std::cerr
<< "PolygonMode: Line." << std::endl
;
258 pPoly
->setFrontMode(GL_FILL
);
259 pPoly
->setBackMode(GL_FILL
);
260 std::cerr
<< "PolygonMode: Fill." << std::endl
;
264 if(bPolyActive
== true)
266 OSG_ASSERT(pCOver
->subChunk(pPoly
) == true);
271 OSG_ASSERT(pCOver
->addChunk(pPoly
) == true);
278 std::cerr
<< "Sending ray through " << x
<< "," << y
<< std::endl
;
280 cam
->calcViewRay( l
, x
, y
, *vp
);
281 std::cerr
<< "From " << l
.getPosition() << ", dir "
296 oldq
= tball
.getRotation();
297 oldv
= tball
.getPosition();
299 move_obj
= ! move_obj
;
302 puts("moving object");
303 // m = scene_trans->getSFMatrix()->getValue();
304 tball
.setMode( OSG::Trackball::OSGCamera
);
309 puts("moving camera");
310 // m = cam_trans->getSFMatrix()->getValue();
311 tball
.setMode( OSG::Trackball::OSGObject
);
315 tball
.setStartPosition( v
, true );
316 tball
.setStartRotation( q
, true );
318 // std::cout << q << std::endl;
319 // std::cout << v << std::endl;
321 // std::cout << " " << m[3][0] << " " << m[3][1] << " " << m[3][2] << std::endl;
329 int init(int argc
, char **argv
)
331 OSG::osgInit(argc
,argv
);
333 OSG::setVBOUsageOnPropertyProtos(true);
337 glutInit(&argc
, argv
);
338 glutInitDisplayMode( GLUT_RGBA
| GLUT_DEPTH
| GLUT_DOUBLE
);
339 int winid
= glutCreateWindow("OpenSG");
340 glutKeyboardFunc(key
);
341 glutVisibilityFunc(vis
);
342 glutReshapeFunc(reshape
);
343 glutDisplayFunc(display
);
344 glutMouseFunc(mouse
);
345 glutMotionFunc(motion
);
347 glutIdleFunc(display
);
349 // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
351 // glEnable( GL_DEPTH_TEST );
352 // glEnable( GL_LIGHTING );
353 // glEnable( GL_LIGHT0 );
354 // glFrontFace(GL_CW);
355 // glEnable(GL_CULL_FACE);
359 OSG::SceneFileHandler::the()->print();
363 // beacon for camera and light
364 OSG::NodeUnrecPtr b1n
= OSG::Node::create();
365 OSG::GroupUnrecPtr b1
= OSG::Group::create();
370 OSG::NodeUnrecPtr t1n
= OSG::Node::create();
371 OSG::TransformUnrecPtr t1
= OSG::Transform::create();
374 t1n
->addChild( b1n
);
380 OSG::NodeUnrecPtr dlight
= OSG::Node::create();
381 OSG::DirectionalLightUnrecPtr dl
= OSG::DirectionalLight::create();
383 dlight
->setCore( dl
);
384 // dlight->setCore( Group::create() );
386 dl
->setAmbient( .0, .0, .0, 1 );
387 dl
->setDiffuse( .8f
, .8f
, .8f
, 1.f
);
388 dl
->setDirection(0,0,1);
392 root
= OSG::Node::create();
393 OSG::GroupUnrecPtr gr1
= OSG::Group::create();
395 root
->setCore( gr1
);
396 root
->addChild( t1n
);
397 root
->addChild( dlight
);
401 OSG::NodeUnrecPtr file
= NULL
;
404 file
= OSG::SceneFileHandler::the()->read(argv
[1], NULL
, NULL
);
408 std::cerr
<< "Couldn't load file, ignoring" << std::endl
;
410 file
= OSG::makeSphere(4, 2.0);
414 OSG::Thread::getCurrentChangeList()->commitChanges();
417 OSG::GeometryPtr pGeo
= cast_dynamic
<OSG::GeometryPtr
>(file
->getCore());
419 if(pGeo
== NULL
&& file
->getNChildren() != 0)
421 pGeo
= cast_dynamic
<OSG::GeometryPtr
>(file
->getChild(0)->getCore());
426 fprintf(stderr
, "no geo\n");
430 OSG::GraphOpRefPtr op
= OSG::GraphOpFactory::the()->create("Stripe");
432 // op->traverse(file);
433 // createOptimizedPrimitives(pGeo);
434 // createSharedIndex(pGeo);
438 file
->updateVolume();
441 const char *outFileName
= "/tmp/foo1.osg";
443 OSG::IndentFileOutStream
outFileStream(outFileName
);
447 std::cerr
<< "Can not open output stream to file: "
448 << outFileName
<< std::endl
;
452 std::cerr
<< "STARTING PRINTOUT:" << std::endl
;
453 OSG::OSGWriter
writer( outFileStream
, 4 );
455 writer
.write( file
);
457 outFileStream
.close();
459 OSG::SceneFileHandler::the()->write(file
, "/tmp/foo.osb");
467 file
->getVolume().getBounds( min
, max
);
470 std::cout
<< "Volume: from " << min
<< " to " << max
<< std::endl
;
473 // NodePtr pChunkOverNode = Node::create();
476 // pChunkOverNode->setCore(pCOver);
477 // pChunkOverNode->addChild(file);
479 OSG::MultiCoreUnrecPtr pMCore
= OSG::MultiCore::create();
481 pCOver
= OSG::ChunkOverrideGroup::create();
482 scene_trans
= OSG::Transform::create();
484 pMCore
->addCore(scene_trans
);
485 pMCore
->addCore(pCOver
);
487 OSG::NodeUnrecPtr sceneTrN
= OSG::Node::create();
489 sceneTrN
->setCore(pMCore
);
490 sceneTrN
->addChild(file
);
493 dlight
->addChild(sceneTrN
);
495 std::cerr
<< "Tree: " << std::endl
;
500 cam
= OSG::PerspectiveCamera::create();
502 cam
->setBeacon( b1n
);
503 cam
->setFov( OSG::osgDegree2Rad( 90 ) );
504 cam
->setNear( 0.1f
);
505 cam
->setFar( 10000 );
508 OSG::SolidBackgroundUnrecPtr bkgnd
= OSG::SolidBackground::create();
510 bkgnd
->setColor(OSG::Color3f(0.1,0.1,0.1));
514 vp
= OSG::Viewport::create();
516 vp
->setCamera( cam
);
517 vp
->setBackground( bkgnd
);
519 vp
->setSize( 0,0, 1,1 );
524 std::cout
<< "GLUT winid: " << winid
<< std::endl
;
529 glGetIntegerv( GL_VIEWPORT
, glvp
);
531 gwin
= OSG::GLUTWindow::create();
532 gwin
->setGlutId(winid
);
533 gwin
->setSize( glvp
[2], glvp
[3] );
543 rentravact
= OSG::RenderAction::create();
544 // renact->setFrustumCulling(false);
546 rentravact
->setCorrectTwoSidedLighting(true);
551 pos
.setValues(min
[0] + ((max
[0] - min
[0]) * 0.5),
552 min
[1] + ((max
[1] - min
[1]) * 0.5),
553 max
[2] + ( max
[2] - min
[2] ) * 1.5 );
555 float scale
= (max
[2] - min
[2] + max
[1] - min
[1] + max
[0] - min
[0]) / 6;
557 OSG::Pnt3f
tCenter(min
[0] + (max
[0] - min
[0]) / 2,
558 min
[1] + (max
[1] - min
[1]) / 2,
559 min
[2] + (max
[2] - min
[2]) / 2);
561 fprintf(stderr
, "Startpos : %f %f %f\n", pos
[0], pos
[1], pos
[2]);
563 tball
.setMode( OSG::Trackball::OSGObject
);
564 tball
.setStartPosition( pos
, true );
565 tball
.setSum( true );
566 tball
.setTranslationMode( OSG::Trackball::OSGFree
);
567 tball
.setTranslationScale(scale
);
568 tball
.setRotationCenter(tCenter
);
570 fprintf(stderr
, "%d\n",
571 OSG::MFUnrecNodePtr ::getClassType().getId());
572 fprintf(stderr
, "%d\n",
573 OSG::MFUnrecFieldContainerPtr::getClassType().getId());
575 // MFNodePtr ::getClassType().dump();
576 // MFFieldContainerPtr::getClassType().dump();
580 pPoly
= OSG::PolygonChunk::create();
582 pCOver
->subChunk(pPoly
);
585 GroupNodePtr pGr
= GroupNodePtr::create();
590 fprintf(stderr
, "A\n");
596 fprintf(stderr
, "B\n");
602 fprintf(stderr
, "C\n");
611 OSG::FrameHandler::the()->init();
616 int main (int argc
, char **argv
)