? ? ? ? 經過一段時間的學習港谊,我發(fā)現OpenGL是不支持字符繪制的贾漏,就是官方的API沒有字符繪制的方法惑申。但是如果有字符繪制的需求,還是有別的辦法來實現翅雏,那就是把字符轉換成圖片圈驼,然后通過渲染紋理的方式把圖片繪制出來。老規(guī)矩望几,先上效果圖:
頂點shader:
uniform mat4 vMatrix;
attribute vec4 vPosition;
attribute vec2 vTextureCoordinate;
varying vec2 aTextureCoordinate;
void main() {????
????aTextureCoordinate = vTextureCoordinate;???
? ? gl_Position = vMatrix * vPosition;
}
片段shader:
precision mediump float;
uniform sampler2D u_TextureUnit;
varying vec2 aTextureCoordinate;
void main() {
????gl_FragColor = texture2D(u_TextureUnit, aTextureCoordinate);
}
模型繪制類:
public class TextureModel {
????private int programId;
????private float[] matrix = new float[16];
????private int glHMatrix;
????private int glHPosition;
????private int glHCoordinates;
????private int glHTexture;
????private FloatBuffer bufPos;
????private FloatBuffer bufCoord;
????private Bitmap bitmap;
????private int[] textureId = new int[1];
????private final float[] sCoord = { 0.0f, 0.0f, 0f, 0.0f, 1.0f, 0f, 1.0f, 0.0f, 0f, 1.0f, 1.0f, 0f };
????public TextureModel(){
????????Matrix.setIdentityM(matrix, 0);
????????bufCoord = GlUtil.createFloatBuffer(sCoord);
????}
????public float[] getMatrix() { return matrix; }
????public void createProgram(){
????????programId = GlUtil.createProgram(ResReadUtils.readResource(R.raw.texture_vertex_shader), ResReadUtils.readResource(R.raw.texture_fragment_shader));
????????glHMatrix = GLES20.glGetUniformLocation(programId, "vMatrix");
????????glHPosition = GLES20.glGetAttribLocation(programId, "vPosition");
????????glHCoordinates = GLES20.glGetAttribLocation(programId, "vTextureCoordinate");
????????glHTexture = GLES20.glGetUniformLocation(programId, "u_TextureUnit");
????}
????public void setBitmap(Bitmap bitmap) { this.bitmap = bitmap; }
????public void createTexture(){
????????if (bitmap!=null && !bitmap.isRecycled()) {
????????????GLES20.glGenTextures(1, textureId, 0);
????????????GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
????????????GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
????????????GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
????????????GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);? ?
?????????????GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
????????????GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
????????????GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
????????????bitmap.recycle();
????????????GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
????????}
????}
????public void setDrawPosition(float[] position){
????????if (position==null || position.length!=12) { return; }
????????bufPos = GlUtil.createFloatBuffer(position);
????}
????public void deleteTexture(){
????????GLES20.glDeleteTextures(programId, textureId, 0);
????}
????public void drawSelf(){
????????GLES20.glUseProgram(programId);
????????GLES20.glUniformMatrix4fv(glHMatrix, 1, false, matrix, 0);
????????GLES20.glEnableVertexAttribArray(glHPosition);
????????GLES20.glEnableVertexAttribArray(glHCoordinates);
????????GLES20.glUniform1i(glHTexture, 0);
????????createTexture();
????????GLES20.glVertexAttribPointer(glHPosition, 3, GLES20.GL_FLOAT, false, 0, bufPos);
????????GLES20.glVertexAttribPointer(glHCoordinates, 3, GLES20.GL_FLOAT, false, 0, bufCoord);
????????GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
????????GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
????????GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
????????GLES20.glDisableVertexAttribArray(glHPosition);
????????GLES20.glDisableVertexAttribArray(glHCoordinates);
????}
現在需要傳入字符的圖片和坐標位置:
Paint paint =new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(100f);
String text ="測試字符串绩脆!";
int textWidth = (int) paint.measureText(text, 0, text.length());
Paint.FontMetrics fm = paint.getFontMetrics();
float textHeight =? fm.bottom - fm.top + fm.leading;;
Bitmap bitmap = Bitmap.createBitmap(textWidth, (int)textHeight, Bitmap.Config.ARGB_8888);
Canvas canvas =new Canvas(bitmap);
canvas.drawText(text, 0, textHeight-fm.bottom, paint);
glSurfaceView.setTexture(bitmap);
RenderFilter調用方法:
public void setTextureBitmap(Bitmap bitmap){
????float[] pos = {
????????????0f, 0f, 10f,
? ? ? ? ? ? 0f, 0f, 0f,
? ? ? ? ? ? 0f, 10f, 10f,
? ? ? ? ? ? 0f, 10f, 0f
? ? };
? ? textureModel.setDrawPosition(pos);
? ? textureModel.setBitmap(bitmap);
}
運行起來看看效果:
咦?怎么是白底的呢橄抹,難道生成的位圖就是白底的靴迫?打個斷點看看
從斷點來看,生成的bitmap是透明的奥ナ摹玉锌??疟羹?那肯定是OpenGLES繪制的問題主守,為什么透明的繪制不出來,變成了白色的呢榄融?
于是参淫,百度一下,還真找到問題了:Android 使用opengles 設置背景透明
在onSurfaceCreated方法增加兩行代碼就搞定愧杯!
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
????GLES20.glEnable(GLES20.GL_DEPTH_TEST);
? ? //就是下面這兩行
? ? GLES20.glEnable(GLES20.GL_BLEND);
? ? GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
? ? renderFilter.createProgram();
}
再運行起來看看效果涎才,OK,收工A拧c疚!