操纵网格对象中的索引

Manipulating Indices in an ofMesh object

本文关键字:索引 对象 网格 操纵      更新时间:2023-10-16

仍然想知道如何正确使用ofMesh,我现在的目标是拥有一个点云,拥有随机连接的点(使用索引),并能够在点击时打乱索引。有人向我指出了使用费雪-耶茨洗牌的方向,但在我找到指数之前,这是行不通的。

目前,我只是通过添加具有随机坐标的顶点来创建网格,但没有明确定义任何索引。当我使用ofSetupIndicesAuto()时,它将索引连接得过于有序,而ofClearIndices()不会做任何事情,除非我以某种方式设置索引。

我不明白的是,当我没有明确地为顶点定义索引时,顶点是如何连接的。我假设索引是按照顶点的顺序自动设置的,但我猜不是。

这是我试图对索引(放在ofApp::mousePressed()中)执行的洗牌:

    for (int i = numVerts - 1; i >= 0; i --) {
        int index = (int)ofRandom(i);
        int tempIndex = mesh.getIndex(index);
        mesh.setIndex(index, mesh.getIndex(i));
        mesh.setIndex(i, tempIndex);
    }

您可以使用混洗向量来设置索引。例如:

vector<int> indices(numVerts);
// Init indices vector
int acc = 0;
for(vector<int>::iterator it = indices.begin(); it != indices.end(); ++it){
    *it = acc;
    ++acc;
}
// Shuffle
random_shuffle(indices.begin(), indices.end());
// Update indices for the mesh
mesh.clearIndices();
mesh.addIndices(indices);

你可以在这里看到指数是如何工作的。基本上,它们可以帮助您告诉图形卡如何连接ofMesh的顶点。