glDrawElements has stopped working

I had glDrawElements working consistently, initially with a simple box and then with more complex shapes made up of a large amount of vertices. Then it simply stopped drawing the mesh. I have taken the code back to it's most basic, just drawing 2 triangles to make a 2D square. This also no longer works.

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);
}

The more advanced code that used to work is shown below:

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);
}

I am at a complete loss as to why it has stopped working as it was working perfectly for a good number of runs, then just completely stopped. I have debugged through and all the code is being reached, also the vertices and indices are populated with data. What could cause this to stop working?

EDIT: So I am really quite confused now. I came back to this issue this morning, and everything worked fine again, as in the meshes would draw with no issues. After doing some tests and running the program a number of times it has simply stopped drawing meshes again!

Could this be something memory related? I am not 100% sure on how glDrawElements stores the data passed to it, so could it be that I have to clear something somewhere that I keep filling up with data?

Answers


You cannot allocate dynamically arrays in stack:

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

In code:

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;
        }
    }
}

Must be

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

than free allocated memory

delete [] indices2;

Triangle strip is pretty tricky mode did you try to work directly with GL_TRIANGLES.


Need Your Help

Tips for finding things in your program that are broken that you don't know about?

testing-strategies usability-testing

I was working on something for a client today when I found a way to break some functionality in our program.

Device orientation towards iBeacon

ios ios7 bluetooth bluetooth-lowenergy ibeacon

I'm starting to work with Bluetooth LE and iBeacons on iOS7.