我需要計算一組三角形紋理座標的幫助。有沒有一個函數根據沒有着色器的頂點座標來計算它們?或者如何使用頂點法線手動計算它們? 我有大量的小三角形,從點雲計算出來,不可能影響它們。我簡單的測試程序是這樣的: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