如何在OpenGL ES 2中创建和使用VBO [英] How to create and use VBOs in OpenGL ES 2

查看:160
本文介绍了如何在OpenGL ES 2中创建和使用VBO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求帮助理解维生素生态系统。我做了大量的研究,并找到了关于这个主题的教程,但它们对我来说仍然含糊不清。我有几个问题:

I am looking for help with understanding VBOs. I have done a ton of research and have found tutorials on the subject, but they are still vague to me. I have a few questions:

应该在哪里创建VBO,我应该如何创建VBO?

Where should a VBO be created, and how should I create one?

我目前正在使用下面的代码来初始化我的顶点和索引缓冲区:

I am currently using the code right below to initialize my vertex and index buffers:

    vertices = new float[]
            {
                    p[0].x, p[0].y, 0.0f,
                    p[1].x, p[1].y, 0.0f,
                    p[2].x, p[2].y, 0.0f,
                    p[3].x, p[3].y, 0.0f,
            };

    // The order of vertex rendering for a quad
    indices = new short[] {0, 1, 2, 0, 2, 3};

    ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    ByteBuffer dlb = ByteBuffer.allocateDirect(indices.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(indices);
    drawListBuffer.position(0);

如果我是正确的,这不是创建VBO。那么,如果我想制作一个VBO,创建VBO的代码是否会在上面列出的代码之后?如果是这样,它将如何创建?

If I am correct, this is not creating a VBO. So, if I wanted to make a VBO, would the code to create a VBO go right after the code listed above? If so, how would it be created?

此外,如何渲染VBO并将其绘制到屏幕上?

Also, how is a VBO rendered and drawn to screen?

它是否以与使用顶点和索引数组相同的方式进行渲染和绘制?如果没有,过程是什么?目前,我渲染并绘制我的对象,如下面的代码所示:

Is it rendered and drawn the same way as just using vertex and index arrays? If not, what is the process? Currently, I render and draw my objects as shown in the code below:

    GLES20.glUseProgram(GraphicTools.sp_SolidColor);

    mPositionHandle =
            GLES20.glGetAttribLocation(GraphicTools.sp_SolidColor, "vPosition");

    GLES20.glEnableVertexAttribArray(mPositionHandle);

    GLES20.glVertexAttribPointer(mPositionHandle, 3,
            GLES20.GL_FLOAT, false,
            0, vertexBuffer);

    mtrxHandle = GLES20.glGetUniformLocation(GraphicTools.sp_SolidColor,
            "uMVPMatrix");

    GLES20.glUniformMatrix4fv(mtrxHandle, 1, false, m, 0);

    GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
            GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    GLES20.glDisableVertexAttribArray(mPositionHandle);

如果您有任何疑问,请与我们联系。在此先感谢。

If you have any questions, let me know. Thanks in advance.

推荐答案

A 顶点缓冲区对象是可以存储顶点数组数据的缓冲区。数据上传一次到图形内存(GPU),可以反复使用来绘制网格。

A Vertex Buffer Object is a buffer where vertex array data can be stored. The data are uploaded one time to the graphics memory (GPU) and can be used repeatedly to draw a mesh.

首先你需要创建2个缓冲区对象,一个用于顶点和一个索引:

First you have to create 2 buffer objects, one for the vertices and one for the indices:

int buffers[] = new int[2];
GLES20.glGenBuffers(2, buffers, 0);
int vbo = buffers[0];
int ibo = buffers[1];

然后你必须绑定缓冲区并传输数据

Then you have to bind the buffer and to transfer the data

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo);
GLES20.glBufferData(
    GLES20.GL_ARRAY_BUFFER,
    vertexBuffer.capacity() * 4, // 4 = bytes per float
    vertexBuffer,
    GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo);
GLES20.glBufferData(
    GLES20.GL_ELEMENT_ARRAY_BUFFER,
    drawListBuffer.capacity() * 2, // 2 = bytes per short
    drawListBuffer,
    GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

如果要绘制网格,则必须定义通用顶点属性数据的数组,你必须绑定索引缓冲区,但你不必将任何数据传输到GPU:

If you want to draw the mesh, then you have to define the array of generic vertex attribute data and you have to bind the index buffer, but you don't have to transfer any data to the GPU:

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo);
GLES20.glVertexAttribPointer(
        mPositionHandle, 3,
        GLES20.GL_FLOAT, false,
        0, 0);                        // <----- 0, because "vbo" is bound
GLES20.glEnableVertexAttribArray(mPositionHandle);

GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo);
GLES20.glDrawElements(
    GLES20.GL_TRIANGLES, indices.length,
    GLES20.GL_UNSIGNED_SHORT, 0);     // <----- 0, because "ibo" is bound

GLES20.glDisableVertexAttribArray(mPositionHandle);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);



参见顶点缓冲对象(VBO)简介

这篇关于如何在OpenGL ES 2中创建和使用VBO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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