我需要帮助计算三角形集的纹理坐标。是否有一个函数根据没有着色器的顶点坐标计算它们?或者如何使用顶点法线手动计算它们? 我有大量的小三角形,从点云计算,不可能影响它们。我的简化测试程序如下所示:
//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