将加载的网格转换为布料模拟的问题

时间:2018-02-25 05:38:42

标签: c++ graphics 3d simulation cloth-simulation

我试图获取网格时遇到一些问题我导入到我的程序中,使用粒子/弹簧系统进行布料模拟物理。我是图形编程的初学者,很抱歉,如果这是非常明显的,我只是遗漏了一些东西。我使用C ++和OpenGL,以及Assimp来导入模型。我相当确定我的代码来计算约束/弹簧和步骤每个粒子是正确的,因为我用生成的网格(用四边形而不是三角形)测试它,它看起来很好,但是idk。

我一直在使用此链接来研究如何实际执行此操作:https://nccastaff.bournemouth.ac.uk/jmacey/MastersProjects/MSc2010/07LuisPereira/Thesis/LuisPereira_Thesis.pdf

引擎内的内容:https://www.youtube.com/watch?v=RyAan27wryU

我很确定它是连接/弹簧的问题,因为导入的模型只是一个平面似乎在大多数情况下工作正常。虽然另一个模型似乎只是崩溃了。我一直在看论文,从我的理解,一切都应该正常工作,因为我连接边缘/弯曲弹簧看似正确,物理方面似乎是从平面工作。我真的无法理解我的生活!任何提示/帮助将非常感谢! :)

将网格加工成布料的代码:

// Container to temporarily hold faces while we process springs
std::vector<Face> faces;

// Go through indices and take the ones making a triangle.
// Indices come from assimp, so i think this is the right thing to do to get each face?
for (int i = 0; i < this->indices.size(); i+=3)
{
        std::vector<unsigned int> faceIds = { this->indices.at(i), this->indices.at(i + 1), this->indices.at(i + 2) };
        Face face;
        face.vertexIDs = faceIds;
        faces.push_back(face);
}

// Iterate through faces and add constraints when needed.
for (int l = 0; l < faces.size(); l++)
{

    // Adding edge springs.
    Face temp = faces[l];
    makeConstraint(particles.at(temp.vertexIDs[0]), particles.at(temp.vertexIDs[1]));
    makeConstraint(particles.at(temp.vertexIDs[0]), particles.at(temp.vertexIDs[2]));
    makeConstraint(particles.at(temp.vertexIDs[1]), particles.at(temp.vertexIDs[2]));


    // We need to get the bending springs as well, and i've just written a function to do that.
    for (int x = 0; x < faces.size(); x++)
    {
        Face temp2 = faces[x];
        if (l != x)
        {
            verticesShared(temp, temp2);
        }

    }
}

还有我处理弯曲弹簧的代码:

// Container for any indices the two faces have in common. 
std::vector<glm::vec2> traversed;

// Loop through both face's indices, to see if they match eachother. 
for (int i = 0; i < a.vertexIDs.size(); i++)
{
    for (int k = 0; k < b.vertexIDs.size(); k++)
    {

        // If we do get a match, we push a vector into the container containing the two indices of the faces so we know which ones are equal.
        if (a.vertexIDs.at(i) == b.vertexIDs.at(k))
        {
            traversed.push_back(glm::vec2(i, k));
        }
    }
    // If we're here, if means we have an edge in common, aka that we have two vertices shared between the two faces.
    if (traversed.size() == 2)
    {
        // Get the adjacent vertices.
         int face_a_adj_ind = 3 - ((traversed[0].x) + (traversed[1].x));
         int face_b_adj_ind = 3 - ((traversed[0].y) + (traversed[1].y));

        // Turn the stored ones from earlier and just get the ACTUAL indices from the face. Indices of indices, eh. 
        unsigned int adj_1 = a.vertexIDs[face_a_adj_ind];
        unsigned int adj_2 = b.vertexIDs[face_b_adj_ind];
        // And finally, make a bending spring between the two adjacent particles. 
        makeConstraint(particles.at(adj_1), particles.at(adj_2));
    }
}

0 个答案:

没有答案