c++ - 每个顶点加权法线请记住OpenGL C++中的折角

标签 c++ visual-studio opengl normals

我正在尝试考虑折痕 Angular 来计算每个顶点的加权法线。
但是,由于我的游戏中有许多(> 12)网格物体,因此代码永远无法运行。
在考虑折痕 Angular 情况下,还有更好的方法来计算每个顶点的加权法线吗?

以下是显示该代码的代码:

if (cosCreaseAngle == 0) { // ignore crease angle, just average all the nermoals keeping area in mind
    float area = 0;
    for (long pos = 0; pos < m->face_index_vertex.size(); pos++) {
        int firstVertex = pos - pos % 3;
        area = calcTriangleArea(m->dot_vertex[m->face_index_vertex[firstVertex]], m->dot_vertex[m->face_index_vertex[firstVertex + 1]], m->dot_vertex[m->face_index_vertex[firstVertex + 2]]);
        m->dot_normalPerVertexWeighted[m->face_index_vertex[pos]] +=
            area*(m->dot_normalPerFace[m->face_index_normalPerFace[pos]]); // multiply by the area
    }
}
else { //average the normals only when the angle between normals is less than the crease angle
    float area = 0;
    for (long pos = 0; pos < m->face_index_vertex.size(); pos++) {
        m->dot_normalPerVertexWeighted[m->face_index_vertex[pos]] = m->dot_normalPerFace[m->face_index_normalPerFace[pos]];
        for (long adjacentFace = pos + 3; adjacentFace < m->face_index_vertex.size(); adjacentFace++) {
            if (m->face_index_vertex[pos] == m->face_index_vertex[adjacentFace]) {
                if (cosCreaseAngle < calcAngleBetweenNormals(m->dot_normalPerFace[m->face_index_normalPerFace[pos]], m->dot_normalPerFace[m->face_index_normalPerFace[adjacentFace]])) {
                    int firstVertex = adjacentFace - adjacentFace % 3;
                    area = calcTriangleArea(m->dot_vertex[m->face_index_vertex[firstVertex]], m->dot_vertex[m->face_index_vertex[firstVertex + 1]], m->dot_vertex[m->face_index_vertex[firstVertex + 2]]);
                    m->dot_normalPerVertexWeighted[m->face_index_vertex[pos]] +=
                        area*(m->dot_normalPerFace[m->face_index_normalPerFace[adjacentFace]]);
                }
            }
        }
    }
}

最佳答案

您的代码具有嵌套循环,可导致二次运行时间。您应该改为构建邻接图-为每个顶点将相邻的面添加到列表中,然后遍历该图。这样可以将运行时间缩短为线性。

关于c++ - 每个顶点加权法线请记住OpenGL C++中的折角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40955357/

相关文章:

c - glDisplayFunc(RenderScene) 回调是否会在以下代码中导致竞争条件?

opengl - 是否可以让OpenGL用笔式笔画绘制GL_LINES?

c++ - 使用来自 OSG Cookbook 的绘图实例渲染点云数据不起作用

c++ - C++中字符串数组中最长的字符串

visual-studio - Azure Webapp 启动命令在部署后重置(Visual Studio -> 发布)

c# - 如何让 Visual Studio 2017 以交互方式调试此行?

C++指针内存使用

C++:运算符重载:类内和类外。预自增运算符的歧义

c# - 单元格事件参数中的动态 DataGridView 引用

c++ - OpenGl翻译