2016-07-31 91 views
0

我需要计算一组三角形纹理坐标的帮助。有没有一个函数根据没有着色器的顶点坐标来计算它们?或者如何使用顶点法线手动计算它们? 我有大量的小三角形,从点云计算出来,不可能影响它们。我简单的测试程序是这样的:OpenSceneGraph:设置三角形的纹理坐标

//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); 

计算的法线很好地工作,纹理坐标不工作 - 我得到的黑色三角形。关于如何使用GL_TEXTURE_GEN_S没有太多的信息,所以任何帮助都会非常感激。 UPD:我计算纹理坐标使用下列公式 http://paulyg.f2s.com/uv.htm

回答

0

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

你不说出你实际上要达到,但是要获得任何纹理坐标手动,该GL_TEXTURE_GEN_*可以工作。否则,您可以迭代点云的顶点和法线,并在osg::Geometry内的CPU侧生成纹理坐标。这将以与将顶点分配给几何图形相同的方式工作,但具有根据您的需要计算实际坐标的不同功能。

+0

感谢您的回复Kento! – Etimr