fixed: gcc8 compile issues
[opensg.git] / Source / Contrib / VTK / testVTK.cpp
blobd006345872bc95e455354aef953e5d3a4f50b3ff
2 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
3 #pragma GCC diagnostic warning "-Wold-style-cast"
4 #endif
6 #include "OSGConfig.h"
8 #ifdef OSG_WITH_VTK
10 #include <iostream>
12 #include "OSGGLUT.h"
14 #include "OSGFieldContainerFactory.h"
15 #include "OSGVector.h"
16 #include "OSGQuaternion.h"
17 #include "OSGMatrix.h"
18 #include "OSGMatrixUtility.h"
19 #include "OSGBoxVolume.h"
20 #include "OSGNode.h"
21 #include "OSGGroup.h"
22 #include "OSGThread.h"
23 #include "OSGTransform.h"
24 #include "OSGAction.h"
25 #include "OSGRenderAction.h"
26 #include "OSGSimpleGeometry.h"
27 #include "OSGSceneFileHandler.h"
28 #include "OSGMaterialGroup.h"
29 #include "OSGSimpleMaterial.h"
31 #include "OSGDirectionalLight.h"
32 #include "OSGLine.h"
34 #include "OSGViewport.h"
35 #include "OSGCamera.h"
36 #include "OSGWindow.h"
37 #include "OSGGLUTWindow.h"
38 #include "OSGCamera.h"
39 #include "OSGPerspectiveCamera.h"
40 #include "OSGSolidBackground.h"
41 //#include "OSGUniformBackground.h"
42 #include "OSGVTKPolyDataMapper.h"
44 #include "OSGTrackball.h"
45 #include "OSGOSGWriter.h"
47 #include "vtkStructuredGridReader.h"
48 #include "vtkStructuredGrid.h"
49 #include "vtkPointData.h"
50 #include "vtkActor.h"
51 #include "vtkStructuredGridOutlineFilter.h"
52 #include "vtkPolyDataMapper.h"
53 #include "vtkProperty.h"
54 #include "vtkStructuredGridGeometryFilter.h"
55 #include "vtkPointSource.h"
56 #include "vtkRungeKutta4.h"
57 #include "vtkStreamLine.h"
58 #include "vtkTubeFilter.h"
59 #include "vtkPolyDataNormals.h"
62 OSG::RenderActionRefPtr renact;
64 OSG::NodeUnrecPtr root;
66 OSG::NodeUnrecPtr file;
68 OSG::PerspectiveCameraUnrecPtr cam;
69 OSG::ViewportUnrecPtr vp;
70 OSG::WindowUnrecPtr win;
72 OSG::TransformUnrecPtr cam_trans;
73 OSG::TransformUnrecPtr scene_trans;
75 OSG::Trackball tball;
77 bool move_obj = false;
79 int mouseb = 0;
80 int lastx=0, lasty=0;
82 OSG::Quaternion oldq;
83 OSG::Vec3f oldv;
85 void
86 display(void)
88 OSG::Matrix m1, m2, m3;
89 OSG::Quaternion q1;
91 tball.getRotation().getValue(m3);
93 q1.setValue(m3);
95 m1.setRotate(q1);
97 // std::cout << "TBROT" << std::endl << tball.getRotation() << endl;
98 // std::cout << "M3" << std::endl << m3 << std::endl;
99 // std::cout << "Q1" << std::endl << q1 << std::endl;
100 // std::cout << "M1" << std::endl << m1 << std::endl;
102 // m1.setRotate( tball.getRotation() );
103 m2.setTranslate( tball.getPosition() );
105 //std::cout << "Pos: " << tball.getPosition() << ", Rot: " << tball.getRotation() << std::endl;
107 // std::cout << tball.getRotation() << std::endl;
109 m1.mult( m2 );
111 // std::cerr << m1 << std::endl;
114 if(move_obj == true)
116 scene_trans->editSFMatrix()->setValue( m1 );
118 else
120 cam_trans->editSFMatrix()->setValue( m1 );
123 OSG::Thread::getCurrentChangeList()->commitChanges();
125 // win->draw( ract );
126 win->render(renact);
129 void reshape( int w, int h )
131 std::cerr << "Reshape: " << w << "," << h << std::endl;
132 win->resize( w, h );
136 void
137 animate(void)
139 glutPostRedisplay();
142 // tballall stuff
145 void
146 motion(int x, int y)
148 OSG::Real32 w = win->getWidth(), h = win->getHeight();
151 OSG::Real32 a = -2. * ( lastx / w - .5 ),
152 b = -2. * ( .5 - lasty / h ),
153 c = -2. * ( x / w - .5 ),
154 d = -2. * ( .5 - y / h );
156 if ( mouseb & ( 1 << GLUT_LEFT_BUTTON ) )
158 tball.updateRotation( a, b, c, d );
160 else if ( mouseb & ( 1 << GLUT_MIDDLE_BUTTON ) )
162 tball.updatePosition( a, b, c, d );
164 else if ( mouseb & ( 1 << GLUT_RIGHT_BUTTON ) )
166 tball.updatePositionNeg( a, b, c, d );
168 lastx = x;
169 lasty = y;
172 void
173 mouse(int button, int state, int x, int y)
175 if ( state == 0 )
177 switch ( button )
179 case GLUT_LEFT_BUTTON: break;
180 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(true);
181 break;
182 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(true);
183 break;
185 mouseb |= 1 << button;
187 else if ( state == 1 )
189 switch ( button )
191 case GLUT_LEFT_BUTTON: break;
192 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(false);
193 break;
194 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(false);
195 break;
197 mouseb &= ~(1 << button);
199 lastx = x;
200 lasty = y;
203 void
204 vis(int visible)
206 if (visible == GLUT_VISIBLE)
208 glutIdleFunc(animate);
210 else
212 glutIdleFunc(NULL);
216 void key(unsigned char key, int x, int y)
218 switch ( key )
220 case 27:
221 root = NULL;
222 file = NULL;
223 cam = NULL;
224 vp = NULL;
225 win = NULL;
226 cam_trans = NULL;
227 scene_trans = NULL;
228 OSG::osgExit();
229 exit(0);
230 case 'a': glDisable( GL_LIGHTING );
231 std::cerr << "Lighting disabled." << std::endl;
232 break;
233 case 's': glEnable( GL_LIGHTING );
234 std::cerr << "Lighting enabled." << std::endl;
235 break;
236 case 'z': glPolygonMode( GL_FRONT_AND_BACK, GL_POINT);
237 std::cerr << "PolygonMode: Point." << std::endl;
238 break;
239 case 'x': glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
240 std::cerr << "PolygonMode: Line." << std::endl;
241 break;
242 case 'c': glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
243 std::cerr << "PolygonMode: Fill." << std::endl;
244 break;
245 case 'r':
247 std::cerr << "Sending ray through " << x << "," << y << std::endl;
248 OSG::Line l;
249 cam->calcViewRay( l, x, y, *vp );
250 std::cerr << "From " << l.getPosition() << ", dir "
251 << l.getDirection()
252 << std::endl;
254 break;
256 case ' ':
258 OSG::Matrix m;
259 OSG::Quaternion q;
260 OSG::Vec3f v;
262 q = oldq;
263 v = oldv;
265 oldq = tball.getRotation();
266 oldv = tball.getPosition();
268 move_obj = ! move_obj;
269 if ( move_obj )
271 puts("moving object");
272 // m = scene_trans->getSFMatrix()->getValue();
273 tball.setMode( OSG::Trackball::OSGCamera );
276 else
278 puts("moving camera");
279 // m = cam_trans->getSFMatrix()->getValue();
280 tball.setMode( OSG::Trackball::OSGObject );
283 // q.setValue(m);
284 tball.setStartPosition( v, true );
285 tball.setStartRotation( q, true );
287 // std::cout << q << std::endl;
288 // std::cout << v << std::endl;
290 // std::cout << " " << m[3][0] << " " << m[3][1] << " " << m[3][2] << std::endl;
293 break;
297 void addActor(OSG::Node *pRoot,
298 vtkActor *pActor)
300 OSG::NodeUnrecPtr pTmpNode = OSG::Node ::create();
301 OSG::VTKPolyDataMapperUnrecPtr pTmpMapper =
302 OSG::VTKPolyDataMapper::create();
304 pTmpMapper->setActor(pActor );
305 pTmpNode ->setCore (pTmpMapper);
306 pRoot ->addChild(pTmpNode );
308 // pTmpMapper->execute();
311 OSG::NodeTransitPtr initVTK(void)
313 OSG::NodeUnrecPtr returnValue = NULL;
315 OSG::Char8 *szDataRoot = getenv("VTK_DATA_ROOT");
317 if(szDataRoot == NULL)
319 fprintf(stderr, "VTK_DATA_ROOT not set\n");
320 exit(0);
323 std::string szFilename;
325 szFilename.assign(szDataRoot);
326 szFilename += "/Data/office.binary.vtk";
328 vtkStructuredGridReader *reader = vtkStructuredGridReader::New();
330 reader->SetFileName(szFilename.c_str());
331 reader->Update();
333 // OSG::Real64 length = reader->GetOutput()->GetLength();
335 // OSG::Real64 maxVelocity =
336 // reader->GetOutput()->GetPointData()->GetVectors()->GetMaxNorm();
338 // OSG::Real64 maxTime = 35.0 * length / maxVelocity;
342 returnValue = OSG::Node::create();
344 returnValue->setCore(OSG::Group::create());
346 // Create source for streamtubes
347 vtkPointSource *seeds = vtkPointSource::New();
348 seeds->SetRadius(0.15);
349 seeds->SetCenter(0.1, 2.1, 0.5);
350 seeds->SetNumberOfPoints(6);
352 vtkRungeKutta4 *integ = vtkRungeKutta4::New();
353 vtkStreamLine *streamer = vtkStreamLine::New();
354 streamer->SetInputConnection(reader->GetOutputPort());
355 #if VTK_MAJOR_VERSION >= 6
356 streamer->SetSourceData(seeds->GetOutput());
357 #else
358 streamer->SetSource(seeds->GetOutput());
359 #endif
360 streamer->SetMaximumPropagationTime(500);
361 streamer->SetStepLength(0.5);
362 streamer->SetIntegrationStepLength(0.05);
363 streamer->SetIntegrationDirectionToIntegrateBothDirections();
364 streamer->SetIntegrator(integ);
366 // The tube is wrapped around the generated streamline. By varying the
367 // radius by the inverse of vector magnitude, we are creating a tube
368 // whose radius is proportional to mass flux (in incompressible flow).
369 vtkTubeFilter *streamTube = vtkTubeFilter::New();
370 streamTube->SetInputConnection(streamer->GetOutputPort());
371 streamTube->SetRadius(0.02);
372 streamTube->SetNumberOfSides(12);
373 streamTube->SetVaryRadiusToVaryRadiusByVector();
374 vtkPolyDataMapper *mapStreamTube = vtkPolyDataMapper::New();
375 mapStreamTube->SetInputConnection(streamTube->GetOutputPort());
376 mapStreamTube->SetScalarRange(
377 reader->GetOutput()->GetPointData()->GetScalars()->GetRange());
378 vtkActor *streamTubeActor = vtkActor::New();
379 streamTubeActor->SetMapper(mapStreamTube);
380 streamTubeActor->GetProperty()->BackfaceCullingOn();
382 addActor(returnValue, streamTubeActor);
385 vtkStructuredGridGeometryFilter *table1 =
386 vtkStructuredGridGeometryFilter::New();
388 table1->SetInputConnection(reader->GetOutputPort());
389 table1->SetExtent(11, 15, 7, 9, 8, 8);
391 vtkPolyDataNormals *normTable1 = vtkPolyDataNormals::New();
392 normTable1->SetInputConnection(table1->GetOutputPort());
394 vtkPolyDataMapper *mapTable1 = vtkPolyDataMapper::New();
395 mapTable1->SetInputConnection(normTable1->GetOutputPort());
396 mapTable1->ScalarVisibilityOff();
398 vtkActor *table1Actor = vtkActor::New();
399 table1Actor->SetMapper(mapTable1);
400 table1Actor->GetProperty()->SetColor(.59, .427, .392);
402 addActor(returnValue, table1Actor);
406 vtkStructuredGridGeometryFilter *table2 =
407 vtkStructuredGridGeometryFilter::New();
408 table2->SetInputConnection(reader->GetOutputPort());
409 table2->SetExtent(11, 15, 10, 12, 8, 8);
411 vtkPolyDataNormals *normTable2 = vtkPolyDataNormals::New();
412 normTable2->SetInputConnection(table2->GetOutputPort());
414 vtkPolyDataMapper *mapTable2 = vtkPolyDataMapper::New();
415 mapTable2->SetInputConnection(normTable2->GetOutputPort());
416 mapTable2->ScalarVisibilityOff();
418 vtkActor *table2Actor = vtkActor::New();
419 table2Actor->SetMapper(mapTable2);
420 table2Actor->GetProperty()->SetColor(.59, .427, .392);
422 addActor(returnValue, table2Actor);
426 vtkStructuredGridGeometryFilter *FilingCabinet1 =
427 vtkStructuredGridGeometryFilter::New();
429 FilingCabinet1->SetInputConnection(reader->GetOutputPort());
430 FilingCabinet1->SetExtent(15, 15, 7, 9, 0, 8);
432 vtkPolyDataNormals *normFilingCabinet1 = vtkPolyDataNormals::New();
433 normFilingCabinet1->SetInputConnection(FilingCabinet1->GetOutputPort());
435 vtkPolyDataMapper *mapFilingCabinet1 = vtkPolyDataMapper::New();
436 mapFilingCabinet1->SetInputConnection(normFilingCabinet1->GetOutputPort());
437 mapFilingCabinet1->ScalarVisibilityOff();
439 vtkActor *FilingCabinet1Actor = vtkActor::New();
440 FilingCabinet1Actor->SetMapper(mapFilingCabinet1);
441 FilingCabinet1Actor->GetProperty()->SetColor(.8, .8, .6);
443 addActor(returnValue, FilingCabinet1Actor);
447 vtkStructuredGridGeometryFilter *FilingCabinet2 =
448 vtkStructuredGridGeometryFilter::New();
449 FilingCabinet2->SetInputConnection(reader->GetOutputPort());
450 FilingCabinet2->SetExtent(15, 15, 10, 12, 0, 8);
452 vtkPolyDataNormals *normFilingCabinet2 = vtkPolyDataNormals::New();
453 normFilingCabinet2->SetInputConnection(FilingCabinet2->GetOutputPort());
455 vtkPolyDataMapper *mapFilingCabinet2 = vtkPolyDataMapper::New();
456 mapFilingCabinet2->SetInputConnection(normFilingCabinet2->GetOutputPort());
457 mapFilingCabinet2->ScalarVisibilityOff();
459 vtkActor *FilingCabinet2Actor = vtkActor::New();
460 FilingCabinet2Actor->SetMapper(mapFilingCabinet2);
461 FilingCabinet2Actor->GetProperty()->SetColor(.8, .8, .6);
463 addActor(returnValue, FilingCabinet2Actor);
467 vtkStructuredGridGeometryFilter *bookshelf1Top =
468 vtkStructuredGridGeometryFilter::New();
469 bookshelf1Top->SetInputConnection(reader->GetOutputPort());
470 bookshelf1Top->SetExtent(13, 13, 0, 4, 0, 11);
472 vtkPolyDataNormals *normbookshelf1Top = vtkPolyDataNormals::New();
473 normbookshelf1Top->SetInputConnection(bookshelf1Top->GetOutputPort());
475 vtkPolyDataMapper *mapBookshelf1Top = vtkPolyDataMapper::New();
476 mapBookshelf1Top->SetInputConnection(normbookshelf1Top->GetOutputPort());
477 mapBookshelf1Top->ScalarVisibilityOff();
479 vtkActor *bookshelf1TopActor = vtkActor::New();
480 bookshelf1TopActor->SetMapper(mapBookshelf1Top);
481 bookshelf1TopActor->GetProperty()->SetColor(.8, .8, .6);
483 addActor(returnValue, bookshelf1TopActor);
487 vtkStructuredGridGeometryFilter *bookshelf1Bottom =
488 vtkStructuredGridGeometryFilter::New();
489 bookshelf1Bottom->SetInputConnection(reader->GetOutputPort());
490 bookshelf1Bottom->SetExtent(20, 20, 0, 4, 0, 11);
492 vtkPolyDataNormals *normbookshelf1Bottom = vtkPolyDataNormals::New();
493 normbookshelf1Bottom->SetInputConnection(
494 bookshelf1Bottom->GetOutputPort());
496 vtkPolyDataMapper *mapBookshelf1Bottom = vtkPolyDataMapper::New();
497 mapBookshelf1Bottom->SetInputConnection(
498 normbookshelf1Bottom->GetOutputPort());
499 mapBookshelf1Bottom->ScalarVisibilityOff();
501 vtkActor *bookshelf1BottomActor = vtkActor::New();
502 bookshelf1BottomActor->SetMapper(mapBookshelf1Bottom);
503 bookshelf1BottomActor->GetProperty()->SetColor(.8, .8, .6);
505 addActor(returnValue, bookshelf1BottomActor);
509 vtkStructuredGridGeometryFilter *bookshelf1Front =
510 vtkStructuredGridGeometryFilter::New();
511 bookshelf1Front->SetInputConnection(reader->GetOutputPort());
512 bookshelf1Front->SetExtent(13, 20, 0, 0, 0, 11);
514 vtkPolyDataNormals *normbookshelf1Front = vtkPolyDataNormals::New();
515 normbookshelf1Front->SetInputConnection(bookshelf1Front->GetOutputPort());
517 vtkPolyDataMapper *mapBookshelf1Front = vtkPolyDataMapper::New();
518 mapBookshelf1Front->SetInputConnection(
519 normbookshelf1Front->GetOutputPort());
520 mapBookshelf1Front->ScalarVisibilityOff();
522 vtkActor *bookshelf1FrontActor = vtkActor::New();
523 bookshelf1FrontActor->SetMapper(mapBookshelf1Front);
524 bookshelf1FrontActor->GetProperty()->SetColor(.8, .8, .6);
526 addActor(returnValue, bookshelf1FrontActor);
530 vtkStructuredGridGeometryFilter *bookshelf1Back =
531 vtkStructuredGridGeometryFilter::New();
532 bookshelf1Back->SetInputConnection(reader->GetOutputPort());
533 bookshelf1Back->SetExtent(13, 20, 4, 4, 0, 11);
535 vtkPolyDataNormals *normbookshelf1Back = vtkPolyDataNormals::New();
536 normbookshelf1Back->SetInputConnection(bookshelf1Back->GetOutputPort());
538 vtkPolyDataMapper *mapBookshelf1Back = vtkPolyDataMapper::New();
539 mapBookshelf1Back->SetInputConnection(normbookshelf1Back->GetOutputPort());
540 mapBookshelf1Back->ScalarVisibilityOff();
542 vtkActor *bookshelf1BackActor = vtkActor::New();
543 bookshelf1BackActor->SetMapper(mapBookshelf1Back);
544 bookshelf1BackActor->GetProperty()->SetColor(.8, .8, .6);
546 addActor(returnValue, bookshelf1BackActor);
550 vtkStructuredGridGeometryFilter *bookshelf1LHS =
551 vtkStructuredGridGeometryFilter::New();
552 bookshelf1LHS->SetInputConnection(reader->GetOutputPort());
553 bookshelf1LHS->SetExtent(13, 20, 0, 4, 0, 0);
555 vtkPolyDataNormals *normbookshelf1LHS = vtkPolyDataNormals::New();
556 normbookshelf1LHS->SetInputConnection(bookshelf1LHS->GetOutputPort());
558 vtkPolyDataMapper *mapBookshelf1LHS = vtkPolyDataMapper::New();
559 mapBookshelf1LHS->SetInputConnection(normbookshelf1LHS->GetOutputPort());
560 mapBookshelf1LHS->ScalarVisibilityOff();
562 vtkActor *bookshelf1LHSActor = vtkActor::New();
563 bookshelf1LHSActor->SetMapper(mapBookshelf1LHS);
564 bookshelf1LHSActor->GetProperty()->SetColor(.8, .8, .6);
566 addActor(returnValue, bookshelf1LHSActor);
570 vtkStructuredGridGeometryFilter *bookshelf1RHS =
571 vtkStructuredGridGeometryFilter::New();
572 bookshelf1RHS->SetInputConnection(reader->GetOutputPort());
573 bookshelf1RHS->SetExtent(13, 20, 0, 4, 11, 11);
575 vtkPolyDataNormals *normbookshelf1RHS = vtkPolyDataNormals::New();
576 normbookshelf1RHS->SetInputConnection(bookshelf1RHS->GetOutputPort());
578 vtkPolyDataMapper *mapBookshelf1RHS = vtkPolyDataMapper::New();
579 mapBookshelf1RHS->SetInputConnection(normbookshelf1RHS->GetOutputPort());
580 mapBookshelf1RHS->ScalarVisibilityOff();
582 vtkActor *bookshelf1RHSActor = vtkActor::New();
583 bookshelf1RHSActor->SetMapper(mapBookshelf1RHS);
584 bookshelf1RHSActor->GetProperty()->SetColor(.8, .8, .6);
586 addActor(returnValue, bookshelf1RHSActor);
590 vtkStructuredGridGeometryFilter *bookshelf2Top =
591 vtkStructuredGridGeometryFilter::New();
592 bookshelf2Top->SetInputConnection(reader->GetOutputPort());
593 bookshelf2Top->SetExtent(13, 13, 15, 19, 0, 11);
595 vtkPolyDataNormals *normbookshelf2Top = vtkPolyDataNormals::New();
596 normbookshelf2Top->SetInputConnection(bookshelf2Top->GetOutputPort());
598 vtkPolyDataMapper *mapBookshelf2Top = vtkPolyDataMapper::New();
599 mapBookshelf2Top->SetInputConnection(normbookshelf2Top->GetOutputPort());
600 mapBookshelf2Top->ScalarVisibilityOff();
602 vtkActor *bookshelf2TopActor = vtkActor::New();
603 bookshelf2TopActor->SetMapper(mapBookshelf2Top);
604 bookshelf2TopActor->GetProperty()->SetColor(.8, .8, .6);
606 addActor(returnValue, bookshelf2TopActor);
609 vtkStructuredGridGeometryFilter *bookshelf2Bottom =
610 vtkStructuredGridGeometryFilter::New();
611 bookshelf2Bottom->SetInputConnection(reader->GetOutputPort());
612 bookshelf2Bottom->SetExtent(20, 20, 15, 19, 0, 11);
614 vtkPolyDataNormals *normbookshelf2Bottom = vtkPolyDataNormals::New();
615 normbookshelf2Bottom->SetInputConnection(
616 bookshelf2Bottom->GetOutputPort());
618 vtkPolyDataMapper *mapBookshelf2Bottom = vtkPolyDataMapper::New();
619 mapBookshelf2Bottom->SetInputConnection(
620 normbookshelf2Bottom->GetOutputPort());
621 mapBookshelf2Bottom->ScalarVisibilityOff();
623 vtkActor *bookshelf2BottomActor = vtkActor::New();
624 bookshelf2BottomActor->SetMapper(mapBookshelf2Bottom);
625 bookshelf2BottomActor->GetProperty()->SetColor(.8, .8, .6);
627 addActor(returnValue, bookshelf2BottomActor);
631 vtkStructuredGridGeometryFilter *bookshelf2Front =
632 vtkStructuredGridGeometryFilter::New();
633 bookshelf2Front->SetInputConnection(reader->GetOutputPort());
634 bookshelf2Front->SetExtent(13, 20, 15, 15, 0, 11);
636 vtkPolyDataNormals *normbookshelf2Front = vtkPolyDataNormals::New();
637 normbookshelf2Front->SetInputConnection(bookshelf2Front->GetOutputPort());
639 vtkPolyDataMapper *mapBookshelf2Front = vtkPolyDataMapper::New();
640 mapBookshelf2Front->SetInputConnection(
641 normbookshelf2Front->GetOutputPort());
642 mapBookshelf2Front->ScalarVisibilityOff();
644 vtkActor *bookshelf2FrontActor = vtkActor::New();
645 bookshelf2FrontActor->SetMapper(mapBookshelf2Front);
646 bookshelf2FrontActor->GetProperty()->SetColor(.8, .8, .6);
648 addActor(returnValue, bookshelf2FrontActor);
652 vtkStructuredGridGeometryFilter *bookshelf2Back =
653 vtkStructuredGridGeometryFilter::New();
654 bookshelf2Back->SetInputConnection(reader->GetOutputPort());
655 bookshelf2Back->SetExtent(13, 20, 19, 19, 0, 11);
657 vtkPolyDataNormals *normbookshelf2Back = vtkPolyDataNormals::New();
658 normbookshelf2Back->SetInputConnection(bookshelf2Back->GetOutputPort());
660 vtkPolyDataMapper *mapBookshelf2Back = vtkPolyDataMapper::New();
661 mapBookshelf2Back->SetInputConnection(normbookshelf2Back->GetOutputPort());
662 mapBookshelf2Back->ScalarVisibilityOff();
664 vtkActor *bookshelf2BackActor = vtkActor::New();
665 bookshelf2BackActor->SetMapper(mapBookshelf2Back);
666 bookshelf2BackActor->GetProperty()->SetColor(.8, .8, .6);
668 addActor(returnValue, bookshelf2BackActor);
672 vtkStructuredGridGeometryFilter *bookshelf2LHS =
673 vtkStructuredGridGeometryFilter::New();
674 bookshelf2LHS->SetInputConnection(reader->GetOutputPort());
675 bookshelf2LHS->SetExtent(13, 20, 15, 19, 0, 0);
677 vtkPolyDataNormals *normbookshelf2LHS = vtkPolyDataNormals::New();
678 normbookshelf2LHS->SetInputConnection(bookshelf2LHS->GetOutputPort());
680 vtkPolyDataMapper *mapBookshelf2LHS = vtkPolyDataMapper::New();
681 mapBookshelf2LHS->SetInputConnection(normbookshelf2LHS->GetOutputPort());
682 mapBookshelf2LHS->ScalarVisibilityOff();
683 vtkActor *bookshelf2LHSActor = vtkActor::New();
684 bookshelf2LHSActor->SetMapper(mapBookshelf2LHS);
685 bookshelf2LHSActor->GetProperty()->SetColor(.8, .8, .6);
687 addActor(returnValue, bookshelf2LHSActor);
691 vtkStructuredGridGeometryFilter *bookshelf2RHS =
692 vtkStructuredGridGeometryFilter::New();
693 bookshelf2RHS->SetInputConnection(reader->GetOutputPort());
694 bookshelf2RHS->SetExtent(13, 20, 15, 19, 11, 11);
696 vtkPolyDataNormals *normbookshelf2RHS = vtkPolyDataNormals::New();
697 normbookshelf2RHS->SetInputConnection(bookshelf2RHS->GetOutputPort());
699 vtkPolyDataMapper *mapBookshelf2RHS = vtkPolyDataMapper::New();
700 mapBookshelf2RHS->SetInputConnection(normbookshelf2RHS->GetOutputPort());
701 mapBookshelf2RHS->ScalarVisibilityOff();
703 vtkActor *bookshelf2RHSActor = vtkActor::New();
704 bookshelf2RHSActor->SetMapper(mapBookshelf2RHS);
705 bookshelf2RHSActor->GetProperty()->SetColor(.8, .8, .6);
707 addActor(returnValue, bookshelf2RHSActor);
711 vtkStructuredGridGeometryFilter *window =
712 vtkStructuredGridGeometryFilter::New();
713 window->SetInputConnection(reader->GetOutputPort());
714 window->SetExtent(20, 20, 6, 13, 10, 13);
716 vtkPolyDataNormals *normWindow = vtkPolyDataNormals::New();
717 normWindow->SetInputConnection(window->GetOutputPort());
719 vtkPolyDataMapper *mapWindow = vtkPolyDataMapper::New();
720 mapWindow->SetInputConnection(normWindow->GetOutputPort());
721 mapWindow->ScalarVisibilityOff();
723 vtkActor *windowActor = vtkActor::New();
724 windowActor->SetMapper(mapWindow);
725 windowActor->GetProperty()->SetColor(.3, .3, .5);
727 addActor(returnValue, windowActor);
731 vtkStructuredGridGeometryFilter *outlet =
732 vtkStructuredGridGeometryFilter::New();
733 outlet->SetInputConnection(reader->GetOutputPort());
734 outlet->SetExtent(0, 0, 9, 10, 14, 16);
736 vtkPolyDataNormals *normoutlet = vtkPolyDataNormals::New();
737 normoutlet->SetInputConnection(outlet->GetOutputPort());
739 vtkPolyDataMapper *mapOutlet = vtkPolyDataMapper::New();
740 mapOutlet->SetInputConnection(normoutlet->GetOutputPort());
741 mapOutlet->ScalarVisibilityOff();
742 vtkActor *outletActor = vtkActor::New();
743 outletActor->SetMapper(mapOutlet);
744 outletActor->GetProperty()->SetColor(0, 0, 0);
746 addActor(returnValue, outletActor);
750 vtkStructuredGridGeometryFilter *inlet =
751 vtkStructuredGridGeometryFilter::New();
752 inlet->SetInputConnection(reader->GetOutputPort());
753 inlet->SetExtent(0, 0, 9, 10, 0, 6);
755 vtkPolyDataNormals *norminlet = vtkPolyDataNormals::New();
756 norminlet->SetInputConnection(inlet->GetOutputPort());
758 vtkPolyDataMapper *mapInlet = vtkPolyDataMapper::New();
759 mapInlet->SetInputConnection(norminlet->GetOutputPort());
761 mapInlet->ScalarVisibilityOff();
762 vtkActor *inletActor = vtkActor::New();
763 inletActor->SetMapper(mapInlet);
764 inletActor->GetProperty()->SetColor(0, 0, 0);
768 vtkStructuredGridOutlineFilter *outline =
769 vtkStructuredGridOutlineFilter::New();
771 outline->SetInputConnection(reader->GetOutputPort());
773 vtkPolyDataMapper *mapOutline = vtkPolyDataMapper::New();
774 mapOutline->SetInputConnection(outline->GetOutputPort());
776 vtkActor *outlineActor = vtkActor::New();
778 outlineActor->SetMapper(mapOutline);
779 outlineActor->GetProperty()->SetColor(0, 0, 0);
781 addActor(returnValue, outlineActor);
783 return OSG::NodeTransitPtr(returnValue);
786 int doMain (int argc, char **argv)
788 OSG::osgInit(argc,argv);
790 // GLUT init
792 glutInit(&argc, argv);
793 glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
794 int winid = glutCreateWindow("OpenSG");
795 glutKeyboardFunc(key);
796 glutVisibilityFunc(vis);
797 glutReshapeFunc(reshape);
798 glutDisplayFunc(display);
799 glutMouseFunc(mouse);
800 glutMotionFunc(motion);
802 glutIdleFunc(display);
804 // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
806 glEnable( GL_DEPTH_TEST );
807 glEnable( GL_LIGHTING );
808 glEnable( GL_LIGHT0 );
810 // OSG
812 OSG::SceneFileHandler::the()->print();
814 // create the graph
816 // beacon for camera and light
817 OSG::NodeUnrecPtr b1n = OSG::Node::create();
818 OSG::GroupUnrecPtr b1 = OSG::Group::create();
819 b1n->setCore( b1 );
821 // transformation
822 OSG::NodeUnrecPtr t1n = OSG::Node::create();
823 OSG::TransformUnrecPtr t1 = OSG::Transform::create();
824 t1n->setCore( t1 );
825 t1n->addChild( b1n );
827 cam_trans = t1;
829 // light
831 OSG::NodeUnrecPtr dlight = OSG::Node::create();
832 OSG::DirectionalLightUnrecPtr dl = OSG::DirectionalLight::create();
834 dlight->setCore( dl );
836 dl->setAmbient( .3, .3, .3, 1 );
837 dl->setDiffuse( 1, 1, 1, 1 );
838 dl->setDirection(0,0,1);
839 dl->setBeacon( b1n);
841 // root
842 root = OSG::Node::create();
843 OSG::GroupUnrecPtr gr1 = OSG::Group::create();
844 root->setCore( gr1 );
845 root->addChild( t1n );
846 root->addChild( dlight );
848 // Load the file
850 OSG::NodeUnrecPtr file = NULL;
851 OSG::NodeUnrecPtr file1 = NULL;
853 if ( argc > 1 )
854 file1 = OSG::SceneFileHandler::the()->read(argv[1]);
856 if ( file1 == NULL )
858 std::cerr << "Couldn't load file, ignoring" << std::endl;
859 file1 = initVTK();
862 OSG::Thread::getCurrentChangeList()->commitChanges();
863 file1->updateVolume();
865 #if 0
866 OSG::IndentFileOutStream outFileStream("/tmp/vsc_out.osg");
868 if(outFileStream)
870 std::cerr << "STARTING PRINTOUT:" << std::endl;
871 OSG::OSGWriter writer(outFileStream, 4);
872 writer.write(file1);
873 outFileStream.close();
875 #endif
877 // file->dump();
879 // return 0;
882 OSG::Vec3f min,max;
883 file1->getVolume().getBounds( min, max );
885 std::cout << "Volume: from " << min << " to " << max << std::endl;
888 file = OSG::Node::create();
889 OSG::MaterialGroupUnrecPtr testMat = OSG::MaterialGroup::create();
891 OSG::SimpleMaterialUnrecPtr defaultMaterial = OSG::SimpleMaterial::create();
893 defaultMaterial->setDiffuse(OSG::Color3f(1,.0,.0));
894 defaultMaterial->setAmbient(OSG::Color3f(0.1,0.1,0.1));
895 defaultMaterial->setSpecular(OSG::Color3f(1,1,1));
896 defaultMaterial->setShininess(20);
899 testMat->setMaterial(defaultMaterial);
901 file->setCore(OSG::Group::create());
902 file->addChild(file1);
904 scene_trans = OSG::Transform::create();
905 OSG::NodeUnrecPtr sceneTrN = OSG::Node::create();
907 sceneTrN->setCore(scene_trans);
908 sceneTrN->addChild(file);
909 // sceneTrN->addChild(makeTorus( .5, 2, 16, 16 ));
911 dlight->addChild(sceneTrN);
913 std::cerr << "Tree: " << std::endl;
914 // root->dump();
916 // Camera
918 cam = OSG::PerspectiveCamera::create();
919 cam->setBeacon( b1n );
920 cam->setFov( OSG::osgDegree2Rad( 90 ) );
921 cam->setNear( 0.1 );
922 cam->setFar( 100000 );
924 // Background
925 OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();
926 bkgnd->setColor(OSG::Color3f(1,1,1));
928 // Viewport
930 vp = OSG::Viewport::create();
931 vp->setCamera( cam );
932 vp->setBackground( bkgnd );
933 vp->setRoot( root );
934 vp->setSize( 0,0, 1,1 );
936 // Window
937 std::cout << "GLUT winid: " << winid << std::endl;
939 OSG::GLUTWindowUnrecPtr gwin;
941 GLint glvp[4];
942 glGetIntegerv( GL_VIEWPORT, glvp );
944 gwin = OSG::GLUTWindow::create();
945 gwin->setGlutId(winid);
946 gwin->setSize( glvp[2], glvp[3] );
948 win = gwin;
950 win->addPort( vp );
952 win->init();
954 // Action
956 renact = OSG::RenderAction::create();
958 // tball
960 OSG::Vec3f pos;
961 pos.setValues(min[0] + ((max[0] - min[0]) * 0.5),
962 min[1] + ((max[1] - min[1]) * 0.5),
963 max[2] + ( max[2] - min[2] ) * 1.5 );
965 float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;
967 OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
968 min[1] + (max[1] - min[1]) / 2,
969 min[2] + (max[2] - min[2]) / 2);
971 tball.setMode( OSG::Trackball::OSGObject );
972 tball.setStartPosition( pos, true );
973 tball.setSum( true );
974 tball.setTranslationMode( OSG::Trackball::OSGFree );
975 tball.setTranslationScale(scale);
976 tball.setRotationCenter(tCenter);
978 return 0;
981 int main (int argc, char **argv)
983 doMain(argc, argv);
985 // run...
986 glutMainLoop();
988 return 0;
991 #else
993 int main (int argc, char **argv)
995 return 0;
998 #endif