[osg-users] Texture wrapping for a geometry object

Dominic Stalder dominic.stalder at bluewin.ch
Sun Nov 22 07:28:38 PST 2009


Hi there

I would like to wrap (repeated) my texture image on a geometry object. I 
read somewhere, that this is not possible directly on the geometry, but 
how can I do it otherwise? Here is my code:

    /*
     * creates the ground geometry
     */
    // creates the vertices array
    ref_ptr<Vec3Array> verticesGround = new Vec3Array;
    verticesGround->push_back(Vec3(-WORLD_INFINITY, 0.0, 
-WORLD_INFINITY)); // back left
    verticesGround->push_back(Vec3(WORLD_INFINITY, 0.0, 
-WORLD_INFINITY));  // back right
    verticesGround->push_back(Vec3(WORLD_INFINITY, 0.0, 
WORLD_INFINITY));   // front right
    verticesGround->push_back(Vec3(-WORLD_INFINITY, 0.0, 
WORLD_INFINITY));  // front left
   
    // creates the normal array
    ref_ptr<Vec3Array> normalsGround = new Vec3Array;
    normalsGround->push_back(Vec3(0.0, 1.0, 0.0));
   
    // creates the texture coordinates array
    ref_ptr<Vec2Array> texCoordGround = new Vec2Array;
    texCoordGround->push_back(Vec2(0.0, 0.0));
    texCoordGround->push_back(Vec2(1.0, 0.0));
    texCoordGround->push_back(Vec2(1.0, 1.0));
    texCoordGround->push_back(Vec2(0.0, 1.0));
   
    // loads the texture image
    ref_ptr<Image> imgGrass = 
osgDB::readImageFile(Utilities::filePath("res/osg/grass.png").toStdString()); 

   
    // attaches the image to the texture
    ref_ptr<Texture2D> texGround = new Texture2D;
    texGround->setDataVariance(Object::DYNAMIC);
    texGround->setWrap(Texture::WRAP_S, Texture::REPEAT);
    texGround->setWrap(Texture::WRAP_T, Texture::REPEAT);
    texGround->setImage(imgGrass.get());
   
    // releases the image after applying
    texGround->setUnRefImageDataAfterApply(true);
   
    // creates the geometry
    ref_ptr<Geometry> geomGround = new Geometry;
    geomGround->setVertexArray(verticesGround.get());
    geomGround->setNormalArray(normalsGround.get());
    geomGround->setTexCoordArray(0, texCoordGround.get());

    // adds the primitive set to the geometry
    geomGround->addPrimitiveSet(new DrawArrays(PrimitiveSet::QUADS, 0, 4));
   
    // enables all the necessary states for the ground geometry node
    ref_ptr<StateSet> stateGroundGeometry = 
geomGround->getOrCreateStateSet();
    stateGroundGeometry->setTextureAttributeAndModes(0, texGround.get(), 
StateAttribute::ON);

    // creates the geode
    ref_ptr<Geode> geoGround = new Geode;
    geoGround->addDrawable(geomGround.get());

Thanks a lot in advance!

Best regards
Dominic


More information about the osg-users mailing list