Soapy
Soapy

Reputation: 567

Opengl VAO not drawing correctly

I've been having a problem recently when getting to grips with Opengl VAOs. Currently I have code which draws a triangle from a array of floats using a VBO.

Here is the code.

float vpp[] = { 0.75f, 0.75f, 0.0f,
                0.75f, -0.75f, 0.0f,
                -0.75f, -0.75f, 0.0f};

// Non Indexed
glGenBuffers(1, &m_mainVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_mainVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vpp), vpp, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);

Now when I try to implement a VAO, the program only draws out 1 point. (I would post a screen cap but I can't).

And the code for this one.

float vp[] = { 0.0f, 0.75f, -0.75f };

//VAO
unsigned short sInds[9] = { 1, 1, 0,
                            1, 2, 0,
                            2, 2, 0};

//Indexed
glGenBuffers(1, &m_mainVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_mainVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vp), vp, GL_STATIC_DRAW);

GLuint elBuf;
glGenBuffers(1, &elBuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elBuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 9 * sizeof(unsigned short), sInds, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawElements(GL_POINTS, 3, GL_UNSIGNED_SHORT, sInds);
glDisableClientState(GL_VERTEX_ARRAY);


glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Can anyone show me what I'm doing wrong?

Upvotes: 0

Views: 1107

Answers (2)

datenwolf
datenwolf

Reputation: 162367

Which OpenGL version do you use? So far your code uses only Vertex Buffer Objects, but there's no Vertex Array Object support code there, yet. If you're on OpenGL-4 core, you need to create a VAO first, otherwise nothing will draw.

If you don't plan on using VAO abstraction, you can create a catch-all VAO right after creation of the context.

GLuint vaoID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);

Update:

I didn't it notice yesterday night, but Nicol Bolas did. Some part of your second code snippet doesn't make sense. vp defines only 3 floats. It now completely depends on the semantics of the vertex shader (using generic vertex attributes mandates the use of shaders) how these are to be interpreted. Your call of glVertexAttribPointer with a element size of 3 tells OpenGL, that each element of that vertex attribute has 3 fields, which are addressed together.

float vp[] = { 0.0f, 0.75f, -0.75f };

              \------------------/ one attribute element

It would make more sense with your first definition vpp

float vpp[] = { 0.75f, 0.75f, 0.0f,

                \----------------/ attribute element 0

                0.75f, -0.75f, 0.0f,

                \-----------------/ attribute element 1

                -0.75f, -0.75f, 0.0f};

                \------------------/ attribute element 2

If you enable several vertex attributes, they together form a long vector of several attributes.

The indices supplied to glDrawElements address such vertex attribute vectors, not their individual fields.

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 474536

Your code is broken, and that has nothing to do with your "VAO" (or as Datenwolf pointed out, your lack of a VAO).

You don't seem to understand what the index in the index buffer actually does. You defined attribute 0 to be an array of 3 floating-point values. In C terms, you said, float attrib0[numberOfPositions][3];. When OpenGL sees the first index, it will fetch three values from the array. It will fetch:

attrib0[index][0]; attrib0index; attrib0[index][2];

Your index is a vertex index, not a GL_FLOAT index. The array elements are 3 floats each, not 1 float each.

What you're trying to do is not really possible or useful. And as Datenwolf pointed out, it has nothing to do with VAOs (since your code doesn't use them). You're talking about indexed rendering, which is a different topic from VAOs.

Upvotes: 1

Related Questions