我需要计算一组三角形纹理坐标的帮助。有没有一个函数根据没有着色器的顶点坐标来计算它们?或者如何使用顶点法线手动计算它们? 我有大量的小三角形,从点云计算出来,不可能影响它们。我简单的测试程序是这样的: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
感谢您的回复Kento! – Etimr