Hello,

I am running into thesame problem (see original e-mail below).
I'm trying to use OpenSG with VR Juggler (which works fine) and a
shadowmapviewport (which doesn't).

To use OpenSG with VR Juggler, you have to derive from the OpenSGApp class.
This class uses a PassiveViewport and PassiveWindow by default.
This works fine with my scene in OpenSG.

But when I want to add shadows, I'll have to use the ShadowMapViewport.
So I changed the OpenSGApp class, replacing the PassiveViewport by the
ShadowMapViewport, and making additional settings to it.

When I run my application now, all I get is a black screen.
I attached the two versions of the VR Juggler OpenSGApp.
The first one is the original, the second one uses the ShadowMapViewport.

My light doesn't get added to the list of lightnodes yet, but even when
you don't add lights, the result should just be shadowless, not black (at
least, that's how it works in tutorial 24)


So; is the ShadowMapViewport even supposed to work with a PassiveWindow?


Any help would be greatly appreciated :)


- Wouter


-------------------------------------------------------------------------
Tue, 04 Jul 2006 03:34:34 -0700

Hi everybody,
I'm attempting to add shadows to a scene with the following code using a
ShadowMapViewport & a PassiveWindow.

ShadowMapViewportPtr svp = ShadowMapViewport::create();
GradientBackgroundPtr gbg = GradientBackground::create();
beginEditCP(gbg);
        gbg->addLine(Color3f(0.7, 0.7, 0.8), 0);
        gbg->addLine(Color3f(0.0, 0.1, 0.3), 1);
endEditCP(gbg);

TransformPtr point1_trans;
PointLightPtr _point1_core;
NodePtr point1 = makeCoredNode<PointLight>(&_point1_core);

// Shadow viewport
beginEditCP(svp);
svp->setBackground(gbg);
svp->setRoot(m_Mgr->getRoot());
svp->setSize(0,0,1,1);
svp->setOffFactor(5.4);
svp->setOffBias(10);
svp->setShadowColor(Color4f(0.2, 0.2, 0.2, 1.0));
svp->setMapSize(1024);
// you can add the light sources here, as default all light source in
// the scenegraph are used.
svp->getLightNodes().push_back(point1);
endEditCP(svp);

    beginEditCP(m_Pwin);//Window
                m_Pwin->replacePort(0, svp);
    endEditCP(m_Pwin);

        beginEditCP(svp);//Viewport
        svp->setCamera(m_Mgr->getCamera());
    endEditCP(svp);

        m_Mgr->turnHeadlightOff();
        NodePtr point1_beacon = makeCoredNode<Transform>(&point1_trans);
        beginEditCP(point1_trans);
                point1_trans->getMatrix().setTranslate(-150.0, 50.0, 0.0);
        endEditCP(point1_trans);

        beginEditCP(_point1_core);
                _point1_core->setAmbient(0.45,0.45,0.45,1);
                _point1_core->setDiffuse(0.5,0.5,0.5,1);
                _point1_core->setSpecular(0.0,0.0,0.0,1);
                _point1_core->setBeacon(point1_beacon);
                _point1_core->setOn(true);
        endEditCP(_point1_core);

        NodePtr n=m_Mgr->getRoot();
        beginEditCP(point1);
                point1->addChild(n);  // was point2
        endEditCP(point1);

    m_Mgr->setRoot(point1);

-------------------------------
now, first problem: when I launch the program, I set a simple root
geometry and
then add the shadowviewport, but the object is always black.
second problem: when I click on the passivewindow the program crashes in an
OpenSG dll.
Where are the bugs?

thanks!
Davide.
/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
 *
 * Original Authors:
 *   Allen Bierbaum, Christopher Just,
 *   Patrick Hartling, Kevin Meinert,
 *   Carolina Cruz-Neira, Albert Baker
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#ifndef _VJ_OPENSG_APP_H_
#define _VJ_OPENSG_APP_H_

#include <vrj/vrjConfig.h>
#include <sstream>

