| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include <iostream> |
|---|
| 7 | #ifdef WIN32 |
|---|
| 8 | #include <windows.h> |
|---|
| 9 | #endif |
|---|
| 10 | #ifdef __APPLE__ |
|---|
| 11 | # include <GLUT/glut.h> |
|---|
| 12 | #else |
|---|
| 13 | # include <GL/glut.h> |
|---|
| 14 | #endif |
|---|
| 15 | |
|---|
| 16 | #include <osgViewer/Viewer> |
|---|
| 17 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 18 | #include <osgGA/TrackballManipulator> |
|---|
| 19 | #include <osgDB/ReadFile> |
|---|
| 20 | |
|---|
| 21 | osg::ref_ptr<osgViewer::Viewer> viewer; |
|---|
| 22 | osg::observer_ptr<osgViewer::GraphicsWindow> window; |
|---|
| 23 | |
|---|
| 24 | void display(void) |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | if (viewer.valid()) viewer->frame(); |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | glutSwapBuffers(); |
|---|
| 31 | glutPostRedisplay(); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | void reshape( int w, int h ) |
|---|
| 35 | { |
|---|
| 36 | |
|---|
| 37 | if (window.valid()) |
|---|
| 38 | { |
|---|
| 39 | window->resized(window->getTraits()->x, window->getTraits()->y, w, h); |
|---|
| 40 | window->getEventQueue()->windowResize(window->getTraits()->x, window->getTraits()->y, w, h ); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void mousebutton( int button, int state, int x, int y ) |
|---|
| 45 | { |
|---|
| 46 | if (window.valid()) |
|---|
| 47 | { |
|---|
| 48 | if (state==0) window->getEventQueue()->mouseButtonPress( x, y, button+1 ); |
|---|
| 49 | else window->getEventQueue()->mouseButtonRelease( x, y, button+1 ); |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void mousemove( int x, int y ) |
|---|
| 54 | { |
|---|
| 55 | if (window.valid()) |
|---|
| 56 | { |
|---|
| 57 | window->getEventQueue()->mouseMotion( x, y ); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void keyboard( unsigned char key, int , int ) |
|---|
| 62 | { |
|---|
| 63 | switch( key ) |
|---|
| 64 | { |
|---|
| 65 | case 27: |
|---|
| 66 | |
|---|
| 67 | if (viewer.valid()) viewer = 0; |
|---|
| 68 | glutDestroyWindow(glutGetWindow()); |
|---|
| 69 | break; |
|---|
| 70 | default: |
|---|
| 71 | if (window.valid()) |
|---|
| 72 | { |
|---|
| 73 | window->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key ); |
|---|
| 74 | window->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key ); |
|---|
| 75 | } |
|---|
| 76 | break; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | int main( int argc, char **argv ) |
|---|
| 81 | { |
|---|
| 82 | glutInit(&argc, argv); |
|---|
| 83 | |
|---|
| 84 | if (argc<2) |
|---|
| 85 | { |
|---|
| 86 | std::cout << argv[0] <<": requires filename argument." << std::endl; |
|---|
| 87 | return 1; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]); |
|---|
| 92 | if (!loadedModel) |
|---|
| 93 | { |
|---|
| 94 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 95 | return 1; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA ); |
|---|
| 99 | glutInitWindowPosition( 100, 100 ); |
|---|
| 100 | glutInitWindowSize( 800, 600 ); |
|---|
| 101 | glutCreateWindow( argv[0] ); |
|---|
| 102 | glutDisplayFunc( display ); |
|---|
| 103 | glutReshapeFunc( reshape ); |
|---|
| 104 | glutMouseFunc( mousebutton ); |
|---|
| 105 | glutMotionFunc( mousemove ); |
|---|
| 106 | glutKeyboardFunc( keyboard ); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | viewer = new osgViewer::Viewer; |
|---|
| 110 | window = viewer->setUpViewerAsEmbeddedInWindow(100,100,800,600); |
|---|
| 111 | viewer->setSceneData(loadedModel.get()); |
|---|
| 112 | viewer->setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| 113 | viewer->addEventHandler(new osgViewer::StatsHandler); |
|---|
| 114 | viewer->realize(); |
|---|
| 115 | |
|---|
| 116 | glutMainLoop(); |
|---|
| 117 | |
|---|
| 118 | return 0; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|