Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OsgApp.h

Go to the documentation of this file.
00001 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00002  *
00003  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Christopher Just,
00007  *   Patrick Hartling, Kevin Meinert,
00008  *   Carolina Cruz-Neira, Albert Baker
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  *
00025  * -----------------------------------------------------------------
00026  * File:          $RCSfile: OsgApp.h,v $
00027  * Date modified: $Date: 2003/12/08 00:45:58 $
00028  * Version:       $Revision: 1.30 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #ifndef _VRJ_OSG_APP_H_
00034 #define _VRJ_OSG_APP_H_
00035 
00036 #include <vrj/vrjConfig.h>
00037 
00038 #include <vrj/Draw/OGL/GlApp.h>
00039 #include <vrj/Draw/OGL/GlContextData.h>
00040 
00041 #include <vrj/Display/CameraProjection.h>
00042 
00043 #include <osg/Vec3>
00044 #include <osg/Matrix>
00045 #include <osg/Transform>
00046 #include <osg/Group>
00047 
00048 #include <osgUtil/SceneView>
00049 
00050 
00051 namespace vrj
00052 {
00053 
00061 class OsgApp : public GlApp
00062 {
00063 public:
00064    OsgApp(Kernel* kern) : GlApp(kern)
00065    {
00066       ;
00067    }
00068 
00069    OsgApp() : GlApp()
00070    {
00071       ;
00072    }
00073 
00074    virtual ~OsgApp()
00075    {
00076       ;
00077    }
00078 
00083    virtual void initScene() = 0;
00084 
00089    virtual osg::Group* getScene() = 0;
00090 
00097    virtual void configSceneView(osgUtil::SceneView* newSceneViewer)
00098    {
00099       newSceneViewer->setDefaults();
00100       newSceneViewer->setBackgroundColor(osg::Vec4(0.0f, 0.0f, 0.0f, 0.0f));
00101    }
00102 
00110    virtual void draw();
00111 
00112    // Initialize
00113    // Make sure to call initScene if you override this function
00114    virtual void init()
00115    {
00116       GlApp::init();
00117 
00118       //Create the scene
00119       this->initScene();
00120    }
00121 
00130    virtual void contextInit();
00131 
00136    virtual void contextClose()
00137    {
00138       ;
00139    }
00140 
00151    virtual void contextPreDraw()
00152    {
00153       ;
00154    }
00155 
00167    virtual void bufferPreDraw()
00168    {
00169       ;
00170    }
00171 
00180    virtual void pipePreDraw()
00181    {
00182       ;
00183    }
00184 
00185 protected:
00186    vrj::GlContextData< osgUtil::SceneView* > sceneViewer;
00187 };
00188 
00189 inline void OsgApp::contextInit()
00190 {
00191    unsigned int unique_context_id = GlDrawManager::instance()->getCurrentContext();
00192 
00193    // --- Create new context specific scene viewer -- //
00194    osgUtil::SceneView* new_sv = new osgUtil::SceneView;
00195    this->configSceneView(new_sv);            // Configure the new viewer
00196    new_sv->getState()->setContextID(unique_context_id);
00197 
00198    // This will eventually be changed to no light and all lighting will be handled
00199    // by the application.  For the time being it fixes the lighting inconsistanies
00200    // over multiple screens
00201    new_sv->setLightingMode(osgUtil::SceneView::SKY_LIGHT);
00202 
00203    (*sceneViewer) = new_sv;
00204 
00205    //Setup OpenGL light
00206    //This should actualy be done in the simulator code
00207    GLfloat light0_ambient[] = { 0.1f,  0.1f,  0.1f,  1.0f};
00208    GLfloat light0_diffuse[] = { 0.8f,  0.8f,  0.8f,  1.0f};
00209    GLfloat light0_specular[] = { 1.0f,  1.0f,  1.0f,  1.0f};
00210    GLfloat light0_position[] = {0.0f, 0.75f, 0.75f, 0.0f};
00211 
00212    GLfloat mat_ambient[] = { 0.7f, 0.7f,  0.7f,  1.0f};
00213    GLfloat mat_diffuse[] = { 1.0f,  0.5f,  0.8f,  1.0f};
00214    GLfloat mat_specular[] = { 1.0f,  1.0f,  1.0f,  1.0f};
00215    GLfloat mat_shininess[] = { 50.0f};
00216    //GLfloat mat_emission[] = { 1.0f,  1.0f,  1.0f,  1.0f};
00217    GLfloat no_mat[] = { 0.0f,  0.0f,  0.0f,  1.0f};
00218 
00219    glLightfv(GL_LIGHT0, GL_AMBIENT,  light0_ambient);
00220    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light0_diffuse);
00221    glLightfv(GL_LIGHT0, GL_SPECULAR,  light0_specular);
00222    glLightfv(GL_LIGHT0, GL_POSITION,  light0_position);
00223 
00224    glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient );
00225    glMaterialfv( GL_FRONT,  GL_DIFFUSE, mat_diffuse );
00226    glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
00227    glMaterialfv( GL_FRONT,  GL_SHININESS, mat_shininess );
00228    glMaterialfv( GL_FRONT,  GL_EMISSION, no_mat);
00229 
00230    glEnable(GL_DEPTH_TEST);
00231    glEnable(GL_NORMALIZE);
00232    glEnable(GL_LIGHTING);
00233    glEnable(GL_LIGHT0);
00234    glEnable(GL_COLOR_MATERIAL);
00235    glShadeModel(GL_SMOOTH);
00236 }
00237 
00238 
00239 inline void OsgApp::draw()
00240 {
00241    glPushAttrib(GL_ALL_ATTRIB_BITS);
00242    glPushAttrib(GL_TRANSFORM_BIT);
00243    glPushAttrib(GL_VIEWPORT_BIT);
00244 
00245    glMatrixMode(GL_MODELVIEW);
00246    glPushMatrix();
00247 
00248    glMatrixMode(GL_PROJECTION);
00249    glPushMatrix();
00250 
00251    glMatrixMode(GL_TEXTURE);
00252    glPushMatrix();
00253 
00254 
00255    osgUtil::SceneView* sv(NULL);
00256    sv = (*sceneViewer);    // Get context specific scene viewer
00257    vprASSERT( sv != NULL);
00258 
00259    GlDrawManager*    gl_manager;    
00260    gl_manager = GlDrawManager::instance();
00261 
00262    // Set the up the viewport (since OSG clears it out)
00263    float vp_ox, vp_oy, vp_sx, vp_sy;         // The float vrj sizes of the view ports
00264    unsigned w_ox, w_oy, w_width, w_height;   // Origin and size of the window
00265    gl_manager->currentUserData()->getViewport()->getOriginAndSize(vp_ox, vp_oy, vp_sx, vp_sy);
00266    gl_manager->currentUserData()->getGlWindow()->getOriginSize(w_ox, w_oy, w_width, w_height);
00267 
00268    // compute unsigned versions of the viewport info (for passing to glViewport)
00269    unsigned ll_x = unsigned(vp_ox*float(w_width));
00270    unsigned ll_y = unsigned(vp_oy*float(w_height));
00271    unsigned x_size = unsigned(vp_sx*float(w_width));
00272    unsigned y_size = unsigned(vp_sy*float(w_height));
00273 
00274    // Add the tree to the scene viewer and set properties
00275    sv->setSceneData(getScene());
00276    //sv->setCalcNearFar(false);
00277    sv->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR);
00278    sv->setViewport(ll_x, ll_y, x_size, y_size);
00279 
00280    //Get the view matrix and the frustrum form the draw manager
00281    GlDrawManager* drawMan = dynamic_cast<GlDrawManager*>(this->getDrawManager());
00282    vprASSERT(drawMan != NULL);
00283    GlUserData* userData = drawMan->currentUserData();
00284 
00285    // Copy the matrix
00286    Projection* project = userData->getProjection();
00287    const float* vj_proj_view_mat = project->getViewMatrix().mData;
00288    osg::RefMatrix* osg_proj_xform_mat = new osg::RefMatrix;
00289    osg_proj_xform_mat->set( vj_proj_view_mat );
00290 
00291    //Get the frustrum
00292    Frustum frustum = project->getFrustum();
00293    sv->setProjectionMatrixAsFrustum(frustum[Frustum::VJ_LEFT],
00294                                     frustum[Frustum::VJ_RIGHT],
00295                                     frustum[Frustum::VJ_BOTTOM],
00296                                     frustum[Frustum::VJ_TOP],
00297                                     frustum[Frustum::VJ_NEAR],
00298                                     frustum[Frustum::VJ_FAR]);
00299 
00300    sv->setViewMatrix(*osg_proj_xform_mat);
00301 
00302    //Draw the scene
00303    sv->update();
00304    sv->cull();
00305    sv->draw();
00306 
00307    glMatrixMode(GL_TEXTURE);
00308    glPopMatrix();
00309 
00310    glMatrixMode(GL_PROJECTION);
00311    glPopMatrix();
00312 
00313    glMatrixMode(GL_MODELVIEW);
00314    glPopMatrix();
00315 
00316    glPopAttrib();
00317    glPopAttrib();
00318    glPopAttrib();
00319 }
00320 
00321 }
00322 
00323 
00324 #endif /* _VRJ_OSG_APP_H_ */

Generated on Sun May 2 15:10:17 2004 for VR Juggler by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002