<div dir="ltr"><div><div><div><br></div>I believe you need to duplicate your vertices so that you can apply the correct texcoords independently for each face.<br></div>Also this allows you to set the correct normal vectors for each face (assuming you want flat shading with hard edges).<br><br></div><div>So overall you would have 4 vertices per face, and 24 vertices in total.<br></div><div><br></div>Christian<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">2016-04-08 22:32 GMT+02:00 Florian GOLESTIN <span dir="ltr"><<a href="mailto:florian.golestin@gmail.com" target="_blank">florian.golestin@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi everyone,<br>
<br>
I'm learning OSG with the quick start guide and the example but I'm struggling drawing a textured cube. The shape is correct but only 4 faces are textured on 6.<br>
<br>
<br>
Below is the shape:<br>
<br>
<br>
Code:<br>
<br>
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(8);<br>
<br>
(*vertices)[0].set( 0.0f, 0.0f, 0.0f);<br>
(*vertices)[1].set( 1.0f, 0.0f, 0.0f);<br>
(*vertices)[2].set( 1.0f, 0.0f, 1.0f);<br>
(*vertices)[3].set( 0.0f, 0.0f, 1.0f);<br>
(*vertices)[4].set( 0.0f, 1.0f, 1.0f);<br>
(*vertices)[5].set( 1.0f, 1.0f, 1.0f);<br>
(*vertices)[6].set( 1.0f, 1.0f, 0.0f);<br>
(*vertices)[7].set( 0.0f, 1.0f, 0.0f);<br>
<br>
<br>
<br>
osg::ref_ptr<osg::DrawElementsUInt> indices = new osg::DrawElementsUInt(GL_TRIANGLES);<br>
// Front<br>
indices->push_back( 0 ); indices->push_back( 1 ); indices->push_back( 2 );<br>
indices->push_back( 3 ); indices->push_back( 0 ); indices->push_back( 2 );<br>
// Top<br>
indices->push_back( 3 ); indices->push_back( 4 ); indices->push_back( 2 );<br>
indices->push_back( 4 ); indices->push_back( 5 ); indices->push_back( 2 );<br>
// Back<br>
indices->push_back( 6 ); indices->push_back( 7 ); indices->push_back( 5 );<br>
indices->push_back( 4 ); indices->push_back( 7 ); indices->push_back( 5 );<br>
// left ---<br>
indices->push_back( 7 ); indices->push_back( 3 ); indices->push_back( 4 );<br>
indices->push_back( 0 ); indices->push_back( 7 ); indices->push_back( 3 );<br>
// right<br>
indices->push_back( 2 ); indices->push_back( 5 ); indices->push_back( 6 );<br>
indices->push_back( 1 ); indices->push_back( 2 ); indices->push_back( 6 );<br>
// Bottom<br>
indices->push_back( 0 ); indices->push_back( 7 ); indices->push_back( 1 );<br>
indices->push_back( 1 ); indices->push_back( 6 ); indices->push_back( 7 );<br>
<br>
<br>
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;<br>
geom->setVertexArray(vertices);<br>
geom->addPrimitiveSet(indices);<br>
osgUtil::SmoothingVisitor::smooth(*geom);<br>
<br>
<br>
<br>
<br>
I've a first question here: Do the indice order has an importance?<br>
<br>
For instance first line could also be:<br>
<br>
Code:<br>
indices->push_back( 2 ); indices->push_back( 1 ); indices->push_back( 0 );<br>
<br>
<br>
<br>
<br>
Then the texture:<br>
<br>
<br>
Code:<br>
<br>
osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(16);<br>
(*texcoords)[0].set( osg::Vec2(0.0f, 0.0f) );<br>
(*texcoords)[1].set( osg::Vec2(0.0f, 1.0f) );<br>
(*texcoords)[2].set( osg::Vec2(1.0f, 1.0f) );<br>
(*texcoords)[3].set( osg::Vec2(1.0f, 0.0f) );<br>
//<br>
(*texcoords)[4].set( osg::Vec2(0.0f, 0.0f) );<br>
(*texcoords)[5].set( osg::Vec2(0.0f, 1.0f) );<br>
(*texcoords)[6].set( osg::Vec2(1.0f, 1.0f) );<br>
(*texcoords)[7].set( osg::Vec2(1.0f, 0.0f) );<br>
<br>
geom->setTexCoordArray(0, texcoords.get());<br>
<br>
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;<br>
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("stone_3_2048x2048.jpg");<br>
texture->setImage(image);<br>
texture->setUnRefImageDataAfterApply(true);<br>
texture->setDataVariance(osg::Object::DYNAMIC);<br>
<br>
node->addDrawable(geom);<br>
node->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture);<br>
<br>
<br>
<br>
<br>
<br>
Here I'm not sure to understand the 'textcoords' array. At the beginning I thought it was the location on the image, but with only 4 parameters, the cube was textured on the front and half of the top face.<br>
Doubling the texture brought texture on 4 (front, top, back, bottom) 6 faces .<br>
<br>
<br>
<br>
Do you have any suggestion or documentation I can look for?<br>
<br>
<br>
Thank you!<br>
<br>
Cheers,<br>
Florian[/img]<br>
<br>
------------------<br>
Read this topic online here:<br>
<a href="http://forum.openscenegraph.org/viewtopic.php?p=66771#66771" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=66771#66771</a><br>
<br>
<br>
<br>
<br>
Attachments:<br>
<a href="http://forum.openscenegraph.org//files/screenshot_from_2016_04_08_22_21_22_108.png" rel="noreferrer" target="_blank">http://forum.openscenegraph.org//files/screenshot_from_2016_04_08_22_21_22_108.png</a><br>
<br>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</blockquote></div><br></div>