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
;
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());
239 glDisable( GL_LIGHTING
);
240 std::cerr
<< "Lighting disabled." << std::endl
;
243 glEnable( GL_LIGHTING
);
244 std::cerr
<< "Lighting enabled." << std::endl
;
247 pPoly
->setFrontMode(GL_POINT
);
248 pPoly
->setBackMode(GL_POINT
);
249 std::cerr
<< "PolygonMode: Point." << std::endl
;
252 pPoly
->setFrontMode(GL_LINE
);
253 pPoly
->setBackMode(GL_LINE
);
254 std::cerr
<< "PolygonMode: Line." << std::endl
;
257 pPoly
->setFrontMode(GL_FILL
);
258 pPoly
->setBackMode(GL_FILL
);
259 std::cerr
<< "PolygonMode: Fill." << std::endl
;
263 if(bPolyActive
== true)
265 OSG_ASSERT(pCOver
->subChunk(pPoly
) == true);
270 OSG_ASSERT(pCOver
->addChunk(pPoly
) == true);
277 std::cerr
<< "Sending ray through " << x
<< "," << y
<< std::endl
;
279 cam
->calcViewRay( l
, x
, y
, *vp
);
280 std::cerr
<< "From " << l
.getPosition() << ", dir "
295 oldq
= tball
.getRotation();
296 oldv
= tball
.getPosition();
298 move_obj
= ! move_obj
;
301 puts("moving object");
302 // m = scene_trans->getSFMatrix()->getValue();
303 tball
.setMode( OSG::Trackball::OSGCamera
);
308 puts("moving camera");
309 // m = cam_trans->getSFMatrix()->getValue();
310 tball
.setMode( OSG::Trackball::OSGObject
);
314 tball
.setStartPosition( v
, true );
315 tball
.setStartRotation( q
, true );
317 // std::cout << q << std::endl;
318 // std::cout << v << std::endl;
320 // std::cout << " " << m[3][0] << " " << m[3][1] << " " << m[3][2] << std::endl;
328 int init(int argc
, char **argv
)
330 OSG::osgInit(argc
,argv
);
332 OSG::setVBOUsageOnPropertyProtos(true);
336 glutInit(&argc
, argv
);
337 glutInitDisplayMode( GLUT_RGBA
| GLUT_DEPTH
| GLUT_DOUBLE
);
338 int winid
= glutCreateWindow("OpenSG");
339 glutKeyboardFunc(key
);
340 glutVisibilityFunc(vis
);
341 glutReshapeFunc(reshape
);
342 glutDisplayFunc(display
);
343 glutMouseFunc(mouse
);
344 glutMotionFunc(motion
);
346 glutIdleFunc(display
);
348 // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
350 // glEnable( GL_DEPTH_TEST );
351 // glEnable( GL_LIGHTING );
352 // glEnable( GL_LIGHT0 );
353 // glFrontFace(GL_CW);
354 // glEnable(GL_CULL_FACE);
358 OSG::SceneFileHandler::the()->print();
362 // beacon for camera and light
363 OSG::NodeUnrecPtr b1n
= OSG::Node::create();
364 OSG::GroupUnrecPtr b1
= OSG::Group::create();
369 OSG::NodeUnrecPtr t1n
= OSG::Node::create();
370 OSG::TransformUnrecPtr t1
= OSG::Transform::create();
373 t1n
->addChild( b1n
);
379 OSG::NodeUnrecPtr dlight
= OSG::Node::create();
380 OSG::DirectionalLightUnrecPtr dl
= OSG::DirectionalLight::create();
382 dlight
->setCore( dl
);
383 // dlight->setCore( Group::create() );
385 dl
->setAmbient( .0, .0, .0, 1 );
386 dl
->setDiffuse( .8f
, .8f
, .8f
, 1.f
);
387 dl
->setDirection(0,0,1);
391 root
= OSG::Node::create();
392 OSG::GroupUnrecPtr gr1
= OSG::Group::create();
394 root
->setCore( gr1
);
395 root
->addChild( t1n
);
396 root
->addChild( dlight
);
400 OSG::NodeUnrecPtr file
= NULL
;
403 file
= OSG::SceneFileHandler::the()->read(argv
[1], NULL
, NULL
);
407 std::cerr
<< "Couldn't load file, ignoring" << std::endl
;
409 file
= OSG::makeSphere(4, 2.0);
413 OSG::Thread::getCurrentChangeList()->commitChanges();
416 OSG::GeometryPtr pGeo
= cast_dynamic
<OSG::GeometryPtr
>(file
->getCore());
418 if(pGeo
== NULL
&& file
->getNChildren() != 0)
420 pGeo
= cast_dynamic
<OSG::GeometryPtr
>(file
->getChild(0)->getCore());
425 fprintf(stderr
, "no geo\n");
429 OSG::GraphOpRefPtr op
= OSG::GraphOpFactory::the()->create("Stripe");
431 // op->traverse(file);
432 // createOptimizedPrimitives(pGeo);
433 // createSharedIndex(pGeo);
437 file
->updateVolume();
440 const char *outFileName
= "/tmp/foo1.osg";
442 OSG::IndentFileOutStream
outFileStream(outFileName
);
446 std::cerr
<< "Can not open output stream to file: "
447 << outFileName
<< std::endl
;
451 std::cerr
<< "STARTING PRINTOUT:" << std::endl
;
452 OSG::OSGWriter
writer( outFileStream
, 4 );
454 writer
.write( file
);
456 outFileStream
.close();
458 OSG::SceneFileHandler::the()->write(file
, "/tmp/foo.osb");
466 file
->getVolume().getBounds( min
, max
);
469 std::cout
<< "Volume: from " << min
<< " to " << max
<< std::endl
;
472 // NodePtr pChunkOverNode = Node::create();
475 // pChunkOverNode->setCore(pCOver);
476 // pChunkOverNode->addChild(file);
478 OSG::MultiCoreUnrecPtr pMCore
= OSG::MultiCore::create();
480 pCOver
= OSG::ChunkOverrideGroup::create();
481 scene_trans
= OSG::Transform::create();
483 pMCore
->addCore(scene_trans
);
484 pMCore
->addCore(pCOver
);
486 OSG::NodeUnrecPtr sceneTrN
= OSG::Node::create();
488 sceneTrN
->setCore(pMCore
);
489 sceneTrN
->addChild(file
);
492 dlight
->addChild(sceneTrN
);
494 std::cerr
<< "Tree: " << std::endl
;
499 cam
= OSG::PerspectiveCamera::create();
501 cam
->setBeacon( b1n
);
502 cam
->setFov( OSG::osgDegree2Rad( 90 ) );
503 cam
->setNear( 0.1f
);
504 cam
->setFar( 10000 );
507 OSG::SolidBackgroundUnrecPtr bkgnd
= OSG::SolidBackground::create();
509 bkgnd
->setColor(OSG::Color3f(0.1,0.1,0.1));
513 vp
= OSG::Viewport::create();
515 vp
->setCamera( cam
);
516 vp
->setBackground( bkgnd
);
518 vp
->setSize( 0,0, 1,1 );
523 std::cout
<< "GLUT winid: " << winid
<< std::endl
;
528 glGetIntegerv( GL_VIEWPORT
, glvp
);
530 gwin
= OSG::GLUTWindow::create();
531 gwin
->setGlutId(winid
);
532 gwin
->setSize( glvp
[2], glvp
[3] );
542 rentravact
= OSG::RenderAction::create();
543 // renact->setFrustumCulling(false);
545 rentravact
->setCorrectTwoSidedLighting(true);
550 pos
.setValues(min
[0] + ((max
[0] - min
[0]) * 0.5),
551 min
[1] + ((max
[1] - min
[1]) * 0.5),
552 max
[2] + ( max
[2] - min
[2] ) * 1.5 );
554 float scale
= (max
[2] - min
[2] + max
[1] - min
[1] + max
[0] - min
[0]) / 6;
556 OSG::Pnt3f
tCenter(min
[0] + (max
[0] - min
[0]) / 2,
557 min
[1] + (max
[1] - min
[1]) / 2,
558 min
[2] + (max
[2] - min
[2]) / 2);
560 fprintf(stderr
, "Startpos : %f %f %f\n", pos
[0], pos
[1], pos
[2]);
562 tball
.setMode( OSG::Trackball::OSGObject
);
563 tball
.setStartPosition( pos
, true );
564 tball
.setSum( true );
565 tball
.setTranslationMode( OSG::Trackball::OSGFree
);
566 tball
.setTranslationScale(scale
);
567 tball
.setRotationCenter(tCenter
);
569 fprintf(stderr
, "%d\n",
570 OSG::MFUnrecNodePtr ::getClassType().getId());
571 fprintf(stderr
, "%d\n",
572 OSG::MFUnrecFieldContainerPtr::getClassType().getId());
574 // MFNodePtr ::getClassType().dump();
575 // MFFieldContainerPtr::getClassType().dump();
579 pPoly
= OSG::PolygonChunk::create();
581 pCOver
->subChunk(pPoly
);
584 GroupNodePtr pGr
= GroupNodePtr::create();
589 fprintf(stderr
, "A\n");
595 fprintf(stderr
, "B\n");
601 fprintf(stderr
, "C\n");
610 OSG::FrameHandler::the()->init();
615 int main (int argc
, char **argv
)