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

vrj::OpenSGApp Class Reference

#include <OpenSGApp.h>

Inheritance diagram for vrj::OpenSGApp:

Inheritance graph
[legend]
Collaboration diagram for vrj::OpenSGApp:

Collaboration graph
[legend]
List of all members.

Public Methods

 OpenSGApp (vrj::Kernel *kern)
virtual ~OpenSGApp ()
virtual void initScene ()=0
 Scene initialization function. More...

virtual OSG::NodePtr getSceneRoot ()=0
 Get the OpenSG Scene root. More...

virtual void init ()
 Initializes OpenSG for drawing. More...

virtual void apiInit ()
 Initializes OpenSG. More...

virtual void exit ()
 Shuts down OpenSG. More...

virtual void bufferPreDraw ()
 Called once per frame, per buffer (basically context). More...

virtual void contextInit ()
 Called once per context at context creation. More...

virtual void contextPreDraw ()
 Function that is called upon entry into the context for a draw. More...

virtual void contextPostDraw ()
 Function that is called upon exit of the context for a draw. More...

virtual void draw ()
 Function to draw the scene. More...


Protected Attributes

vrj::GlContextData< context_datamContextData
 OpenSG context data. More...


Constructor & Destructor Documentation

vrj::OpenSGApp::OpenSGApp vrj::Kernel   kern [inline]
 

Definition at line 87 of file OpenSGApp.h.

References vrj::GlApp::GlApp.

00088       : GlApp(kern)
00089    {;}

virtual vrj::OpenSGApp::~OpenSGApp   [inline, virtual]
 

Definition at line 91 of file OpenSGApp.h.

00092    {;}


Member Function Documentation

virtual void vrj::OpenSGApp::initScene   [pure virtual]
 

Scene initialization function.

User code for initializing the OpenSG scene should be placed here.

Referenced by apiInit.

virtual OSG::NodePtr vrj::OpenSGApp::getSceneRoot   [pure virtual]
 

Get the OpenSG Scene root.

Returns:
NodePtr to the root of the scene to render.

Referenced by apiInit, and draw.

void vrj::OpenSGApp::init   [inline, virtual]
 

Initializes OpenSG for drawing.

If overridden, the overriding method MUST call this method.

Reimplemented from vrj::App.

Definition at line 152 of file OpenSGApp.h.

References OSG_MAIN_ASPECT_ID.

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 }

void vrj::OpenSGApp::apiInit   [inline, virtual]
 

Initializes OpenSG.

Make sure to call initScene if you override this function. If a derived class overrides this method, the overriding function MUST call OpenSGApp::apiInit().

Reimplemented from vrj::App.

Definition at line 178 of file OpenSGApp.h.

References getSceneRoot, and initScene.

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 }

void vrj::OpenSGApp::exit   [inline, virtual]
 

Shuts down OpenSG.

If overridden, the overriding method call this method.

Reimplemented from vrj::App.

Definition at line 188 of file OpenSGApp.h.

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 }

void vrj::OpenSGApp::contextInit   [inline, virtual]
 

Called once per context at context creation.

If user code overrides these functions, the overriding functions MUST call these methods.

Reimplemented from vrj::GlApp.

Definition at line 198 of file OpenSGApp.h.

References OSG_MAIN_ASPECT_ID.

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 }

void vrj::OpenSGApp::contextPreDraw   [inline, virtual]
 

Function that is called upon entry into the context for a draw.

Precondition:
The OpenGL context has been set to the context for drawing.
Postcondition:
User application has executed any commands that need to only be executed once per context, per frame.
Note:
This function can be used for things that need to happen every frame, but only once per context.
Ex: Dynamically Create display lists.

Reimplemented from vrj::GlApp.

Definition at line 257 of file OpenSGApp.h.

00258 {
00259    context_data* c_data = &(*mContextData);
00260    c_data->mWin->frameInit();
00261 }

void vrj::OpenSGApp::contextPostDraw   [inline, virtual]
 

Function that is called upon exit of the context for a draw.

Precondition:
The OpenGL context has been set to the context for drawing.

Reimplemented from vrj::GlApp.

Definition at line 263 of file OpenSGApp.h.

00264 {
00265    context_data* c_data = &(*mContextData);
00266    c_data->mWin->frameExit();
00267 }

void vrj::OpenSGApp::draw   [inline, virtual]
 

Function to draw the scene.

Override this function with the user draw routine.

Precondition:
OpenGL state has correct transformation and buffer selected.
Postcondition:
The current scene has been drawn.

Implements vrj::GlApp.

Definition at line 269 of file OpenSGApp.h.

References vrj::GlDrawManager::currentUserData, vrj::GlApp::getDrawManager, vrj::GlUserData::getProjection, getSceneRoot, vrj::Projection::getViewMatrix, vrj::Frustum::VJ_BOTTOM, vrj::Frustum::VJ_FAR, vrj::Frustum::VJ_LEFT, vrj::Frustum::VJ_NEAR, vrj::Frustum::VJ_RIGHT, and vrj::Frustum::VJ_TOP.

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 }

virtual void vrj::OpenSGApp::bufferPreDraw   [inline, virtual]
 

Called once per frame, per buffer (basically context).

This is needed so that we can use subviewports.

Reimplemented from vrj::GlApp.

Definition at line 141 of file OpenSGApp.h.

00142    {
00143       glClearColor(0.0, 0.0, 0.0, 0.0);
00144       glClear(GL_COLOR_BUFFER_BIT);
00145    }


Member Data Documentation

vrj::GlContextData<context_data> vrj::OpenSGApp::mContextData [protected]
 

OpenSG context data.

Definition at line 148 of file OpenSGApp.h.


The documentation for this class was generated from the following file:
Generated on Sun May 2 15:11:13 2004 for VR Juggler by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002