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