Applying textures to a vertex buffer object primitive
How do you apply textures to a vertex buffer object in Android?
ANSWER:
The code works fine, except it is missing a call to
glEnable(GL_TEXTURE_2D);
This and the call of
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
are both required for in order for vertex buffer object to draw texture.
QUESTION:
From what I know, first you must create a NIO Buffer:
ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
FloatBuffer textureBuffer = tbb.asFloatBuffer();
textureBuffer.put(texCoords);
textureBuffer.position(0);
In this code sample, the array texCoords contains the 2-component (s, t) texture data.
After creating the NIO Buffer, you need to pass it to opengl and create 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);
So that takes care of all the initalization. Next we need to draw it, and we do so like this:
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.
So even though this is what I would think would be needed in order for OpenGL to draw the texture, I have an error, and only a red triangle (without my modulated stone texture) renders开发者_StackOverflow中文版.
Both functions for VBO's need to be called to enable textures.
gl.glEnable(GL11.GL_TEXTURE_2D); gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
精彩评论