Hi Enrico,

On Mon, 2004-05-17 at 10:18, Enrico Borrione wrote:
> after a while i returned on my former project.
> I am trying to cube texture a sphere, using a cube map. 
> I set up all the stuff, but the cube map just wont appear. What should
> be the prob?

Not sure, it's working for other people, so in general it's ok. Take a
look at the attached example.

Do you get warnings on the console? When using debug libraries OpenSG
does more error checking on OpenGL, and that might help finding out what
the problem is. It might also be useful to set OSG_LOG_LEVEL to debug
and OSG_LOG_FILE to osg_out.log and send me the file (compressed, please
;).

Andreas' comment is important, too: you need to have texture
coordinates, otherwise it won't show. A texGenChunk is the easiest way
to get them.

> I've looked at the sample files and discovered that before drawing any
> geometry, the map is activated via a
> 
> map->activate( dact );
> 
> where dact is a drawing action. 
> Do i have to insert that snippet of code every time a cube map is
> involved?

No, that's just for very low-level testing, an application will never
need to do that.

Hope it helps

        Dirk


#include <OSGGLUT.h>
#include <OSGGLEXT.h>
#include <OSGConfig.h>
#include <OSGSimpleGeometry.h>
#include <OSGPassiveWindow.h>
#include <OSGSimpleSceneManager.h>
#include <OSGSceneFileHandler.h>

#include <OSGMaterialGroup.h>
#include <OSGSimpleMaterial.h>
#include <OSGCubeTextureChunk.h>
#include <OSGTexGenChunk.h>
#include <OSGTextureTransformChunk.h>

#include <OSGImage.h>

OSG_USING_NAMESPACE

SimpleSceneManager *mgr;

SimpleMaterialPtr cuberefmat;
TextureTransformChunkPtr ttransform;

// redraw the window
void display(void)
{      
     
    // transform the cube reflection texture coords by the inverse viewer, 
    // orientation only
    NodePtr beacon = mgr->getAction()->getCamera()->getBeacon();    
    Matrix m;
    beacon->getToWorld(m);
    
    m.invert();
    m[3].setValues(0,0,0,0);
    
    beginEditCP(ttransform);
    ttransform->setMatrix(m);
    endEditCP(ttransform);
    
    // render
    
    mgr->redraw();

    // all done, swap    
    glutSwapBuffers();
}

// 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:    exit(1);
    }
}



Action::ResultE setMaterial(NodePtr& node)
{   
    GeometryPtr geo = GeometryPtr::dcast(node->getCore());
    
    if(geo!=NullFC)
    {
        geo->setMaterial(cuberefmat);
    }   
    
    MaterialGroupPtr mg = MaterialGroupPtr::dcast(node->getCore());
    
    if(mg!=NullFC)
    {
        mg->setMaterial(cuberefmat);
    }   
    
    return Action::Continue; 
}


int main(int argc, char **argv)
{
    osgInit(argc,argv);

    // GLUT init
    glutInit(&argc, argv);
    
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    glutCreateWindow("OpenSG");
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    PassiveWindowPtr pwin=PassiveWindow::create();
    pwin->init();

    // create the scene
    NodePtr scene;
    
    if(argc > 1)
    {
        scene = SceneFileHandler::the().read(argv[1]);
    }
    else
    {
        // scene = makeTorus(.5, 3, 16, 16);
        scene = makeSphere(3, 1);
    }

    // create the cube reflection material
    cuberefmat = SimpleMaterial::create();
    
    ttransform = TextureTransformChunk::create();
    
    CubeTextureChunkPtr cubetex = CubeTextureChunk::create();
   
    UChar8 negz[] = {  255,0,0,  128,0,0,  64,0,0,   255,255,255 },
           posz[] = {  0,255,0,  0,128,0,  0,64,0,  255,255,255 },
           negy[] = {  0,0,255,  0,0,128,  0,0,64,  255,255,255 },
           posy[] = {  255,255,0,  128,128,0,  64,64,0,  255,255,255 },
           negx[] = {  255,0,255,  128,0,128,  64,0,64,  255,255,255 },
           posx[] = {  0,255,255,  0,128,128,  0,64,64,  255,255,255 };
    
    ImagePtr inegz = Image::create();
    ImagePtr iposz = Image::create();
    ImagePtr inegy = Image::create();
    ImagePtr iposy = Image::create();
    ImagePtr inegx = Image::create();
    ImagePtr iposx = Image::create();

    inegz->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, negz );
    iposz->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, posz );
    inegy->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, negy );
    iposy->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, posy );
    inegx->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, negx );
    iposx->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, posx );
        
    beginEditCP(cubetex);
    cubetex->setImage( inegz );
    cubetex->setPosZImage( iposz );
    cubetex->setPosYImage( iposy );
    cubetex->setNegYImage( inegy );
    cubetex->setPosXImage( iposx );
    cubetex->setNegXImage( inegx );
    cubetex->setMinFilter( GL_LINEAR );
    cubetex->setMagFilter( GL_NEAREST );
    cubetex->setWrapS( GL_REPEAT );
    cubetex->setWrapT( GL_REPEAT );
    cubetex->setEnvMode( GL_MODULATE );
    endEditCP(cubetex);

    
    TexGenChunkPtr texgen = TexGenChunk::create();
    beginEditCP(texgen);
    texgen->setGenFuncS(GL_REFLECTION_MAP_ARB);
    texgen->setGenFuncT(GL_REFLECTION_MAP_ARB);
    texgen->setGenFuncR(GL_REFLECTION_MAP_ARB);   
    endEditCP(texgen);
  
    beginEditCP(cuberefmat);
    cuberefmat->setDiffuse( Color3f( .8,.8,.8 ) );
    cuberefmat->setAmbient( Color3f( .5,.5,.5 ) );
    cuberefmat->setSpecular( Color3f( 1,1,1 ) );
    cuberefmat->setShininess( 30 );
    cuberefmat->addChunk(cubetex);
    cuberefmat->addChunk(ttransform);
    cuberefmat->addChunk(texgen); 
    endEditCP(cuberefmat);
    
    // set all geos to use the cube reflection material
    traverse(scene, 
             osgTypedFunctionFunctor1CPtrRef<Action::ResultE, NodePtr>
             (setMaterial));

    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // create the window and initial camera/viewport
    mgr->setWindow(pwin );
    // tell the manager what to manage
    mgr->setRoot  (scene);
    
    // show the whole scene
    mgr->showAll();
    mgr->redraw();
   
    // GLUT main loop
    glutMainLoop();

    return 0;
}

Reply via email to