00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
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
00161
00162 OSG::osgInit(0,0);
00163
00164 #if 0
00165
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();
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);
00205
00206
00207
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);
00218 }
00219 }
00220
00221
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
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
00238 OSG::beginEditCP(c_data->mWin);
00239 c_data->mWin->addPort(c_data->mViewport);
00240 OSG::endEditCP (c_data->mWin);
00241
00242
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
00249 c_data->mRenderAction = OSG::RenderAction::create();
00250
00251
00252
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
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);
00297
00298
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 );
00303 c_data->mCamera->setModelviewMatrix( view_xform_mat );
00304 OSG::endEditCP(c_data->mCamera);
00305
00306
00307 OSG::beginEditCP(c_data->mViewport);
00308 c_data->mViewport->setRoot(getSceneRoot());
00309 OSG::endEditCP (c_data->mViewport);
00310
00311
00312
00313
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 }
00328
00329 #endif