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 "OSGTextureEnvChunk.h"
27 #include "OSGTextureObjChunk.h"
28 #include "OSGSimpleMaterial.h"
29 #include "OSGPolygonForeground.h"
31 #include "OSGViewport.h"
32 #include "OSGCamera.h"
33 #include "OSGWindow.h"
34 #include "OSGGLUTWindow.h"
35 #include "OSGCamera.h"
36 #include "OSGPerspectiveCamera.h"
37 #include "OSGSolidBackground.h"
38 //#include "OSGUniformBackground.h"
39 #include "OSGOSGWriter.h"
40 #include "OSGChangeList.h"
41 #include "OSGIOStream.h"
42 #include "OSGGeoFunctions.h"
43 #include "OSGGraphOp.h"
44 #include "OSGGraphOpFactory.h"
45 #include "OSGMultiCore.h"
47 #include "OSGFBOViewport.h"
48 #include "OSGFrameBufferObject.h"
49 #include "OSGTextureBuffer.h"
50 #include "OSGRenderBuffer.h"
52 #include "OSGTrackball.h"
54 OSG::RenderActionRefPtr rentravact
;
58 //OSG::NodeRecPtr file;
60 OSG::PerspectiveCameraRecPtr cam
;
61 OSG::ViewportRecPtr vp
;
62 OSG::WindowRecPtr win
;
64 OSG::TransformRecPtr cam_trans
;
65 OSG::TransformRecPtr scene_trans
;
67 OSG::PolygonChunkRecPtr pPoly
;
68 bool bPolyActive
= false;
69 OSG::ChunkOverrideGroupRecPtr pCOver
;
71 OSG::GLUTWindowUnrecPtr gwin
;
73 OSG::TextureObjChunkUnrecPtr tx1o
;
74 OSG::TextureEnvChunkUnrecPtr tx1e
;
75 OSG::FBOViewportUnrecPtr vpFBO
;
76 OSG::FBOViewportUnrecPtr vpFBO_RB
;
77 OSG::TextureBufferUnrecPtr pTexBuffer
;
78 OSG::RenderBufferUnrecPtr pRenBuffer
;
79 OSG::ImageUnrecPtr pImg
;
83 bool move_obj
= false;
91 OSG::UInt32 oldTravMask
;
94 OSG::UInt32 oldTravMask_RB
;
95 bool dumpImg_RB
= false;
99 OSG::Matrix m1
, m2
, m3
;
102 tball
.getRotation().getValue(m3
);
108 // std::cout << "TBROT" << std::endl << tball.getRotation() << endl;
109 // std::cout << "M3" << std::endl << m3 << std::endl;
110 // std::cout << "Q1" << std::endl << q1 << std::endl;
111 // std::cout << "M1" << std::endl << m1 << std::endl;
113 // m1.setRotate( tball.getRotation() );
114 m2
.setTranslate( tball
.getPosition() );
116 //std::cout << "Pos: " << tball.getPosition() << ", Rot: " << tball.getRotation() << std::endl;
118 // std::cout << tball.getRotation() << std::endl;
122 // std::cerr << m1 << std::endl;
124 m1
= tball
.getFullTrackballMatrix();
128 scene_trans
->editSFMatrix()->setValue( m1
);
132 cam_trans
->editSFMatrix()->setValue( m1
);
135 OSG::commitChangesAndClear();
137 win
->render(rentravact
);
141 vpFBO
->setTravMask(oldTravMask
);
143 pTexBuffer
->setReadBack (false);
144 vpFBO
->getFrameBufferObject()->setPostProcessOnDeactivate(false);
146 pImg
->write("/tmp/test.png");
151 if(dumpImg_RB
== true)
153 pRenBuffer
->getImage()->write("/tmp/test1.png");
157 win
->subPortByObj(vpFBO_RB
);
164 // OSG::FieldContainerFactory::the()->dump();
168 // win->render(renact);
170 // std::cerr << "------------- AR START --------------" << std::endl;
172 // Thread::getCurrentChangeList()->dump();
175 void reshape( int w
, int h
)
177 std::cerr
<< "Reshape: " << w
<< "," << h
<< std::endl
;
190 void motion(int x
, int y
)
192 OSG::Real32 w
= win
->getWidth(), h
= win
->getHeight();
195 OSG::Real32 a
= -2. * ( lastx
/ w
- .5 ),
196 b
= -2. * ( .5 - lasty
/ h
),
197 c
= -2. * ( x
/ w
- .5 ),
198 d
= -2. * ( .5 - y
/ h
);
200 if ( mouseb
& ( 1 << GLUT_LEFT_BUTTON
) )
202 tball
.updateRotation( a
, b
, c
, d
);
204 else if ( mouseb
& ( 1 << GLUT_MIDDLE_BUTTON
) )
206 tball
.updatePosition( a
, b
, c
, d
);
208 else if ( mouseb
& ( 1 << GLUT_RIGHT_BUTTON
) )
210 tball
.updatePositionNeg( a
, b
, c
, d
);
216 void mouse(int button
, int state
, int x
, int y
)
222 case GLUT_LEFT_BUTTON
: break;
223 case GLUT_MIDDLE_BUTTON
:tball
.setAutoPosition(true);
225 case GLUT_RIGHT_BUTTON
: tball
.setAutoPositionNeg(true);
228 mouseb
|= 1 << button
;
230 else if ( state
== 1 )
234 case GLUT_LEFT_BUTTON
: break;
235 case GLUT_MIDDLE_BUTTON
:tball
.setAutoPosition(false);
237 case GLUT_RIGHT_BUTTON
: tball
.setAutoPositionNeg(false);
240 mouseb
&= ~(1 << button
);
246 void vis(int visible
)
248 if (visible
== GLUT_VISIBLE
)
250 glutIdleFunc(animate
);
258 void key(unsigned char key
, int x
, int y
)
289 vpFBO
->setTravMask(~vpFBO
->getTravMask());
292 oldTravMask
= vpFBO
->getTravMask();
294 if(oldTravMask
== 0x0000)
296 vpFBO
->setTravMask(~vpFBO
->getTravMask());
301 pTexBuffer
->setReadBack (true);
302 vpFBO
->getFrameBufferObject()->setPostProcessOnDeactivate(true);
308 // OSG::FieldContainerFactory::the()->dump();
309 //RenderBuffer Readback
311 OSG::SolidBackgroundUnrecPtr bkgndFBO_RB
=
312 OSG::SolidBackground::create();
314 bkgndFBO_RB
->setColor(OSG::Color3f(0.5,1.0,0.5));
318 vpFBO_RB
= OSG::FBOViewport::create();
320 vpFBO_RB
->setCamera (cam
);
321 vpFBO_RB
->setBackground(bkgndFBO_RB
);
322 vpFBO_RB
->setRoot (root
);
323 vpFBO_RB
->setSize (0, 0, 1, 1 );
325 OSG::FrameBufferObjectUnrecPtr pFBO_RB
=
326 OSG::FrameBufferObject::create();
328 pRenBuffer
= OSG::RenderBuffer::create();
330 OSG::RenderBufferUnrecPtr pDepthBuffer_RB
=
331 OSG::RenderBuffer::create();
333 pDepthBuffer_RB
->setInternalFormat(GL_DEPTH_COMPONENT24
);
334 pRenBuffer
->setInternalFormat(GL_RGBA
);
336 OSG::ImageUnrecPtr pImg_RB
= OSG::Image::create();
338 OSG::UInt32 uiImgSize
= 4096;
340 pImg_RB
->set(OSG::Image::OSG_RGB_PF
, uiImgSize
, uiImgSize
);
342 fprintf(stderr
, "Image data : %p\n", pImg_RB
->getData());
344 pRenBuffer
->setImage(pImg_RB
);
346 pFBO_RB
->setSize(uiImgSize
, uiImgSize
);
348 pFBO_RB
->setColorAttachment(pRenBuffer
, 0);
349 pFBO_RB
->setDepthAttachment(pDepthBuffer_RB
);
351 pFBO_RB
->editMFDrawBuffers()->clear();
352 pFBO_RB
->editMFDrawBuffers()->push_back(GL_COLOR_ATTACHMENT0_EXT
);
354 vpFBO_RB
->setFrameBufferObject(pFBO_RB
);
356 pRenBuffer
->setReadBack (true);
357 vpFBO_RB
->getFrameBufferObject()->setPostProcessOnDeactivate(true);
359 win
->addPort(vpFBO_RB
);
366 rentravact
->setVolumeDrawing(!rentravact
->getVolumeDrawing());
369 glDisable( GL_LIGHTING
);
370 std::cerr
<< "Lighting disabled." << std::endl
;
373 glEnable( GL_LIGHTING
);
374 std::cerr
<< "Lighting enabled." << std::endl
;
377 pPoly
->setFrontMode(GL_POINT
);
378 pPoly
->setBackMode(GL_POINT
);
379 std::cerr
<< "PolygonMode: Point." << std::endl
;
382 pPoly
->setFrontMode(GL_LINE
);
383 pPoly
->setBackMode(GL_LINE
);
384 std::cerr
<< "PolygonMode: Line." << std::endl
;
387 pPoly
->setFrontMode(GL_FILL
);
388 pPoly
->setBackMode(GL_FILL
);
389 std::cerr
<< "PolygonMode: Fill." << std::endl
;
393 if(bPolyActive
== true)
395 OSG_ASSERT(pCOver
->subChunk(pPoly
) == true);
400 OSG_ASSERT(pCOver
->addChunk(pPoly
) == true);
415 oldq
= tball
.getRotation();
416 oldv
= tball
.getPosition();
418 move_obj
= ! move_obj
;
421 puts("moving object");
422 // m = scene_trans->getSFMatrix()->getValue();
423 tball
.setMode( OSG::Trackball::OSGCamera
);
428 puts("moving camera");
429 // m = cam_trans->getSFMatrix()->getValue();
430 tball
.setMode( OSG::Trackball::OSGObject
);
434 tball
.setStartPosition( v
, true );
435 tball
.setStartRotation( q
, true );
437 // std::cout << q << std::endl;
438 // std::cout << v << std::endl;
440 // std::cout << " " << m[3][0] << " " << m[3][1] << " " << m[3][2] << std::endl;
448 int init(int argc
, char **argv
)
450 OSG::osgInit(argc
,argv
);
454 glutInit(&argc
, argv
);
455 glutInitDisplayMode( GLUT_RGBA
| GLUT_DEPTH
| GLUT_DOUBLE
);
456 int winid
= glutCreateWindow("OpenSG");
457 glutKeyboardFunc(key
);
458 glutVisibilityFunc(vis
);
459 glutReshapeFunc(reshape
);
460 glutDisplayFunc(display
);
461 glutMouseFunc(mouse
);
462 glutMotionFunc(motion
);
464 glutIdleFunc(display
);
466 // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
468 // glEnable( GL_DEPTH_TEST );
469 // glEnable( GL_LIGHTING );
470 // glEnable( GL_LIGHT0 );
471 // glFrontFace(GL_CW);
472 glEnable(GL_CULL_FACE
);
476 OSG::SceneFileHandler::the()->print();
480 // beacon for camera and light
481 OSG::NodeUnrecPtr b1n
= OSG::Node::create();
482 OSG::GroupUnrecPtr b1
= OSG::Group::create();
487 OSG::NodeUnrecPtr t1n
= OSG::Node::create();
488 OSG::TransformUnrecPtr t1
= OSG::Transform::create();
491 t1n
->addChild( b1n
);
497 OSG::NodeUnrecPtr dlight
= OSG::Node::create();
498 OSG::DirectionalLightUnrecPtr dl
= OSG::DirectionalLight::create();
500 dlight
->setCore( dl
);
501 // dlight->setCore( Group::create() );
503 dl
->setAmbient( .0, .0, .0, 1 );
504 dl
->setDiffuse( .8f
, .8f
, .8f
, 1.f
);
505 dl
->setDirection(0,0,1);
509 root
= OSG::Node::create();
510 OSG::GroupUnrecPtr gr1
= OSG::Group::create();
512 root
->setCore( gr1
);
513 root
->addChild( t1n
);
514 root
->addChild( dlight
);
518 OSG::NodeUnrecPtr file
= NULL
;
521 file
= OSG::SceneFileHandler::the()->read(argv
[1], NULL
);
525 std::cerr
<< "Couldn't load file, ignoring" << std::endl
;
527 file
= OSG::makeSphere(4, 2.0);
532 OSG::GeometryPtr pGeo
= cast_dynamic
<OSG::GeometryPtr
>(file
->getCore());
534 if(pGeo
== NULL
&& file
->getNChildren() != 0)
536 pGeo
= cast_dynamic
<OSG::GeometryPtr
>(file
->getChild(0)->getCore());
541 fprintf(stderr
, "no geo\n");
545 // OSG::GraphOpRefPtr op = OSG::GraphOpFactory::the()->create("Stripe");
547 // op->traverse(file);
548 // createOptimizedPrimitives(pGeo);
549 // createSharedIndex(pGeo);
551 OSG::Thread::getCurrentChangeList()->commitChanges();
554 file
->updateVolume();
557 const char *outFileName
= "/tmp/foo1.osg";
559 OSG::IndentFileOutStream
outFileStream(outFileName
);
563 std::cerr
<< "Can not open output stream to file: "
564 << outFileName
<< std::endl
;
568 std::cerr
<< "STARTING PRINTOUT:" << std::endl
;
569 OSGWriter
writer( outFileStream
, 4 );
571 writer
.write( file
);
573 outFileStream
.close();
575 OSG::SceneFileHandler::the()->write(file
, "/tmp/foo.osb");
583 file
->getVolume().getBounds( min
, max
);
586 std::cout
<< "Volume: from " << min
<< " to " << max
<< std::endl
;
589 OSG::MultiCoreUnrecPtr pMCore
= OSG::MultiCore::create();
591 pCOver
= OSG::ChunkOverrideGroup::create();
592 scene_trans
= OSG::Transform::create();
594 pMCore
->addCore(scene_trans
);
595 pMCore
->addCore(pCOver
);
597 OSG::NodeUnrecPtr sceneTrN
= OSG::Node::create();
599 sceneTrN
->setCore(pMCore
);
600 sceneTrN
->addChild(file
);
603 dlight
->addChild(sceneTrN
);
605 std::cerr
<< "Tree: " << std::endl
;
610 cam
= OSG::PerspectiveCamera::create();
612 cam
->setBeacon( b1n
);
613 cam
->setFov( OSG::osgDegree2Rad( 90 ) );
614 cam
->setNear( 0.1f
);
615 cam
->setFar( 100000 );
618 OSG::SolidBackgroundUnrecPtr bkgnd
= OSG::SolidBackground::create();
620 bkgnd
->setColor(OSG::Color3f(1,0,0));
624 vp
= OSG::Viewport::create();
626 vp
->setCamera( cam
);
627 vp
->setBackground( bkgnd
);
629 vp
->setSize( 0,0, 1,1 );
632 OSG::UInt8 imgdata
[] =
634 64,64,64, 128,128,128, 192,192,192, 255,255,255
638 pImg
= OSG::Image::create();
640 pImg
->set(OSG::Image::OSG_RGB_PF
, 128, 128); //, 1, 1, 1, 0.0, imgdata);
642 tx1o
= OSG::TextureObjChunk::create();
643 tx1e
= OSG::TextureEnvChunk::create();
645 tx1o
->setImage (pImg
);
646 tx1o
->setMinFilter(GL_LINEAR
);
647 tx1o
->setMagFilter(GL_LINEAR
);
648 tx1o
->setWrapS (GL_CLAMP
);
649 tx1o
->setWrapT (GL_CLAMP
);
650 tx1e
->setEnvMode (GL_REPLACE
);
652 OSG::SimpleMaterialUnrecPtr mat
= OSG::SimpleMaterial::create();
654 mat
->setDiffuse(OSG::Color3f(1,1,1));
655 mat
->setLit (false );
656 mat
->addChunk (tx1o
);
657 mat
->addChunk (tx1e
);
659 OSG::PolygonForegroundUnrecPtr pFG
= OSG::PolygonForeground::create();
661 pFG
->setMaterial(mat
);
663 OSG::MFPnt2f
*pPos
= pFG
->editMFPositions();
664 OSG::MFVec3f
*pTex
= pFG
->editMFTexCoords();
666 pPos
->push_back(OSG::Pnt2f(0.0f
, 0.0f
));
667 pPos
->push_back(OSG::Pnt2f(0.3f
, 0.0f
));
668 pPos
->push_back(OSG::Pnt2f(0.3f
, 0.3f
));
669 pPos
->push_back(OSG::Pnt2f(0.0f
, 0.3f
));
671 pTex
->push_back(OSG::Vec3f(0.0f
, 1.0f
, 0.0f
));
672 pTex
->push_back(OSG::Vec3f(1.0f
, 1.0f
, 0.0f
));
673 pTex
->push_back(OSG::Vec3f(1.0f
, 0.0f
, 0.0f
));
674 pTex
->push_back(OSG::Vec3f(0.0f
, 0.0f
, 0.0f
));
676 vp
->addForeground(pFG
);
680 OSG::SimpleMaterialUnrecPtr matFgCheck
= OSG::SimpleMaterial::create();
682 matFgCheck
->setDiffuse(OSG::Color3f(0,1,0));
683 matFgCheck
->setLit (false );
685 OSG::PolygonForegroundUnrecPtr pFGCheck
= OSG::PolygonForeground::create();
687 pFGCheck
->setMaterial(matFgCheck
);
689 pPos
= pFGCheck
->editMFPositions();
691 pPos
->push_back(OSG::Pnt2f(0.6f
, 0.0f
));
692 pPos
->push_back(OSG::Pnt2f(0.9f
, 0.0f
));
693 pPos
->push_back(OSG::Pnt2f(0.9f
, 0.3f
));
694 pPos
->push_back(OSG::Pnt2f(0.6f
, 0.3f
));
697 vp
->addForeground(pFG
);
698 vp
->addForeground(pFGCheck
);
703 OSG::SolidBackgroundUnrecPtr bkgndFBO
= OSG::SolidBackground::create();
705 bkgndFBO
->setColor(OSG::Color3f(1.0,0.5,0.5));
709 vpFBO
= OSG::FBOViewport::create();
711 vpFBO
->setCamera (cam
);
712 vpFBO
->setBackground(bkgndFBO
);
713 vpFBO
->setRoot (root
);
714 vpFBO
->setSize (0, 0, 1, 1);
716 vpFBO
->addForeground(pFGCheck
);
718 OSG::FrameBufferObjectUnrecPtr pFBO
= OSG::FrameBufferObject::create();
720 pTexBuffer
= OSG::TextureBuffer::create();
721 OSG::RenderBufferUnrecPtr pDepthBuffer
= OSG::RenderBuffer ::create();
723 pDepthBuffer
->setInternalFormat(GL_DEPTH_COMPONENT24
);
725 pTexBuffer
->setTexture(tx1o
);
727 pFBO
->setSize(128, 128);
729 pFBO
->setColorAttachment(pTexBuffer
, 0);
730 pFBO
->setDepthAttachment(pDepthBuffer
);
732 pFBO
->editMFDrawBuffers()->clear();
733 pFBO
->editMFDrawBuffers()->push_back(GL_COLOR_ATTACHMENT0_EXT
);
736 vpFBO
->setFrameBufferObject(pFBO
);
741 std::cout
<< "GLUT winid: " << winid
<< std::endl
;
746 glGetIntegerv( GL_VIEWPORT
, glvp
);
748 gwin
= OSG::GLUTWindow::create();
749 gwin
->setGlutId(winid
);
750 gwin
->setSize( glvp
[2], glvp
[3] );
754 win
->addPort(vpFBO
);
761 rentravact
= OSG::RenderAction::create();
762 // renact->setFrustumCulling(false);
768 pos
.setValues(min
[0] + ((max
[0] - min
[0]) * 0.5),
769 min
[1] + ((max
[1] - min
[1]) * 0.5),
770 max
[2] + ( max
[2] - min
[2] ) * 1.5 );
772 float scale
= (max
[2] - min
[2] + max
[1] - min
[1] + max
[0] - min
[0]) / 6;
774 OSG::Pnt3f
tCenter(min
[0] + (max
[0] - min
[0]) / 2,
775 min
[1] + (max
[1] - min
[1]) / 2,
776 min
[2] + (max
[2] - min
[2]) / 2);
778 fprintf(stderr
, "Startpos : %f %f %f\n", pos
[0], pos
[1], pos
[2]);
780 tball
.setMode( OSG::Trackball::OSGObject
);
781 tball
.setStartPosition( pos
, true );
782 tball
.setSum( true );
783 tball
.setTranslationMode( OSG::Trackball::OSGFree
);
784 tball
.setTranslationScale(scale
);
785 tball
.setRotationCenter(tCenter
);
789 pPoly
= OSG::PolygonChunk::create();
791 pCOver
->subChunk(pPoly
);
796 int main (int argc
, char **argv
)