fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / WebInterface / testWebInterface.cpp
blob18f336a21d2df13af3c3aa3125217122d87149e2
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"
42 #include "OSGWebInterface.h"
44 #include "OSGTrackball.h"
46 OSG::RenderActionRefPtr rentravact;
48 OSG::NodeRecPtr root;
50 //OSG::NodeRecPtr file;
52 OSG::PerspectiveCameraRecPtr cam;
53 OSG::ViewportRecPtr vp;
54 OSG::WindowRecPtr win;
56 OSG::TransformRecPtr cam_trans;
57 OSG::TransformRecPtr scene_trans;
59 OSG::PolygonChunkRecPtr pPoly;
60 bool bPolyActive = false;
61 OSG::ChunkOverrideGroupRecPtr pCOver;
63 OSG::GLUTWindowUnrecPtr gwin;
65 OSG::Trackball tball;
67 bool move_obj = false;
69 int mouseb = 0;
70 int lastx=0, lasty=0;
72 OSG::Quaternion oldq;
73 OSG::Vec3f oldv;
75 OSG::WebInterfaceRefPtr webInterface;
77 void
78 display(void)
80 OSG::Matrix m1, m2, m3;
81 OSG::Quaternion q1;
83 tball.getRotation().getValue(m3);
85 q1.setValue(m3);
87 m1.setRotate(q1);
89 // std::cout << "TBROT" << std::endl << tball.getRotation() << endl;
90 // std::cout << "M3" << std::endl << m3 << std::endl;
91 // std::cout << "Q1" << std::endl << q1 << std::endl;
92 // std::cout << "M1" << std::endl << m1 << std::endl;
94 // m1.setRotate( tball.getRotation() );
95 m2.setTranslate( tball.getPosition() );
97 //std::cout << "Pos: " << tball.getPosition() << ", Rot: " << tball.getRotation() << std::endl;
99 // std::cout << tball.getRotation() << std::endl;
101 m1.mult( m2 );
103 // std::cerr << m1 << std::endl;
105 m1 = tball.getFullTrackballMatrix();
107 if(move_obj == true)
109 scene_trans->editSFMatrix()->setValue( m1 );
111 else
113 cam_trans->editSFMatrix()->setValue( m1 );
116 OSG::FrameHandler::the()->frame();
118 OSG::commitChanges();
120 win->render(rentravact);
122 webInterface->waitRequest(0.01);
123 webInterface->handleRequests();
125 // win->render(renact);
127 // std::cerr << "------------- AR START --------------" << std::endl;
129 // Thread::getCurrentChangeList()->dump();
132 void reshape( int w, int h )
134 std::cerr << "Reshape: " << w << "," << h << std::endl;
135 win->resize( w, h );
139 void
140 animate(void)
142 glutPostRedisplay();
145 // tballall stuff
148 void
149 motion(int x, int y)
151 OSG::Real32 w = win->getWidth(), h = win->getHeight();
154 OSG::Real32 a = -2. * ( lastx / w - .5 ),
155 b = -2. * ( .5 - lasty / h ),
156 c = -2. * ( x / w - .5 ),
157 d = -2. * ( .5 - y / h );
159 if ( mouseb & ( 1 << GLUT_LEFT_BUTTON ) )
161 tball.updateRotation( a, b, c, d );
163 else if ( mouseb & ( 1 << GLUT_MIDDLE_BUTTON ) )
165 tball.updatePosition( a, b, c, d );
167 else if ( mouseb & ( 1 << GLUT_RIGHT_BUTTON ) )
169 tball.updatePositionNeg( a, b, c, d );
171 lastx = x;
172 lasty = y;
175 void
176 mouse(int button, int state, int x, int y)
178 if ( state == 0 )
180 switch ( button )
182 case GLUT_LEFT_BUTTON: break;
183 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(true);
184 break;
185 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(true);
186 break;
188 mouseb |= 1 << button;
190 else if ( state == 1 )
192 switch ( button )
194 case GLUT_LEFT_BUTTON: break;
195 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(false);
196 break;
197 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(false);
198 break;
200 mouseb &= ~(1 << button);
202 lastx = x;
203 lasty = y;
206 void
207 vis(int visible)
209 if (visible == GLUT_VISIBLE)
211 glutIdleFunc(animate);
213 else
215 glutIdleFunc(NULL);
219 void key(unsigned char key, int x, int y)
221 switch ( key )
223 case 27:
224 OSG::FrameHandler::the()->shutdown();
226 root = NULL;
227 // file = NULL;
228 cam = NULL;
229 vp = NULL;
230 win = NULL;
231 cam_trans = NULL;
232 scene_trans = NULL;
233 pPoly = NULL;
234 pCOver = NULL;
235 gwin = NULL;
237 webInterface = NULL;
238 rentravact = NULL;
240 OSG::osgExit();
242 exit(0);
243 case 'v':
244 rentravact->setVolumeDrawing(!rentravact->getVolumeDrawing());
245 case 'a':
246 glDisable( GL_LIGHTING );
247 std::cerr << "Lighting disabled." << std::endl;
248 break;
249 case 's':
250 glEnable( GL_LIGHTING );
251 std::cerr << "Lighting enabled." << std::endl;
252 break;
253 case 'z':
254 pPoly->setFrontMode(GL_POINT);
255 pPoly->setBackMode(GL_POINT);
256 std::cerr << "PolygonMode: Point." << std::endl;
257 break;
258 case 'x':
259 pPoly->setFrontMode(GL_LINE);
260 pPoly->setBackMode(GL_LINE);
261 std::cerr << "PolygonMode: Line." << std::endl;
262 break;
263 case 'c':
264 pPoly->setFrontMode(GL_FILL);
265 pPoly->setBackMode(GL_FILL);
266 std::cerr << "PolygonMode: Fill." << std::endl;
267 break;
268 case 'p':
270 if(bPolyActive == true)
272 OSG_ASSERT(pCOver->subChunk(pPoly) == true);
273 bPolyActive = false;
275 else
277 OSG_ASSERT(pCOver->addChunk(pPoly) == true);
278 bPolyActive = true;
280 break;
282 case 'r':
284 std::cerr << "Sending ray through " << x << "," << y << std::endl;
285 OSG::Line l;
286 cam->calcViewRay( l, x, y, *vp );
287 std::cerr << "From " << l.getPosition() << ", dir "
288 << l.getDirection()
289 << std::endl;
291 break;
293 case ' ':
295 OSG::Matrix m;
296 OSG::Quaternion q;
297 OSG::Vec3f v;
299 q = oldq;
300 v = oldv;
302 oldq = tball.getRotation();
303 oldv = tball.getPosition();
305 move_obj = ! move_obj;
306 if ( move_obj )
308 puts("moving object");
309 // m = scene_trans->getSFMatrix()->getValue();
310 tball.setMode( OSG::Trackball::OSGCamera );
313 else
315 puts("moving camera");
316 // m = cam_trans->getSFMatrix()->getValue();
317 tball.setMode( OSG::Trackball::OSGObject );
320 // q.setValue(m);
321 tball.setStartPosition( v, true );
322 tball.setStartRotation( q, true );
324 // std::cout << q << std::endl;
325 // std::cout << v << std::endl;
327 // std::cout << " " << m[3][0] << " " << m[3][1] << " " << m[3][2] << std::endl;
330 break;
335 int init(int argc, char **argv)
337 OSG::osgInit(argc,argv);
339 OSG::setVBOUsageOnPropertyProtos(true);
341 // GLUT init
343 glutInit(&argc, argv);
344 glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
345 int winid = glutCreateWindow("OpenSG");
346 glutKeyboardFunc(key);
347 glutVisibilityFunc(vis);
348 glutReshapeFunc(reshape);
349 glutDisplayFunc(display);
350 glutMouseFunc(mouse);
351 glutMotionFunc(motion);
353 glutIdleFunc(display);
355 // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
357 // glEnable( GL_DEPTH_TEST );
358 // glEnable( GL_LIGHTING );
359 // glEnable( GL_LIGHT0 );
360 // glFrontFace(GL_CW);
361 // glEnable(GL_CULL_FACE);
363 // OSG
365 OSG::SceneFileHandler::the()->print();
367 // create the graph
369 // beacon for camera and light
370 OSG::NodeUnrecPtr b1n = OSG::Node::create();
371 OSG::GroupUnrecPtr b1 = OSG::Group::create();
373 b1n->setCore( b1 );
375 // transformation
376 OSG::NodeUnrecPtr t1n = OSG::Node::create();
377 OSG::TransformUnrecPtr t1 = OSG::Transform::create();
379 t1n->setCore( t1 );
380 t1n->addChild( b1n );
382 cam_trans = t1;
384 // light
386 OSG::NodeUnrecPtr dlight = OSG::Node::create();
387 OSG::DirectionalLightUnrecPtr dl = OSG::DirectionalLight::create();
389 dlight->setCore( dl );
390 // dlight->setCore( Group::create() );
392 dl->setAmbient( .0, .0, .0, 1 );
393 dl->setDiffuse( .8f, .8f, .8f, 1.f );
394 dl->setDirection(0,0,1);
395 dl->setBeacon( b1n);
397 // root
398 root = OSG::Node::create();
399 OSG::GroupUnrecPtr gr1 = OSG::Group::create();
401 root->setCore( gr1 );
402 root->addChild( t1n );
403 root->addChild( dlight );
405 // Load the file
407 OSG::NodeUnrecPtr file = NULL;
409 if(argc > 1)
410 file = OSG::SceneFileHandler::the()->read(argv[1], NULL, NULL);
412 if ( file == NULL )
414 std::cerr << "Couldn't load file, ignoring" << std::endl;
416 file = OSG::makeSphere(4, 2.0);
420 OSG::Thread::getCurrentChangeList()->commitChanges();
422 #if 0
423 OSG::GeometryPtr pGeo = cast_dynamic<OSG::GeometryPtr>(file->getCore());
425 if(pGeo == NULL && file->getNChildren() != 0)
427 pGeo = cast_dynamic<OSG::GeometryPtr>(file->getChild(0)->getCore());
430 if(pGeo == NULL)
432 fprintf(stderr, "no geo\n");
434 #endif
436 OSG::GraphOpRefPtr op = OSG::GraphOpFactory::the()->create("Stripe");
438 // op->traverse(file);
439 // createOptimizedPrimitives(pGeo);
440 // createSharedIndex(pGeo);
443 // file->dump();
444 file->updateVolume();
446 #if 0
447 const char *outFileName = "/tmp/foo1.osg";
449 OSG::IndentFileOutStream outFileStream(outFileName);
451 if( !outFileStream )
453 std::cerr << "Can not open output stream to file: "
454 << outFileName << std::endl;
455 return -1;
458 std::cerr << "STARTING PRINTOUT:" << std::endl;
459 OSG::OSGWriter writer( outFileStream, 4 );
461 writer.write( file );
463 outFileStream.close();
465 OSG::SceneFileHandler::the()->write(file, "/tmp/foo.osb");
466 #endif
469 // return 0;
472 OSG::Vec3f min,max;
473 file->getVolume().getBounds( min, max );
476 std::cout << "Volume: from " << min << " to " << max << std::endl;
479 // NodePtr pChunkOverNode = Node::create();
482 // pChunkOverNode->setCore(pCOver);
483 // pChunkOverNode->addChild(file);
485 OSG::MultiCoreUnrecPtr pMCore = OSG::MultiCore::create();
487 pCOver = OSG::ChunkOverrideGroup::create();
488 scene_trans = OSG::Transform::create();
490 pMCore->addCore(scene_trans);
491 pMCore->addCore(pCOver );
493 OSG::NodeUnrecPtr sceneTrN = OSG::Node::create();
495 sceneTrN->setCore(pMCore);
496 sceneTrN->addChild(file);
499 dlight->addChild(sceneTrN);
501 std::cerr << "Tree: " << std::endl;
502 // root->dump();
504 // Camera
506 cam = OSG::PerspectiveCamera::create();
508 cam->setBeacon( b1n );
509 cam->setFov( OSG::osgDegree2Rad( 90 ) );
510 cam->setNear( 0.1f );
511 cam->setFar( 10000 );
513 // Background
514 OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();
516 bkgnd->setColor(OSG::Color3f(0.1,0.1,0.1));
518 // Viewport
520 vp = OSG::Viewport::create();
522 vp->setCamera( cam );
523 vp->setBackground( bkgnd );
524 vp->setRoot( root );
525 vp->setSize( 0,0, 1,1 );
527 // vp->dump();
529 // Window
530 std::cout << "GLUT winid: " << winid << std::endl;
533 GLint glvp[4];
535 glGetIntegerv( GL_VIEWPORT, glvp );
537 gwin = OSG::GLUTWindow::create();
538 gwin->setGlutId(winid);
539 gwin->setSize( glvp[2], glvp[3] );
541 win = gwin;
543 win->addPort( vp );
545 win->init();
547 // Action
549 rentravact = OSG::RenderAction::create();
550 // renact->setFrustumCulling(false);
552 rentravact->setCorrectTwoSidedLighting(true);
554 // tball
556 OSG::Vec3f pos;
557 pos.setValues(min[0] + ((max[0] - min[0]) * 0.5),
558 min[1] + ((max[1] - min[1]) * 0.5),
559 max[2] + ( max[2] - min[2] ) * 1.5 );
561 float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;
563 OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
564 min[1] + (max[1] - min[1]) / 2,
565 min[2] + (max[2] - min[2]) / 2);
567 fprintf(stderr, "Startpos : %f %f %f\n", pos[0], pos[1], pos[2]);
569 tball.setMode( OSG::Trackball::OSGObject );
570 tball.setStartPosition( pos, true );
571 tball.setSum( true );
572 tball.setTranslationMode( OSG::Trackball::OSGFree );
573 tball.setTranslationScale(scale);
574 tball.setRotationCenter(tCenter);
576 fprintf(stderr, "%d\n",
577 OSG::MFUnrecNodePtr ::getClassType().getId());
578 fprintf(stderr, "%d\n",
579 OSG::MFUnrecFieldContainerPtr::getClassType().getId());
581 // MFNodePtr ::getClassType().dump();
582 // MFFieldContainerPtr::getClassType().dump();
584 // run...
586 pPoly = OSG::PolygonChunk::create();
588 pCOver->subChunk(pPoly);
590 #if 0
591 GroupNodePtr pGr = GroupNodePtr::create();
593 NodePtr pN;
594 GroupPtr pG;
596 fprintf(stderr, "A\n");
598 if(pGr == NULL)
602 fprintf(stderr, "B\n");
604 if(pGr == pN)
608 fprintf(stderr, "C\n");
610 if(pGr == pG)
614 pGr = NULL;
615 #endif
617 OSG::FrameHandler::the()->init();
620 webInterface = OSG::WebInterface::create();
621 webInterface->setRoot(root);
623 return 0;
626 int main (int argc, char **argv)
628 init(argc, argv);
630 glutMainLoop();
632 return 0;