简体   繁体   English

GLFW glDrawElements绘制三角形很奇怪

[英]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. 当我将顶点数更改为4以尝试最终渲染四边形时。

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. Ive试图找到答案,但我找不到。 I have looked at this persons problem and his code looks the same as mine after applying the answer. 我已经看过这个人的问题,应用答案后,他的代码与我的代码相同。 No display from glDrawElements glDrawElements不显示

You missinterpret the second parameter of glVertexAttribPointer . 您错过了解释glVertexAttribPointer的第二个参数。

The size parameter specifies the number of components of an attribute (which basically is the number of floats it gets). size参数指定属性的组件数(基本上是它获得的浮点数)。 Since you still have a vec3 (3 floats) per vertex when drawing a quad, this number shouldn't be changed. 由于绘制四边形时每个顶点仍具有vec3(3个浮点),因此不应更改此数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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