不能用EBO

时间:2016-07-21 01:22:19

标签: c++ opengl

在我编写的C ++应用程序中,我正在尝试使用EBO(元素缓冲区对象)绘制四边形。每当我尝试时,我根本无法绘制四边形。我究竟做错了什么? 代码:

//vertices and indices
GLfloat vertices[]={
//position              texture coordinate
-0.005f,0.02f,0.0f,      0.0f,1.0f,
0.02f,0.02f,0.0f,        1.0f,1.0f,
0.02f,-0.02f,0.0f,       1.0f,0.0f,
-0.005f,-0.02f,0.0f,     0.0f,0.0f,
};

GLfloat indices[]={
0,1,3,
2,3,1
};
//initialization
glCreateVertexArrays(1,&VAO);
glBindVertexArray(VAO);
glCreateBuffers(1,&VBO);
glCreateBuffers(1,&EBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,5*sizeof(GLfloat),(GLvoid*)nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,5*sizeof(GLfloat),(GLvoid*)(3*sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);

//drawing commands
transformLocation=glGetUniformLocation(textureProgram,"transform");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,woodTexture);
glUseProgram(textureProgram);
glUniformMatrix4fv(transformLocation,1,GL_FALSE,glm::value_ptr(transform));
glBindVertexArray(bowHandleVAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,bowHandleEBO);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr);

这适用于与此相当的glDrawArrays,但每当我尝试使用EBO时,它都不会绘制任何东西。如果您需要更多信息,请发表评论。

2 个答案:

答案 0 :(得分:0)

我能看到的最直接的错误是您的索引定义与调用glDrawElements时的用法之间的类型不匹配

建议:将GLFloat更改为GLuint,即将索引定义为:

GLuint indices[]={ //...

答案 1 :(得分:0)

除了Amadeus关于将index数组从float更改为GLuint的说法之外,您似乎使用了错误的VAO和EBO。在您向我们展示的代码中,您将所有数据缓冲到VAO中的缓冲区对象中,并将索引缓存到EBO,但是当您尝试绘制时,使用bowHandleVAObowHandleEBO