1
如何将纹理应用于Android中的顶点缓冲区对象?将纹理应用于顶点缓冲区对象基元
答:
的代码工作正常,但它缺少对
glEnable(GL_TEXTURE_2D);
这一个电话和
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
通话都需要以便顶点缓冲区对象绘制纹理。
问题:
据我所知,首先你必须创建一个NIO缓冲区:
ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
FloatBuffer textureBuffer = tbb.asFloatBuffer();
textureBuffer.put(texCoords);
textureBuffer.position(0);
在此代码示例,该阵列texCoords包含双组分(s,t)纹理数据。
创建NIO缓冲区后,你需要将它传递给OpenGL和创建顶点缓冲区对象:
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);
这样通吃initalization的照顾。接下来,我们需要借助它,我们这样做是这样的:
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)'启用,但似乎我需要调用两者。 –