【问题标题】:C++ OpenGL Large Mesh is missing trianglesC ++ OpenGL大网格缺少三角形
【发布时间】:2014-10-17 12:52:45
【问题描述】:

所以我正在组装一个高度图渲染器,它将在顶点着色器中完成大部分工作,但首先我当然会生成一个要渲染的网格,目前我正在使用 openGL 的上限和C++ 来查看我可以渲染的网格有多密集(所以我稍后在 LoD 网格划分方面有一些事情要做)

ANYWAY!直截了当;

我在测试 32、64 和 128 的 meshResolution 后注意到的问题我遇到了运行时崩溃,我使用自制的类“indexFace”停止了它们,该类包含 6 个索引以降低数组长度,问题在 128分辨率只有网格实际显示的三分之一,我想知道 openGL 使用一组 BufferObjects 可以渲染或保存的索引数量是否存在限制,或者这是否与我处理 C++ 方面的问题有关。

我正在通过以下方式生成网格:

void HeightMapMesh::GenerateMesh(GLfloat meshScale, GLushort meshResolution)
{
    GLushort vertexCount = (meshResolution + 1) * (meshResolution + 1);
    Vertex_Texture* vertexData = new Vertex_Texture[vertexCount];
    GLushort indexCount = (meshResolution * meshResolution) * 6;

//indexFace holds 6 GLushort's in an attempt to overcome the array size limit
    indexFace* indexData = new indexFace[meshResolution * meshResolution];
    GLfloat scalar = meshScale / ((GLfloat)meshResolution);
    GLfloat posX = 0;
    GLfloat posY = 0;
    for (int x = 0; x <= meshResolution; x++)
    {
        posX = ((GLfloat)x) * scalar;
        for (int y = 0; y <= meshResolution; y++)
        {
            posY = ((GLfloat)y) * scalar;
            vertexData[y + (x * (meshResolution + 1))] = Vertex_Texture(posX, posY, 0.0f, x, y);
        }
    }
    GLint indexPosition;
    GLint TL, TR, BL, BR;
    for (int x = 0; x < meshResolution; x++)
    {
        for (int y = 0; y < meshResolution; y++)
        {
            indexPosition = (y + (x * (meshResolution)));
            BL = y + (x * (meshResolution + 1));
            TL = y + 1 + (x * (meshResolution + 1));
            BR = y + ((x + 1) * (meshResolution + 1));
            TR = y + 1 + ((x + 1) * (meshResolution + 1));
            indexData[indexPosition] = indexFace(
                    BL, TR, TL,
                    BL, BR, TR
                    );
        }
    }
    mesh.Fill(vertexData, vertexCount, (void *)indexData, indexCount, GL_STATIC_DRAW, GL_STATIC_DRAW);
    delete [] vertexData;
    delete [] indexData;
}

//This is for mesh.Fill()
    void Fill(T* vertData, GLushort vertCount, void* indData, GLushort indCount, GLenum vertUsage, GLenum indUsage)
    {
        indexCount = indCount;
        vertexCount = vertCount;
        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectID);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObjectID);
        glBufferData(GL_ARRAY_BUFFER, sizeof(T) * vertexCount, vertData, vertUsage);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * indexCount, indData, indUsage);
    }

【问题讨论】:

    标签: c++ arrays opengl vbo heightmap


    【解决方案1】:

    因为你把指数做空了。

    例如:GLushort indexCount = (meshResolution * meshResolution) * 6;meshResolution 的值 105 处达到 USHRT_MAX。 (105*105*6 = 66150 > 65535)

    使用整数作为索引。因此,将您的索引更改为无符号整数,并像这样进行最终绘制调用:

    glDrawElements( GL_QUADS, indCount, GL_UNSIGNED_INT, indices); //do this
    //glDrawElements( GL_QUADS, indCount, GL_UNSIGNED_SHORT, indices); //instead of this
    //also GL_QUADS is deprecated but it seems your data is in that format so I left it that way
    

    如果您改为绘制 GL_TRIANGLE_STRIPs 或更好地在 GPU 上进行曲面细分,则可以保存一堆索引,因为这就像它的完美用例。

    【讨论】:

    • 轻微修正,虽然 ushort 和 uint 是我的问题,但因为我使用 ushort 来传递索引计数,顶点仍然在 ushort 限制内,因此将索引保持为安全ushorts,感谢您的帮助,尽管出于某种原因,我将短裤限制与整数和整数与多头混淆了。 GL_TRIANGLE_STRIP 当你有多行时,我会查找它的顺序总是让我感到困惑,但我想研究是完美的,再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 2018-02-11
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多