#include <vpr/Util/Debug.h>
#include <vrj/Draw/OGL/GlContextData.h>

/*-----------------------------OpenSG includes--------------------------------*/
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGThread.h>
#include <OpenSG/OSGMatrixCamera.h>
#include <OpenSG/OSGPassiveWindow.h>
#include <OpenSG/OSGPassiveViewport.h>
#include <OpenSG/OSGPassiveBackground.h>
#include <OpenSG/OSGMatrixUtility.h>

#if OSG_MAJOR_VERSION >= 2
#include <OpenSG/OSGRenderTraversalAction.h>
#else
#include <OpenSG/OSGRenderAction.h>
#endif

/*----------------------------------------------------------------------------*/

#include <vrj/Draw/OGL/GlApp.h>

namespace vrj
{

/** \class OpenSGApp OpenSGApp.h vrj/Draw/OpenSG/OpenSGApp.h
 *
 * Base type for OpenSG application objects.
 */
class OpenSGApp : public vrj::GlApp
{
public:
   struct context_data
   {
      context_data()
        : mRenderAction(NULL)
        , mContextThreadInitialized(false)
        , mOsgThread(NULL)
      {
         ;
      }
#if OSG_MAJOR_VERSION >= 2
      OSG::RenderTraversalAction* mRenderAction;    /**< The render trav action 
for the scene */
#else
      OSG::RenderAction*         mRenderAction;    /**< The render action for 
the scene */
#endif

      OSG::PassiveWindowPtr      mWin;             /**< passive window to 
render with (the context) */
      OSG::PassiveViewportPtr    mViewport;        /**< passive viewport to 
render with (the context) */
      OSG::PassiveBackgroundPtr  mBackground;      /**< passive background to 
render with (the context) */
      OSG::MatrixCameraPtr       mCamera;

      bool                       mContextThreadInitialized;
      OSG::ExternalThread*       mOsgThread;
   };

public:
   OpenSGApp(vrj::Kernel* kern=NULL)
      : GlApp(kern)
   {
      ;
   }

   virtual ~OpenSGApp()
   {
      ;
   }

   /**
    * Scene initialization function.
    * User code for initializing the OpenSG scene should be placed here.
    */
   virtual void initScene() = 0;

   /**
    * Get the OpenSG Scene root.
    * @return NodePtr to the root of the scene to render.
    */
   virtual OSG::NodePtr getScene() = 0;

   /**
    * Initializes OpenSG for drawing.
    * If overridden, the overriding method MUST call this method.
    */
   virtual void init();

   /**
    * 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().
    */
   virtual void apiInit();

   /**
    * Shuts down OpenSG.  If overridden, the overriding method must call this
    * method.
    */
   virtual void exit();

   /**
    * For OpenSG 1.x, this method does nothing. For OpenSG 2, it ensures that
    * all scene graph changes have been committed prior to rendering. If
    * overridden, the overriding method must call this method. It is
    * recommended that the invocation of this method be the last step in the
    * override of latePreFrame().
    *
    * @since 2.1.28
    */
   virtual void latePreFrame()
   {
#if OSG_MAJOR_VERSION >= 2
      OSG::commitChanges();
#endif
   }

   /**
    * OpenGL Drawing functions.
    * If user code overrides these functions, the overriding functions MUST
    * call these methods.
    */
   //@{
   virtual void contextInit();
   virtual void contextPreDraw();
   virtual void contextPostDraw();
   virtual void draw();
   //@}

   /**
    * Called once per frame, per buffer (basically context).
    * This is needed so that we can use subviewports.
    */
   virtual void bufferPreDraw()
   {
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glClear(GL_COLOR_BUFFER_BIT);
   }

protected:
   vrj::GlContextData<context_data>  mContextData;  /**< OpenSG context data */

