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;
30 "uniform mat4 uMVPMatrix;" +
31 "attribute vec4 vPosition;" +
32 "attribute vec2 a_texCoord;" +
33 "varying vec2 v_texCoord;" +
35 " gl_Position = uMVPMatrix * vPosition;" +
36 " v_texCoord = a_texCoord;" +
39 "precision mediump float;" +
40 "uniform float uColor;" +
41 "varying vec2 v_texCoord;" +
42 "uniform sampler2D s_texture;" +
44 " gl_FragColor = texture2D( s_texture, v_texCoord );" +
45 " gl_FragColor.rgb *= uColor;" +
79 float[] mCharacterWidth;
84 vecs =
new float[3 * 10];
85 uvs =
new float[2 * 10];
86 indices =
new short[10];
101 mTextures =
new int[1];
102 GLES20.glGenTextures(1, mTextures, 0);
105 Bitmap bitmap = Bitmap.createBitmap(RI_TEXT_TEXTURE_SIZE, RI_TEXT_TEXTURE_SIZE, Bitmap.Config.ARGB_4444);
107 Canvas canvas =
new Canvas(bitmap);
108 bitmap.eraseColor(0);
111 TextPaint textPaint =
new TextPaint();
112 textPaint.setTextSize(RI_TEXT_HEIGHT_BASE);
113 textPaint.setColor(Color.WHITE);
114 textPaint.setAntiAlias(
true);
115 textPaint.setTypeface(Typeface.create(
"Arial", Typeface.BOLD));
121 Rect textBounds =
new Rect();
122 textPaint.getTextBounds(String.valueOf(c), 0, 1, textBounds);
123 if(textBounds.height() + textBounds.bottom >
mTextHeight)
125 mTextHeight = textBounds.height() + textBounds.bottom;
129 mUVHeight = mTextHeight/(float)RI_TEXT_TEXTURE_SIZE;
132 int colCount = RI_TEXT_TEXTURE_SIZE/(int)RI_TEXT_HEIGHT_BASE;
137 Rect textBounds =
new Rect();
138 textPaint.getTextBounds(String.valueOf(c), 0, 1, textBounds);
139 canvas.drawText(String.valueOf(c), (i%colCount)*RI_TEXT_HEIGHT_BASE, (i/colCount) * mTextHeight +
RI_TEXT_HEIGHT_BASE, textPaint);
140 mCharacterWidth[i] = textPaint.measureText(String.valueOf(c));
144 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);
145 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
146 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
147 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
162 int shader = GLES20.glCreateShader(type);
165 GLES20.glShaderSource(shader, shaderCode);
166 GLES20.glCompileShader(shader);
179 short base = (short) (index_vecs / 3);
182 for(
int i=0;i<vec.length;i++)
189 for(
int i=0;i<uv.length;i++)
196 for(
int j=0;j<indi.length;j++)
215 if(!(txt.text==null))
217 charcount += txt.text.length();
227 vecs =
new float[charcount * 12];
228 uvs =
new float[charcount * 8];
229 indices =
new short[charcount * 6];
239 for( Iterator< TextObject > it = txtcollection.iterator(); it.hasNext() ; )
244 if(!(txt.
text==null))
256 GLES20.glDisable(GLES20.GL_DEPTH_TEST);
257 GLES20.glEnable(GLES20.GL_BLEND);
260 GLES20.glUseProgram(sp_Text);
263 ByteBuffer bb = ByteBuffer.allocateDirect(vecs.length * 4);
264 bb.order(ByteOrder.nativeOrder());
265 vertexBuffer = bb.asFloatBuffer();
266 vertexBuffer.put(vecs);
267 vertexBuffer.position(0);
270 ByteBuffer bb2 = ByteBuffer.allocateDirect(uvs.length * 4);
271 bb2.order(ByteOrder.nativeOrder());
272 textureBuffer = bb2.asFloatBuffer();
273 textureBuffer.put(uvs);
274 textureBuffer.position(0);
277 ByteBuffer dlb = ByteBuffer.allocateDirect(indices.length * 2);
278 dlb.order(ByteOrder.nativeOrder());
279 drawListBuffer = dlb.asShortBuffer();
280 drawListBuffer.put(indices);
281 drawListBuffer.position(0);
284 int mPositionHandle = GLES20.glGetAttribLocation(sp_Text,
"vPosition");
287 GLES20.glEnableVertexAttribArray(mPositionHandle);
290 GLES20.glVertexAttribPointer(mPositionHandle, 3,
291 GLES20.GL_FLOAT,
false,
294 int mTexCoordLoc = GLES20.glGetAttribLocation(sp_Text,
"a_texCoord" );
297 GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
301 GLES20.glEnableVertexAttribArray ( mPositionHandle );
302 GLES20.glEnableVertexAttribArray ( mTexCoordLoc );
305 int mtrxhandle = GLES20.glGetUniformLocation(sp_Text,
"uMVPMatrix");
308 GLES20.glUniformMatrix4fv(mtrxhandle, 1,
false, m, 0);
311 int colorhandle = GLES20.glGetUniformLocation(sp_Text,
"uColor");
312 GLES20.glUniform1f(colorhandle, mColor);
314 int mSamplerLoc = GLES20.glGetUniformLocation (sp_Text,
"s_texture" );
317 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
319 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);
321 GLES20.glUniform1i ( mSamplerLoc, texturenr);
324 GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
327 GLES20.glDisableVertexAttribArray(mPositionHandle);
328 GLES20.glDisableVertexAttribArray(mTexCoordLoc);
337 String text = val.
text;
340 for(
int j=0; j<text.length(); j++)
343 char c = text.charAt(j);
348 if(indx<0 || indx>=mCharacterWidth.length) {
353 int colCount = RI_TEXT_TEXTURE_SIZE/(int)RI_TEXT_HEIGHT_BASE;
356 int row = indx / colCount;
357 int col = indx % colCount;
362 float u2 = u + mCharacterWidth[indx]/(float)RI_TEXT_TEXTURE_SIZE;
365 float[] vec =
new float[12];
366 float[] uv =
new float[8];
367 float[] colors =
new float[16];
398 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