简体   繁体   中英

Glfw glDrawElements drawing triangle weird

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 change the number of vertices to 4 to try and render a quad eventually.

int lengthV = 4;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM