1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| static osg::Geode* CreateCube(float fRadius,osg::Vec3 vPosition)
{
//déclaration d'une géométrie : un cube
osg::Geometry* cube = new osg::Geometry();
//déclaration d'un noeud géométrique
osg::Geode* geodeCube = new osg::Geode();
//ajout du cube dans le noeud
geodeCube->addDrawable(cube);
geodeCube->addDrawable(cube);
//déclaration des points composant le carré
osg::Vec3Array* cubeVertices= new osg::Vec3Array;
cubeVertices->push_back( osg::Vec3( vPosition.x()-fRadius/2, vPosition.y(), vPosition.z()+fRadius/2) ); // front left
cubeVertices->push_back( osg::Vec3( vPosition.x()+fRadius/2, vPosition.y(), vPosition.z()+fRadius/2) ); // front right
cubeVertices->push_back( osg::Vec3( vPosition.x()+fRadius/2, vPosition.y(), vPosition.z()-fRadius/2) ); // back right
cubeVertices->push_back( osg::Vec3( vPosition.x()-fRadius/2, vPosition.y(), vPosition.z()-fRadius/2) ); // back left
//association des points composant le carré et le carré
cube->setVertexArray( cubeVertices );
osg::DrawElementsUInt* cubeBase = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
cubeBase->push_back(3);
cubeBase->push_back(2);
cubeBase->push_back(1);
cubeBase->push_back(0);
//ajout au cube de ces nouvelles caractéristiques
cube->addPrimitiveSet(cubeBase);
osg::Vec3Array* texcoords = new osg::Vec3Array(4);
(*texcoords)[0].set(vPosition.x()-fRadius/2, vPosition.y(), vPosition.z()+fRadius/2 ); // front left
(*texcoords)[1].set(vPosition.x()+fRadius/2, vPosition.y(), vPosition.z()+fRadius/2 ); // front right
(*texcoords)[2].set(vPosition.x()+fRadius/2, vPosition.y(), vPosition.z()-fRadius/2 ); // back right
(*texcoords)[3].set(vPosition.x()-fRadius/2, vPosition.y(), vPosition.z()-fRadius/2 ); // back left
cube->setTexCoordArray(0,texcoords);
osg::ref_ptr<osg::Texture2D>faceTexture->setImage(osgDB::readImageFile("rss.gif"));
std::cout<<faceTexture->getImage()->getFileName()<<std::endl;
osg::ref_ptr<osg::StateSet> stateOne (new osg::StateSet());
// Assign texture unit 0 of our new StateSet to the texture
// we just created and enable the texture.
stateOne->setTextureAttributeAndModes(3,faceTexture.get(),osg::StateAttribute::ON);
// Associate this state set with the Geode that contains
// the pyramid:
//cube->setStateSet(stateOne);
geodeCube->setStateSet(stateOne.get());
//geodeCube->setStateSet(stateOne);
return(geodeCube);
}
//puis dans le main :
osg::ref_ptr<osg::Geode> model = Cube::CreateCube(0.9f,tabCube[i]);
// on ajoute le node du modele
model_position->addChild(model.get());
root->addChild(model_position.get()); |
Partager