0
votes

Runs only once

GLfloat vertices[] = {
    -0.5f, -0.5f,  0.0f,
     0.5f, -0.5f,  0.0f,
     0.5f,  0.5f,  0.0f,
    -0.5f,  0.5f,  0.0f
};

GLuint triangles[] = {0,1,2};

int lengthV = 3; //Number of vertices
int lenfthT = 1; //Number of triangles

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

glGenBuffers(vaoID, &verticesVBOID);
glBindBuffer(GL_ARRAY_BUFFER, verticesVBOID);
glBufferData(GL_ARRAY_BUFFER, lengthV*sizeof(GLfloat)*3, vertices, GL_STATIC_DRAW);

glGenBuffers(vaoID, &trianglesVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, trianglesVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, lengthT*sizeof(GLuint)*3, triangles, GL_STATIC_DRAW);

Runs once per frame

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, verticesVBOID);
glVertexAttribPointer(0, lengthV, GL_FLOAT, GL_FALSE, 0, nullptr);

glUseProgram(program);
//glDrawArrays(GL_TRIANGLES, 0, verticeCount*3);
glDrawElements(GL_TRIANGLES, lengthT,GL_UNSIGNED_INT, nullptr);
glDisableVertexAttribArray(0);

glfwSwapBuffers(window);

When I run this, this renders, which is right.
When I run this, this renders, which is right.

When I change the number of vertices to 4 to try and render a quad eventually.

int lengthV = 4;

This happens.
This happens.
I don't understand why this is happening because I only changed the number of vertices.

Ive Tried to find an answer but I could not. I have looked at this persons problem and his code looks the same as mine after applying the answer. No display from glDrawElements

1

1 Answers

3
votes

You missinterpret the second parameter of glVertexAttribPointer.

The size parameter specifies the number of components of an attribute (which basically is the number of floats it gets). Since you still have a vec3 (3 floats) per vertex when drawing a quad, this number shouldn't be changed.