Hi Jerome,

On Wed, 2004-11-10 at 22:11, Jérôme Scholler wrote:
> Thanks Dirk for your answer.
> 
> You understand well my problem. But I don't know what to do with your
> answer. :)
> Actually, I load VRML with the buildin loader. So I don't see TextureChunks.
> I have a lot of objects in my VRML file so I can't afford to treat
> individually all my objects.
> Is there a way to set globally that GL_STUFF?

Hm, not really. That's specified in the VRML file (every ImageTexture
has repeatS and repeatT fields, which are true by default). If you
create the VRML files yourself, try to set these to false. 

If you get your VRML from a program you can't change you'll have to
traverse the graph and manipulate the Chunks in the graph. I attached an
example program that traverses the graph and changes all the textures it
can find.

Hope it helps

        Dirk



// OpenSG Tutorial Example: Loading
//
// This example shows how to load a scene file using OpenSG.
// The supported formats right now are VRML97, OBJ, OFF and RAW, so just
// calling this program with a scene file as a parameter should load the scene
// file.
//

// Headers
#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGAction.h>
#include <OpenSG/OSGSimpleTexturedMaterial.h>
#include <OpenSG/OSGTextureChunk.h>
#include <OpenSG/OSGMaterialGroup.h>

#include <OpenSG/OSGSceneFileHandler.h>


// Activate the OpenSG namespace
OSG_USING_NAMESPACE

// The SimpleSceneManager to manage simple applications
SimpleSceneManager *mgr;

// forward declaration so we can have the interesting stuff upfront
int setupGLUT( int *argc, char *argv[] );

void doMaterial(MaterialPtr m)
{
    if(m == NullFC)
        return;
        
    if(m->getType().isDerivedFrom(SimpleTexturedMaterial::getClassType()))
    {
        FWARNING(("Found SimpleTexturedMaterial, can't change wrap mode "
                  "for it!\n"));
        return;
    }
    
    ChunkMaterialPtr cm = ChunkMaterialPtr::dcast(m);

    if(cm != NullFC)
    {
        TextureChunkPtr tc;

        MFStateChunkPtr &f = cm->getChunks();
        
        for(int i = 0; i < f.size(); ++i)
        {
            StateChunkPtr sc = f[i];
            
            tc = TextureChunkPtr::dcast(sc);

            if(tc != NullFC)
            {
                beginEditCP(tc, TextureChunk::WrapSFieldMask |
                                TextureChunk::WrapTFieldMask);
                tc->setWrapS(GL_CLAMP_TO_EDGE);
                tc->setWrapT(GL_CLAMP_TO_EDGE);
                endEditCP(tc,   TextureChunk::WrapSFieldMask |
                                TextureChunk::WrapTFieldMask);

            }
        }
    }
}

Action::ResultE changeTexture(NodePtr& node)
{   
    if(node->getCore()->getType().isDerivedFrom(Geometry::getClassType()))
    {
        GeometryPtr g = GeometryPtr::dcast(node->getCore());
        
        doMaterial(g->getMaterial());
    }   
    if(node->getCore()->getType().isDerivedFrom(MaterialGroup::getClassType()))
    {
        MaterialGroupPtr mg = MaterialGroupPtr::dcast(node->getCore());
        
        doMaterial(mg->getMaterial());
    }   
    
    return Action::Continue; 
}


// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    // GLUT init
    int winid = setupGLUT(&argc, argv);

    // the connection between GLUT and OpenSG
    GLUTWindowPtr gwin= GLUTWindow::create();
    gwin->setId(winid);
    gwin->init();

    // load the scene

    NodePtr scene;
    
    if(argc < 2)
    {
        FWARNING(("No file given!\n"));
        FWARNING(("Supported file formats:\n"));
        
        std::list<const char*> suffixes;
        SceneFileHandler::the().getSuffixList(suffixes);
        
        for(std::list<const char*>::iterator it  = suffixes.begin();
                                             it != suffixes.end();
                                           ++it)
        {
            FWARNING(("%s\n", *it));
        }

        scene = makeTorus(.5, 2, 16, 16);
    }
    else
    {
        /*
            All scene file loading is handled via the SceneFileHandler.
        */
        scene = SceneFileHandler::the().read(argv[1]);
    }

     // Change all textures
    traverse(scene, 
             osgTypedFunctionFunctor1CPtrRef<Action::ResultE,
                                             NodePtr        >(changeTexture));

    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // tell the manager what to manage
    mgr->setWindow(gwin );
    mgr->setRoot  (scene);

    // show the whole scene
    mgr->showAll();

    // GLUT main loop
    glutMainLoop();

    return 0;
}

//
// GLUT callback functions
//

// redraw the window
void display(void)
{
    mgr->idle();
    mgr->redraw();
}

// react to size changes
void reshape(int w, int h)
{
    mgr->resize(w, h);
    glutPostRedisplay();
}

// react to mouse button presses
void mouse(int button, int state, int x, int y)
{

    if (state)
        mgr->mouseButtonRelease(button, x, y);
    else
        mgr->mouseButtonPress(button, x, y);
        
    glutPostRedisplay();
}

// react to mouse motions with pressed buttons
void motion(int x, int y)
{

    mgr->mouseMove(x, y);
    glutPostRedisplay();
}

// react to keys
void keyboard(unsigned char k, int , int )
{
    switch(k)
    {
        case 27:    
        {
            OSG::osgExit();
            exit(0);
        }
        break;
        
        case 'f': 
        {  
            mgr->setNavigationMode(Navigator::FLY);
        }
        break;
        
        case 't':   
        {
            mgr->setNavigationMode(Navigator::TRACKBALL);
        }
        break;
    }
}

// setup the GLUT library which handles the windows for us
int setupGLUT(int *argc, char *argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    int winid = glutCreateWindow("OpenSG");
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    return winid;
}

Reply via email to