glDrawElements已停止工作

问题描述:

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

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如何存储传递给它的数据,那么我是否可以在某处清理某些我不断填充数据的地方?

+2

“已停止工作”有点模糊。当你运行代码时会发生什么,它与你的期望有什么不同?是否有编译时/运行时错误? – user463035818

+0

啊我的不好,它用来为我有的顶点创建一个可见的网格,无论这是矩阵的简单正方形还是一大组顶点。然后它就停止创建网格,不会出现任何错误,它不会再绘制任何东西。 – Calco

+0

如果您尚未这样做,请尝试调用'glGetError()'。你创建什么类型/版本的上下文? –

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

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工作。

+0

不知何故错过了我的问题,更新了顶部的代码。但它确实存在于原始代码中。即使这是问题,第一个二维广场应该工作。澄清,这不是一个修复。 – Calco

+0

这是一个问题或答案? – user463035818

+0

当然,最初的2D广场应该可以正常工作,因为它不受此影响? – Calco