[英]glDrawElements only draws first triangle specified by indices
我剛剛開始學習OpenGL,但glDrawElements遇到了一些麻煩。 我正在嘗試使用帶有GL_TRIANGLE的glDrawElements繪制兩個三角形,但是僅出現一個三角形。
期望什么 :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
顯卡 :nVidia GTX 275
GPU驅動程序 :331.67
GLEW版本 :1.10.0
GLFW版本 :3.0.4
逗號很重要。 這個:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.