[osg-users] Drawing a textured cube
Christian Buchner
christian.buchner at gmail.com
Sun Apr 10 08:59:07 PDT 2016
I believe you need to duplicate your vertices so that you can apply the
correct texcoords independently for each face.
Also this allows you to set the correct normal vectors for each face
(assuming you want flat shading with hard edges).
So overall you would have 4 vertices per face, and 24 vertices in total.
Christian
2016-04-08 22:32 GMT+02:00 Florian GOLESTIN <florian.golestin at gmail.com>:
> Hi everyone,
>
> 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.
>
>
> Below is the shape:
>
>
> Code:
>
> osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(8);
>
> (*vertices)[0].set( 0.0f, 0.0f, 0.0f);
> (*vertices)[1].set( 1.0f, 0.0f, 0.0f);
> (*vertices)[2].set( 1.0f, 0.0f, 1.0f);
> (*vertices)[3].set( 0.0f, 0.0f, 1.0f);
> (*vertices)[4].set( 0.0f, 1.0f, 1.0f);
> (*vertices)[5].set( 1.0f, 1.0f, 1.0f);
> (*vertices)[6].set( 1.0f, 1.0f, 0.0f);
> (*vertices)[7].set( 0.0f, 1.0f, 0.0f);
>
>
>
> osg::ref_ptr<osg::DrawElementsUInt> indices = new
> osg::DrawElementsUInt(GL_TRIANGLES);
> // Front
> indices->push_back( 0 ); indices->push_back( 1 ); indices->push_back( 2 );
> indices->push_back( 3 ); indices->push_back( 0 ); indices->push_back( 2 );
> // Top
> indices->push_back( 3 ); indices->push_back( 4 ); indices->push_back( 2 );
> indices->push_back( 4 ); indices->push_back( 5 ); indices->push_back( 2 );
> // Back
> indices->push_back( 6 ); indices->push_back( 7 ); indices->push_back( 5 );
> indices->push_back( 4 ); indices->push_back( 7 ); indices->push_back( 5 );
> // left ---
> indices->push_back( 7 ); indices->push_back( 3 ); indices->push_back( 4 );
> indices->push_back( 0 ); indices->push_back( 7 ); indices->push_back( 3 );
> // right
> indices->push_back( 2 ); indices->push_back( 5 ); indices->push_back( 6 );
> indices->push_back( 1 ); indices->push_back( 2 ); indices->push_back( 6 );
> // Bottom
> indices->push_back( 0 ); indices->push_back( 7 ); indices->push_back( 1 );
> indices->push_back( 1 ); indices->push_back( 6 ); indices->push_back( 7 );
>
>
> osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
> geom->setVertexArray(vertices);
> geom->addPrimitiveSet(indices);
> osgUtil::SmoothingVisitor::smooth(*geom);
>
>
>
>
> I've a first question here: Do the indice order has an importance?
>
> For instance first line could also be:
>
> Code:
> indices->push_back( 2 ); indices->push_back( 1 ); indices->push_back( 0 );
>
>
>
>
> Then the texture:
>
>
> Code:
>
> osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(16);
> (*texcoords)[0].set( osg::Vec2(0.0f, 0.0f) );
> (*texcoords)[1].set( osg::Vec2(0.0f, 1.0f) );
> (*texcoords)[2].set( osg::Vec2(1.0f, 1.0f) );
> (*texcoords)[3].set( osg::Vec2(1.0f, 0.0f) );
> //
> (*texcoords)[4].set( osg::Vec2(0.0f, 0.0f) );
> (*texcoords)[5].set( osg::Vec2(0.0f, 1.0f) );
> (*texcoords)[6].set( osg::Vec2(1.0f, 1.0f) );
> (*texcoords)[7].set( osg::Vec2(1.0f, 0.0f) );
>
> geom->setTexCoordArray(0, texcoords.get());
>
> osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
> osg::ref_ptr<osg::Image> image =
> osgDB::readImageFile("stone_3_2048x2048.jpg");
> texture->setImage(image);
> texture->setUnRefImageDataAfterApply(true);
> texture->setDataVariance(osg::Object::DYNAMIC);
>
> node->addDrawable(geom);
> node->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture);
>
>
>
>
>
> 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.
> Doubling the texture brought texture on 4 (front, top, back, bottom) 6
> faces .
>
>
>
> Do you have any suggestion or documentation I can look for?
>
>
> Thank you!
>
> Cheers,
> Florian[/img]
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=66771#66771
>
>
>
>
> Attachments:
>
> http://forum.openscenegraph.org//files/screenshot_from_2016_04_08_22_21_22_108.png
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20160410/ca1c4ab6/attachment-0003.htm>
More information about the osg-users
mailing list