I have adapted the code from Draw Text in OpenGL ES Android into my app, but the text isn't really showing up.
Here's what it currently looks like:
The green rectangle box is actually a Drawable bitmap.
I am expecting some sort of text, either white or black on top of the background, but nothing is ever showing up.
Here's the code for the Text class.
package gl.es;
import java.nio.*;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.opengl.*;
import android.opengl.Matrix;
public class Text {
public int textTexture;
public FloatBuffer textFloatBuffer;
public FloatBuffer textureFloatBuffer;
public int bufferSize;
public int program;
private final int textWidth = 112;
private final int textHeight = 16;
//private float posX;
private int aTextPositionLocation;
private int aTexturePositionLocation;
private int uMatrixLocation;
private int uTextureUnitLocation;
public Text(Context context) {
program = Shader.buildProgram(context, R.raw.text_vert, R.raw.text_frag);
aTextPositionLocation = GLES20.glGetAttribLocation(program, "a_textPosition");
aTexturePositionLocation = GLES20.glGetAttribLocation(program, "a_texturePosition");
uMatrixLocation = GLES20.glGetUniformLocation(program, "u_matrix");
uTextureUnitLocation = GLES20.glGetUniformLocation(program, "u_textureUnit");
textTexture = createTexture(context);
setBuffer();
//posX = 0f;
}
public int createTexture(Context context) {
//Create empty mutable bitmap.
Bitmap bitmap = Bitmap.createBitmap(textWidth, textHeight, Bitmap.Config.ARGB_8888);
//Use Canvas to paint over it.
Canvas canvas = new Canvas(bitmap);
bitmap.eraseColor(0);
//Draw background
Drawable background = context.getResources().getDrawable(R.drawable.text_bg);
background.setBounds(0, 0, 112, 16);
background.draw(canvas);
//Draw text
Paint textPaint = new Paint();
textPaint.setTextSize(6f);
textPaint.setAntiAlias(false);
textPaint.setARGB(0xff, 0, 0, 0);
//Draw text centered.
canvas.drawText("Text.", 0, 0, textPaint);
int[] texture = new int[1];
GLES20.glGenTextures(1, texture, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
//Recycle.
bitmap.recycle();
return texture[0];
}
public void setBuffer() {
final float[] vertexData = {
0f, 0f,
textWidth, 0f,
0f, textHeight,
0f, textHeight,
textWidth, 0f,
textWidth, textHeight
};
final float[] texData = {
0f, 1f,
1f, 1f,
0f, 0f,
0f, 0f,
1f, 1f,
1f, 0f
};
textFloatBuffer = ByteBuffer.allocateDirect(vertexData.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
textFloatBuffer.put(vertexData);
textureFloatBuffer = ByteBuffer.allocateDirect(texData.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
textureFloatBuffer.put(texData);
bufferSize = vertexData.length / 2;
}
public void transform(float[] model, float[] projection, float[] mvp) {
Matrix.setIdentityM(model, 0);
Matrix.translateM(model, 0, 0f, 0f, -60f);
Matrix.translateM(model, 0, -60f, 0f, 0f);
Matrix.multiplyMM(mvp, 0, projection, 0, model, 0);
//posX = (posX - 0.3f) % 112f;
}
public void setVertexPointers() {
textFloatBuffer.position(0);
GLES20.glVertexAttribPointer(aTextPositionLocation, 2, GLES20.GL_FLOAT, false, 2 * 4, textFloatBuffer);
GLES20.glEnableVertexAttribArray(aTextPositionLocation);
textFloatBuffer.position(0);
textureFloatBuffer.position(0);
GLES20.glVertexAttribPointer(aTexturePositionLocation, 2, GLES20.GL_FLOAT, false, 2 * 4, textureFloatBuffer);
GLES20.glEnableVertexAttribArray(aTexturePositionLocation);
textureFloatBuffer.position(0);
}
public void draw() {
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, bufferSize);
}
public void useProgram() {
GLES20.glUseProgram(program);
}
public void setUniforms(float[] matrix) {
GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textTexture);
GLES20.glUniform1i(uTextureUnitLocation, 1);
}
}
Could someone tell me where I'm doing wrong? What should I do in order to fix this? Thanks in advance.