我刚刚开始学习OpenGL,我在使用glDrawElements时遇到了一些麻烦。我正在尝试使用glDrawElements和GL_TRIANGLE绘制两个三角形,但只出现一个三角形。
预期的内容:glDrawElements绘制两个带顶点的三角形
(0,0)(1,1)( - 1,1)和(0,0)( - 1,-1)(1,-1)
会发生什么:glDrawElements绘制一个带顶点的三角形(0,0)(1,1)( - 1,1)
短,自包含,正确(可编译),例如:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(int argc, char *argv[])
{
glfwInit();
GLFWwindow* window = glfwCreateWindow(640, 480, "Sample code does not display two triangles", NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
GLfloat vertices[] {
+0.0f, +0.0f, //0
+1.0f, +1.0f, //1
-1.0f, +1.0f, //2
-1.0f, -1.0f //3
+1.0f, -1.0f //4
};
GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLuint indexBufferID;
GLushort indices[] {0,1,2, 0,3,4};
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
while (!glfwWindowShouldClose(window))
{
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
此示例打开一个640x480 GLFW窗口,显示一个三角形,其顶角和顶部位于窗口中间的第三个顶点。缺少另一个三角形,其中两个底角中的顶点和中间的第三个顶点。那是为什么?
规格:
操作系统:Linux Mint 17
GPU :nVidia GTX 275
GPU驱动程序:331.67
GLEW版:1.10.0
GLFW版:3.0.4
答案 0 :(得分:5)
逗号很重要。这样:
GLfloat vertices[] =
{
+0.0f, +0.0f, //0
+1.0f, +1.0f, //1
-1.0f, +1.0f, //2
-1.0f, -1.0f //3
+1.0f, -1.0f //4
};
size_t elements = sizeof( vertices ) / sizeof( GLfloat ) = 9(!)
与此不同:
GLfloat vertices[] =
{
+0.0f, +0.0f, //0
+1.0f, +1.0f, //1
-1.0f, +1.0f, //2
-1.0f, -1.0f, //3
+1.0f, -1.0f //4
};
size_t elements = sizeof( vertices ) / sizeof( GLfloat ) = 10
请注意第3
行末尾添加的逗号。
如果vertices
只有9
个元素,那么当glDrawElements()
尝试读取第五个顶点时,只有X坐标才有效。如果幸运的话,Y坐标将包含垃圾,如果不是,则会出现段错误。
此外,如果不指定某些着色器,则不应使用通用顶点属性函数。
所有在一起:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(int argc, char *argv[])
{
glfwInit();
GLFWwindow* window = glfwCreateWindow(640, 480, "Sample code does not display two triangles", NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
GLfloat vertices[] =
{
+0.0f, +0.0f, //0
+1.0f, +1.0f, //1
-1.0f, +1.0f, //2
-1.0f, -1.0f, //3
+1.0f, -1.0f, //4
};
GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 2, GL_FLOAT, 0, 0 );
GLuint indexBufferID;
GLushort indices[] = { 0,1,2, 0,3,4 };
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
while (!glfwWindowShouldClose(window))
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}