I'm just learning open-angle and I need to render a texture on my existing object in the scene. I get the texture id and then I want to output it to my object. Here is my object code
val sqrSize = 20
val vtx = FloatArray(1530)
var vtxCtr = 0
//we use the offset to produce the checkered pattern
var x = -130
var offset = 0
while (x < 130) {
var y = -130 + offset
while (y < 130) {
//each square is 2 triangles = 6 vertices = 18 floats [x/y/z]
vtx[vtxCtr + 0] = x.toFloat()
vtx[vtxCtr + 1] = -20f //floor is 2 points below 0
vtx[vtxCtr + 2] = y.toFloat()
vtx[vtxCtr + 3] = (x + sqrSize).toFloat()
vtx[vtxCtr + 4] = -20f
vtx[vtxCtr + 5] = y.toFloat()
vtx[vtxCtr + 6] = x.toFloat()
vtx[vtxCtr + 7] = -20f
vtx[vtxCtr + 8] = (y + sqrSize).toFloat()
vtx[vtxCtr + 9] = (x + sqrSize).toFloat()
vtx[vtxCtr + 10] = -20f
vtx[vtxCtr + 11] = y.toFloat()
vtx[vtxCtr + 12] = x.toFloat()
vtx[vtxCtr + 13] = -20f
vtx[vtxCtr + 14] = (y + sqrSize).toFloat()
vtx[vtxCtr + 15] = (x + sqrSize).toFloat()
vtx[vtxCtr + 16] = -20f
vtx[vtxCtr + 17] = (y + sqrSize).toFloat()
vtxCtr += 18
y += sqrSize * 2
}
x += sqrSize
offset = sqrSize - offset
}
StoreVertexData(gl, vtx, mFLOOR)
Next, I save the object in the buffer and then draw it. Render code below
private fun DrawObject(gl: GL11, pShapeType: Int, pObjNum: Int) {
//activate vertex array type
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY)
//get vertices for this object id
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, pObjNum)
//each vertex is made up of 3 floats [x\y\z]
gl.glVertexPointer(3, GL11.GL_FLOAT, 0, 0)
//draw triangles
gl.glDrawArrays(pShapeType, 0, mBufferLen[pObjNum])
//unbind from memory
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0)
}
And then I draw the shape in OnDrawFrame, but before that I apply the texture.
gl.glColor4f(0.2f, 1f, 1f, 1f)
gl.glEnable(GL11.GL_TEXTURE_2D)
gl.glBindTexture(GL11.GL_TEXTURE_2D,textureHandle)
gl.glTexCoordPointer(2,GL11.GL_FLOAT,0,bufferTexture)
DrawObject(gl, GL11.GL_TRIANGLES, mFLOOR)
The texture does not appear at startup, what could be the problem? And what is the best way to display a texture on an object in OpenGl ES 1.1
I thought that the problem might be that I incorrectly specified the texture coordinates (they are below), but apparently, these coordinates indicate that the texture should cover the entire object
private var textureCoord = floatArrayOf( 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, )