Bartvbl Bartvbl - 4 months ago 20
C Question

OpenGL 4.1: Vertex Array Object draws nothing

I'm trying to draw some vertices and indices in OpenGL 4.1 using a vertex array object:

float vertices[] = {-0.8, -0.8, 0, 0.8, -0.8, 0, 0, 0.8, 0};
unsigned int indices[] = { 0, 1, 2 };


After setting up the VAO and drawing it using glDrawElements(), I get an empty screen. I also tried using a simple pair of shaders just returning the original vertex position and a hardcoded colour, but no luck there either.

Here's the code which sets up the VBO and VAO:

unsigned int vertexArray;
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);

unsigned int vertexBufferID;
unsigned int indexBufferID;

glGenBuffers(1, &vertexBufferID);
glGenBuffers(1, &indexBufferID);

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, 4 * vertexCount, vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * vertexCount, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * indexCount, indices, GL_STATIC_DRAW);


And I draw the VAO using:

glBindVertexArray(vertexArray);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)0);


Here's a trace of all OpenGL calls:

// Setup code

glViewport(0,0,1024,768)
glGetString(GL_RENDERER)="GeForce GT 540M/PCIe/SSE2"
glGetString(GL_VENDOR)="NVIDIA Corporation"
glGetString(GL_VERSION)="4.1.0 NVIDIA 359.46"
glGetString(GL_SHADING_LANGUAGE_VERSION)="4.10 NVIDIA via Cg compil..."
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
glGenVertexArrays(1,000000000015F9B4)
glBindVertexArray(1)
glGenBuffers(1,000000000015F9D4)
glGenBuffers(1,000000000015F9F4)
glBindBuffer(GL_ARRAY_BUFFER,1)
glBufferData(GL_ARRAY_BUFFER,36,000000000015FA48,GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0,3,GL_FLOAT,false,36,0000000000000000)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,2)
glBufferData(GL_ELEMENT_ARRAY_BUFFER,12,000000000015FA88,GL_STATIC_DRAW)
glClearColor(0.300000,0.300000,0.400000,1.000000)

// End of setup, here's the code rendering two frames (they just repeat):

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
glBindVertexArray(1)
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0000000000000000)
glGetError()=GL_NO_ERROR
wglSwapBuffers(0000000069011A82)=true

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
glBindVertexArray(1)
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0000000000000000)
glGetError()=GL_NO_ERROR
wglSwapBuffers(0000000069011A82)=true


From what I can tell I'm doing what pretty much every tutorial says I should be doing, but for some reason nothing ever appears on my screen. What am I missing?

Answer

You data are tightly packed in the array, so the stride parameter in glVertexAttribPointershould be 0. By the way, if you declare static arrays, you should use sizeof to pass the size of the array instead of implementation dependent things like 4 * vertexCount:

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);