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

OpenSGApp.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: OpenSGApp.h,v $
00027  * Date modified: $Date: 2004/04/20 23:20:42 $
00028  * Version:       $Revision: 1.16 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #ifndef _VJ_OPENSG_APP_H_
00034 #define _VJ_OPENSG_APP_H_
00035 
00036 #include <vrj/vrjConfig.h>
00037 #include <iostream>
00038 #include <iomanip>
00039 
00040 #include <vpr/Util/Debug.h>
00041 #include <vrj/Draw/OGL/GlContextData.h>
00042 
00043 /*-----------------------------OpenSG includes--------------------------------*/
00044 #include <OpenSG/OSGConfig.h>
00045 #include <OpenSG/OSGThread.h>
00046 #include <OpenSG/OSGRenderAction.h>
00047 #include <OpenSG/OSGMatrixCamera.h>
00048 #include <OpenSG/OSGPassiveWindow.h>
00049 #include <OpenSG/OSGPassiveViewport.h>
00050 #include <OpenSG/OSGPassiveBackground.h>
00051 #include <OpenSG/OSGMatrixUtility.h>
00052 
00053 /*----------------------------------------------------------------------------*/
00054 
00055 #include <vrj/Draw/OGL/GlApp.h>
00056 
00057 namespace
00058 {
00059    OSG::UInt32 OSG_MAIN_ASPECT_ID;     
00060 }
00061 
00062 namespace vrj
00063 {
00064 
00065 class OpenSGApp : public vrj::GlApp
00066 {
00067 public:
00068    struct context_data
00069    {
00070       context_data()
00071         : mRenderAction(NULL),
00072           mContextThreadInitialized(false),
00073           mOsgThread(NULL)
00074       {;}
00075 
00076       OSG::RenderAction*         mRenderAction;    
00077       OSG::PassiveWindowPtr      mWin;             
00078       OSG::PassiveViewportPtr    mViewport;        
00079       OSG::PassiveBackgroundPtr  mBackground;      
00080       OSG::MatrixCameraPtr       mCamera;
00081 
00082       bool                       mContextThreadInitialized;
00083       OSG::ExternalThread*       mOsgThread;
00084    };
00085 
00086 public:
00087    OpenSGApp(vrj::Kernel* kern)
00088       : GlApp(kern)
00089    {;}
00090    
00091    virtual ~OpenSGApp()
00092    {;}
00093    
00098    virtual void initScene() = 0;
00099    
00104    virtual OSG::NodePtr getSceneRoot() = 0;
00105    
00110    virtual void init();
00111    
00118    virtual void apiInit();
00119 
00123    virtual void exit();
00124    
00131    virtual void contextInit();
00132    virtual void contextPreDraw();
00133    virtual void contextPostDraw();
00134    virtual void draw();
00136    
00141    virtual void bufferPreDraw()
00142    {
00143       glClearColor(0.0, 0.0, 0.0, 0.0);
00144       glClear(GL_COLOR_BUFFER_BIT);
00145    }
00146 
00147 protected:
00148    vrj::GlContextData<context_data>  mContextData;  
00149 };
00150 
00151 // Handle any initialization needed before API
00152 inline void OpenSGApp::init()
00153 {
00154    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
00155                         "vrj::OpenSGApp::init() entered.\n",
00156                         "vrj::OpenSGApp::init() exited.\n");
00157 
00158    GlApp::init();
00159 
00160    // XXX: Complete initialization
00161    // if(!osgInitAlreadyCalled())
00162    OSG::osgInit(0,0);                  // Binds to primordial thread
00163 
00164 #if 0
00165    // Work around to disable display list caching
00166    OSG::FieldContainerPtr pProto = OSG::Geometry::getClassType().getPrototype();
00167    OSG::GeometryPtr pGeoProto = OSG::GeometryPtr::dcast(pProto);
00168 
00169    if(pGeoProto != OSG::NullFC)
00170    {
00171        pGeoProto->setDlistCache(false);
00172    }
00173 #endif
00174 
00175    OSG_MAIN_ASPECT_ID = OSG::Thread::getAspect();   // Gets the base aspect id to use
00176 }
00177 
00178 inline void OpenSGApp::apiInit()
00179 {
00180    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
00181                         "vrj::OpenSGApp::apiInit() entered.\n",
00182                         "vrj::OpenSGApp::apiInit() exited.\n");
00183 
00184    this->initScene();
00185    vprASSERT(getSceneRoot() != OSG::NullFC);
00186 }
00187 
00188 inline void OpenSGApp::exit()
00189 {
00190    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
00191                         "vrj::OpenSGApp::exit() entered.\n",
00192                         "vrj::OpenSGApp::exit() exited.\n");
00193 
00194    OSG::osgExit();
00195 }
00196 
00198 inline void OpenSGApp::contextInit()
00199 {
00200    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
00201                         "vrj::OpenSGApp::contextInit() entered.\n",
00202                         "vrj::OpenSGApp::contextInit() exited.\n");
00203 
00204    context_data* c_data = &(*mContextData);  // Context specific data. Should be one copy per context
00205 
00206    // Check for thread initialized
00207    // This will only happen for the first initialized context per pipe
00208    if(!c_data->mContextThreadInitialized)
00209    {
00210       c_data->mContextThreadInitialized = true;
00211 
00212       char thread_name_buffer[255];
00213       sprintf(thread_name_buffer, "vprThread:%d", vpr::Thread::self()->getTID());
00214       c_data->mOsgThread = OSG::ExternalThread::get(thread_name_buffer);
00215       if(!(c_data->mOsgThread->isInitialized()))
00216       {
00217          c_data->mOsgThread->initialize(OSG_MAIN_ASPECT_ID);     // XXX: In future this might need to be different thread
00218       }
00219    }
00220 
00221    // Allocate OpenSG stuff
00222    c_data->mWin        = OSG::PassiveWindow::create();
00223    c_data->mViewport   = OSG::PassiveViewport::create();
00224    c_data->mBackground = OSG::PassiveBackground::create();
00225    c_data->mCamera     = OSG::MatrixCamera::create();
00226 
00227    // Setup the viewport
00228    OSG::beginEditCP(c_data->mViewport);
00229       c_data->mViewport->setLeft(0);
00230       c_data->mViewport->setRight(1);
00231       c_data->mViewport->setBottom(0);
00232       c_data->mViewport->setTop(1);
00233       c_data->mViewport->setCamera(c_data->mCamera);
00234       c_data->mViewport->setBackground(c_data->mBackground);
00235    OSG::endEditCP  (c_data->mViewport);
00236    
00237    // Setup the Window
00238    OSG::beginEditCP(c_data->mWin);
00239       c_data->mWin->addPort(c_data->mViewport);
00240    OSG::endEditCP  (c_data->mWin);
00241 
00242    // Setup the camera
00243    OSG::beginEditCP(c_data->mCamera);
00244       c_data->mCamera->setNear(0.1);
00245       c_data->mCamera->setFar (10000);
00246    OSG::endEditCP(c_data->mCamera);
00247 
00248    // Could actually make one of these per thread instead of context.
00249    c_data->mRenderAction = OSG::RenderAction::create();
00250    // c_data->mRenderAction->setAutoFrustum(false);         // Turn off auto frustum
00251 
00252    // Initialize OpenSG's OpenGL state
00253 
00254    c_data->mWin->init();
00255 }
00256 
00257 inline void OpenSGApp::contextPreDraw()
00258 {
00259    context_data* c_data = &(*mContextData);
00260    c_data->mWin->frameInit();
00261 }
00262 
00263 inline void OpenSGApp::contextPostDraw()
00264 {
00265    context_data* c_data = &(*mContextData);
00266    c_data->mWin->frameExit();
00267 }
00268 
00269 inline void OpenSGApp::draw()
00270 {
00271    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_HVERB_LVL,
00272                         "vrj::OpenSGApp::draw() entered.\n",
00273                         "vrj::OpenSGApp::draw() exited.\n");
00274 
00275    glClear(GL_DEPTH_BUFFER_BIT);
00276 
00277    context_data* c_data = &(*mContextData);
00278 
00279    vrj::GlDrawManager* drawMan = dynamic_cast<vrj::GlDrawManager*> ( this->getDrawManager() );
00280    vprASSERT(drawMan != NULL);
00281    vrj::GlUserData* userData = drawMan->currentUserData();
00282 
00283    // Copy the matrix
00284    vrj::Projection* project = userData->getProjection();
00285    vrj::Frustum vrj_frustum = userData->getProjection()->getFrustum();
00286 
00287    const float* vj_proj_view_mat = project->getViewMatrix().mData;
00288    OSG::Matrix frustum_matrix, view_xform_mat;
00289    view_xform_mat.setValue(vj_proj_view_mat);
00290 
00291    OSG::MatrixFrustum(frustum_matrix, vrj_frustum[vrj::Frustum::VJ_LEFT], vrj_frustum[vrj::Frustum::VJ_RIGHT],
00292                                       vrj_frustum[vrj::Frustum::VJ_BOTTOM], vrj_frustum[vrj::Frustum::VJ_TOP],
00293                                       vrj_frustum[vrj::Frustum::VJ_NEAR], vrj_frustum[vrj::Frustum::VJ_FAR]);
00294 
00295    OSG::Matrix full_view_matrix = frustum_matrix;
00296    full_view_matrix.mult(view_xform_mat);   // Compute complete projection matrix
00297 
00298    // Setup the camera
00299    OSG::beginEditCP(c_data->mCamera);
00300       c_data->mCamera->setNear(vrj_frustum[vrj::Frustum::VJ_NEAR]);
00301       c_data->mCamera->setFar(vrj_frustum[vrj::Frustum::VJ_FAR]);
00302       c_data->mCamera->setProjectionMatrix( frustum_matrix );  // Set projection matrix
00303       c_data->mCamera->setModelviewMatrix( view_xform_mat );   // Set modelview matrix
00304    OSG::endEditCP(c_data->mCamera);
00305 
00306    // Setup the viewport
00307    OSG::beginEditCP(c_data->mViewport);
00308       c_data->mViewport->setRoot(getSceneRoot());
00309    OSG::endEditCP  (c_data->mViewport);
00310 
00311    // --- Trigger the draw --- //  
00312 
00313    // Push the matrix so that drawing after this is not affected by the scene graph
00314    glMatrixMode(GL_PROJECTION);
00315    glPushMatrix();
00316    glMatrixMode(GL_MODELVIEW);
00317    glPushMatrix();   
00318       c_data->mWin->render(c_data->mRenderAction);
00319    glPopMatrix();
00320    glMatrixMode(GL_PROJECTION);
00321    glPopMatrix();
00322    glMatrixMode(GL_MODELVIEW);
00323    
00324    FINFO(("Frame done on Window %lx.\n", c_data->mWin.getCPtr() ));
00325 }
00326 
00327 } // End of vrj namespace
00328 
00329 #endif

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