【问题标题】:glDrawElements has stopped workingglDrawElements 已停止工作
【发布时间】:2016-04-26 13:15:37
【问题描述】:

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 如何存储传递给它的数据,所以我是否必须在某个地方清除一些我不断填充数据的东西?

【问题讨论】:

  • “已停止工作”有点含糊。当您运行代码时会发生什么,它与您的预期有何不同?是否有任何编译时/运行时错误?
  • 啊,我的错,它曾经为我拥有的顶点创建一个可见的网格,无论是简单的正方形还是矩阵中的一大组顶点。然后它只是停止创建网格,看不到任何错误,它不再绘制任何东西。
  • 如果您还没有这样做,请尝试致电glGetError()。您正在创建什么类型/版本的上下文?

标签: c++ visual-studio opengl graphics vertex


【解决方案1】:

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

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

【讨论】:

  • 不知何故错过了我的问题中的那一行,更新了顶部的代码。但它肯定存在于原始代码中。即使这是问题所在,第一个 2D 正方形也应该可以工作。澄清一下,这不是解决办法。
  • 这是一个问题还是一个答案?
  • 当然,最初的 2D 正方形应该可以正常工作,因为它不受此影响?
  • @Calco 是的,它可以工作,但是 short indices2[(amountOfHorizontalScans * 480 * 3)]; 你是否将amountOfHorizontalScans 声明为常量?
  • amountOfHorizo​​ntalScans 是一个常数。我倾向于相信它与内存有关,因为它在关闭我的电脑后再次工作!现在,当调用 glDrawElements 时,创建指针并调用 delete 会在运行时导致“访问冲突”。
猜你喜欢
  • 1970-01-01
  • 2014-05-22
  • 2014-10-10
  • 2016-12-14
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多