简体   繁体   English

glDrawElements仅绘制由索引指定的第一个三角形

[英]glDrawElements only draws first triangle specified by indices

I just started learning OpenGL and I am having some trouble with glDrawElements. 我刚刚开始学习OpenGL,但glDrawElements遇到了一些麻烦。 I am trying to draw two triangles using glDrawElements with GL_TRIANGLE, but only one triangle appears. 我正在尝试使用带有GL_TRIANGLE的glDrawElements绘制两个三角形,但是仅出现一个三角形。

What is expected : glDrawElements draw two triangles with vertices 期望什么 :glDrawElements绘制两个带有顶点的三角形

(0,0) (1,1) (-1,1) and (0,0) (-1,-1) (1,-1) (0,0)(1,1)(-1,1)和(0,0)(-1,-1)(1,-1)

What happens : glDrawElements draw one triangle with vertices (0,0) (1,1) (-1,1) 发生了什么 :glDrawElements绘制一个顶点为(0,0)(1,1)(-1,1)的三角形

Short, Self Contained, Correct (Compilable), Example: 简短,自包含,正确(可编译),例如:

#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;
}

This sample opens a 640x480 GLFW window, displays a triangle with vertices in both upper corners and the third vertex in the middle of the window. 此示例打开一个640x480 GLFW窗口,显示一个三角形,该三角形的顶点在窗口的两个上角和第三个顶点。 The other triangle, with vertices in both bottom corners and the third vertex in the middle, is missing. 另一个三角形(两个顶点的底角和中间的第三个顶点)均丢失。 Why is that? 这是为什么?

Specs: 眼镜:
OS : Linux Mint 17 操作系统 :Linux Mint 17
GPU : nVidia GTX 275 显卡 :nVidia GTX 275
GPU driver : 331.67 GPU驱动程序 :331.67
GLEW version : 1.10.0 GLEW版本 :1.10.0
GLFW version : 3.0.4 GLFW版本 :3.0.4

Commas are important. 逗号很重要。 This: 这个:

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(!)

is not the same as this: 与此不同:

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

Note the added comma at the end of line 3 . 注意第3行末尾添加的逗号。

If vertices only has 9 elements then when glDrawElements() goes to try to read the fifth vertex only the X coordinate will be valid. 如果vertices只有9元素,则当glDrawElements()尝试读取第五个顶点时,只有X坐标有效。 The Y coordinate will contain garbage if you're lucky, a segfault if you're not. 如果幸运的话,Y坐标将包含垃圾,如果不幸运,则将包含段错误。

Also, you shouldn't use the generic vertex attribute functions without specifying some shaders. 另外,在未指定某些着色器的情况下,不应使用通用顶点属性功能。

All together: 全部一起:

#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.

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