2 package com.introlab.rtabmap;
4 import java.nio.ByteBuffer;
5 import java.nio.ByteOrder;
6 import java.nio.FloatBuffer;
7 import java.nio.ShortBuffer;
8 import java.util.Iterator;
9 import java.util.Vector;
11 import android.content.Context;
12 import android.graphics.Bitmap;
13 import android.graphics.Canvas;
14 import android.graphics.Color;
15 import android.graphics.Rect;
16 import android.graphics.Typeface;
17 import android.opengl.GLES20;
18 import android.opengl.GLUtils;
19 import android.text.TextPaint;
20 import android.util.Log;
31 "uniform mat4 uMVPMatrix;" +
32 "attribute vec4 vPosition;" +
33 "attribute vec2 a_texCoord;" +
34 "varying vec2 v_texCoord;" +
36 " gl_Position = uMVPMatrix * vPosition;" +
37 " v_texCoord = a_texCoord;" +
40 "precision mediump float;" +
41 "uniform float uColor;" +
42 "varying vec2 v_texCoord;" +
43 "uniform sampler2D s_texture;" +
45 " gl_FragColor = texture2D( s_texture, v_texCoord );" +
46 " gl_FragColor.rgb *= uColor;" +
80 float[] mCharacterWidth;
85 vecs =
new float[3 * 10];
86 uvs =
new float[2 * 10];
87 indices =
new short[10];
102 mTextures =
new int[1];
103 GLES20.glGenTextures(1, mTextures, 0);
106 Bitmap bitmap = Bitmap.createBitmap(RI_TEXT_TEXTURE_SIZE, RI_TEXT_TEXTURE_SIZE, Bitmap.Config.ARGB_4444);
108 Canvas canvas =
new Canvas(bitmap);
109 bitmap.eraseColor(0);
112 TextPaint textPaint =
new TextPaint();
113 textPaint.setTextSize(RI_TEXT_HEIGHT_BASE);
114 textPaint.setColor(Color.WHITE);
115 textPaint.setAntiAlias(
true);
116 textPaint.setTypeface(Typeface.create(
"Arial", Typeface.BOLD));
122 Rect textBounds =
new Rect();
123 textPaint.getTextBounds(String.valueOf(c), 0, 1, textBounds);
124 if(textBounds.height() + textBounds.bottom >
mTextHeight)
126 mTextHeight = textBounds.height() + textBounds.bottom;
130 mUVHeight = mTextHeight/(float)RI_TEXT_TEXTURE_SIZE;
133 int colCount = RI_TEXT_TEXTURE_SIZE/(int)RI_TEXT_HEIGHT_BASE;
138 Rect textBounds =
new Rect();
139 textPaint.getTextBounds(String.valueOf(c), 0, 1, textBounds);
140 canvas.drawText(String.valueOf(c), (i%colCount)*RI_TEXT_HEIGHT_BASE, (i/colCount) * mTextHeight +
RI_TEXT_HEIGHT_BASE, textPaint);
141 mCharacterWidth[i] = textPaint.measureText(String.valueOf(c));
145 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);
146 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
147 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
148 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
163 int shader = GLES20.glCreateShader(type);
166 GLES20.glShaderSource(shader, shaderCode);
167 GLES20.glCompileShader(shader);
180 short base = (short) (index_vecs / 3);
183 for(
int i=0;i<vec.length;i++)
190 for(
int i=0;i<uv.length;i++)
197 for(
int j=0;j<indi.length;j++)
216 if(!(txt.text==null))
218 charcount += txt.text.length();
228 vecs =
new float[charcount * 12];
229 uvs =
new float[charcount * 8];
230 indices =
new short[charcount * 6];
240 for( Iterator< TextObject > it = txtcollection.iterator(); it.hasNext() ; )
245 if(!(txt.
text==null))
257 GLES20.glDisable(GLES20.GL_DEPTH_TEST);
258 GLES20.glEnable(GLES20.GL_BLEND);
261 GLES20.glUseProgram(sp_Text);
264 ByteBuffer bb = ByteBuffer.allocateDirect(vecs.length * 4);
265 bb.order(ByteOrder.nativeOrder());
266 vertexBuffer = bb.asFloatBuffer();
267 vertexBuffer.put(vecs);
268 vertexBuffer.position(0);
271 ByteBuffer bb2 = ByteBuffer.allocateDirect(uvs.length * 4);
272 bb2.order(ByteOrder.nativeOrder());
273 textureBuffer = bb2.asFloatBuffer();
274 textureBuffer.put(uvs);
275 textureBuffer.position(0);
278 ByteBuffer dlb = ByteBuffer.allocateDirect(indices.length * 2);
279 dlb.order(ByteOrder.nativeOrder());
280 drawListBuffer = dlb.asShortBuffer();
281 drawListBuffer.put(indices);
282 drawListBuffer.position(0);
285 int mPositionHandle = GLES20.glGetAttribLocation(sp_Text,
"vPosition");
288 GLES20.glEnableVertexAttribArray(mPositionHandle);
291 GLES20.glVertexAttribPointer(mPositionHandle, 3,
292 GLES20.GL_FLOAT,
false,
295 int mTexCoordLoc = GLES20.glGetAttribLocation(sp_Text,
"a_texCoord" );
298 GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
302 GLES20.glEnableVertexAttribArray ( mPositionHandle );
303 GLES20.glEnableVertexAttribArray ( mTexCoordLoc );
306 int mtrxhandle = GLES20.glGetUniformLocation(sp_Text,
"uMVPMatrix");
309 GLES20.glUniformMatrix4fv(mtrxhandle, 1,
false, m, 0);
312 int colorhandle = GLES20.glGetUniformLocation(sp_Text,
"uColor");
313 GLES20.glUniform1f(colorhandle, mColor);
315 int mSamplerLoc = GLES20.glGetUniformLocation (sp_Text,
"s_texture" );
318 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
320 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);
322 GLES20.glUniform1i ( mSamplerLoc, texturenr);
325 GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
328 GLES20.glDisableVertexAttribArray(mPositionHandle);
329 GLES20.glDisableVertexAttribArray(mTexCoordLoc);
338 String text = val.
text;
340 Log.i(
"RTABMapActivity", String.format(
"convertTextToTriangleInfo() set status=%s", text));
343 for(
int j=0; j<text.length(); j++)
346 char c = text.charAt(j);
351 if(indx<0 || indx>=mCharacterWidth.length) {
356 int colCount = RI_TEXT_TEXTURE_SIZE/(int)RI_TEXT_HEIGHT_BASE;
359 int row = indx / colCount;
360 int col = indx % colCount;
365 float u2 = u + mCharacterWidth[indx]/(float)RI_TEXT_TEXTURE_SIZE;
368 float[] vec =
new float[12];
369 float[] uv =
new float[8];
370 float[] colors =
new float[16];
401 short[] inds = {0, 1, 2, 0, 2, 3};
static int loadShader(int type, String shaderCode)
static final char RI_TEXT_START
static final char RI_TEXT_STOP
TextManager(Context context)
GLM_FUNC_DECL genType::row_type row(genType const &m, length_t const &index)
FloatBuffer textureBuffer
void setColor(float color)
void PrepareDrawInfo(Vector< TextObject > txtcollection)
ShortBuffer drawListBuffer
static final String vs_Text
void setUniformscale(float uniformscale)
void setTextureID(int val)
static final int RI_TEXT_TEXTURE_SIZE
static final float RI_TEXT_HEIGHT_BASE
void AddCharRenderInformation(float[] vec, float[] cs, float[] uv, short[] indi)
void convertTextToTriangleInfo(TextObject val)
void PrepareDraw(Vector< TextObject > txtcollection)
static final String fs_Text