繁体   English   中英

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

[英]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.

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