Hello,

I want to use an open source terrain loader called Demeter to load and render
terrain in an OpenSG program.  I could not get it to work in the original
context, so I tried to get it to work in a simple OpenSG sample program.  The
code for OpenSGNav.cpp is where the modifications were made and is included at
the end of the email.  One of the first problems I encountered was trying to use
the custom elevation loaders to load the data and then attach it to the scene
graph.  So I had to work around the drawing and call the necessary functions
directly.  In this way, it would compile but give me a segmentation fault at the
following line:

glActiveTextureARB(GL_TEXTURE0_ARB);

I check immediately before this line for any GL errors and find none.  I think
one of the problems is that Demeter is built with OSG and there may be some
compatibility issue there.  If I put the Demeter code I have into an OSG sample
program, it works.  I would appreciate any feedback on what I am doing wrong and
how to fix it.

Thank you,
Levi

OpenSGNav.cpp code with comments where I added stuff:

/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2005 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.
 *
 * -----------------------------------------------------------------
 * File:          OpenSGNav.cpp,v
 * Date modified: 2005/08/11 02:38:32
 * Version:       1.19.2.1
 * -----------------------------------------------------------------
 *
 *************** < auto-copyright.pl END do not edit this line> ***************/
#define checkGLErrors(); {int err = glGetError(); if(err)printf("gl Error at
line %i of %s:  %s\n", __LINE__, __FILE__, gluErrorString(err));}

#include <vrj/vrjConfig.h>

// Added to make Demeter work with program
#include <GL/glew.h>

#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include <gmtl/Matrix.h>
#include <gmtl/MatrixOps.h>
#include <gmtl/Generate.h>
#include <gmtl/Vec.h>
#include <gmtl/Coord.h>
#include <gmtl/Xforms.h>

#include <vrj/Display/Projection.h>
#include <vrj/Display/CameraProjection.h>

#include <OpenSGNav.h>

#include <OpenSG/OSGMatrix.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGDirectionalLight.h>
#include <OpenSG/OSGSceneFileHandler.h>


//demeter include files
#include <OpenSG/OSGConfig.h>
#include <iostream>
#include <OpenSG/OSGGL.h>
#include <OpenSG/OSGAction.h>
#include <OpenSG/OSGDrawAction.h>
#include <OpenSG/OSGRenderAction.h>
#include <OpenSG/OSGState.h>
#include <OpenSG/OSGMaterial.h>
#include <string.h>


void OpenSGNav::initMethod (void)
{
}

// Handle any initialization needed before API
void OpenSGNav::init()
{
    std::cout << "init()" << std::endl;
   vrj::OpenSGApp::init();      // Call base class init
}

/** Called once per context at context creation */
void OpenSGNav::contextInit()
{
    std::cout << "contextInit()" << std::endl;
   vrj::OpenSGApp::contextInit();
   initGLState();
}

void OpenSGNav::draw()
{
   // Can't draw this way because can't add the terrain to the root node
   //vrj::OpenSGApp::draw();
 
 
   // instead have to draw the terrain this way:
   //////////////////////////////////////////////////////////////////
   // Draw using the Demeter Terrain Engine
   // Main rendering loop
        glDisable(GL_LIGHTING);
        glPushMatrix();
        glScalef(1,1,-1);
        glTranslatef(mPos[0],mPos[1],0);

        if (mWireframe)
            glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
        assert(mTerrain);
        mTerrain->SetDetailThreshold(mThreshold);
        mTerrain->ModelViewMatrixChanged();
        glPushMatrix();
        mTerrain->Render(); ////////////////////// This is where the code 
crashes
        glPopMatrix();

        if (mWireframe)
            glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
        glPopMatrix();
        if(mNeedUpdate && mTerrain->GetUsingUAVMap())
        {
            mTerrain->UpdateUAVMap(mMapBuffer, mMapWidth, mMapHeight, mTopLeft,
mBottomRight);
            mNeedUpdate = false;
        }   
        glEnable(GL_LIGHTING);
    // End of rendering loop
    //////////////////////////////////////////////////////////////////
  
}

