glDrawElements仅绘制由索引指定的第一个三角形 [英] glDrawElements only draws first triangle specified by indices

查看:55
本文介绍了glDrawElements仅绘制由索引指定的第一个三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习OpenGL,但glDrawElements遇到了一些麻烦.我正在尝试使用带有GL_TRIANGLE的glDrawElements绘制两个三角形,但是仅出现一个三角形.

I just started learning OpenGL and I am having some trouble with glDrawElements. I am trying to draw two triangles using glDrawElements with GL_TRIANGLE, but only one triangle appears.

期望:glDrawElements绘制带有顶点的两个三角形

What is expected: glDrawElements draw two triangles with vertices

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

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

会发生什么:glDrawElements绘制一个顶点为(0,0)(1,1)(-1,1)的三角形

What happens: glDrawElements draw one triangle with vertices (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;
}

此示例打开一个640x480 GLFW窗口,显示一个三角形,该三角形的两个顶点均位于窗口的上角,而第三个顶点位于窗口的中间.另一个三角形,两个顶点的底角和中间的第三个顶点均缺失.为什么呢?

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. The other triangle, with vertices in both bottom corners and the third vertex in the middle, is missing. Why is that?

规格:
操作系统:Linux Mint 17
GPU :nVidia GTX 275
GPU驱动程序:331.67
GLEW版本:1.10.0
GLFW版本:3.0.4

Specs:
OS: Linux Mint 17
GPU: nVidia GTX 275
GPU driver: 331.67
GLEW version: 1.10.0
GLFW version: 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行的末尾添加的逗号.

Note the added comma at the end of line 3.

如果vertices仅具有9个元素,则当glDrawElements()尝试读取第五个顶点时,只有X坐标有效.如果幸运的话,Y坐标将包含垃圾,如果不幸运,则将包含段错误.

If vertices only has 9 elements then when glDrawElements() goes to try to read the fifth vertex only the X coordinate will be valid. The Y coordinate will contain garbage if you're lucky, a segfault if you're not.

此外,在不指定某些着色器的情况下,不应使用通用顶点属性功能.

Also, you shouldn't use the generic vertex attribute functions without specifying some shaders.

一起:

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

这篇关于glDrawElements仅绘制由索引指定的第一个三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