Note that there are some explanatory texts on larger screens.

plurals
  1. POApplying textures to a vertex buffer object primitive
    text
    copied!<p>How do you apply textures to a vertex buffer object in Android?</p> <p><strong>ANSWER:</strong> </p> <hr> <p>The code works fine, except it is missing a call to</p> <pre><code>glEnable(GL_TEXTURE_2D); </code></pre> <p>This and the call of</p> <pre><code>glEnableClientState(GL_TEXTURE_COORD_ARRAY); </code></pre> <p>are both required for in order for vertex buffer object to draw texture.</p> <hr> <p><strong>QUESTION:</strong></p> <p>From what I know, first you must create a NIO Buffer:</p> <pre><code>ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4); tbb.order(ByteOrder.nativeOrder()); FloatBuffer textureBuffer = tbb.asFloatBuffer(); textureBuffer.put(texCoords); textureBuffer.position(0); </code></pre> <p>In this code sample, the array <em>texCoords</em> contains the 2-component (s, t) texture data.</p> <p>After creating the NIO Buffer, you need to pass it to opengl and create Vertex Buffer Object:</p> <pre><code>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); </code></pre> <p>So that takes care of all the initalization. Next we need to draw it, and we do so like this:</p> <pre><code>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. </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload