Hello again,

I home I'm not swamping the resources of the list with my questions... Basically
I'm trying things as I'm pretty new to working with OpenSG, and I learn by
doing... So I appreciate your help and can understand if you think some of my
questions are a bit basic...

I am experimenting with adding shaders to objects. Basically I created a wrapper
class to encapsulate an OpenSG object, which has several useful methods like
setTexture(), translate(), rotate(), scale() etc. which I like because it keeps
beginEditCP()/endEditCP() calls out of my main code (mostly). Well, the class
has a setShaderProgram() method :

void OSGObject::setShaderProgram( std::string vertexProgramFile,
                                  std::string fragmentProgramFile ) {
    // Read shader files
    std::string vertexProgram   = TextFile::read(   vertexProgramFile );
    std::string fragmentProgram = TextFile::read( fragmentProgramFile );

    // Create the shader chunk if it wasn't already.
    if ( mShaderProgram == NullFC ) {
        mShaderProgram = OSG::SHLChunk::create();
    }

    // Add the shader program code to the shader chunk.
    OSG::beginEditCP( mShaderProgram );
    {
        mShaderProgram->setVertexProgram  (   vertexProgram );
        mShaderProgram->setFragmentProgram( fragmentProgram );
    }
    OSG::endEditCP  ( mShaderProgram );

    // Create a default material if we don't have one already.
    if ( mMaterial == NullFC ) {
        mMaterial = OSG::SimpleMaterial::create();
        OSG::beginEditCP( mMaterial );
        {
            // Default material properties
            mMaterial->setAmbient(Color3f(0.1,0.1,0.1));
            mMaterial->setDiffuse(Color3f(1,1,1));
            mMaterial->setSpecular(Color3f(1,1,1));
            mMaterial->setShininess(20);
        }
        OSG::endEditCP  ( mMaterial );
    }

    // Add the shader chunk to the material.
    OSG::beginEditCP( mMaterial );
    {
        mMaterial->addChunk( mShaderProgram );
    }
    OSG::endEditCP  ( mMaterial );

    // Add the material to a MaterialGroup and add that to the hierarchy
    addMaterialToGroup( mMaterial );
}

void OSGObject::addMaterialToGroup( OSG::SimpleMaterialPtr material ) {
    // Put the material in the material group, creating it if it isn't already
    if ( mMaterialGroup == NullFC ) {
        // Create a node that will hold the transform, and will be a child of
        // the root and a parent to the object.
        OSG::NodePtr child = OSG::Node::create();
        OSG::beginEditCP( child );
        {
            child->setCore( mTransform );
            child->addChild( mObject );
        }
        OSG::endEditCP( child );

        // Create the material node in the hierarchy
        mMaterialGroup = MaterialGroup::create();
        // Not sure if I need a begin/endEditCP here
        mMaterialGroup->setMaterial( material );

        OSG::beginEditCP( mRoot );
        {
            mRoot->setCore( mMaterialGroup );
            // If this is the first time we create the MaterialGroup, the object
            // will still be a child of the root, so we need to remove it so
            // it's a child only of the transform node (child)
            if ( mRoot->findChild( mObject ) != -1 )
                mRoot->subChild( mObject );

            mRoot->addChild( child );
        }
        OSG::endEditCP  ( mRoot );
    }
    else {
        // Not sure if I need a begin/endEditCP here
        mMaterialGroup->setMaterial( material );
    }
}

Now, I have this code in my main program which creates an object adn assigns a
vertex shader and a fragment shader to it.

    OSGObject *sphere = new OSGObject( OSG::makeSphere(3, 2.0f) );
    sphere->setShaderProgram( "shaders/minimal.vert", "shaders/minimal.frag" );
    mBoat->addChild( sphere );

The shaders are real simple (from the shader tutorial on lighthouse3d.com :

// minimal.vert: minimal vertex shader
// www.lighthouse3d.com

void main()
{
        gl_Position = ftransform();
}

// minimal.frag: minimal fragment shader
// www.lighthouse3d.com

void main()
{
        gl_FragColor = vec4(0.4,0.4,0.8,1.0);
}

The result is that after all initialisation and scenegraph building is done (as
I said before, I'm using VR Juggler with OpenSG), the first call to
OpenSGApp::draw() generates a segfault. I ran the program in gdb, and what I
got at the segfault is :

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 104516528 (LWP 21228)]
0x0075db6f in std::_Rb_tree_decrement () from /usr/lib/libstdc++.so.6

Now, I know that even though the segfault is in an STL tree method, it can (and
probably is) generated by my code. What I don't know is how to go about
debugging this.

If I comment out the addShaderProgram() line in my main program, it runs fine.
So I guess that the problem is in the shader handling somewhere. Does my
subgraph seem good? In a nutshell, it looks like this for every OSGObject :


If the object has a material (either a SimpleMaterial, a texture or a shader) :

mRoot (core=MaterialGroup containing the SHLChunk)
  |
node (core=mTransform (Transform))
  |
mObject (geometry, in this case a sphere created with OSG::makeSphere)

If not :

mRoot (core=mTransform (Transform))
  |
mObject (geometry, could be loaded from a VRML97 file or created with OpenSG's
         simple geometry generators)


How could I go about debugging this? And on that same subject, are there any
general tips about debugging OpenSG programs? I know my way around gdb for most
of my debugging needs, but if there are any specific techniques you find useful
in debugging OpenSG programs, I'd love to read them.


Thanks in advance,

--
______________________________________________________
Jean-Sebastien Guay     [EMAIL PROTECTED]
                         http://whitestar02.webhop.org


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to