简体   繁体   English

Android opengl es 2.0顶点/索引缓冲区不起作用

[英]Android opengl es 2.0 Vertex/Index Buffer not working

I've got a shape I'm trying to draw through OpenGL ES 2.0 on Android 3.2. 我正在尝试绘制Android 3.2上的OpenGL ES 2.0的形状。 I can get my scene rendering as intended with DrawArrays and a FloatBuffer, but not through glGenBuffers and a vbo/ibo. 我可以使用DrawArrays和FloatBuffer达到预期的场景渲染,但不能通过glGenBuffers和vbo / ibo实现。

Here are some snippets I'm using with irrelevant sections taken out 这是我正在使用的一些摘录,其中包含不相关的部分

    GLES20.glGenBuffers(1, vbo, 0);
    GLES20.glGenBuffers(1, ibo, 0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[0]); 
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, this.vertexBuffer.capacity() * 4, vertexBuffer, GLES20.GL_STATIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo[0]);
    GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer.capacity() * 2, indexBuffer, GLES20.GL_STATIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

Above is my code for creating my arrays. 上面是我创建数组的代码。 Below is my code for rendering. 以下是我的渲染代码。

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[0]);
    GLES20.glEnableVertexAttribArray(positionHandle);
    GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo[0]);

    GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, 1, GLES20.GL_UNSIGNED_SHORT, 0);

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

I know for a fact that my vertex float buffer is working correctly because If I replace the above code with the below code, it works as intended : 我知道我的顶点浮点缓冲区可以正常工作,因为如果用下面的代码替换上面的代码,则它可以正常工作:

    GLES20.glEnableVertexAttribArray(positionHandle);
    GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 3);

While I was debugging to find out if DrawArrays would work (which it does if I remove all code pertaining to binding buffers), I found that I could leave all the gen/bind/data buffer calls EXCEPT *GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[0]);*, this one exclusively would prevent rendering when using drawArrays 当我调试以确定DrawArrays是否可以工作时(如果删除所有与绑定缓冲区有关的代码,它会执行此操作),但是我发现可以留下所有gen / bind / data缓冲区调用,但* GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER ,vbo [0]); *,这一点将在使用drawArrays时专门阻止渲染

I've spent all day trying changing little details and calls in different orders to no avail. 我花了整整一天的时间尝试更改一些小细节,并按不同的顺序打电话给我,无济于事。

At first I was trying to render a cube with the following data : private float[] pts = { -1f, 1f, -1f, 最初,我尝试使用以下数据渲染多维数据集:private float [] pts = {-1f,1f,-1f,

                         1f,    1f,     -1f,
                         -1f,   1f,     1f,
                         1f,    1f,     1f,
                         -1f,   -1f,    -1f,
                         1f,    -1f,    -1f,
                         -1f,   -1f,    1f,
                         1f,    -1f,    1f };
private short indecies[] = {0,1,2,3,6,7,4,5,0,1,1,5,3,7,7,6,6,2,4,0};

But I decided to go with this test data and this works in drawArrays without the use of the indicies 但是我决定使用这个测试数据,并且可以在不使用索引的情况下在drawArrays中工作

    private float[] pts = {     -1,0,0,
                            1f,0,0,
                            0f,1,0};
private short[] indecies = {0,1,2};

In case someone else struggles with this later, Chiral Code told me how to fix my problem by replacing 万一以后有人遇到麻烦,手性规范告诉我如何通过替换来解决我的问题

GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);

with

GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3*4, 0);

Also use the indicies count for the corresponding argument to glDrawElements. 还可以对glDrawElements的相应参数使用索引计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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