在android上使用OpenGL绘图文本

时间:2017-01-19 01:44:53

标签: android opengl-es

我想绘制正方形并在GLSurfaceView相机预览上绘制文字。

首先我尝试绘制方形

private void drawSquare() {
    GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
    GLES20.glScissor(20, 300, 500, 50);
    GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
}

此方法调用onDrawFrame(); 并在相机预览glsurfaceview上显示正方形。

我尝试绘制文字

public void GLText(GL10 gl) {

    Bitmap bitmap = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(bitmap);
    bitmap.eraseColor(0);

    Paint paint = new Paint();
    paint.setTextSize(18);
    paint.setAntiAlias(true);
    paint.setARGB(0xff, 0xff, 0xff, 0xff);
    paint.setTextAlign(Paint.Align.LEFT);
    paint.setTextScaleX(0.5f);
    canvas.drawText("testGLText", 0.f, 15.f, paint);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    bitmap.recycle();
}

并显示文字。但背景颜色为绿色

enter image description here

为什么背景颜色为绿色? 请帮帮我。

感谢。

1 个答案:

答案 0 :(得分:1)

我没有看到你的代码使用openGL api绘制文本,但没有理由将你的fbo设置为绿色。

    GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
    GLES20.glScissor(20, 300, 500, 50);
    GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // here you initialize your fbo color as black
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glDisable(GLES20.GL_SCISSOR_TEST);

如果您想绘制正方形,请按照以下步骤操作。

glclearcolor(...); //Actually, glclearColor() and glclear() are not required.
glclear();
glviewport(....);
gluseprogram() // a shader program you compiled 
bindVBO() // VBO you already set in this case, it saves four vertices
glactivetexture() and bindtexture()
gluniform1f(..) // for the texture
gldrawarray(...) or gldrawelement(...)