2016-07-31 92 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