   OSG::UInt32 OSG_MAIN_ASPECT_ID;
};

// Handle any initialization needed before API
inline void OpenSGApp::init()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
                        "vrj::OpenSGApp::init() entered.\n",
                        "vrj::OpenSGApp::init() exited.\n");

   GlApp::init();

   // XXX: Complete initialization
   // if(!osgInitAlreadyCalled())
   OSG::osgInit(0,0);                  // Binds to primordial thread

#if 0
   // Work around to disable display list caching
   OSG::FieldContainerPtr pProto = OSG::Geometry::getClassType().getPrototype();
   OSG::GeometryPtr pGeoProto = OSG::GeometryPtr::dcast(pProto);

   if(pGeoProto != OSG::NullFC)
   {
       pGeoProto->setDlistCache(false);
   }
#endif

#if OSG_MAJOR_VERSION >= 2
   OSG_MAIN_ASPECT_ID = OSG::Thread::getCurrentAspect();   // Gets the base 
aspect id to use
#else
   OSG_MAIN_ASPECT_ID = OSG::Thread::getAspect();   // Gets the base aspect id 
to use
#endif
}

inline void OpenSGApp::apiInit()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
                        "vrj::OpenSGApp::apiInit() entered.\n",
                        "vrj::OpenSGApp::apiInit() exited.\n");

   this->initScene();
   vprASSERT(getScene() != OSG::NullFC);
}

inline void OpenSGApp::exit()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
                        "vrj::OpenSGApp::exit() entered.\n",
                        "vrj::OpenSGApp::exit() exited.\n");

   OSG::osgExit();
}

/** Called once per context at context creation */
inline void OpenSGApp::contextInit()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
                        "vrj::OpenSGApp::contextInit() entered.\n",
                        "vrj::OpenSGApp::contextInit() exited.\n");

   context_data* c_data = &(*mContextData);  // Context specific data. Should 
be one copy per context

   // Check for thread initialized
   // This will only happen for the first initialized context per pipe
   if(!c_data->mContextThreadInitialized)
   {
      c_data->mContextThreadInitialized = true;

      std::ostringstream thread_name;
      thread_name << "vprThread:" << vpr::Thread::self()->getTID();
      c_data->mOsgThread = OSG::ExternalThread::get(thread_name.str().c_str());

      if(!(c_data->mOsgThread->isInitialized()))
      {
         c_data->mOsgThread->initialize(OSG_MAIN_ASPECT_ID);     // XXX: In 
future this might need to be different thread
      }
   }

   // Allocate OpenSG stuff
   c_data->mWin        = OSG::PassiveWindow::create();
   c_data->mViewport   = OSG::PassiveViewport::create();
   c_data->mBackground = OSG::PassiveBackground::create();
   c_data->mCamera     = OSG::MatrixCamera::create();

#if OSG_MAJOR_VERSION < 2
   // Create container pointer editors for the field containers modified
   // below.
   OSG::CPEditor vp_editor(c_data->mViewport);
   OSG::CPEditor win_editor(c_data->mWin);
   OSG::CPEditor camera_editor(c_data->mCamera);
#endif

   // Set up the viewport.
   c_data->mViewport->setLeft(0);
   c_data->mViewport->setRight(1);
   c_data->mViewport->setBottom(0);
   c_data->mViewport->setTop(1);
   c_data->mViewport->setCamera(c_data->mCamera);
   c_data->mViewport->setBackground(c_data->mBackground);

   // Set up the Window.
   c_data->mWin->addPort(c_data->mViewport);

   // Set up the camera.
   c_data->mCamera->setNear(0.1);
   c_data->mCamera->setFar (10000);

   // Could actually make one of these per thread instead of context.
#if OSG_MAJOR_VERSION >= 2
   c_data->mRenderAction = OSG::RenderTraversalAction::create();
#else
   c_data->mRenderAction = OSG::RenderAction::create();
#endif
   // c_data->mRenderAction->setAutoFrustum(false);         // Turn off auto 
frustum

   // Initialize OpenSG's OpenGL state

   c_data->mWin->init();

#if OSG_MAJOR_VERSION >= 2
   OSG::commitChanges();
