OpenGL ES2.0不能直接绘制文本。 我采用的是将文本转换为位图,然后渲染为插图的方法。 虽然网上也有人把文字做成圆点,一个个画出来,但我个人觉得这个方法还是很麻烦的。 文章分为两个部分。 一个在canvas中绘制并过渡到Bitmap,另一个在贴图纹理中绘制。
将文本转换为位图
这个部分很麻烦。 网上介绍的canvas.drawText ()资料太多,看了也一窍不通。 我也看了一天才明白。
根据文本的样式和大小动态获取要使用的宽度和高度
Paint p=new Paint (;
p.setcolor(GLtext.color );
p.settypeface(GLtext.font );
p.settextsize(GLtext.size );
//取高度
paint.fontmetricsintmetrics=p.getfontmetricsint (;
int height=metrics.bottom-metrics.top;
//取宽度
Rect rect=new Rect (;
p.gettextbounds(GLtext.text,0,glText.text.length ),rect );
int width=rect.width (; //文本宽度
生成大小与字符大小相应的位图,统一位图大小会导致字符扭曲或扭曲。
//根据字符的高度和宽度生成位图
bitmap bitmap=bitmap.create bitmap (width,height,Bitmap.Config.ARGB_8888 );
canvas canvas=new canvas (位图);
根据文字尺寸生成对应OpenGL描绘矩形坐标范围尺寸
float mHeight=RADIO*this.height;
float mWidth=RADIO*this.width;
returnnewglrectangle (GL点,
newGLpoint(GLpoint.getx )、glPoint.getY )、mHeight )、
newGLpoint(GLpoint.getx(mwidth,glPoint.getY ) (mHeight ) ) ) ) ) ) ) )。
);
要画背景色和文字,drawText ()的x,y设置很麻烦,如果设置不正确,文字显示容易不完整,这里我采用的是
if(GLText.backgroundcolor!=0}{
//有背景色就画背景色
canvas.draw color (GL text.background color );
}
canvas.drawtext(GLtext.text,0,-metrics.ascent,p );
canvas.save (;
OpenGL渲染绘制纹理图像
顶点着色器
final static String VERTEX=
'统一mat 4u _ matrix;'
' attribute vec4 a_Position;'
' attribute ve c2a _ texture coordinates;'
' varying ve c2v _ texture coordinates;'
' void main () )。
' {}
' GL _ position=u _ matrix * a _ position;'
' v _ texture coordinates=a _ texture coordinates;'
() );
片上着色器
final static String FRAGMENT=
' precision mediump float;'
'统一样本2d u _ texture unit;'
' varying ve c2v _ texture coordinates;'
' void main () )。
' {}
' GL _ frag color=texture 2d (u _ texture unit,v_TextureCoordinates ); ttn '
() );
点位数据的处理
vertexData=ByteBuffer
. allocatedirect (data.length * bytes _ per _ float )。
. order(byteorder.nativeorder () ).asFloatBuffer );
vertex data.put (数据;
pointCount=4;
stride=(position _ component _ count texture _ coordinates _ component _ count ) * BYTES_PER_FLOAT;
//打开颜色混合以解决图像背景中的黑色问题
总帐启用(GL _ blend;
//glblendfunc(GL_src_alpha,GL_ONE_MINUS_SRC_ALPHA );
lpixelstorei (GL _ unpack _ alignment,1;
glblendfunc(GL_one,GL_ONE_MINUS_SRC_ALPHA );
vertex shader=shader helper.compilevertexshader (shader code.vertex );
fragmentShader=ShaderHelper
.compilefragmentshader (shader code.fragment;
program=shader helper.link program (vertex shader,fragmentShader );
guseprogram (程序);
位置位置=glgetattriblocation (程序,A_POSITION );
umatrix location=glgetuniformlocation (程序,U_MATRIX );
utextureunitlocation=glgetuniformlocation (程序,U_TEXTURE_UNIT );
atexturecoordinateslocation=glgetattriblocation (程序,A_TEXTURE_COORDINATES );
存储图像(位图;
通用统一1 I (纹理单元定位,0 );
vertexdata.position(0;
lvertexattribpointer (apositionlocation,POSITION_COMPONENT_COUNT,GL_FLOAT,
false、STRIDE、vertex数据;
全球发展战略联盟(apositionlocation;
vertex data.position (position _ component _ count );
lvertexattribpointer (atexturecoordinateslocation,texture _ coordinates _ component _ count,GL_FLOAT,
false、STRIDE、vertex数据;
通用向量存储器(atexturecoordinateslocation;
//材质纹理处理
protectedstaticvoidstoreimage{
int[] texture=new int[1];
glgentextures(1,texture,0 );
glactivetexture (gles 20.GL _ texture 0;
glbindtexture(GL_texture_2d,texture[0];
ltexparameteri(GL_texture_2d,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR );
ltexparameteri(GL_texture_2d,GL_TEXTURE_MAG_FILTER,GL_LINEAR );
gltexparameteri(GL_texture_2d,GL_TEXTURE_WRAP_S,GL_REPEAT );
gltexparameteri(GL_texture_2d,GL_TEXTURE_WRAP_T,GL_REPEAT );
teximage 2d (gles 20.GL _ texture _ 2d,0,位图,0 );
glgeneratemipmap(GL_texture_2d;
bitmap.recycle (;
}
//设定投影变换
通用矩阵4 Fv (矩阵位置,1,false,矩阵,0 );
gldrawarrays(GL_triangle_fan,0,4 );
最后的效果图如下所示