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 _VRJ_OSG_APP_H_
00034 #define _VRJ_OSG_APP_H_
00035
00036 #include <vrj/vrjConfig.h>
00037
00038 #include <vrj/Draw/OGL/GlApp.h>
00039 #include <vrj/Draw/OGL/GlContextData.h>
00040
00041 #include <vrj/Display/CameraProjection.h>
00042
00043 #include <osg/Vec3>
00044 #include <osg/Matrix>
00045 #include <osg/Transform>
00046 #include <osg/Group>
00047
00048 #include <osgUtil/SceneView>
00049
00050
00051 namespace vrj
00052 {
00053
00061 class OsgApp : public GlApp
00062 {
00063 public:
00064 OsgApp(Kernel* kern) : GlApp(kern)
00065 {
00066 ;
00067 }
00068
00069 OsgApp() : GlApp()
00070 {
00071 ;
00072 }
00073
00074 virtual ~OsgApp()
00075 {
00076 ;
00077 }
00078
00083 virtual void initScene() = 0;
00084
00089 virtual osg::Group* getScene() = 0;
00090
00097 virtual void configSceneView(osgUtil::SceneView* newSceneViewer)
00098 {
00099 newSceneViewer->setDefaults();
00100 newSceneViewer->setBackgroundColor(osg::Vec4(0.0f, 0.0f, 0.0f, 0.0f));
00101 }
00102
00110 virtual void draw();
00111
00112
00113
00114 virtual void init()
00115 {
00116 GlApp::init();
00117
00118
00119 this->initScene();
00120 }
00121
00130 virtual void contextInit();
00131
00136 virtual void contextClose()
00137 {
00138 ;
00139 }
00140
00151 virtual void contextPreDraw()
00152 {
00153 ;
00154 }
00155
00167 virtual void bufferPreDraw()
00168 {
00169 ;
00170 }
00171
00180 virtual void pipePreDraw()
00181 {
00182 ;
00183 }
00184
00185 protected:
00186 vrj::GlContextData< osgUtil::SceneView* > sceneViewer;
00187 };
00188
00189 inline void OsgApp::contextInit()
00190 {
00191 unsigned int unique_context_id = GlDrawManager::instance()->getCurrentContext();
00192
00193
00194 osgUtil::SceneView* new_sv = new osgUtil::SceneView;
00195 this->configSceneView(new_sv);
00196 new_sv->getState()->setContextID(unique_context_id);
00197
00198
00199
00200
00201 new_sv->setLightingMode(osgUtil::SceneView::SKY_LIGHT);
00202
00203 (*sceneViewer) = new_sv;
00204
00205
00206
00207 GLfloat light0_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f};
00208 GLfloat light0_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f};
00209 GLfloat light0_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
00210 GLfloat light0_position[] = {0.0f, 0.75f, 0.75f, 0.0f};
00211
00212 GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f};
00213 GLfloat mat_diffuse[] = { 1.0f, 0.5f, 0.8f, 1.0f};
00214 GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
00215 GLfloat mat_shininess[] = { 50.0f};
00216
00217 GLfloat no_mat[] = { 0.0f, 0.0f, 0.0f, 1.0f};
00218
00219 glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
00220 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
00221 glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
00222 glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
00223
00224 glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient );
00225 glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse );
00226 glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
00227 glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );
00228 glMaterialfv( GL_FRONT, GL_EMISSION, no_mat);
00229
00230 glEnable(GL_DEPTH_TEST);
00231 glEnable(GL_NORMALIZE);
00232 glEnable(GL_LIGHTING);
00233 glEnable(GL_LIGHT0);
00234 glEnable(GL_COLOR_MATERIAL);
00235 glShadeModel(GL_SMOOTH);
00236 }
00237
00238
00239 inline void OsgApp::draw()
00240 {
00241 glPushAttrib(GL_ALL_ATTRIB_BITS);
00242 glPushAttrib(GL_TRANSFORM_BIT);
00243 glPushAttrib(GL_VIEWPORT_BIT);
00244
00245 glMatrixMode(GL_MODELVIEW);
00246 glPushMatrix();
00247
00248 glMatrixMode(GL_PROJECTION);
00249 glPushMatrix();
00250
00251 glMatrixMode(GL_TEXTURE);
00252 glPushMatrix();
00253
00254
00255 osgUtil::SceneView* sv(NULL);
00256 sv = (*sceneViewer);
00257 vprASSERT( sv != NULL);
00258
00259 GlDrawManager* gl_manager;
00260 gl_manager = GlDrawManager::instance();
00261
00262
00263 float vp_ox, vp_oy, vp_sx, vp_sy;
00264 unsigned w_ox, w_oy, w_width, w_height;
00265 gl_manager->currentUserData()->getViewport()->getOriginAndSize(vp_ox, vp_oy, vp_sx, vp_sy);
00266 gl_manager->currentUserData()->getGlWindow()->getOriginSize(w_ox, w_oy, w_width, w_height);
00267
00268
00269 unsigned ll_x = unsigned(vp_ox*float(w_width));
00270 unsigned ll_y = unsigned(vp_oy*float(w_height));
00271 unsigned x_size = unsigned(vp_sx*float(w_width));
00272 unsigned y_size = unsigned(vp_sy*float(w_height));
00273
00274
00275 sv->setSceneData(getScene());
00276
00277 sv->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR);
00278 sv->setViewport(ll_x, ll_y, x_size, y_size);
00279
00280
00281 GlDrawManager* drawMan = dynamic_cast<GlDrawManager*>(this->getDrawManager());
00282 vprASSERT(drawMan != NULL);
00283 GlUserData* userData = drawMan->currentUserData();
00284
00285
00286 Projection* project = userData->getProjection();
00287 const float* vj_proj_view_mat = project->getViewMatrix().mData;
00288 osg::RefMatrix* osg_proj_xform_mat = new osg::RefMatrix;
00289 osg_proj_xform_mat->set( vj_proj_view_mat );
00290
00291
00292 Frustum frustum = project->getFrustum();
00293 sv->setProjectionMatrixAsFrustum(frustum[Frustum::VJ_LEFT],
00294 frustum[Frustum::VJ_RIGHT],
00295 frustum[Frustum::VJ_BOTTOM],
00296 frustum[Frustum::VJ_TOP],
00297 frustum[Frustum::VJ_NEAR],
00298 frustum[Frustum::VJ_FAR]);
00299
00300 sv->setViewMatrix(*osg_proj_xform_mat);
00301
00302
00303 sv->update();
00304 sv->cull();
00305 sv->draw();
00306
00307 glMatrixMode(GL_TEXTURE);
00308 glPopMatrix();
00309
00310 glMatrixMode(GL_PROJECTION);
00311 glPopMatrix();
00312
00313 glMatrixMode(GL_MODELVIEW);
00314 glPopMatrix();
00315
00316 glPopAttrib();
00317 glPopAttrib();
00318 glPopAttrib();
00319 }
00320
00321 }
00322
00323
00324 #endif