#endif
}

inline void OpenSGApp::contextPreDraw()
{
   context_data* c_data = &(*mContextData);
   c_data->mWin->frameInit();
}

inline void OpenSGApp::contextPostDraw()
{
   context_data* c_data = &(*mContextData);
   c_data->mWin->frameExit();
}

inline void OpenSGApp::draw()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_HVERB_LVL,
                        "vrj::OpenSGApp::draw() entered.\n",
                        "vrj::OpenSGApp::draw() exited.\n");

   glClear(GL_DEPTH_BUFFER_BIT);
   glPushAttrib(GL_ALL_ATTRIB_BITS);

   context_data* c_data = &(*mContextData);

   vrj::GlDrawManager* drawMan =
      dynamic_cast<vrj::GlDrawManager*>(this->getDrawManager());
   vprASSERT(drawMan != NULL);
   vrj::GlUserData* userData = drawMan->currentUserData();

   // Copy the matrix
   vrj::Projection* project = userData->getProjection();
   const vrj::Frustum& vrj_frustum = userData->getProjection()->getFrustum();

   const float* vj_proj_view_mat = project->getViewMatrix().mData;
   OSG::Matrix frustum_matrix, view_xform_mat;
   view_xform_mat.setValue(vj_proj_view_mat);

   OSG::MatrixFrustum(frustum_matrix,
                      vrj_frustum[vrj::Frustum::VJ_LEFT],
                      vrj_frustum[vrj::Frustum::VJ_RIGHT],
                      vrj_frustum[vrj::Frustum::VJ_BOTTOM],
                      vrj_frustum[vrj::Frustum::VJ_TOP],
                      vrj_frustum[vrj::Frustum::VJ_NEAR],
                      vrj_frustum[vrj::Frustum::VJ_FAR]);

   OSG::Matrix full_view_matrix = frustum_matrix;
   full_view_matrix.mult(view_xform_mat);   // Compute complete projection 
matrix

#if OSG_MAJOR_VERSION < 2
   // Create container pointer editors for the field containers modified
   // below.
   OSG::CPEditor camera_editor(c_data->mCamera,
                               OSG::MatrixCamera::NearFieldMask |
                                  OSG::MatrixCamera::FarFieldMask |
                                  OSG::MatrixCamera::ProjectionMatrixFieldMask |
                                  OSG::MatrixCamera::ModelviewMatrixFieldMask);
   OSG::CPEditor vp_editor(c_data->mViewport, OSG::Viewport::RootFieldMask);
#endif

   // Set up the camera.
   c_data->mCamera->setNear(vrj_frustum[vrj::Frustum::VJ_NEAR]);
   c_data->mCamera->setFar(vrj_frustum[vrj::Frustum::VJ_FAR]);
   c_data->mCamera->setProjectionMatrix(frustum_matrix);  // Set projection 
matrix
   c_data->mCamera->setModelviewMatrix(view_xform_mat);   // Set modelview 
matrix

   // Set up the viewport.
   c_data->mViewport->setRoot(getScene());

   // --- Trigger the draw --- //

   // Push the matrix so that drawing after this is not affected by the scene
   // graph.
   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
      c_data->mWin->render(c_data->mRenderAction);
   glPopMatrix();
   glMatrixMode(GL_PROJECTION);
   glPopMatrix();
   glMatrixMode(GL_MODELVIEW);

   glPopAttrib();    // Pop the attribute store
#if OSG_MAJOR_VERSION < 2
   FINFO(("Frame done on Window %lx.\n", c_data->mWin.getCPtr() ));
#endif
}

} // End of vrj namespace

#endif
/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
 *
 * Original Authors:
 *   Allen Bierbaum, Christopher Just,
 *   Patrick Hartling, Kevin Meinert,
 *   Carolina Cruz-Neira, Albert Baker
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#ifndef _VJ_OPENSG_APP_H_
#define _VJ_OPENSG_APP_H_

#include <vrj/vrjConfig.h>
#include <sstream>

