OpenSceneGraph - Texture on each face of model

1k views Asked by At

I've got another question. Here is my code:

#include <iostream>
#include <osg/Light>
#include <osg/LightSource>
#include <osg/PositionAttitudeTransform>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>

int main(int argc, char** argv)
{
    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cube.3ds"); //importing object
    osg::ref_ptr<osg::StateSet> ss = loadedModel->getOrCreateStateSet();
    osg::ref_ptr<osg::Image> image = osgDB::readImageFile("box_1.png"); //loading texture from file
    osg::ref_ptr<osg::Texture2D> tex(new osg::Texture2D());  /creating texture for model from image
    tex->setImage(image);
    ss->setTextureAttributeAndModes(0, tex);
    osg::ref_ptr<osg::TexGen> texGen(new osg::TexGen());
    texGen->setPlane(osg::TexGen::S, osg::Plane(0.075, 0.0, 0.0, 0.5));
    texGen->setPlane(osg::TexGen::T, osg::Plane(0.0, 0.035, 0.0, 0.3));
    ss->setTextureAttributeAndModes(0, texGen);
    osgViewer::Viewer viewer; //Creating viewer
    viewer.setSceneData(loadedModel);
    viewer.setUpViewOnSingleScreen(0);
    return viewer.run();
    }

So what this code is doing: It takes model from file (i.e. "cube.3s") and adding texture (image from file "box_1.png" to it. You can change texture on object by switching one image to another. But here is a question: He stretching image on whole model. How it can be done just by placing all image on each face of model (they are platonic solid (like pyramid, cube and etc.)).

1

There are 1 answers

0
XenonofArcticus On BEST ANSWER

This all boils down to the texture coordinates that were assigned to the model's vertices when it was built. If you convert cube.3ds to .OSG or .OSGT text format you can see the UV coordinates that the texture mapper uses to determine where in the texture map to look for each corner of each face. This is how textures are "pinned" to faces.

Just load and edit that cube or pyramid in a 3d modeling tool and fix up the UV coordinates to what you want.

Alternately, you could edit the .osgt file to change the UV coordinates assigned to each vertex.