void OpenSGNav::preFrame()
{
    std::cout << "preFrame()" << std::endl;
   const float inc_vel(0.005f);
   const float max_vel(0.5f);

   // Update velocity
   if(mButton0->getData() == gadget::Digital::ON)
   {
      mVelocity += inc_vel;
      std::cout << "vel: " << mVelocity << std::endl;
   }
   else if(mVelocity > 0)
   {
      std::cout << "vel: " << mVelocity << std::endl;
      mVelocity -= inc_vel;
   }

   // Restrict range
   if(mVelocity < 0)
   {
      mVelocity = 0;
   }
   if(mVelocity > max_vel)
   {
      mVelocity = max_vel;
   }

   if(mButton1->getData() == gadget::Digital::ON)
   {
      mVelocity = 0;
   }

   // Travel in model
   // - Find forward direction of wand
   // - Translate along that direction

   // Get the wand matrix
   // NOTE: We ask for the wand transformation matrix in the same units that
   // we are using for this application object.
   gmtl::Matrix44f wand_mat(mWandPos->getData(getDrawScaleFactor()));
   gmtl::Vec3f z_dir = gmtl::Vec3f( 0.0f, 0.0f, mVelocity);
   gmtl::Vec3f trans = wand_mat * z_dir;

   OSG::Matrix trans_mat(OSG::Matrix::identity());
   trans_mat.setTranslate(trans[0], trans[1], trans[2]);

   OSG::beginEditCP(mSceneTransform);
      mSceneTransform->getMatrix().multLeft(trans_mat);
   OSG::endEditCP(mSceneTransform);
}

/** Initialize GL state. Hold over from regular OGL apps */
void OpenSGNav::initGLState()
{
    std::cout << "initGLState()" << std::endl;
   GLfloat light0_ambient[]  = { 0.1f,  0.1f,  0.1f, 1.0f };
   GLfloat light0_diffuse[]  = { 0.7f,  0.7f,  0.7f, 1.0f };
   GLfloat light0_specular[] = { 1.0f,  1.0f,  1.0f, 1.0f };
   GLfloat light0_position[] = { 0.5f, 0.75f, 0.75f, 0.0f };

   GLfloat mat_ambient[]   = { 0.7f, 0.7f, 0.7f, 1.0f };
   GLfloat mat_diffuse[]   = { 1.0f, 0.5f, 0.8f, 1.0f };
   GLfloat mat_specular[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
   GLfloat mat_shininess[] = { 50.0f };
//   GLfloat mat_emission[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
   GLfloat no_mat[]        = { 0.0f, 0.0f, 0.0f, 1.0f };

   glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
   glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
   glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);

   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);

   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   glEnable(GL_COLOR_MATERIAL);
   glShadeModel(GL_SMOOTH);

   // OpenSG does not handle this yet, being smart about it is not
   // that trivial...
   glEnable(GL_NORMALIZE);
}

void OpenSGNav::initScene()
{
   vprDEBUG_OutputGuard(vprDBG_ALL, vprDBG_CRITICAL_LVL,
                        "OpenSGNav::initScene() called.\n",
                        "OpenSGNav::initScene() exiting.\n");

   // --- Initialize vrj devices --- //
   std::string wand("VJWand");
   std::string but0("VJButton0");
   std::string but1("VJButton1");
   std::string but2("VJButton2");
   mWandPos.init(wand);
   mButton0.init(but0);
   mButton1.init(but1);
   mButton2.init(but2);


    //////////////////////////////////////////////////////////////////
    // Loading terrain with the Demeter Terrain Engine
        mTerrain = NULL;

        Demeter::Settings::GetInstance()->SetMediaPath("./data");
        mThreshold = 8;

        mTerrain = new Demeter::Terrain();
   
        std::string artPath = getenv("UAV_ART_DIR");
        std::string dted = artPath + "/dted";
        strcpy(mElevationFile,dted.c_str());
        std::string groundTexture = artPath + "/ground_center.jpg";
        strcpy(mTextureFile,groundTexture.c_str ());
        std::string dirt = artPath + "/dirt2.jpg";
        strcpy(mCommonTexFile,dirt.c_str());

        assert(mTerrain && "mTerrain is NULL");
   
        // Commands to load the terrain
       
Demeter::NellisElevationLoader::GetInstance()->LoadElevations(mElevationFile,700.0,3.0,mTerrain);
       
Demeter::SDLTextureLoader::GetInstance()->LoadTerrainTexture(mTextureFile,mTerrain);
       
Demeter::SDLTextureLoader::GetInstance()->LoadCommonTerrainTexture(mCommonTexFile,mTerrain);
   
        mTerrain->SetMaximumVisibleBlockSize(512);
        mTerrain->SetUsingShadow(true);
        mTerrain->SetUsingTrailAndShadow(false);
        mTerrain->SetUsingUAVMap(true);
   
        mPos[0] = -mTerrain->GetHeight()*0.5;
        mPos[1] = -mTerrain->GetWidth()*0.5;
        mPos[2] = -mTerrain->GetMaxElevation()*0.5;

        std::cout<<"Depth:"<<2*mPos[0]
             <<" ,Width:"<<2*mPos[1]
             <<" ,Height"<<2*mPos[2]<<std::endl;

        mWireframe = false;

        mMapWidth = 0;
        mMapHeight = 0;
        mMapBuffer = NULL;
        mNeedUpdate = false;
    // End of code to load the terrain

    // Cannot execute the next line to make the terrain the model root
    //mModelRoot = mTerrain;
    //////////////////////////////////////////////////////////////////


   // --- Light setup --- //
   // - Add directional light for scene
   // - Create a beacon for it and connect to that beacon
   mLightNode   = OSG::Node::create();
   mLightBeacon = OSG::Node::create();
   OSG::DirectionalLightPtr light_core = OSG::DirectionalLight::create();
   OSG::TransformPtr light_beacon_core = OSG::Transform::create();

   // Setup light beacon
   OSG::Matrix light_pos;
   light_pos.setTransform(OSG::Vec3f( 2.0f, 5.0f, 4.0f));

   OSG::beginEditCP(light_beacon_core, OSG::Transform::MatrixFieldMask);
      light_beacon_core->setMatrix(light_pos);
   OSG::endEditCP(light_beacon_core, OSG::Transform::MatrixFieldMask);

   OSG::beginEditCP(mLightBeacon);
      mLightBeacon->setCore(light_beacon_core);
   OSG::endEditCP(mLightBeacon);

   // Setup light node
   OSG::addRefCP(mLightNode);
   OSG::beginEditCP(mLightNode);
      mLightNode->setCore(light_core);
      mLightNode->addChild(mLightBeacon);
   OSG::endEditCP(mLightNode);

   OSG::beginEditCP(light_core);
      light_core->setAmbient   (0.9, 0.8, 0.8, 1);
      light_core->setDiffuse   (0.6, 0.6, 0.6, 1);
      light_core->setSpecular  (1, 1, 1, 1);
      light_core->setDirection (0, 0, 1);
      light_core->setBeacon    (mLightNode);
   OSG::endEditCP(light_core);

   // --- Setup Scene -- //
   // add the loaded scene to the light node, so that it is lit by the light
   OSG::addRefCP(mModelRoot);
   OSG::beginEditCP(mLightNode);
      mLightNode->addChild(mModelRoot);
   OSG::endEditCP(mLightNode);

   // create the root part of the scene
   mSceneRoot = OSG::Node::create();
   mSceneTransform = OSG::Transform::create();

   // Set up the root node
   OSG::beginEditCP(mSceneRoot);
      mSceneRoot->setCore(mSceneTransform);
      mSceneRoot->addChild(mLightNode);
   OSG::endEditCP(mSceneRoot);
  
   std::cout << "done initializing terrain in initScene()" << std::endl;
}



-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to