c++ - glDrawElements 已停止工作

标签 c++ visual-studio opengl graphics vertex

我让 glDrawElements 始终如一地工作,最初使用一个简单的盒子,然后使用由大量顶点组成的更复杂的形状。然后它就停止绘制网格。我已经将代码恢复到最基本的状态,只需绘制 2 个三角形来制作一个 2D 正方形。这也不再有效。

void createMesh(void) {
    float vertices[12];
    vertices[0] = -0.5; vertices[1] = -0.5; vertices[2] = 0.0; // Bottom left corner
    vertices[3] = -0.5; vertices[4] = 0.5; vertices[5] = 0.0; // Top left corner
    vertices[6] = 0.5; vertices[7] = 0.5; vertices[8] = 0.0; // Top Right corner
    vertices[9] = 0.5; vertices[10] = -0.5; vertices[11] = 0.0; // Bottom right corner

    short indices[] = { 0, 1, 2, 0, 2, 3};

    glEnableClientState(GL_VERTEX_ARRAY);        // Enable Vertex Arrays

    glVertexPointer(3, GL_FLOAT, 0, vertices); // Set The Vertex Pointer To Our Vertex Data

    glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_SHORT, indices); 

    glDisableClientState(GL_VERTEX_ARRAY);
}

用于工作的更高级代码如下所示:

void createMesh(void) {
float vertices[(amountOfHorizontalScans * 480 * 3)];// Amount of vertices
    //build the array of vertices from a matrix of data
    int currentVertex = -1;
    std::vector <std::vector<double>> currentPointCloudMatrix = distanceCalculator.getPointCloudMatrix();
    double plotY = 0;
    double plotX = 0;
    for (int j = 0; j < currentPointCloudMatrix.size(); j++){
        std::vector <double> singleDistancesVector = currentPointCloudMatrix.at(j); 
        for (int i = 0; i < singleDistancesVector.size(); i++){
            if (singleDistancesVector.at(i) != 0){
                vertices[++currentVertex] = plotX;
                vertices[++currentVertex] = plotY;
                vertices[++currentVertex] = singleDistancesVector.at(i);
            }
            plotX += 0.1;
        }
        plotX = 0;
        plotY += 0.2; //increment y by 0.02     
    }

    //Creating the array of indices, 480 is the amount of columns
    int i = 0;
    short indices2[(amountOfHorizontalScans * 480 * 3)];
    for (int row = 0; row<amountOfHorizontalScans - 1; row++) {
        if ((row & 1) == 0) { // even rows
            for (int col = 0; col<480; col++) {
                indices2[i++] = col + row * 480;
                indices2[i++] = col + (row + 1) * 480;
            }
        }
        else { // odd rows
            for (int col = 480 - 1; col>0; col--) {
                indices2[i++] = col + (row + 1) * 480;
                indices2[i++] = col - 1 + +row * 480;
            }
        }
    }

    glEnableClientState(GL_VERTEX_ARRAY);        // Enable Vertex Arrays

    glVertexPointer(3, GL_FLOAT, 0, vertices); // Set The Vertex Pointer To Our Vertex Data

    glDrawElements(GL_TRIANGLE_STRIP, (amountOfHorizontalScans * 480 * 3), GL_UNSIGNED_SHORT, indices2);

    glDisableClientState(GL_VERTEX_ARRAY);
}

我完全不知道为什么它会停止工作,因为它在很多次运行中都运行良好,然后就完全停止了。我已经调试通过并且正在访问所有代码,顶点和索引也填充了数据。什么会导致它停止工作?

编辑: 所以我现在真的很迷茫。今天早上我又回到了这个问题,一切又都正常了,因为在网格中绘制没有问题。在做了一些测试并多次运行程序后,它再次停止绘制网格!

这可能与内存有关吗?我不是 100% 确定 glDrawElements 如何存储传递给它的数据,所以我是否必须清除某个地方不断填充数据的内容?

最佳答案

你不能在堆栈中动态分配数组:

short indices2[(amountOfHorizontalScans * 480 * 3)];

在代码中:

short indices2[(amountOfHorizontalScans * 480 * 3)];
for (int row = 0; row<amountOfHorizontalScans - 1; row++) {
    if ((row & 1) == 0) { // even rows
        for (int col = 0; col<480; col++) {
            indices2[i++] = col + row * 480;
            indices2[i++] = col + (row + 1) * 480;
        }
    }
    else { // odd rows
        for (int col = 480 - 1; col>0; col--) {
            indices2[i++] = col + (row + 1) * 480;
            indices2[i++] = col - 1 + +row * 480;
        }
    }
}

必须是

short* indices2 = new short[(amountOfHorizontalScans * 480 * 3)];

比释放分配的内存

delete [] indices2;

您尝试直接使用 GL_TRIANGLES 时,三角形带是一个非常棘手的模式。

关于c++ - glDrawElements 已停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34904339/

相关文章:

c++ - 如何在没有boost的情况下在C++03中实现一个通用的回调机制?

c++ - 如何使用 windows.h 在 C++ 中创建新文件

c# - 异步测试在自己的测试完成后执行其他测试中的方法

java - 某个 z 深度处的 xy 位置

c++ - 无法使用单独的着色器程序 "<program> object is not successfully linked."

c++ - OpenGL三角形邻接计算

c++ - 对静态成员的 undefined reference 是什么意思?

java - opencv pyrDown() 对javacv 的相等方法是什么?

c# - OpenGL 不在我的 C# 面板上显示/绘制

visual-studio - TFS 无基础合并包含变更集中未更改的文件