opengl 如何使用顶点数组对象?

hs1rzwqc  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(197)

我试图模拟一个立方体在墙壁之间弹跳。为此,我构造了2个顶点数组对象,其中一个代表立方体,另一个代表墙壁。首先,我创建了墙壁。我使用glDrawElements调用绘制墙壁,因为我使用了index我也创建了一个立方体模型,但是我没有使用索引缓冲区。所以我将使用glDrawArrays调用来绘制它。我的墙显示得很好,但我不知道如何绘制立方体(在这一步,我甚至没有试图动画立方体,我只是想绘制它)这是我的代码看起来像:


# include "Cube.h"

# include <cstring>

GLuint vao[2];

void init()
{
    cube();

    GLuint program = InitShader( "vshader.glsl", "fshader.glsl" );
    glUseProgram( program );
    GLuint vPosition = glGetAttribLocation(program, "vPosition");
    glGenVertexArrays(2, vao);

    glBindVertexArray(vao[0]);
    GLuint cube_buffer;
    glGenBuffers(1, &cube_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, cube_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cubePoints), cubePoints, GL_STATIC_DRAW);
    glEnableVertexAttribArray(vPosition);
    glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);

    const float mid_value  = 0.0f;
    const float far_value  = 0.35f;
    const float near_value = 1.0f;
    const float positions[60] = {
        /// positions               /// colors
        mid_value, -near_value, 1.0, 0.0, 0.0, /// 0
        near_value, -near_value, 1.0, 0.0, 0.0, /// 1
        near_value, mid_value, 0.0, 1.0, 0.0, /// 2
        near_value, near_value, 0.0, 1.0, 0.0, /// 3
        mid_value, near_value, 1.0, 1.0, 0.0, /// 4
        -near_value, near_value, 1.0, 1.0, 0.0, /// 5
        -near_value, mid_value, 0.0, 0.0, 1.0, /// 6
        -near_value, -near_value, 0.0, 0.0, 1.0, /// 7
        far_value, -far_value, 1.0, 0.0, 0.0, /// 8
        far_value, far_value, 0.0, 1.0, 0.0, /// 9
        -far_value, far_value, 1.0, 1.0, 0.0, /// 10
        -far_value, -far_value, 0.0, 0.0, 1.0 /// 11
    };

    unsigned int indices[36] = {
        7,11,0,
        0,8,1,
        1,8,2,
        2,9,3,
        3,9,4,
        4,10,5,
        5,10,6,
        6,11,7,
        11,0,8,
        8,2,9,
        9,4,10,
        10,6,11,
    };

    glBindVertexArray(vao[1]);
    GLuint room_buffer;
    glGenBuffers(1, &room_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, room_buffer);
    glBufferData(GL_ARRAY_BUFFER, 60 * sizeof(float), positions, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)0);  /// positions
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(2* sizeof(float)));  /// colors

    GLuint index_buffer;
    glGenBuffers(1, &index_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(unsigned int), indices, GL_STATIC_DRAW);

    glClearColor(1.0, 1.0, 1.0, 1.0);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);

    // glDrawArrays(...); --> how to use this, or should I really use this here?

    glutSwapBuffers();
}

void idle() {
    glutPostRedisplay();
}

void reshape(int width, int height) {
    glViewport(0, 0, width, height);
}

int main( int argc, char**argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize( 600, 600 );

    glutCreateWindow( "Bouncing Cube" );
    glewExperimental = GL_TRUE;
    glewInit();

    init();

    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutReshapeFunc(reshape);

    glutMainLoop();
}

在这段代码中,cubePoints是由另一个翻译单元中的cube()方法填充的。我不太清楚如何在屏幕上绘制立方体,以及如何在这里使用glDrawArrays?我应该绑定还是取消绑定任何东西来绘制它?我觉得我没有正确使用顶点数组对象。下面是我的墙的样子:

我只想让立方体出现在白色矩形所在的远端。是否可以使用glDrawElements调用glDrawArrays?

ssm49v7z

ssm49v7z1#

您必须在“draw”调用之前绑定顶点数组对象。绘图指令使用存储在当前绑定的顶点数组对象中的数据:

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(vao[0]);
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);

    glBindVertexArray(vao[1]);
    glDrawArrays(...);

    glutSwapBuffers();
}

相关问题