I noticed some strange behavior when trying to texture in OpenGL-ES for Android. I've followed some tutorials and applied the implementations to my code. Where things differ is where I encapsulate the drawing code in an entity class, and point the code to my existing (but altered to fit) shader. This is the texture that I'm trying to draw:
Just needed something with vertical and horizontal lines. Here's how it is drawn:
It looks to be the texture coordinates, But nothing seems to happen when I switch to trianglefan either.
Below is the full Entity class:
Additionally, I call the constructors in the render class as such:
...
public void draw(float[] mMVPMatrix) {
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(
mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
GLRenderClass.checkGlError("glGetUniformLocation");
//Texture start
mTextureUniformHandle = GLES20.glGetUniformLocation(mProgram, "u_Texture");
mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinate");
// Set the active texture unit to texture unit 0.
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
// Bind the texture to this unit.
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);
// Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
GLES20.glUniform1i(mTextureUniformHandle, 0);
Matrix.translateM(mMVPMatrix, 0, Tx, Ty, Tz);
Matrix.scaleM(mMVPMatrix, 0, Sx, Sy, Sz);
Matrix.rotateM(mMVPMatrix, 0, Rangle, Rx, Ry, Rz);
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLRenderClass.checkGlError("glUniformMatrix4fv");
// Draw the square
GLES20.glDrawElements(
GLES20.GL_TRIANGLES, drawOrder.length,
GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
//Render Class
private Entity mSquare;
...
Class constructor...
...
mSquare = new Entity (mMVPMatrix, mActivityContext);
And for drawing:
mPlayer.draw(mMVPMatrix);