我需要帮助计算三角形集的纹理坐标 . 是否有一个函数根据没有着色器的顶点坐标计算它们?或者如何使用顶点法线手动计算它们?我有大量的小三角形,从点 Cloud 计算,不可能影响它们 . 我的简化测试程序如下所示:
//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
1 回答
GL_TEXTURE_GEN_* generates texture coordinates according to some parametric function (e.g. mapping world or model coordinates, reflection vectors etc.)
你实际上并没有想要实现,但为了获得任何纹理坐标,
GL_TEXTURE_GEN_*
可以工作 . 否则,您可以迭代点 Cloud 的顶点和法线,并在osg::Geometry
内的CPU侧生成纹理坐标 . 这与您将顶点分配给几何体的方式相同,但使用不同的函数来计算实际坐标,具体取决于您的需要 .