0

I need help with calculating texture coordinates for the set of triangles. Is there a function that calculates them according to the vertex coordinates without shaders? Or how one can calculate them manually using vertex normals? I have a big amount of small triangles, calculated from a point cloud and have no possibility to influence them. My simplified test program looks like this:

    //read texture
    text = new_message->text;
    osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
    texture->setDataVariance(osg::Object::DYNAMIC);
    osg::ref_ptr<osg::Image> image = osgDB::readImageFile( "Images/" +text );

    if (!image)
    {
    std::cout << "Couldn't load texture." << std::endl;
    }
    texture->setImage( image.get() );
    ...
    //create and fill an array of vertices
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
    vertices->push_back( osg::Vec3(...) );
    ...
    osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
    quad->setVertexArray( vertices.get() );
    quad->addPrimitiveSet( new osg::DrawArrays(GL_TRIANGLES, 0, 12) );
    //calculate normals
    osgUtil::SmoothingVisitor::smooth( *quad );
    geode = new osg::Geode;
    geode->addDrawable( quad.get() );
    //calculate texture coordinates
    osg::StateSet *state = geode->getOrCreateStateSet();
    state->setTextureAttributeAndModes(1, texture.get(), osg::StateAttribute::ON);
    state->setTextureMode(1, GL_TEXTURE_GEN_S, osg::StateAttribute::ON);
    state->setTextureMode(1, GL_TEXTURE_GEN_T, osg::StateAttribute::ON);
    state->setTextureMode(1, GL_TEXTURE_GEN_R, osg::StateAttribute::ON);
    state->setTextureMode(1, GL_TEXTURE_GEN_Q, osg::StateAttribute::ON);
    geode->setStateSet(state);

The calculated normals work perfectly, texture coordinates are not working at all - I get black triangles. There is not much information on how to use GL_TEXTURE_GEN_S, so any help would be really appreciated. UPD: I calculated texture coordinates manually using following formulas http://paulyg.f2s.com/uv.htm

Etimr
  • 51
  • 6

1 Answers1

0

GL_TEXTURE_GEN_* generates texture coordinates according to some parametric function (e.g. mapping world or model coordinates, reflection vectors etc.)

You don't state what you're actually trying to achieve, but to obtain any texture coordinates, the GL_TEXTURE_GEN_* could work. Otherwise, you could iterate over your point cloud's vertices and normals and generate the texture coordinates on the CPU-side, within the osg::Geometry. This would work in the same manner as you assigned the vertices to the geometry, but with a different function to calculate the actual coordinates, depending on your needs.

Kento Asashima
  • 312
  • 1
  • 9