#include <vpr/Util/Debug.h>
#include <vrj/Draw/OGL/GlContextData.h>

/*-----------------------------OpenSG includes--------------------------------*/
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGThread.h>
#include <OpenSG/OSGMatrixCamera.h>
#include <OpenSG/OSGPassiveWindow.h>
#include <OpenSG/OSGPassiveViewport.h>
#include <OpenSG/OSGPassiveBackground.h>
#include <OpenSG/OSGMatrixUtility.h>

#if OSG_MAJOR_VERSION >= 2
#include <OpenSG/OSGRenderTraversalAction.h>
#else
#include <OpenSG/OSGRenderAction.h>
#endif

/*----------------------------------------------------------------------------*/

#include <vrj/Draw/OGL/GlApp.h>

namespace vrj
{

/** \class OpenSGApp OpenSGApp.h vrj/Draw/OpenSG/OpenSGApp.h
 *
 * Base type for OpenSG application objects.
 */
class OpenSGApp : public vrj::GlApp
{
public: 

   struct context_data
   {
      context_data()
        : mRenderAction(NULL)
        , mContextThreadInitialized(false)
        , mOsgThread(NULL)
      {
         ;
      }
#if OSG_MAJOR_VERSION >= 2
      OSG::RenderTraversalAction* mRenderAction;    /**< The render trav action 
for the scene */
#else
      OSG::RenderAction*         mRenderAction;    /**< The render action for 
the scene */
#endif

          OSG::PassiveWindowPtr      mWin;             /**< passive window to 
render with (the context) */
      OSG::ShadowViewportPtr     mViewport;        /**< shadow viewport to 
render with (the context) */
      OSG::SolidBackgroundPtr    mBackground;      /**< solid background to 
render with (the context) */
      OSG::MatrixCameraPtr       mCamera;         

      bool                       mContextThreadInitialized;
      OSG::ExternalThread*       mOsgThread;
   };

public:
   OpenSGApp(vrj::Kernel* kern=NULL)
      : GlApp(kern)
   {
      ;
   }

   virtual ~OpenSGApp()
   {
      ;
   }

   /**
    * Scene initialization function.
    * User code for initializing the OpenSG scene should be placed here.
    */
   virtual void initScene() = 0;

   /**
    * Get the OpenSG Scene root.
    * @return NodePtr to the root of the scene to render.
    */
   virtual OSG::NodePtr getScene() = 0;

   /**
    * Initializes OpenSG for drawing.
    * If overridden, the overriding method MUST call this method.
    */
   virtual void init();

   /**
    * 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().
    */
   virtual void apiInit();

   /**
    * Shuts down OpenSG.  If overridden, the overriding method must call this
    * method.
    */
   virtual void exit();

   /**
    * For OpenSG 1.x, this method does nothing. For OpenSG 2, it ensures that
    * all scene graph changes have been committed prior to rendering. If
    * overridden, the overriding method must call this method. It is
    * recommended that the invocation of this method be the last step in the
    * override of latePreFrame().
    *
    * @since 2.1.28
    */
   virtual void latePreFrame()
   {
#if OSG_MAJOR_VERSION >= 2
      OSG::commitChanges();
#endif
   }

   /**
    * OpenGL Drawing functions.
    * If user code overrides these functions, the overriding functions MUST
    * call these methods.
    */
   //@{
   virtual void contextInit();
   virtual void contextPreDraw();
   virtual void contextPostDraw();
   virtual void draw();
   //@}

   /**
    * Called once per frame, per buffer (basically context).
    * This is needed so that we can use subviewports.
    */
   virtual void bufferPreDraw()
   {
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glClear(GL_COLOR_BUFFER_BIT);
   }

protected:
   vrj::GlContextData<context_data>  mContextData;  /**< OpenSG context data */

