[osg-users] Using multiples texture for a geometry

Florian GOLESTIN florian.golestin at gmail.com
Mon May 2 13:08:28 PDT 2016


Hi Robert,

Thanks for your response!

I might have the two vectors for the textures coordinate.
Basically my idea is to have cubes to draw Labyrinth.
I read a file such as:

Code:

xxxxx
x.....x
xxxxx



Where 'x' represents a wall.

I've a class named Level that hold two Vec2Array for the two textures, it also hold vertices and normals.
What I do is
 - Even: the first vector is filled with texture coordinates while the second is filled with 0
 - Odd: then the second time the first vector is filled with 0 and the second with texture coordinates.

Here is the code:

Code:

float	x = 0;
float	y = 0;
int wallCount = 0;

while(std::getline(stream, line))
      {
	std::stringstream linestream(line);

	while (linestream >> block)
	  {
	    float posx = 1.0 * x; 
	    float posy = 1.0 * y;	      
	    float endx = posx + 1.0;
	    float endy = posy + 1.0;
	    switch (block)
	      {
	      case 'x':
		if (wallCount % 2) // Odd or Even?
		    createCube(level.get(), *level->texcoords, *level->texcoords2, posx, posy, endx, endy);
		else
		    createCube(level.get(), *level->texcoords2, *level->texcoords, posx, posy, endx, endy);
		wallCount++;
		break;
	    x += 1.0;
	  }
	x  = 0;
	y -= 1.0; /* We decrements y To keep the labyrinth as on the file */
      }


    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
    geom->setVertexArray(level->vertices);
    geom->setNormalArray(level->normals, osg::Array::Binding::BIND_PER_VERTEX);
    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 24*wallCount));
    osgUtil::SmoothingVisitor::smooth(*geom);



  /*
   * @param Level	the container of the vertices, normals and textures for the walls of our labyrinth
   * @param tex		the Vector that should receive the coordinate for the textures
   * @param skip	the second vector for texture filled with '0' to 'skip' this wall
   */
  void LevelReader::createCube(Soleil::Level *level, osg::Vec2Array &tex, osg::Vec2Array &skip,
		  float posx, float posy, float endx, float endy) const
{    
    // ------ Front
    level->vertices->push_back(osg::Vec3(posx, posy, 0.0f));
    level->vertices->push_back(osg::Vec3(endx, posy, 0.0f));
    level->vertices->push_back(osg::Vec3(endx, posy, 1.0f));
    level->vertices->push_back(osg::Vec3(posx, posy, 1.0f));

    level->normals->push_back(osg::Vec3(0.0f,-1.0f, 0.0f));
    level->normals->push_back(osg::Vec3(0.0f,-1.0f, 0.0f));
    level->normals->push_back(osg::Vec3(0.0f,-1.0f, 0.0f));
    level->normals->push_back(osg::Vec3(0.0f,-1.0f, 0.0f));

    tex.push_back( osg::Vec2(0.0f, 0.0f)); 
    tex.push_back( osg::Vec2(0.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 0.0f)); 
    //
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
  
  
    // ------ top
    level->vertices->push_back(osg::Vec3(posx, posy, 1.0f));
    level->vertices->push_back(osg::Vec3(endx, posy, 1.0f));
    level->vertices->push_back(osg::Vec3(endx, endy, 1.0f));
    level->vertices->push_back(osg::Vec3(posx, endy, 1.0f));

    level->normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
    level->normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
    level->normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
    level->normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));

    tex.push_back( osg::Vec2(0.0f, 0.0f)); 
    tex.push_back( osg::Vec2(0.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 0.0f)); 
    //
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
  
  
    // ------ back
    level->vertices->push_back(osg::Vec3(posx, endy, 0.0f));
    level->vertices->push_back(osg::Vec3(endx, endy, 0.0f));
    level->vertices->push_back(osg::Vec3(endx, endy, 1.0f));
    level->vertices->push_back(osg::Vec3(posx, endy, 1.0f));

    level->normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
    level->normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
    level->normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
    level->normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));

    tex.push_back( osg::Vec2(0.0f, 0.0f)); 
    tex.push_back( osg::Vec2(0.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 0.0f)); 
      //
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 

  
    // ------ Bottom
    level->vertices->push_back(osg::Vec3(posx, posy, 0.0f));
    level->vertices->push_back(osg::Vec3(endx, posy, 0.0f));
    level->vertices->push_back(osg::Vec3(endx, endy, 0.0f));
    level->vertices->push_back(osg::Vec3(posx, endy, 0.0f));

    level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));
    level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));
    level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));
    level->normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));

    tex.push_back( osg::Vec2(0.0f, 0.0f)); 
    tex.push_back( osg::Vec2(0.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 0.0f)); 
    //
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 


    // ------ Left
    level->vertices->push_back(osg::Vec3(posx, posy, 0.0f));
    level->vertices->push_back(osg::Vec3(posx, endy, 0.0f));
    level->vertices->push_back(osg::Vec3(posx, endy, 1.0f));
    level->vertices->push_back(osg::Vec3(posx, posy, 1.0f));

    level->normals->push_back(osg::Vec3(-1.0f, 0.0f, 0.0f));
    level->normals->push_back(osg::Vec3(-1.0f, 0.0f, 0.0f));
    level->normals->push_back(osg::Vec3(-1.0f, 0.0f, 0.0f));
    level->normals->push_back(osg::Vec3(-1.0f, 0.0f, 0.0f));

    tex.push_back( osg::Vec2(0.0f, 0.0f)); 
    tex.push_back( osg::Vec2(0.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 0.0f)); 
      //
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 

  
    // ------ Right
    level->vertices->push_back(osg::Vec3(endx, posy, 0.0f));
    level->vertices->push_back(osg::Vec3(endx, endy, 0.0f));
    level->vertices->push_back(osg::Vec3(endx, endy, 1.0f));
    level->vertices->push_back(osg::Vec3(endx, posy, 1.0f));

    level->normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
    level->normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
    level->normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
    level->normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));

    tex.push_back( osg::Vec2(0.0f, 0.0f)); 
    tex.push_back( osg::Vec2(0.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 1.0f)); 
    tex.push_back( osg::Vec2(1.0f, 0.0f)); 
    //
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 
    skip.push_back( osg::Vec2(0.0f, 0.0f)); 

  }




Thanks,
Florian

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=67041#67041








More information about the osg-users mailing list