vrj::OpenSGApp Class Reference

Base type for OpenSG application objects. More...

#include <vrj/Draw/OpenSG/OpenSGApp.h>

Inheritance diagram for vrj::OpenSGApp:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 OpenSGApp (vrj::Kernel *kern=NULL)
virtual ~OpenSGApp ()
virtual void initScene ()=0
 Scene initialization function.
virtual OSG::NodePtr getScene ()=0
 Get the OpenSG Scene root.
virtual void init ()
 Initializes OpenSG for drawing.
virtual void apiInit ()
 Initializes OpenSG.
virtual void exit ()
 Shuts down OpenSG.
virtual void bufferPreDraw ()
 Called once per frame, per buffer (basically context).
virtual void contextInit ()
 Called once per context at context creation.
virtual void contextPreDraw ()
 Function that is called upon entry into the context before rendering.
virtual void contextPostDraw ()
 Function that is called upon exit of the context after rendering.
virtual void draw ()
 Function that renders the scene.

Protected Attributes

vrj::GlContextData< context_datamContextData
 OpenSG context data.
OSG::UInt32 OSG_MAIN_ASPECT_ID

Classes

struct  context_data

Detailed Description

Base type for OpenSG application objects.

Definition at line 64 of file OpenSGApp.h.


Constructor & Destructor Documentation

vrj::OpenSGApp::OpenSGApp ( vrj::Kernel kern = NULL  )  [inline]

Definition at line 88 of file OpenSGApp.h.

00089       : GlApp(kern)
00090    {
00091       ;
00092    }

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

Definition at line 94 of file OpenSGApp.h.

00095    {
00096       ;
00097    }


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::getScene (  )  [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 160 of file OpenSGApp.h.

References vrj::App::init(), and OSG_MAIN_ASPECT_ID.

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 }

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 186 of file OpenSGApp.h.

References getScene(), and initScene().

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 }

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

Shuts down OpenSG.

If overridden, the overriding method call this method.

Reimplemented from vrj::App.

Definition at line 196 of file OpenSGApp.h.

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 }

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 206 of file OpenSGApp.h.

References vrj::OpenSGApp::context_data::mBackground, vrj::OpenSGApp::context_data::mCamera, vrj::OpenSGApp::context_data::mContextThreadInitialized, vrj::OpenSGApp::context_data::mOsgThread, vrj::OpenSGApp::context_data::mRenderAction, vrj::OpenSGApp::context_data::mViewport, vrj::OpenSGApp::context_data::mWin, and OSG_MAIN_ASPECT_ID.

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 }

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

Function that is called upon entry into the context before rendering.

This can be used to allocate context-specific data dynamically.

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

Reimplemented from vrj::GlApp.

Definition at line 266 of file OpenSGApp.h.

References vrj::OpenSGApp::context_data::mWin.

00267 {
00268    context_data* c_data = &(*mContextData);
00269    c_data->mWin->frameInit();
00270 }

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

Function that is called upon exit of the context after rendering.

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

Reimplemented from vrj::GlApp.

Definition at line 272 of file OpenSGApp.h.

References vrj::OpenSGApp::context_data::mWin.

00273 {
00274    context_data* c_data = &(*mContextData);
00275    c_data->mWin->frameExit();
00276 }

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

Function that renders the scene.

Override this function with the user rendering routine.

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

Implements vrj::GlApp.

Definition at line 278 of file OpenSGApp.h.

References vrj::GlDrawManager::currentUserData(), vrj::GlApp::getDrawManager(), vrj::Projection::getFrustum(), vrj::GlUserData::getProjection(), getScene(), vrj::Projection::getViewMatrix(), vrj::OpenSGApp::context_data::mCamera, vrj::OpenSGApp::context_data::mRenderAction, vrj::OpenSGApp::context_data::mViewport, vrj::OpenSGApp::context_data::mWin, 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.

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 }

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 147 of file OpenSGApp.h.

00148    {
00149       glClearColor(0.0, 0.0, 0.0, 0.0);
00150       glClear(GL_COLOR_BUFFER_BIT);
00151    }


Member Data Documentation

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

OpenSG context data.

Definition at line 154 of file OpenSGApp.h.

OSG::UInt32 vrj::OpenSGApp::OSG_MAIN_ASPECT_ID [protected]

Definition at line 156 of file OpenSGApp.h.

Referenced by contextInit(), and init().


The documentation for this class was generated from the following file:
Generated on Thu Jan 4 10:58:24 2007 for VR Juggler by  doxygen 1.5.1