C ++ OpenGL Large Mesh缺少三角形

时间:2014-10-17 12:52:46

标签: c++ arrays opengl vbo heightmap

所以我正在组装一个高度贴图渲染器,它将完成顶点着色器中的大部分工作,但首先我生成一个渲染的网格,此刻我正在玩openGL的上限和C ++看看我可以渲染的网格有多密集(所以我后来在LoD网格划分方面有了一些东西)

无论如何!切入问题;

在测试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);
    }

1 个答案:

答案 0 :(得分:1)

因为你的指数很短暂。

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

使用int作为索引。因此,将您的索引更改为无符号整数并执行最终绘制调用,如下所示:

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_STRIP代替或更好,但在GPU上进行细分,因为这就像它的完美用例。