21 package com.arm.malideveloper.openglessdk.highqualitytextjava;
25 import java.nio.ByteBuffer;
26 import java.nio.ByteOrder;
27 import java.nio.FloatBuffer;
29 import android.graphics.Bitmap;
30 import android.graphics.Canvas;
31 import android.graphics.Color;
32 import android.graphics.Paint;
33 import android.graphics.PorterDuffXfermode;
35 import android.opengl.GLES20;
36 import android.opengl.GLUtils;
37 import android.opengl.Matrix;
43 private String
theText =
new String(
"Empty");;
65 thePosition.set(aX, aY, aZ, 1.0f);
69 public void setRelPos(
float aX,
float aY,
float aZ) {
78 thePosition.z = -4.0f;
100 Vector4f vl = Vector4f.sub(cLB, cLT);
101 Vector4f vr = Vector4f.sub(cRB, cRT);
102 textSize = (vl.length3() + vr.length3()) / 2.0
f;
120 HighQualityTextRenderer.checkGLError(
"loadShaders");
122 mProgram = GLES20.glCreateProgram();
123 GLES20.glAttachShader(
mProgram, vertexShader);
124 HighQualityTextRenderer.checkGLError(
"glAttachShader:vert");
125 GLES20.glAttachShader(
mProgram, fragmentShader);
126 HighQualityTextRenderer.checkGLError(
"glAttachShader:frag");
128 HighQualityTextRenderer.checkGLError(
"glLinkProgram");
130 HighQualityTextRenderer.checkGLError(
"glUseProgram");
134 HighQualityTextRenderer.checkGLError(
"glGetAttribLocation:vPosition");
137 HighQualityTextRenderer.checkGLError(
"glGetAttribLocation:vTexCoord");
140 HighQualityTextRenderer.checkGLError(
"glGetUniformLocation:uMVPMatrix");
142 HighQualityTextRenderer.checkGLError(
"glGetUniformLocation:u_s2dTexture");
144 GLES20.glHint(GLES20.GL_GENERATE_MIPMAP_HINT, GLES20.GL_NICEST);
145 HighQualityTextRenderer.checkGLError(
"glHint");
146 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
147 HighQualityTextRenderer.checkGLError(
"glActiveTexture");
149 HighQualityTextRenderer.checkGLError(
"glGenTextures");
152 HighQualityTextRenderer.checkGLError(
"glUniform1i");
153 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
154 HighQualityTextRenderer.checkGLError(
"glBindTexture");
156 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
157 HighQualityTextRenderer.checkGLError(
"glTexParameterf:GL_TEXTURE_MAG_FILTER");
158 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
159 HighQualityTextRenderer.checkGLError(
"glTexParameterf:GL_TEXTURE_MIN_FILTER");
160 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
161 HighQualityTextRenderer.checkGLError(
"glTexParameterf:GL_TEXTURE_WRAP_S");
162 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
163 HighQualityTextRenderer.checkGLError(
"glTexParameterf:GL_TEXTURE_WRAP_T");
192 GLES20.glDisable(GLES20.GL_CULL_FACE);
193 GLES20.glEnable(GLES20.GL_BLEND);
194 GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
195 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
198 void updateCamera(
int aWidth,
203 float ratio = (
float)aWidth / (
float)aHeight;
206 Matrix.frustumM(
mProjMatrix, 0, -ratio, ratio, -1, 1, 0.1f, 100.0f);
207 Matrix.setLookAtM(
mVMatrix, 0, 0, 0, 1, 0
f, 0
f, 0
f, 0
f, 1.0f, 0.0f);
217 if (aFontSize < 8.0
f)
220 if (aFontSize > 500.0
f)
223 Paint textPaint =
new Paint();
224 textPaint.setTextSize(aFontSize);
225 textPaint.setFakeBoldText(
false);
226 textPaint.setAntiAlias(
true);
227 textPaint.setARGB(255, 255, 255, 255);
230 textPaint.setSubpixelText(
true);
231 textPaint.setXfermode(
new PorterDuffXfermode(
Mode.SCREEN));
233 float realTextWidth = textPaint.measureText(aText);
240 textBitmap.eraseColor(Color.argb(0, 255, 255, 255));
242 Canvas bitmapCanvas =
new Canvas(textBitmap);
245 bitmapCanvas.drawText(aText, 1, 1.0f + aFontSize * 0.75f, textPaint);
247 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,
textureId[0]);
248 HighQualityTextRenderer.checkGLError(
"glBindTexture");
250 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, textBitmap, 0);
252 textBitmap.recycle();
255 GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
256 HighQualityTextRenderer.checkGLError(
"glGenerateMipmap");
262 float quadVerts[] = {
270 float quadCoords[] = {
279 ByteBuffer vbb = ByteBuffer.allocateDirect(
281 quadVerts.length * 4);
282 vbb.order(ByteOrder.nativeOrder());
283 quadVB = vbb.asFloatBuffer();
284 quadVB.put(quadVerts);
287 vbb = ByteBuffer.allocateDirect(
289 quadCoords.length * 4);
290 vbb.order(ByteOrder.nativeOrder());
291 quadCB = vbb.asFloatBuffer();
292 quadCB.put(quadCoords);
300 int shader = GLES20.glCreateShader(
type);
303 GLES20.glShaderSource(shader, shaderCode);
304 GLES20.glCompileShader(shader);
327 "uniform mat4 uMVPMatrix; \n" +
329 "attribute vec4 vPosition; \n" +
330 "attribute vec4 vTexCoord; \n" +
332 "varying vec4 v_v4TexCoord; \n" +
337 " v_v4TexCoord = vTexCoord; \n" +
338 " gl_Position = uMVPMatrix * vPosition; \n" +
343 "precision mediump float; \n" +
344 "uniform sampler2D u_s2dTexture; \n" +
345 "varying vec4 v_v4TexCoord; \n" +
347 " gl_FragColor = texture2D(u_s2dTexture, v_v4TexCoord.xy); \n" +
void setText(String aText)
void setPosition(float aX, float aY, float aZ)
final String vertexShaderCode
final String fragmentShaderCode
int loadShader(int type, String shaderCode)
GLint GLint GLint GLint GLint x
void drawCanvasToTexture(String aText, float aFontSize)
void setRelPos(float aX, float aY, float aZ)