【发布时间】:2011-10-13 10:39:59
【问题描述】:
如何将纹理应用到 Android 中的顶点缓冲区对象?
回答:
代码运行良好,只是缺少调用
glEnable(GL_TEXTURE_2D);
这和呼唤
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
为了让顶点缓冲对象绘制纹理,两者都是必需的。
问题:
据我所知,首先你必须创建一个 NIO Buffer:
ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
FloatBuffer textureBuffer = tbb.asFloatBuffer();
textureBuffer.put(texCoords);
textureBuffer.position(0);
在此代码示例中,数组 texCoords 包含 2 分量 (s, t) 纹理数据。
创建NIO Buffer后,需要将其传递给opengl,并创建Vertex Buffer Object:
int[] id = new int[1];//stores the generated ID.
gl11.glGenBuffers(1, id, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, id[0]);
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, texCoords.length * 4, textureBuffer, GL11.GL_STATIC_DRAW);
这样就完成了所有的初始化。接下来我们需要绘制它,我们这样做:
gl11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);//enable for textures
gl11.glActiveTexture(GL11.GL_TEXTURE0);
//lets pretend we created our texture elsewheres and we have an ID to represent it.
gl11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
//Now we bind the VBO and point to the buffer.
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, id[0])//the id generated earlier.
gl11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);//this points to the bound buffer
//Lets also pretend we have our Vertex and Index buffers specified.
//and they are bound/drawn correctly.
因此,即使这是我认为 OpenGL 绘制纹理所需要的,但我有一个错误,并且只有一个红色三角形(没有我调制的石头纹理)渲染。
【问题讨论】:
-
我很抱歉。我可以确认我提供的所有代码都有效。我只错过了一件事
gl11.glEnable(GL11.GL_TEXTURE_2D)。我以为我是通过使用gl11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY)来启用的,但看来我需要同时调用两者。
标签: android opengl-es textures vbo