android - Android : Pointing out the correct image in texture coordinate buffers 上的 VBO

标签 android opengl-es

有谁知道如何指出存储在 HW 缓冲区中的纹理缓冲区数组的给定部分?我正在绘制一个三角形 strip 并用方形图像填充它。在我的纹理中,我有两个相邻的正方形图像,因此纹理坐标缓冲区用总共 16 个 float 指出它们。

使用软件缓冲区我这样做是为了访问纹理中的第二个图像:

textureCoordinateBuffer.position(8);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureCoordinateBuffer);

使用硬件缓冲区我假设我做了这样的事情:

// setup HW buffers
// ...
// select HW buffers
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER,vertexCoordinateBufferIndex);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, textureCoordinateBufferIndex);

// Point out the first image in the texture coordinate buffer
gl11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);
// Draw
// ...

如果您想指出纹理中的第一张图像,这会很好地工作。 但我想访问第二张图片 - 所以我假设我在最后一行这样做:

// Point out the second image in the texture coordinate buffer - doesn't work!
gl11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 8);

但是这会呈现出扭曲和变色的图像。

有人知道如何正确地做到这一点吗?

最佳答案

您可能想看看 NeHe Android 教程。他们对此进行了详细介绍,并向您展示了您需要做什么。

具体来说,您正在寻找的类(class)在这里: http://insanitydesign.com/wp/projects/nehe-android-ports/

第六课

您可能没有绑定(bind)和启用缓冲区,这里是教程中的一个 fragment :

public void draw(GL10 gl) {
        //Bind our only previously generated texture in this case
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        //Point to our buffers
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        //Set the face rotation
        gl.glFrontFace(GL10.GL_CCW);

        //Enable the vertex and texture state
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        //Draw the vertices as triangles, based on the Index Buffer information
        gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);

        //Disable the client state before leaving
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

来源:Insanity Design - http://insanitydesign.com/

编辑: 我明白你在问什么。这里有更多的代码应该能够帮助你。如果您查看适用于 Android 的 SpriteMethodTest 应用程序: http://apps-for-android.googlecode.com/svn/trunk/SpriteMethodTest

您会注意到 Chris Pruett(此应用程序的开发者)向您展示了多种在屏幕上绘制纹理的方法。以下是您正在寻找的代码(我相信)。

网格.java

public void beginDrawingStrips(GL10 gl, boolean useTexture) {
        beginDrawing(gl, useTexture);
        if (!mUseHardwareBuffers) {
            gl.glVertexPointer(3, mCoordinateType, 0, mVertexBuffer);

            if (useTexture) {
                gl.glTexCoordPointer(2, mCoordinateType, 0, mTexCoordBuffer);
            } 

        } else {
            GL11 gl11 = (GL11)gl;
            // draw using hardware buffers
            gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertBufferIndex);
            gl11.glVertexPointer(3, mCoordinateType, 0, 0);

            gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex);
            gl11.glTexCoordPointer(2, mCoordinateType, 0, 0);

            gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferIndex);
        }
    }

    // Assumes beginDrawingStrips() has been called before this.
    public void drawStrip(GL10 gl, boolean useTexture, int startIndex, int indexCount) {
        int count = indexCount;
        if (startIndex + indexCount >= mIndexCount) {
                count = mIndexCount - startIndex;
        }
        if (!mUseHardwareBuffers) {
            gl.glDrawElements(GL10.GL_TRIANGLES, count,
                    GL10.GL_UNSIGNED_SHORT, mIndexBuffer.position(startIndex));
        } else {
                GL11 gl11 = (GL11)gl;
            gl11.glDrawElements(GL11.GL_TRIANGLES, count,
                    GL11.GL_UNSIGNED_SHORT, startIndex * CHAR_SIZE);

        }
    }

具体来说,您需要查看使用 !mUseHardwareBuffers 的错误分支的代码。我建议您查看完整的 Grid.java 文件以更好地说明如何执行此操作,因为他还设置了纹理指针并启用 OpenGL 以开始绘制。

旁注:我建议也阅读 Chris 的这篇文章: http://www.scribd.com/doc/16917369/Writing-Real-Time-Games-for-Android

他详细介绍了这个应用程序的功能,以及他发现绘制纹理最有效的方法是什么。

关于android - Android : Pointing out the correct image in texture coordinate buffers 上的 VBO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4950978/

相关文章:

c++ - 没有 GLkit 的 OpenGL ES。 GLKMatrix 的类似物和纯 OpenGL ES 中的函数

java - 无纹理四边形未在 OpenGL ES 3.0 中渲染

android - 字符串末尾的正则表达式替换在 Android 版 Kotlin 中不起作用

android - 如何使用 rxandroidble 禁用通知?

java - 生成并播放声音信号

java - Android 将值存储在返回 null 的字符串中

opengl - 我是否应该在统一缓冲区或着色器存储缓冲区对象内使用 `vec3`?

android - 球体上的 OpenGL 纹理

ios - 为什么我可以从相机胶卷的视频中获得 GL_LUMINANCE 而不是 GL_LUMINANCE_ALPHA?

android - 如何使用 flutter 将新闻(例如应用程序)大小最小化到 10 MB 以下,并添加音频播放器?