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-2005 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$
00027  * Date modified: $Date: 2005-05-11 18:01:17 -0500 (Wed, 11 May 2005) $
00028  * Version:       $Revision: 17456 $
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 vrj
00058 {
00059 
00064 class OpenSGApp : public vrj::GlApp
00065 {
00066 public:
00067    struct context_data
00068    {
00069       context_data()
00070         : mRenderAction(NULL)
00071         , mContextThreadInitialized(false)
00072         , mOsgThread(NULL)
00073       {
00074          ;
00075       }
00076 
00077       OSG::RenderAction*         mRenderAction;    
00078       OSG::PassiveWindowPtr      mWin;             
00079       OSG::PassiveViewportPtr    mViewport;        
00080       OSG::PassiveBackgroundPtr  mBackground;      
00081       OSG::MatrixCameraPtr       mCamera;
00082 
00083       bool                       mContextThreadInitialized;
00084       OSG::ExternalThread*       mOsgThread;
00085    };
00086 
00087 public:
00088    OpenSGApp(vrj::Kernel* kern=NULL)
00089       : GlApp(kern)
00090    {
00091       ;
00092    }
00093 
00094    virtual ~OpenSGApp()
00095    {
00096       ;
00097    }
00098 
00103    virtual void initScene() = 0;
00104 
00109    virtual OSG::NodePtr getScene() = 0;
00110 
00115    virtual void init();
00116 
00123    virtual void apiInit();
00124 
00129    virtual void exit();
00130 
00137    virtual void contextInit();
00138    virtual void contextPreDraw();
00139    virtual void contextPostDraw();
00140    virtual void draw();
00142 
00147    virtual void bufferPreDraw()
00148    {
00149       glClearColor(0.0, 0.0, 0.0, 0.0);
00150       glClear(GL_COLOR_BUFFER_BIT);
00151    }
00152 
00153 protected:
00154    vrj::GlContextData<context_data>  mContextData;  
00156    OSG::UInt32 OSG_MAIN_ASPECT_ID;
00157 };
00158 
00159 // Handle any initialization needed before API
00160 inline void OpenSGApp::init()
00161 {
00162    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
00163                         "vrj::OpenSGApp::init() entered.\n",
00164                         "vrj::OpenSGApp::init() exited.\n");
00165 
00166    GlApp::init();
00167 
00168    // XXX: Complete initialization
00169    // if(!osgInitAlreadyCalled())
00170    OSG::osgInit(0,0);                  // Binds to primordial thread
00171 
00172 #if 0
00173    // Work around to disable display list caching
00174    OSG::FieldContainerPtr pProto = OSG::Geometry::getClassType().getPrototype();
00175    OSG::GeometryPtr pGeoProto = OSG::GeometryPtr::dcast(pProto);
00176 
00177    if(pGeoProto != OSG::NullFC)
00178    {
00179        pGeoProto->setDlistCache(false);
00180    }
00181 #endif
00182 
00183    OSG_MAIN_ASPECT_ID = OSG::Thread::getAspect();   // Gets the base aspect id to use
00184 }
00185 
00186 inline void OpenSGApp::apiInit()
00187 {
00188    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
00189                         "vrj::OpenSGApp::apiInit() entered.\n",
00190                         "vrj::OpenSGApp::apiInit() exited.\n");
00191 
00192    this->initScene();
00193    vprASSERT(getScene() != OSG::NullFC);
00194 }
00195 
00196 inline void OpenSGApp::exit()
00197 {
00198    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
00199                         "vrj::OpenSGApp::exit() entered.\n",
00200                         "vrj::OpenSGApp::exit() exited.\n");
00201 
00202    OSG::osgExit();
00203 }
00204 
00206 inline void OpenSGApp::contextInit()
00207 {
00208    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
00209                         "vrj::OpenSGApp::contextInit() entered.\n",
00210                         "vrj::OpenSGApp::contextInit() exited.\n");
00211 
00212    context_data* c_data = &(*mContextData);  // Context specific data. Should be one copy per context
00213 
00214    // Check for thread initialized
00215    // This will only happen for the first initialized context per pipe
00216    if(!c_data->mContextThreadInitialized)
00217    {
00218       c_data->mContextThreadInitialized = true;
00219 
00220       char thread_name_buffer[255];
00221       sprintf(thread_name_buffer, "vprThread:%d",
00222               vpr::Thread::self()->getTID());
00223       c_data->mOsgThread = OSG::ExternalThread::get(thread_name_buffer);
00224       if(!(c_data->mOsgThread->isInitialized()))
00225       {
00226          c_data->mOsgThread->initialize(OSG_MAIN_ASPECT_ID);     // XXX: In future this might need to be different thread
00227       }
00228    }
00229 
00230    // Allocate OpenSG stuff
00231    c_data->mWin        = OSG::PassiveWindow::create();
00232    c_data->mViewport   = OSG::PassiveViewport::create();
00233    c_data->mBackground = OSG::PassiveBackground::create();
00234    c_data->mCamera     = OSG::MatrixCamera::create();
00235 
00236    // Setup the viewport
00237    OSG::beginEditCP(c_data->mViewport);
00238       c_data->mViewport->setLeft(0);
00239       c_data->mViewport->setRight(1);
00240       c_data->mViewport->setBottom(0);
00241       c_data->mViewport->setTop(1);
00242       c_data->mViewport->setCamera(c_data->mCamera);
00243       c_data->mViewport->setBackground(c_data->mBackground);
00244    OSG::endEditCP  (c_data->mViewport);
00245 
00246    // Setup the Window
00247    OSG::beginEditCP(c_data->mWin);
00248       c_data->mWin->addPort(c_data->mViewport);
00249    OSG::endEditCP  (c_data->mWin);
00250 
00251    // Setup the camera
00252    OSG::beginEditCP(c_data->mCamera);
00253       c_data->mCamera->setNear(0.1);
00254       c_data->mCamera->setFar (10000);
00255    OSG::endEditCP(c_data->mCamera);
00256 
00257    // Could actually make one of these per thread instead of context.
00258    c_data->mRenderAction = OSG::RenderAction::create();
00259    // c_data->mRenderAction->setAutoFrustum(false);         // Turn off auto frustum
00260 
00261    // Initialize OpenSG's OpenGL state
00262 
00263    c_data->mWin->init();
00264 }
00265 
00266 inline void OpenSGApp::contextPreDraw()
00267 {
00268    context_data* c_data = &(*mContextData);
00269    c_data->mWin->frameInit();
00270 }
00271 
00272 inline void OpenSGApp::contextPostDraw()
00273 {
00274    context_data* c_data = &(*mContextData);
00275    c_data->mWin->frameExit();
00276 }
00277 
00278 inline void OpenSGApp::draw()
00279 {
00280    vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_HVERB_LVL,
00281                         "vrj::OpenSGApp::draw() entered.\n",
00282                         "vrj::OpenSGApp::draw() exited.\n");
00283 
00284    glClear(GL_DEPTH_BUFFER_BIT);
00285    glPushAttrib(GL_ALL_ATTRIB_BITS);
00286 
00287    context_data* c_data = &(*mContextData);
00288 
00289    vrj::GlDrawManager* drawMan =
00290       dynamic_cast<vrj::GlDrawManager*>(this->getDrawManager());
00291    vprASSERT(drawMan != NULL);
00292    vrj::GlUserData* userData = drawMan->currentUserData();
00293 
00294    // Copy the matrix
00295    vrj::Projection* project = userData->getProjection();
00296    vrj::Frustum vrj_frustum = userData->getProjection()->getFrustum();
00297 
00298    const float* vj_proj_view_mat = project->getViewMatrix().mData;
00299    OSG::Matrix frustum_matrix, view_xform_mat;
00300    view_xform_mat.setValue(vj_proj_view_mat);
00301 
00302    OSG::MatrixFrustum(frustum_matrix,
00303                       vrj_frustum[vrj::Frustum::VJ_LEFT],
00304                       vrj_frustum[vrj::Frustum::VJ_RIGHT],
00305                       vrj_frustum[vrj::Frustum::VJ_BOTTOM],
00306                       vrj_frustum[vrj::Frustum::VJ_TOP],
00307                       vrj_frustum[vrj::Frustum::VJ_NEAR],
00308                       vrj_frustum[vrj::Frustum::VJ_FAR]);
00309 
00310    OSG::Matrix full_view_matrix = frustum_matrix;
00311    full_view_matrix.mult(view_xform_mat);   // Compute complete projection matrix
00312 
00313    // Setup the camera
00314    OSG::beginEditCP(c_data->mCamera);
00315       c_data->mCamera->setNear(vrj_frustum[vrj::Frustum::VJ_NEAR]);
00316       c_data->mCamera->setFar(vrj_frustum[vrj::Frustum::VJ_FAR]);
00317       c_data->mCamera->setProjectionMatrix( frustum_matrix );  // Set projection matrix
00318       c_data->mCamera->setModelviewMatrix( view_xform_mat );   // Set modelview matrix
00319    OSG::endEditCP(c_data->mCamera);
00320 
00321    // Setup the viewport
00322    OSG::beginEditCP(c_data->mViewport);
00323       c_data->mViewport->setRoot(getScene());
00324    OSG::endEditCP  (c_data->mViewport);
00325 
00326    // --- Trigger the draw --- //
00327 
00328    // Push the matrix so that drawing after this is not affected by the scene
00329    // graph.
00330    glMatrixMode(GL_PROJECTION);
00331    glPushMatrix();
00332    glMatrixMode(GL_MODELVIEW);
00333    glPushMatrix();
00334       c_data->mWin->render(c_data->mRenderAction);
00335    glPopMatrix();
00336    glMatrixMode(GL_PROJECTION);
00337    glPopMatrix();
00338    glMatrixMode(GL_MODELVIEW);
00339 
00340    glPopAttrib();    // Pop the attribute store
00341 
00342    FINFO(("Frame done on Window %lx.\n", c_data->mWin.getCPtr() ));
00343 }
00344 
00345 } // End of vrj namespace
00346 
00347 #endif

Generated on Thu Jan 4 10:56:51 2007 for VR Juggler by  doxygen 1.5.1