fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / WindowSystem / GLUT / testWindowGLUT.cpp
blob9641155832a100fa00a3b24d4ddb8e2cbe4bdae9
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 break;
239 case 'a':
240 glDisable( GL_LIGHTING );
241 std::cerr << "Lighting disabled." << std::endl;
242 break;
243 case 's':
244 glEnable( GL_LIGHTING );
245 std::cerr << "Lighting enabled." << std::endl;
246 break;
247 case 'z':
248 pPoly->setFrontMode(GL_POINT);
249 pPoly->setBackMode(GL_POINT);
250 std::cerr << "PolygonMode: Point." << std::endl;
251 break;
252 case 'x':
253 pPoly->setFrontMode(GL_LINE);
254 pPoly->setBackMode(GL_LINE);
255 std::cerr << "PolygonMode: Line." << std::endl;
256 break;
257 case 'c':
258 pPoly->setFrontMode(GL_FILL);
259 pPoly->setBackMode(GL_FILL);
260 std::cerr << "PolygonMode: Fill." << std::endl;
261 break;
262 case 'p':
264 if(bPolyActive == true)
266 OSG_ASSERT(pCOver->subChunk(pPoly) == true);
267 bPolyActive = false;
269 else
271 OSG_ASSERT(pCOver->addChunk(pPoly) == true);
272 bPolyActive = true;
274 break;
276 case 'r':
278 std::cerr << "Sending ray through " << x << "," << y << std::endl;
279 OSG::Line l;
280 cam->calcViewRay( l, x, y, *vp );
281 std::cerr << "From " << l.getPosition() << ", dir "
282 << l.getDirection()
283 << std::endl;
285 break;
287 case ' ':
289 OSG::Matrix m;
290 OSG::Quaternion q;
291 OSG::Vec3f v;
293 q = oldq;
294 v = oldv;
296 oldq = tball.getRotation();
297 oldv = tball.getPosition();
299 move_obj = ! move_obj;
300 if ( move_obj )
302 puts("moving object");
303 // m = scene_trans->getSFMatrix()->getValue();
304 tball.setMode( OSG::Trackball::OSGCamera );
307 else
309 puts("moving camera");
310 // m = cam_trans->getSFMatrix()->getValue();
311 tball.setMode( OSG::Trackball::OSGObject );
314 // q.setValue(m);
315 tball.setStartPosition( v, true );
316 tball.setStartRotation( q, true );
318 // std::cout << q << std::endl;
319 // std::cout << v << std::endl;
321 // std::cout << " " << m[3][0] << " " << m[3][1] << " " << m[3][2] << std::endl;
324 break;
329 int init(int argc, char **argv)
331 OSG::osgInit(argc,argv);
333 OSG::setVBOUsageOnPropertyProtos(true);
335 // GLUT init
337 glutInit(&argc, argv);
338 glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
339 int winid = glutCreateWindow("OpenSG");
340 glutKeyboardFunc(key);
341 glutVisibilityFunc(vis);
342 glutReshapeFunc(reshape);
343 glutDisplayFunc(display);
344 glutMouseFunc(mouse);
345 glutMotionFunc(motion);
347 glutIdleFunc(display);
349 // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
351 // glEnable( GL_DEPTH_TEST );
352 // glEnable( GL_LIGHTING );
353 // glEnable( GL_LIGHT0 );
354 // glFrontFace(GL_CW);
355 // glEnable(GL_CULL_FACE);
357 // OSG
359 OSG::SceneFileHandler::the()->print();
361 // create the graph
363 // beacon for camera and light
364 OSG::NodeUnrecPtr b1n = OSG::Node::create();
365 OSG::GroupUnrecPtr b1 = OSG::Group::create();
367 b1n->setCore( b1 );
369 // transformation
370 OSG::NodeUnrecPtr t1n = OSG::Node::create();
371 OSG::TransformUnrecPtr t1 = OSG::Transform::create();
373 t1n->setCore( t1 );
374 t1n->addChild( b1n );
376 cam_trans = t1;
378 // light
380 OSG::NodeUnrecPtr dlight = OSG::Node::create();
381 OSG::DirectionalLightUnrecPtr dl = OSG::DirectionalLight::create();
383 dlight->setCore( dl );
384 // dlight->setCore( Group::create() );
386 dl->setAmbient( .0, .0, .0, 1 );
387 dl->setDiffuse( .8f, .8f, .8f, 1.f );
388 dl->setDirection(0,0,1);
389 dl->setBeacon( b1n);
391 // root
392 root = OSG::Node::create();
393 OSG::GroupUnrecPtr gr1 = OSG::Group::create();
395 root->setCore( gr1 );
396 root->addChild( t1n );
397 root->addChild( dlight );
399 // Load the file
401 OSG::NodeUnrecPtr file = NULL;
403 if(argc > 1)
404 file = OSG::SceneFileHandler::the()->read(argv[1], NULL, NULL);
406 if ( file == NULL )
408 std::cerr << "Couldn't load file, ignoring" << std::endl;
410 file = OSG::makeSphere(4, 2.0);
414 OSG::Thread::getCurrentChangeList()->commitChanges();
416 #if 0
417 OSG::GeometryPtr pGeo = cast_dynamic<OSG::GeometryPtr>(file->getCore());
419 if(pGeo == NULL && file->getNChildren() != 0)
421 pGeo = cast_dynamic<OSG::GeometryPtr>(file->getChild(0)->getCore());
424 if(pGeo == NULL)
426 fprintf(stderr, "no geo\n");
428 #endif
430 OSG::GraphOpRefPtr op = OSG::GraphOpFactory::the()->create("Stripe");
432 // op->traverse(file);
433 // createOptimizedPrimitives(pGeo);
434 // createSharedIndex(pGeo);
437 // file->dump();
438 file->updateVolume();
440 #if 0
441 const char *outFileName = "/tmp/foo1.osg";
443 OSG::IndentFileOutStream outFileStream(outFileName);
445 if( !outFileStream )
447 std::cerr << "Can not open output stream to file: "
448 << outFileName << std::endl;
449 return -1;
452 std::cerr << "STARTING PRINTOUT:" << std::endl;
453 OSG::OSGWriter writer( outFileStream, 4 );
455 writer.write( file );
457 outFileStream.close();
459 OSG::SceneFileHandler::the()->write(file, "/tmp/foo.osb");
460 #endif
463 // return 0;
466 OSG::Vec3f min,max;
467 file->getVolume().getBounds( min, max );
470 std::cout << "Volume: from " << min << " to " << max << std::endl;
473 // NodePtr pChunkOverNode = Node::create();
476 // pChunkOverNode->setCore(pCOver);
477 // pChunkOverNode->addChild(file);
479 OSG::MultiCoreUnrecPtr pMCore = OSG::MultiCore::create();
481 pCOver = OSG::ChunkOverrideGroup::create();
482 scene_trans = OSG::Transform::create();
484 pMCore->addCore(scene_trans);
485 pMCore->addCore(pCOver );
487 OSG::NodeUnrecPtr sceneTrN = OSG::Node::create();
489 sceneTrN->setCore(pMCore);
490 sceneTrN->addChild(file);
493 dlight->addChild(sceneTrN);
495 std::cerr << "Tree: " << std::endl;
496 // root->dump();
498 // Camera
500 cam = OSG::PerspectiveCamera::create();
502 cam->setBeacon( b1n );
503 cam->setFov( OSG::osgDegree2Rad( 90 ) );
504 cam->setNear( 0.1f );
505 cam->setFar( 10000 );
507 // Background
508 OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();
510 bkgnd->setColor(OSG::Color3f(0.1,0.1,0.1));
512 // Viewport
514 vp = OSG::Viewport::create();
516 vp->setCamera( cam );
517 vp->setBackground( bkgnd );
518 vp->setRoot( root );
519 vp->setSize( 0,0, 1,1 );
521 // vp->dump();
523 // Window
524 std::cout << "GLUT winid: " << winid << std::endl;
527 GLint glvp[4];
529 glGetIntegerv( GL_VIEWPORT, glvp );
531 gwin = OSG::GLUTWindow::create();
532 gwin->setGlutId(winid);
533 gwin->setSize( glvp[2], glvp[3] );
535 win = gwin;
537 win->addPort( vp );
539 win->init();
541 // Action
543 rentravact = OSG::RenderAction::create();
544 // renact->setFrustumCulling(false);
546 rentravact->setCorrectTwoSidedLighting(true);
548 // tball
550 OSG::Vec3f pos;
551 pos.setValues(min[0] + ((max[0] - min[0]) * 0.5),
552 min[1] + ((max[1] - min[1]) * 0.5),
553 max[2] + ( max[2] - min[2] ) * 1.5 );
555 float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;
557 OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
558 min[1] + (max[1] - min[1]) / 2,
559 min[2] + (max[2] - min[2]) / 2);
561 fprintf(stderr, "Startpos : %f %f %f\n", pos[0], pos[1], pos[2]);
563 tball.setMode( OSG::Trackball::OSGObject );
564 tball.setStartPosition( pos, true );
565 tball.setSum( true );
566 tball.setTranslationMode( OSG::Trackball::OSGFree );
567 tball.setTranslationScale(scale);
568 tball.setRotationCenter(tCenter);
570 fprintf(stderr, "%d\n",
571 OSG::MFUnrecNodePtr ::getClassType().getId());
572 fprintf(stderr, "%d\n",
573 OSG::MFUnrecFieldContainerPtr::getClassType().getId());
575 // MFNodePtr ::getClassType().dump();
576 // MFFieldContainerPtr::getClassType().dump();
578 // run...
580 pPoly = OSG::PolygonChunk::create();
582 pCOver->subChunk(pPoly);
584 #if 0
585 GroupNodePtr pGr = GroupNodePtr::create();
587 NodePtr pN;
588 GroupPtr pG;
590 fprintf(stderr, "A\n");
592 if(pGr == NULL)
596 fprintf(stderr, "B\n");
598 if(pGr == pN)
602 fprintf(stderr, "C\n");
604 if(pGr == pG)
608 pGr = NULL;
609 #endif
611 OSG::FrameHandler::the()->init();
613 return 0;
616 int main (int argc, char **argv)
618 init(argc, argv);
620 glutMainLoop();
622 return 0;