OpenGL 3.3 glDrawElements, function call ignored.

I am working with OpenGL 3.3 with a Radeon HD 6000 Series graphics card on Ubuntu 14.04 64-bit.

The function call to glDrawElements is silently ignored by the GL. No GL error codes are generated by this action, and no primitives are drawn on screen.

This was tested on the Gallium 0.4 driver, as well as fglrx (AMD Catalyst). Neither driver differed in outcome.

I tried changing the type of the indices to byte, int, and short. I also tried using the various other miscellaneous DrawElements functions.

The function call to glDrawArrays does produce primitives drawn into the frame buffer.

OpenGL extension loading is done by GLEW.


#include <GL/glew.h>
#include <assert.h>
#include "shaderSetup.h"

const char* vertexShaderAddr = "shaders//default.vert";
const char* fragmentShaderAddr = "shaders//color.frag";

GLuint vertexBufferObject;
GLuint indexBufferObject;
GLuint vertexArrayObject;
GLuint shaderProgram;

#define VERT1 -1.0f, -1.0f, -1.0f
#define VERT2 -1.0f, -1.0f, 1.0f
#define VERT3 1.0f, -1.0f, 1.0f
#define VERT4 1.0f, -1.0f, -1.0f
#define VERT5 1.0f, 1.0f, -1.0f
#define VERT6 1.0f, 1.0f, 1.0f
#define VERT7 -1.0f, 1.0f, 1.0f
#define VERT8 -1.0f, 1.0f, -1.0f

const float vertexData[] =
{
        VERT1, VERT2, VERT3, VERT4, // front
        VERT4, VERT3, VERT6, VERT5, // right
        VERT5, VERT6, VERT7, VERT8, // back
        VERT8, VERT7, VERT2, VERT1, // left
        VERT2, VERT7, VERT6, VERT3, // top
        VERT4, VERT5, VERT8, VERT1, // bottom
};

const GLushort index[] =
{
        0, 1, 2, // front
        0, 2, 3,

        4, 5, 6, // right
        4, 6, 7,

        8, 9, 10, // back
        8, 10, 11,

        12, 13, 14, // left
        12, 14, 15,

        16, 17, 18, // top
        16, 18, 19,

        20, 21, 22, // bottom
        20, 22, 23,
};

const size_t normalDataOffset = sizeof(float) * 3 * 24;
const size_t arrayCount = sizeof(index) / sizeof(index[0]);

void setup()
{
    glGenVertexArrays(1, &vertexArrayObject);
    glBindVertexArray(vertexArrayObject);

    glGenBuffers(1, &vertexBufferObject);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glGenBuffers(1, &indexBufferObject);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    setupShader(GL_VERTEX_SHADER, vertexShaderAddr);
    setupShader(GL_FRAGMENT_SHADER, fragmentShaderAddr);
    setupProgram(shaderProgram);

    assert( glIsProgram(shaderProgram) == GL_TRUE );
    assert( glIsBuffer(vertexBufferObject) == GL_TRUE );
    assert( glIsBuffer(indexBufferObject) == GL_TRUE );
}

void render()
{
    glClearColor(0.8f, 0.8f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(shaderProgram);

    glDrawElements(GL_TRIANGLES, arrayCount, GL_UNSIGNED_SHORT, 0);

    glUseProgram(0);
}

Thanks for your time.


    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

The GL_ELEMENT_ARRAY_BUFFER is necessary for doing indexed rendering (unless you’re using client-side arrays, which is unlikely as core 3.3 doesn’t support them, and you passed NULL to glDrawElements). Since you unbound the element buffer, you will need to bind it again before you render.

Note that, in accord with the spec, rendering with a zero buffer is not an error; it only yields undefined behavior (presumably for backwards-compatibility reasons). Section 10.3.10 in GL 4.5’s spec. It’s silly, but true.

The GL_ELEMENT_ARRAY_BUFFER is necessary for doing indexed rendering (unless you’re using client-side arrays, which is unlikely as core 3.3 doesn’t support them, and you passed NULL to glDrawElements). Since you unbound the element buffer, you will need to bind it again before you render.

Note that, in accord with the spec, rendering with a zero buffer is not an error; it only yields undefined behavior (presumably for backwards-compatibility reasons). Section 10.3.10 in GL 4.5’s spec. It’s silly, but true.

Thank you so much for your effective advice.
You really got to the point of the problem.
That is exactly what was wrong.