首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android和opengl es 1.1中使用顶点缓冲区

如何在android和opengl es 1.1中使用顶点缓冲区
EN

Stack Overflow用户
提问于 2012-07-23 02:22:55
回答 1查看 1.8K关注 0票数 2

我几乎花了一整天的时间尝试使用opengl 1.1和顶点缓冲区渲染简单的多边形,但没有运气。我找了又找,但没找到多少。

这就是我到目前为止所知道的:

代码语言:javascript
复制
public class Polygon {

    int bufferId = 0;

    private FloatBuffer vertexBuffer;  // Buffer for vertex-array

    private float[] vertices = {  // Vertices for the square
            -1.0f, -1.0f, 0.0f,  // 0. left-bottom
            1.0f, -1.0f, 0.0f,  // 1. right-bottom
            -1.0f, 1.0f, 0.0f,  // 2. left-top
            1.0f, 1.0f, 0.0f   // 3. right-top
    };

    private ByteBuffer indexBuffer;

    private byte[] indices = {0, 1, 2, 3}; // Indices to above vertices (in CCW)


    // Constructor - Setup the vertex buffer
    public Polygon() {
        // Setup vertex array buffer. Vertices in float. A float has 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder()); // Use native byte order
        vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
        vertexBuffer.put(vertices);         // Copy data into buffer
        vertexBuffer.position(0);           // Rewind


        indexBuffer = ByteBuffer.allocateDirect(indices.length);
        indexBuffer.put(indices);
        indexBuffer.position(0);


        int[] buffers = new int[1];
        GLES11.glGenBuffers(1, buffers, 0);
        bufferId = buffers[0];

        GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, bufferId);
        GLES11.glBufferData(GLES11.GL_ARRAY_BUFFER, vertices.length, vertexBuffer, GLES11.GL_STATIC_DRAW);
        GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, 0);
    }


    // Render the shape
    public void draw(GL10 gl) {

        GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, bufferId);

        GLES11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        GLES11.glVertexPointer(3, GLES11.GL_FLOAT, 0, 0);
        GLES11.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length);
        GLES11.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, 0);
    }
}

它不渲染任何东西,在android logcat中也没有相关的错误。我省略了其余的代码。问题显然出在这个类中,因为当我将draw方法更改为下面的方法时,它工作得很好:

代码语言:javascript
复制
public void draw(GL10 gl) {

            GLES11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            GLES11.glVertexPointer(3, GLES11.GL_FLOAT, 0, vertexBuffer);
            GLES11.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length);
            GLES11.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        }

那么,我做错了什么呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-23 02:28:12

  1. 不要在logCat中查找错误,您希望使用glGetError()检查OpenGL错误,并检查返回值是否为零(没有错误),或者非零的glDrawArrays是glDrawArrays的错误参数。您希望提供顶点数,而不是浮点数。它应该是vertices.length/3 (每个顶点3个浮点数)。您当前正在将数组绘制到一些垃圾数据中,所以我不确定这会产生什么样的后果。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11602846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档