changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / WindowSystem / GLUT / testWindowGLUT.cpp
blobaba91555d05a898d6b0194be4865c0f5402e7bd9
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 "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;
47 OSG::NodeRecPtr root;
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;
64 OSG::Trackball tball;
66 bool move_obj = false;
68 int mouseb = 0;
69 int lastx=0, lasty=0;
71 OSG::Quaternion oldq;
72 OSG::Vec3f oldv;
74 void
75 display(void)
77 OSG::Matrix m1, m2, m3;
78 OSG::Quaternion q1;
80 tball.getRotation().getValue(m3);
82 q1.setValue(m3);
84 m1.setRotate(q1);
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;
98 m1.mult( m2 );
100 // std::cerr << m1 << std::endl;
102 m1 = tball.getFullTrackballMatrix();
104 if(move_obj == true)
106 scene_trans->editSFMatrix()->setValue( m1 );
108 else
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;
129 win->resize( w, h );
133 void
134 animate(void)
136 glutPostRedisplay();
139 // tballall stuff
142 void
143 motion(int x, int y)
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 );
165 lastx = x;
166 lasty = y;
169 void
170 mouse(int button, int state, int x, int y)
172 if ( state == 0 )
174 switch ( button )
176 case GLUT_LEFT_BUTTON: break;
177 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(true);
178 break;
179 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(true);
180 break;
182 mouseb |= 1 << button;
184 else if ( state == 1 )
186 switch ( button )
188 case GLUT_LEFT_BUTTON: break;
189 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(false);
190 break;
191 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(false);
192 break;
194 mouseb &= ~(1 << button);
196 lastx = x;
197 lasty = y;
200 void
201 vis(int visible)
203 if (visible == GLUT_VISIBLE)
205 glutIdleFunc(animate);
207 else
209 glutIdleFunc(NULL);
213 void key(unsigned char key, int x, int y)
215 switch ( key )
217 case 27:
218 OSG::FrameHandler::the()->shutdown();
220 root = NULL;
221 file = NULL;
222 cam = NULL;
223 vp = NULL;
224 win = NULL;
225 cam_trans = NULL;
226 scene_trans = NULL;
227 pPoly = NULL;
228 pCOver = NULL;
229 gwin = NULL;
231 rentravact = NULL;
233 OSG::osgExit();
235 exit(0);
236 case 'v':
237 rentravact->setVolumeDrawing(!rentravact->getVolumeDrawing());
238 case 'a':
239 glDisable( GL_LIGHTING );
240 std::cerr << "Lighting disabled." << std::endl;
241 break;
242 case 's':
243 glEnable( GL_LIGHTING );
244 std::cerr << "Lighting enabled." << std::endl;
245 break;
246 case 'z':
247 pPoly->setFrontMode(GL_POINT);
248 pPoly->setBackMode(GL_POINT);
249 std::cerr << "PolygonMode: Point." << std::endl;
250 break;
251 case 'x':
252 pPoly->setFrontMode(GL_LINE);
253 pPoly->setBackMode(GL_LINE);
254 std::cerr << "PolygonMode: Line." << std::endl;
255 break;
256 case 'c':
257 pPoly->setFrontMode(GL_FILL);
258 pPoly->setBackMode(GL_FILL);
259 std::cerr << "PolygonMode: Fill." << std::endl;
260 break;
261 case 'p':
263 if(bPolyActive == true)
265 OSG_ASSERT(pCOver->subChunk(pPoly) == true);
266 bPolyActive = false;
268 else
270 OSG_ASSERT(pCOver->addChunk(pPoly) == true);
271 bPolyActive = true;
273 break;
275 case 'r':
277 std::cerr << "Sending ray through " << x << "," << y << std::endl;
278 OSG::Line l;
279 cam->calcViewRay( l, x, y, *vp );
280 std::cerr << "From " << l.getPosition() << ", dir "
281 << l.getDirection()
282 << std::endl;
284 break;
286 case ' ':
288 OSG::Matrix m;
289 OSG::Quaternion q;
290 OSG::Vec3f v;
292 q = oldq;
293 v = oldv;
295 oldq = tball.getRotation();
296 oldv = tball.getPosition();
298 move_obj = ! move_obj;
299 if ( move_obj )
301 puts("moving object");
302 // m = scene_trans->getSFMatrix()->getValue();
303 tball.setMode( OSG::Trackball::OSGCamera );
306 else
308 puts("moving camera");
309 // m = cam_trans->getSFMatrix()->getValue();
310 tball.setMode( OSG::Trackball::OSGObject );
313 // q.setValue(m);
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;
323 break;
328 int init(int argc, char **argv)
330 OSG::osgInit(argc,argv);
332 OSG::setVBOUsageOnPropertyProtos(true);
334 // GLUT init
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);
356 // OSG
358 OSG::SceneFileHandler::the()->print();
360 // create the graph
362 // beacon for camera and light
363 OSG::NodeUnrecPtr b1n = OSG::Node::create();
364 OSG::GroupUnrecPtr b1 = OSG::Group::create();
366 b1n->setCore( b1 );
368 // transformation
369 OSG::NodeUnrecPtr t1n = OSG::Node::create();
370 OSG::TransformUnrecPtr t1 = OSG::Transform::create();
372 t1n->setCore( t1 );
373 t1n->addChild( b1n );
375 cam_trans = t1;
377 // light
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);
388 dl->setBeacon( b1n);
390 // root
391 root = OSG::Node::create();
392 OSG::GroupUnrecPtr gr1 = OSG::Group::create();
394 root->setCore( gr1 );
395 root->addChild( t1n );
396 root->addChild( dlight );
398 // Load the file
400 OSG::NodeUnrecPtr file = NULL;
402 if(argc > 1)
403 file = OSG::SceneFileHandler::the()->read(argv[1], NULL, NULL);
405 if ( file == NULL )
407 std::cerr << "Couldn't load file, ignoring" << std::endl;
409 file = OSG::makeSphere(4, 2.0);
413 OSG::Thread::getCurrentChangeList()->commitChanges();
415 #if 0
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());
423 if(pGeo == NULL)
425 fprintf(stderr, "no geo\n");
427 #endif
429 OSG::GraphOpRefPtr op = OSG::GraphOpFactory::the()->create("Stripe");
431 // op->traverse(file);
432 // createOptimizedPrimitives(pGeo);
433 // createSharedIndex(pGeo);
436 // file->dump();
437 file->updateVolume();
439 #if 0
440 const char *outFileName = "/tmp/foo1.osg";
442 OSG::IndentFileOutStream outFileStream(outFileName);
444 if( !outFileStream )
446 std::cerr << "Can not open output stream to file: "
447 << outFileName << std::endl;
448 return -1;
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");
459 #endif
462 // return 0;
465 OSG::Vec3f min,max;
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;
495 // root->dump();
497 // Camera
499 cam = OSG::PerspectiveCamera::create();
501 cam->setBeacon( b1n );
502 cam->setFov( OSG::osgDegree2Rad( 90 ) );
503 cam->setNear( 0.1f );
504 cam->setFar( 10000 );
506 // Background
507 OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();
509 bkgnd->setColor(OSG::Color3f(0.1,0.1,0.1));
511 // Viewport
513 vp = OSG::Viewport::create();
515 vp->setCamera( cam );
516 vp->setBackground( bkgnd );
517 vp->setRoot( root );
518 vp->setSize( 0,0, 1,1 );
520 // vp->dump();
522 // Window
523 std::cout << "GLUT winid: " << winid << std::endl;
526 GLint glvp[4];
528 glGetIntegerv( GL_VIEWPORT, glvp );
530 gwin = OSG::GLUTWindow::create();
531 gwin->setGlutId(winid);
532 gwin->setSize( glvp[2], glvp[3] );
534 win = gwin;
536 win->addPort( vp );
538 win->init();
540 // Action
542 rentravact = OSG::RenderAction::create();
543 // renact->setFrustumCulling(false);
545 rentravact->setCorrectTwoSidedLighting(true);
547 // tball
549 OSG::Vec3f pos;
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();
577 // run...
579 pPoly = OSG::PolygonChunk::create();
581 pCOver->subChunk(pPoly);
583 #if 0
584 GroupNodePtr pGr = GroupNodePtr::create();
586 NodePtr pN;
587 GroupPtr pG;
589 fprintf(stderr, "A\n");
591 if(pGr == NULL)
595 fprintf(stderr, "B\n");
597 if(pGr == pN)
601 fprintf(stderr, "C\n");
603 if(pGr == pG)
607 pGr = NULL;
608 #endif
610 OSG::FrameHandler::the()->init();
612 return 0;
615 int main (int argc, char **argv)
617 init(argc, argv);
619 glutMainLoop();
621 return 0;