changed: gcc8 base update
[opensg.git] / Source / System / NodeCores / Groups / DynamicStateGenerators / testCubemapGenerator.cpp
blobcb8a06e51d8c308d676173585b7c5f424ece4090
1 #include "OSGConfig.h"
3 #include <iostream>
5 #include "OSGGLUT.h"
6 #include "OSGNode.h"
8 #include "OSGTrackball.h"
9 #include "OSGLine.h"
10 #include "OSGPerspectiveCamera.h"
11 #include "OSGTransform.h"
12 #include "OSGComponentTransform.h"
13 #include "OSGRenderAction.h"
14 #include "OSGWindow.h"
15 #include "OSGSceneFileHandler.h"
16 #include "OSGSolidBackground.h"
17 #include "OSGSkyBackground.h"
18 #include "OSGGLUTWindow.h"
19 #include "OSGDirectionalLight.h"
20 #include "OSGSimpleGeometry.h"
21 #include "OSGSimpleMaterial.h"
22 #include "OSGCubeMapGenerator.h"
23 #include "OSGImageFileHandler.h"
24 #include "OSGVisitSubTree.h"
26 OSG::RenderActionRefPtr rentravact = NULL;
28 OSG::NodeRecPtr hdrroot = NULL;
29 OSG::NodeRecPtr root = NULL;
30 //OSG::NodeRecPtr file = NULL;
32 OSG::PerspectiveCameraRecPtr cam = NULL;
33 OSG::ViewportRecPtr vp = NULL;
34 OSG::WindowRecPtr win = NULL;
36 OSG::TransformRecPtr cam_trans = NULL;
37 OSG::TransformRecPtr scene_trans = NULL;
39 OSG::ComponentTransformRecPtr pAnimTrs[6] =
41 NULL,
42 NULL,
43 NULL,
44 NULL,
45 NULL,
46 NULL
49 OSG::Trackball tball;
51 bool move_obj = false;
53 int mouseb = 0;
54 int lastx = 0;
55 int lasty = 0;
57 OSG::Quaternion oldq;
58 OSG::Vec3f oldv;
60 void display(void)
62 #if 0
63 Matrix m1, m2, m3;
64 Quaternion q1;
66 tball.getRotation().getValue(m3);
68 q1.setValue(m3);
70 m1.setRotate(q1);
72 m2.setTranslate( tball.getPosition() );
74 m1.mult( m2 );
76 if(move_obj == true)
78 scene_trans->editSFMatrix()->setValue( m1 );
80 else
82 cam_trans->editSFMatrix()->setValue( m1 );
84 #endif
86 cam_trans->editSFMatrix()->setValue(tball.getFullTrackballMatrix());
88 #if 0
89 fprintf(stderr, "%f %f %f\n",
90 cam_trans->getMatrix()[3][0],
91 cam_trans->getMatrix()[3][1],
92 cam_trans->getMatrix()[3][2]);
93 #endif
95 OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME);
97 for(OSG::UInt32 i = 0; i < 6; ++i)
99 if(pAnimTrs[i] != NULL)
101 pAnimTrs[i]->editRotation().setValueAsAxisDeg(
102 0.f, 1.f, 0.f,
103 t / 50.f);
107 OSG::commitChanges();
109 // fprintf(stderr, "Frame start\n");
110 // fprintf(stderr, "============================================\n");
113 win->render(rentravact);
116 void reshape( int w, int h )
118 std::cerr << "Reshape: " << w << "," << h << std::endl;
119 win->resize( w, h );
123 void animate(void)
125 glutPostRedisplay();
128 // tballall stuff
131 void motion(int x, int y)
133 OSG::Real32 w = win->getWidth(), h = win->getHeight();
136 OSG::Real32
137 a = -2. * ( lastx / w - .5 ),
138 b = -2. * ( .5 - lasty / h ),
139 c = -2. * ( x / w - .5 ),
140 d = -2. * ( .5 - y / h );
142 if ( mouseb & ( 1 << GLUT_LEFT_BUTTON ) )
144 tball.updateRotation( a, b, c, d );
146 else if ( mouseb & ( 1 << GLUT_MIDDLE_BUTTON ) )
148 tball.updatePosition( a, b, c, d );
150 else if ( mouseb & ( 1 << GLUT_RIGHT_BUTTON ) )
152 tball.updatePositionNeg( a, b, c, d );
155 lastx = x;
156 lasty = y;
159 void mouse(int button, int state, int x, int y)
161 if ( state == 0 )
163 switch ( button )
165 case GLUT_LEFT_BUTTON:
166 break;
168 case GLUT_MIDDLE_BUTTON:
169 tball.setAutoPosition(true);
170 break;
172 case GLUT_RIGHT_BUTTON:
173 tball.setAutoPositionNeg(true);
174 break;
177 mouseb |= 1 << button;
179 else if ( state == 1 )
181 switch ( button )
183 case GLUT_LEFT_BUTTON: break;
185 case GLUT_MIDDLE_BUTTON:
186 tball.setAutoPosition(false);
187 break;
189 case GLUT_RIGHT_BUTTON:
190 tball.setAutoPositionNeg(false);
191 break;
194 mouseb &= ~(1 << button);
196 lastx = x;
197 lasty = y;
200 void vis(int visible)
202 if (visible == GLUT_VISIBLE)
204 glutIdleFunc(animate);
206 else
208 glutIdleFunc(NULL);
212 void key(unsigned char key, int x, int y)
214 switch ( key )
216 case 27:
218 fprintf(stderr, "Start Destruction\n\n");
220 rentravact = NULL;
222 hdrroot = NULL;
223 root = NULL;
224 // file = NULL;
226 cam = NULL;
227 vp = NULL;
228 win = NULL;
230 cam_trans = NULL;
231 scene_trans = NULL;
233 pAnimTrs[0] = NULL;
234 pAnimTrs[1] = NULL;
235 pAnimTrs[2] = NULL;
236 pAnimTrs[3] = NULL;
237 pAnimTrs[4] = NULL;
238 pAnimTrs[5] = NULL;
241 OSG::osgExit();
242 exit(0);
243 case 'a':
244 glDisable( GL_LIGHTING );
245 std::cerr << "Lighting disabled." << std::endl;
246 break;
247 case 's':
248 glEnable( GL_LIGHTING );
249 std::cerr << "Lighting enabled." << std::endl;
250 break;
251 case 'r':
253 std::cerr << "Sending ray through " << x << "," << y << std::endl;
254 OSG::Line l;
255 cam->calcViewRay( l, x, y, *vp );
256 std::cerr << "From " << l.getPosition() << ", dir "
257 << l.getDirection()
258 << std::endl;
260 break;
262 case 'd':
263 OSG::ActionDataSlotPool::the()->dumpState();
264 OSG::StageIdPool ::the()->dumpState();
265 rentravact->dumpStore();
266 break;
267 case ' ':
269 OSG::Matrix m;
270 OSG::Quaternion q;
271 OSG::Vec3f v;
273 q = oldq;
274 v = oldv;
276 oldq = tball.getRotation();
277 oldv = tball.getPosition();
279 move_obj = ! move_obj;
280 if ( move_obj )
282 puts("moving object");
283 tball.setMode( OSG::Trackball::OSGCamera );
286 else
288 puts("moving camera");
289 tball.setMode( OSG::Trackball::OSGObject );
292 tball.setStartPosition( v, true );
293 tball.setStartRotation( q, true );
295 break;
300 OSG::NodeTransitPtr setupAnim(void)
302 OSG::NodeTransitPtr returnValue = OSG::Node::create();
304 returnValue->setCore(OSG::Group::create());
306 static const OSG::Real32 aOffsets[6][3] =
308 { -5.5, 0.0, 0.0 },
309 { 5.5, 0.0, 0.0 },
310 { 0.0, -5.5, 0.0 },
311 { 0.0, 5.5, 0.0 },
312 { 0.0, 0.0, -5.5 },
313 { 0.0, 0.0, 5.5 }
316 static const OSG::Real32 aDiffuse[6][3] =
318 { 1.f, 0.f, 0.f },
319 { 0.f, 1.f, 0.f },
320 { 0.f, 0.f, 1.f },
321 { 1.f, 1.f, 0.f },
322 { 1.f, 0.f, 1.f },
323 { 0.f, 1.f, 1.f }
326 for(OSG::UInt32 i = 0; i < 6; ++i)
328 OSG::NodeUnrecPtr pTN = OSG::Node::create();
330 pAnimTrs[i] = OSG::ComponentTransform::create();
332 OSG::GeometryUnrecPtr pGeo = OSG::makeBoxGeo(1.f, 1.f, 1.f,
333 2, 2, 2);
334 OSG::NodeUnrecPtr pGeoNode = OSG::Node::create();
336 pGeoNode->setCore(pGeo);
338 OSG::SimpleMaterialUnrecPtr pMat = OSG::SimpleMaterial::create();
340 pMat->setDiffuse(OSG::Color3f(aDiffuse[i][0],
341 aDiffuse[i][1],
342 aDiffuse[i][2]));
343 pMat->setAmbient(OSG::Color3f(aDiffuse[i][0],
344 aDiffuse[i][1],
345 aDiffuse[i][2]));
347 pGeo->setMaterial(pMat);
349 pAnimTrs[i]->editTranslation().setValues(aOffsets[i][0],
350 aOffsets[i][1],
351 aOffsets[i][2]);
353 pTN->setCore (pAnimTrs[i]);
354 pTN->addChild(pGeoNode );
356 returnValue->addChild(pTN);
360 return returnValue;
363 int doMain (int argc, char **argv)
365 OSG::osgInit(argc,argv);
367 // GLUT init
369 glutInit(&argc, argv);
370 glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
371 glutInitWindowSize(800, 800);
374 int winid = glutCreateWindow("OpenSG");
375 glutKeyboardFunc(key);
376 glutVisibilityFunc(vis);
377 glutReshapeFunc(reshape);
378 glutDisplayFunc(display);
379 glutMouseFunc(mouse);
380 glutMotionFunc(motion);
382 glutIdleFunc(display);
384 // OSG
386 OSG::SceneFileHandler::the()->print();
388 // create the graph
390 // beacon for camera and light
391 OSG::NodeUnrecPtr b1n = OSG::Node::create();
392 OSG::GroupUnrecPtr b1 = OSG::Group::create();
394 fprintf(stderr, "Create b1n %p %d %d \n",
395 static_cast<void *>(b1n.get()),
396 b1n->getRefCount(),
397 b1n->getWeakRefCount());
399 b1n->setCore( b1 );
401 // transformation
402 OSG::NodeUnrecPtr t1n = OSG::Node::create();
403 OSG::TransformUnrecPtr t1 = OSG::Transform::create();
405 t1n->setCore (t1 );
406 t1n->addChild(b1n);
408 fprintf(stderr, "Create t1n %p %d %d \n",
409 static_cast<void *>(t1n.get()),
410 t1n->getRefCount(),
411 t1n->getWeakRefCount());
413 cam_trans = t1;
415 // light
417 OSG::NodeUnrecPtr dlight = OSG::Node::create();
418 OSG::DirectionalLightUnrecPtr dl = OSG::DirectionalLight::create();
421 dlight->setCore(dl);
423 dl->setAmbient( .3f, .3f, .3f, 1 );
424 dl->setDiffuse( .8f, .8f, .8f, .8f );
425 dl->setDirection(0,0,1);
426 dl->setBeacon( b1n);
429 fprintf(stderr, "Create dlight %p %d %d \n",
430 static_cast<void *>(dlight.get()),
431 dlight->getRefCount(),
432 dlight->getWeakRefCount());
434 hdrroot = OSG::Node::create();
436 hdrroot->editVolume().setInfinite();
437 hdrroot->editVolume().setStatic ();
439 hdrroot->setCore(OSG::Group::create());
441 // root
442 root = OSG::Node:: create();
444 OSG::GroupUnrecPtr gr1 = OSG::Group::create();
446 root->setCore(gr1);
449 hdrroot->addChild(root);
451 root->addChild(t1n );
452 root->addChild(dlight);
454 fprintf(stderr, "Create root %p %d %d \n",
455 static_cast<void *>(root.get()),
456 root->getRefCount(),
457 root->getWeakRefCount());
459 // Load the file
461 OSG::NodeUnrecPtr file = NULL;
463 if(argc > 1)
465 file = OSG::SceneFileHandler::the()->read(argv[1], NULL);
468 if(file == NULL)
470 std::cerr << "Couldn't load file, ignoring" << std::endl;
472 // file = makeBox(2.f, 2.f, 2.f, 2, 2, 2);
473 file = OSG::makeSphere(4, 2.0);
476 OSG::NodeUnrecPtr pCubeRoot = OSG::Node::create();
477 OSG::CubeMapGeneratorUnrecPtr pCubeGen = OSG::CubeMapGenerator::create();
479 pCubeRoot->addChild(file);
480 pCubeRoot->setCore(pCubeGen);
481 // pCubeRoot->setCore(Group::create());
483 OSG::NodeUnrecPtr pCubeSceneRoot = OSG::Node::create();
484 OSG::VisitSubTreeUnrecPtr pCubeVisit = OSG::VisitSubTree::create();
486 pCubeSceneRoot->setCore(pCubeVisit);
487 pCubeVisit->setSubTreeRoot(root);
489 pCubeGen->setRoot (pCubeSceneRoot);
490 pCubeGen->setTextureFormat(GL_RGB32F_ARB );
491 pCubeGen->setSize (512,
492 512 );
493 pCubeGen->setTexUnit (3);
495 // Cubemap Background
496 OSG::SolidBackgroundUnrecPtr cubeBkgnd = OSG::SolidBackground::create();
498 cubeBkgnd->setColor(OSG::Color3f(0.5f, 0.3f, 0.3f));
501 pCubeGen->setBackground(cubeBkgnd);
504 OSG::NodeUnrecPtr pAnimRoot = setupAnim();
506 scene_trans = OSG::Transform::create();
507 OSG::NodeUnrecPtr sceneTrN = OSG::Node::create();
509 scene_trans->editMatrix()[3][2] = -50.f;
511 sceneTrN->setCore (scene_trans);
512 sceneTrN->addChild(pCubeRoot );
513 sceneTrN->addChild(pAnimRoot );
515 OSG::Thread::getCurrentChangeList()->commitChanges();
517 OSG::Vec3f min,max;
518 sceneTrN->updateVolume();
519 sceneTrN->getVolume().getBounds(min, max);
521 std::cout << "Volume: from " << min << " to " << max << std::endl;
524 dlight->addChild(sceneTrN);
526 // Camera
528 cam = OSG::PerspectiveCamera::create();
530 cam->setBeacon( b1n );
531 cam->setFov( OSG::osgDegree2Rad( 90 ) );
532 cam->setNear( 0.1f );
533 cam->setFar( 100000 );
536 // Background
537 OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();
539 bkgnd->setColor(OSG::Color3f(0.3f, 0.3f, 0.3f));
542 // Viewport
543 vp = OSG::Viewport::create();
545 vp->setCamera( cam );
546 vp->setBackground( bkgnd );
547 vp->setRoot( hdrroot );
548 // vp->setRoot( root );
549 vp->setSize( 0,0, 1,1 );
553 // Window
554 OSG::GLUTWindowUnrecPtr gwin;
556 GLint glvp[4];
558 glGetIntegerv(GL_VIEWPORT, glvp);
560 gwin = OSG::GLUTWindow::create();
562 gwin->setGlutId(winid);
563 gwin->setSize( glvp[2], glvp[3] );
565 win = gwin;
567 win->addPort( vp );
569 win->init();
572 // Action
573 rentravact = OSG::RenderAction::create();
575 rentravact->setVolumeDrawing(true);
576 // rentravact->setFrustumCulling(false);
578 // tball
579 OSG::Vec3f pos;
581 pos.setValues(min[0] + ((max[0] - min[0]) * 0.5),
582 min[1] + ((max[1] - min[1]) * 0.5),
583 max[2] + ( max[2] - min[2] ) * 1.5 );
585 float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;
587 OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
588 min[1] + (max[1] - min[1]) / 2,
589 min[2] + (max[2] - min[2]) / 2);
591 fprintf(stderr, "Startpos : %f %f %f\n", pos[0], pos[1], pos[2]);
593 tball.setMode (OSG::Trackball::OSGObject);
594 tball.setStartPosition (pos, true );
595 tball.setSum (true );
596 tball.setTranslationMode (OSG::Trackball::OSGFree );
597 tball.setTranslationScale(scale );
598 tball.setRotationCenter (tCenter );
600 fprintf(stderr, "Create b1n %p %d %d \n",
601 static_cast<void *>(b1n.get()),
602 b1n->getRefCount(),
603 b1n->getWeakRefCount());
605 fprintf(stderr, "Create t1n %p %d %d \n",
606 static_cast<void *>(t1n.get()),
607 t1n->getRefCount(),
608 t1n->getWeakRefCount());
610 fprintf(stderr, "Create dlight %p %d %d \n",
611 static_cast<void *>(dlight.get()),
612 dlight->getRefCount(),
613 dlight->getWeakRefCount());
615 fprintf(stderr, "Create hdrroot %p %d %d \n",
616 static_cast<void *>(hdrroot.get()),
617 hdrroot->getRefCount(),
618 hdrroot->getWeakRefCount());
620 fprintf(stderr, "Create root %p %d %d \n",
621 static_cast<void *>(root.get()),
622 root->getRefCount(),
623 root->getWeakRefCount());
625 return 0;
628 int main (int argc, char **argv)
630 doMain(argc, argv);
632 fprintf(stderr, "Create hdrroot %p %d %d \n",
633 static_cast<void *>(hdrroot.get()),
634 hdrroot->getRefCount(),
635 hdrroot->getWeakRefCount());
637 fprintf(stderr, "Create root %p %d %d \n",
638 static_cast<void *>(root.get()),
639 root->getRefCount(),
640 root->getWeakRefCount());
642 // run...
643 glutMainLoop();
645 return 0;