[osg-users] Using multiples texture for a geometry

Sebastian Messerschmidt sebastian.messerschmidt at gmx.de
Mon May 9 02:00:01 PDT 2016


Hi Florian,

see the attached example. It will load to textures and apply them to two 
different sets of texture coordinates.
Simply replace the image file paths to test it.
If you use the 0,0 texture coordinate for all triangle/quad edges you 
will, depending on the wrap mode, end up having the image's pixel color 
over the whole primitive.



<snip>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>



osg::Node* createMT_Graph()
{
     osg::Geometry* geom = new osg::Geometry;
     osg::Vec3Array* vert_array = new osg::Vec3Array;
     osg::Vec2Array* uv_array = new osg::Vec2Array;
     osg::Vec2Array* uv2_array = new osg::Vec2Array;
     osg::Vec3Array* normal_array = new osg::Vec3Array;
     osg::Vec4Array* color_array = new osg::Vec4Array;

     normal_array->push_back(osg::Vec3(0, 0, 1));
     color_array->push_back(osg::Vec4(1, 1, 1, 1));

     for (unsigned i = 0; i < 40; ++i)
     {
         bool even = (0 == i % 2);
         float x = (i) / 2;
         float y = even ? 0 : 1;
         std::cout << x << "," << y << std::endl;
         vert_array->push_back(osg::Vec3(x,y,0 ));
         uv_array->push_back(osg::Vec2(x, y));
         uv2_array->push_back(osg::Vec2(0.5*x, y));
     }

     geom->setVertexArray(vert_array);
     geom->setTexCoordArray(0, uv_array);
     geom->setTexCoordArray(1, uv2_array);
     geom->setNormalArray(normal_array, osg::Array::BIND_OVERALL);
     geom->setNormalArray(color_array, osg::Array::BIND_OVERALL);
     geom->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP,0, 
vert_array->size()));

     return geom;
}



int main(int argc, char** argv)
{
     osgViewer::Viewer viewer;
     viewer.addEventHandler( new 
osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
     viewer.addEventHandler(new osgViewer::StatsHandler());

     osg::Node* node = createMT_Graph();

     osg::Texture2D* tex1 = new 
osg::Texture2D(osgDB::readImageFile("./resources/textures/ziggis.png"));
     tex1->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
     tex1->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
node->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex1, 
osg::StateAttribute::ON);

     osg::Texture2D* tex2 = new 
osg::Texture2D(osgDB::readImageFile("./resources/textures/wasch.png"));
     tex2->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
     tex2->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
node->getOrCreateStateSet()->setTextureAttributeAndModes(1, tex2, 
osg::StateAttribute::ON);

     viewer.setUpViewInWindow(10,10,1280,1024, 0);
     viewer.setSceneData( node );

     return( viewer.run() );
}
</snip>





More information about the osg-users mailing list