   OSG::UInt32 OSG_MAIN_ASPECT_ID;
};

// Handle any initialization needed before API
inline void OpenSGApp::init()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
                        "vrj::OpenSGApp::init() entered.\n",
                        "vrj::OpenSGApp::init() exited.\n");

   GlApp::init();

   // XXX: Complete initialization
   // if(!osgInitAlreadyCalled())
   OSG::osgInit(0,0);                  // Binds to primordial thread

#if 0
   // Work around to disable display list caching
   OSG::FieldContainerPtr pProto = OSG::Geometry::getClassType().getPrototype();
   OSG::GeometryPtr pGeoProto = OSG::GeometryPtr::dcast(pProto);

   if(pGeoProto != OSG::NullFC)
   {
       pGeoProto->setDlistCache(false);
   }
#endif

#if OSG_MAJOR_VERSION >= 2
   OSG_MAIN_ASPECT_ID = OSG::Thread::getCurrentAspect();   // Gets the base 
aspect id to use
#else
   OSG_MAIN_ASPECT_ID = OSG::Thread::getAspect();   // Gets the base aspect id 
to use
#endif
}

inline void OpenSGApp::apiInit()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
                        "vrj::OpenSGApp::apiInit() entered.\n",
                        "vrj::OpenSGApp::apiInit() exited.\n");

   this->initScene();
   vprASSERT(getScene() != OSG::NullFC);
}

inline void OpenSGApp::exit()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
                        "vrj::OpenSGApp::exit() entered.\n",
                        "vrj::OpenSGApp::exit() exited.\n");

   OSG::osgExit();
}

/** Called once per context at context creation */
inline void OpenSGApp::contextInit()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_VERB_LVL,
                        "vrj::OpenSGApp::contextInit() entered.\n",
                        "vrj::OpenSGApp::contextInit() exited.\n");

   context_data* c_data = &(*mContextData);  // Context specific data. Should 
be one copy per context

   // Check for thread initialized
   // This will only happen for the first initialized context per pipe
   if(!c_data->mContextThreadInitialized)
   {
      c_data->mContextThreadInitialized = true;

      std::ostringstream thread_name;
      thread_name << "vprThread:" << vpr::Thread::self()->getTID();
      c_data->mOsgThread = OSG::ExternalThread::get(thread_name.str().c_str());

      if(!(c_data->mOsgThread->isInitialized()))
      {
         c_data->mOsgThread->initialize(OSG_MAIN_ASPECT_ID);     // XXX: In 
future this might need to be different thread
      }
   }

   // Allocate OpenSG stuff
   c_data->mWin        = OSG::PassiveWindow::create();   
   c_data->mViewport   = OSG::ShadowViewport::create();   
   c_data->mBackground = OSG::SolidBackground::create();
   c_data->mBackground->setColor(OSG::Color3f(1,1,1));
   c_data->mCamera     = OSG::MatrixCamera::create();

#if OSG_MAJOR_VERSION < 2
   // Create container pointer editors for the field containers modified
   // below.
   OSG::CPEditor vp_editor(c_data->mViewport);
   OSG::CPEditor win_editor(c_data->mWin);
   OSG::CPEditor camera_editor(c_data->mCamera);
#endif

   // Set up the viewport.
   c_data->mViewport->setMapSize(1024);
   c_data->mViewport->setShadowMode(OSG::ShadowViewport::PCF_SHADOW_MAP);
   c_data->mViewport->setShadowSmoothness(0.5);
   c_data->mViewport->setLeft(0);
   c_data->mViewport->setRight(1);
   c_data->mViewport->setBottom(0);
   c_data->mViewport->setTop(1);
   c_data->mViewport->setCamera(c_data->mCamera);
   c_data->mViewport->setBackground(c_data->mBackground);

   // Set up the Window.
   c_data->mWin->addPort(c_data->mViewport);

   // Set up the camera.
   c_data->mCamera->setNear(0.1);
   c_data->mCamera->setFar (10000);

   // Could actually make one of these per thread instead of context.
#if OSG_MAJOR_VERSION >= 2
   c_data->mRenderAction = OSG::RenderTraversalAction::create();
