changed: gcc8 base update
[opensg.git] / Source / System / Window / FrameBufferObjects / testFBOViewport.cpp
blob43db8273af6f9f5aeffb0b6de831d0d01c738d81
1 #include "OSGConfig.h"
3 #include <iostream>
5 #include "OSGGLUT.h"
7 #include "OSGFieldContainerFactory.h"
8 #include "OSGVector.h"
9 #include "OSGQuaternion.h"
10 #include "OSGMatrix.h"
11 #include "OSGMatrixUtility.h"
12 #include "OSGBoxVolume.h"
13 #include "OSGLine.h"
14 #include "OSGNode.h"
15 #include "OSGGroup.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;
56 OSG::NodeRecPtr root;
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;
81 OSG::Trackball tball;
83 bool move_obj = false;
85 int mouseb = 0;
86 int lastx=0, lasty=0;
88 OSG::Quaternion oldq;
89 OSG::Vec3f oldv;
91 OSG::UInt32 oldTravMask;
92 bool dumpImg = false;
94 OSG::UInt32 oldTravMask_RB;
95 bool dumpImg_RB = false;
97 void display(void)
99 OSG::Matrix m1, m2, m3;
100 OSG::Quaternion q1;
102 tball.getRotation().getValue(m3);
104 q1.setValue(m3);
106 m1.setRotate(q1);
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;
120 m1.mult( m2 );
122 // std::cerr << m1 << std::endl;
124 m1 = tball.getFullTrackballMatrix();
126 if(move_obj == true)
128 scene_trans->editSFMatrix()->setValue( m1 );
130 else
132 cam_trans->editSFMatrix()->setValue( m1 );
135 OSG::commitChangesAndClear();
137 win->render(rentravact);
139 if(dumpImg == true)
141 vpFBO->setTravMask(oldTravMask);
143 pTexBuffer ->setReadBack (false);
144 vpFBO->getFrameBufferObject()->setPostProcessOnDeactivate(false);
146 pImg->write("/tmp/test.png");
148 dumpImg = false;
151 if(dumpImg_RB == true)
153 pRenBuffer->getImage()->write("/tmp/test1.png");
155 dumpImg_RB = false;
157 win->subPortByObj(vpFBO_RB);
159 vpFBO_RB = NULL;
160 pRenBuffer = NULL;
162 win->runFrameExit();
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;
178 win->resize( w, h );
182 void animate(void)
184 glutPostRedisplay();
187 // tballall stuff
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 );
212 lastx = x;
213 lasty = y;
216 void mouse(int button, int state, int x, int y)
218 if ( state == 0 )
220 switch ( button )
222 case GLUT_LEFT_BUTTON: break;
223 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(true);
224 break;
225 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(true);
226 break;
228 mouseb |= 1 << button;
230 else if ( state == 1 )
232 switch ( button )
234 case GLUT_LEFT_BUTTON: break;
235 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(false);
236 break;
237 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(false);
238 break;
240 mouseb &= ~(1 << button);
242 lastx = x;
243 lasty = y;
246 void vis(int visible)
248 if (visible == GLUT_VISIBLE)
250 glutIdleFunc(animate);
252 else
254 glutIdleFunc(NULL);
258 void key(unsigned char key, int x, int y)
260 switch ( key )
262 case 27:
263 root = NULL;
264 // file = NULL;
265 cam = NULL;
266 vp = NULL;
267 win = NULL;
268 cam_trans = NULL;
269 scene_trans = NULL;
270 pPoly = NULL;
271 pCOver = NULL;
272 gwin = NULL;
274 tx1o = NULL;
275 tx1e = NULL;
277 vpFBO = NULL;
278 vpFBO_RB = NULL;
279 pTexBuffer = NULL;
280 pRenBuffer = NULL;
281 pImg = NULL;
283 rentravact = NULL;
285 OSG::osgExit();
287 exit(0);
288 case 'f':
289 vpFBO->setTravMask(~vpFBO->getTravMask());
290 break;
291 case 'd':
292 oldTravMask = vpFBO->getTravMask();
294 if(oldTravMask == 0x0000)
296 vpFBO->setTravMask(~vpFBO->getTravMask());
299 dumpImg = true;
301 pTexBuffer ->setReadBack (true);
302 vpFBO->getFrameBufferObject()->setPostProcessOnDeactivate(true);
304 break;
306 case 'r':
308 // OSG::FieldContainerFactory::the()->dump();
309 //RenderBuffer Readback
310 // Background
311 OSG::SolidBackgroundUnrecPtr bkgndFBO_RB =
312 OSG::SolidBackground::create();
314 bkgndFBO_RB->setColor(OSG::Color3f(0.5,1.0,0.5));
316 // Viewport
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);
361 dumpImg_RB = true;
363 break;
365 case 'v':
366 rentravact->setVolumeDrawing(!rentravact->getVolumeDrawing());
367 break;
368 case 'a':
369 glDisable( GL_LIGHTING );
370 std::cerr << "Lighting disabled." << std::endl;
371 break;
372 case 's':
373 glEnable( GL_LIGHTING );
374 std::cerr << "Lighting enabled." << std::endl;
375 break;
376 case 'z':
377 pPoly->setFrontMode(GL_POINT);
378 pPoly->setBackMode(GL_POINT);
379 std::cerr << "PolygonMode: Point." << std::endl;
380 break;
381 case 'x':
382 pPoly->setFrontMode(GL_LINE);
383 pPoly->setBackMode(GL_LINE);
384 std::cerr << "PolygonMode: Line." << std::endl;
385 break;
386 case 'c':
387 pPoly->setFrontMode(GL_FILL);
388 pPoly->setBackMode(GL_FILL);
389 std::cerr << "PolygonMode: Fill." << std::endl;
390 break;
391 case 'p':
393 if(bPolyActive == true)
395 OSG_ASSERT(pCOver->subChunk(pPoly) == true);
396 bPolyActive = false;
398 else
400 OSG_ASSERT(pCOver->addChunk(pPoly) == true);
401 bPolyActive = true;
403 break;
406 case ' ':
408 OSG::Matrix m;
409 OSG::Quaternion q;
410 OSG::Vec3f v;
412 q = oldq;
413 v = oldv;
415 oldq = tball.getRotation();
416 oldv = tball.getPosition();
418 move_obj = ! move_obj;
419 if ( move_obj )
421 puts("moving object");
422 // m = scene_trans->getSFMatrix()->getValue();
423 tball.setMode( OSG::Trackball::OSGCamera );
426 else
428 puts("moving camera");
429 // m = cam_trans->getSFMatrix()->getValue();
430 tball.setMode( OSG::Trackball::OSGObject );
433 // q.setValue(m);
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;
443 break;
448 int init(int argc, char **argv)
450 OSG::osgInit(argc,argv);
452 // GLUT init
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);
474 // OSG
476 OSG::SceneFileHandler::the()->print();
478 // create the graph
480 // beacon for camera and light
481 OSG::NodeUnrecPtr b1n = OSG::Node::create();
482 OSG::GroupUnrecPtr b1 = OSG::Group::create();
484 b1n->setCore( b1 );
486 // transformation
487 OSG::NodeUnrecPtr t1n = OSG::Node::create();
488 OSG::TransformUnrecPtr t1 = OSG::Transform::create();
490 t1n->setCore( t1 );
491 t1n->addChild( b1n );
493 cam_trans = t1;
495 // light
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);
506 dl->setBeacon( b1n);
508 // root
509 root = OSG::Node::create();
510 OSG::GroupUnrecPtr gr1 = OSG::Group::create();
512 root->setCore( gr1 );
513 root->addChild( t1n );
514 root->addChild( dlight );
516 // Load the file
518 OSG::NodeUnrecPtr file = NULL;
520 if(argc > 1)
521 file = OSG::SceneFileHandler::the()->read(argv[1], NULL);
523 if ( file == NULL )
525 std::cerr << "Couldn't load file, ignoring" << std::endl;
527 file = OSG::makeSphere(4, 2.0);
531 #if 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());
539 if(pGeo == NULL)
541 fprintf(stderr, "no geo\n");
543 #endif
545 // OSG::GraphOpRefPtr op = OSG::GraphOpFactory::the()->create("Stripe");
547 // op->traverse(file);
548 // createOptimizedPrimitives(pGeo);
549 // createSharedIndex(pGeo);
551 OSG::Thread::getCurrentChangeList()->commitChanges();
553 // file->dump();
554 file->updateVolume();
556 #if 0
557 const char *outFileName = "/tmp/foo1.osg";
559 OSG::IndentFileOutStream outFileStream(outFileName);
561 if( !outFileStream )
563 std::cerr << "Can not open output stream to file: "
564 << outFileName << std::endl;
565 return -1;
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");
576 #endif
579 // return 0;
582 OSG::Vec3f min,max;
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;
606 // root->dump();
608 // Camera
610 cam = OSG::PerspectiveCamera::create();
612 cam->setBeacon( b1n );
613 cam->setFov( OSG::osgDegree2Rad( 90 ) );
614 cam->setNear( 0.1f );
615 cam->setFar( 100000 );
617 // Background
618 OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();
620 bkgnd->setColor(OSG::Color3f(1,0,0));
622 // Viewport
624 vp = OSG::Viewport::create();
626 vp->setCamera( cam );
627 vp->setBackground( bkgnd );
628 vp->setRoot( root );
629 vp->setSize( 0,0, 1,1 );
631 #if 0
632 OSG::UInt8 imgdata[] =
634 64,64,64, 128,128,128, 192,192,192, 255,255,255
636 #endif
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);
699 // vp->dump();
702 // Background
703 OSG::SolidBackgroundUnrecPtr bkgndFBO = OSG::SolidBackground::create();
705 bkgndFBO->setColor(OSG::Color3f(1.0,0.5,0.5));
707 // Viewport
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);
740 // Window
741 std::cout << "GLUT winid: " << winid << std::endl;
744 GLint glvp[4];
746 glGetIntegerv( GL_VIEWPORT, glvp );
748 gwin = OSG::GLUTWindow::create();
749 gwin->setGlutId(winid);
750 gwin->setSize( glvp[2], glvp[3] );
752 win = gwin;
754 win->addPort(vpFBO );
755 win->addPort(vp );
757 win->init();
759 // Action
761 rentravact = OSG::RenderAction::create();
762 // renact->setFrustumCulling(false);
765 // tball
767 OSG::Vec3f pos;
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);
787 // run...
789 pPoly = OSG::PolygonChunk::create();
791 pCOver->subChunk(pPoly);
793 return 0;
796 int main (int argc, char **argv)
798 init(argc, argv);
800 glutMainLoop();
802 return 0;