#else
   c_data->mRenderAction = OSG::RenderAction::create();
#endif
   // c_data->mRenderAction->setAutoFrustum(false);         // Turn off auto 
frustum

   // Initialize OpenSG's OpenGL state

   c_data->mWin->init();

#if OSG_MAJOR_VERSION >= 2
   OSG::commitChanges();
#endif
}

inline void OpenSGApp::contextPreDraw()
{
   context_data* c_data = &(*mContextData);
   c_data->mWin->frameInit();
}

inline void OpenSGApp::contextPostDraw()
{
   context_data* c_data = &(*mContextData);
   c_data->mWin->frameExit();
}

inline void OpenSGApp::draw()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_HVERB_LVL,
                        "vrj::OpenSGApp::draw() entered.\n",
                        "vrj::OpenSGApp::draw() exited.\n");

   glClear(GL_DEPTH_BUFFER_BIT);
   glPushAttrib(GL_ALL_ATTRIB_BITS);

   context_data* c_data = &(*mContextData);

   vrj::GlDrawManager* drawMan =
      dynamic_cast<vrj::GlDrawManager*>(this->getDrawManager());
   vprASSERT(drawMan != NULL);
   vrj::GlUserData* userData = drawMan->currentUserData();

   // Copy the matrix
   vrj::Projection* project = userData->getProjection();
   const vrj::Frustum& vrj_frustum = userData->getProjection()->getFrustum();

   const float* vj_proj_view_mat = project->getViewMatrix().mData;
   OSG::Matrix frustum_matrix, view_xform_mat;
   view_xform_mat.setValue(vj_proj_view_mat);

   OSG::MatrixFrustum(frustum_matrix,
                      vrj_frustum[vrj::Frustum::VJ_LEFT],
                      vrj_frustum[vrj::Frustum::VJ_RIGHT],
                      vrj_frustum[vrj::Frustum::VJ_BOTTOM],
                      vrj_frustum[vrj::Frustum::VJ_TOP],
                      vrj_frustum[vrj::Frustum::VJ_NEAR],
                      vrj_frustum[vrj::Frustum::VJ_FAR]);

   OSG::Matrix full_view_matrix = frustum_matrix;
   full_view_matrix.mult(view_xform_mat);   // Compute complete projection 
matrix

#if OSG_MAJOR_VERSION < 2
   // Create container pointer editors for the field containers modified
   // below.
   OSG::CPEditor camera_editor(c_data->mCamera,
                               OSG::MatrixCamera::NearFieldMask |
                                  OSG::MatrixCamera::FarFieldMask |
                                  OSG::MatrixCamera::ProjectionMatrixFieldMask |
                                  OSG::MatrixCamera::ModelviewMatrixFieldMask);
   OSG::CPEditor vp_editor(c_data->mViewport, OSG::Viewport::RootFieldMask);
#endif

   // Set up the camera.
   c_data->mCamera->setNear(vrj_frustum[vrj::Frustum::VJ_NEAR]);
   c_data->mCamera->setFar(vrj_frustum[vrj::Frustum::VJ_FAR]);
   c_data->mCamera->setProjectionMatrix(frustum_matrix);  // Set projection 
matrix
   c_data->mCamera->setModelviewMatrix(view_xform_mat);   // Set modelview 
matrix

   // Set up the viewport.
   c_data->mViewport->setRoot(getScene());

   // --- Trigger the draw --- //

   // Push the matrix so that drawing after this is not affected by the scene
   // graph.
   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
      c_data->mWin->render(c_data->mRenderAction);
   glPopMatrix();
   glMatrixMode(GL_PROJECTION);
   glPopMatrix();
   glMatrixMode(GL_MODELVIEW);

   glPopAttrib();    // Pop the attribute store
#if OSG_MAJOR_VERSION < 2
   FINFO(("Frame done on Window %lx.\n", c_data->mWin.getCPtr() ));
#endif
}

} // End of vrj namespace

#endif
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to