2017-01-19 64 views
0

我想在GLSurfaceView相机预览中绘制正方形并绘制文本。使用OpenGL在android上绘制文本

首先我试着画正方形

上onDrawFrame
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); 
} 

这个方法调用(); 并在相机预览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

我没有看到你的代码使用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(...) 
+0

你的来源是平方。广场不是绿色的背景。只有文字画。 – chohyunwook

+0

@chohyunwook你不知道如何画一个正方形。上面的代码从不画方形。 gldraw ****()只能绘制一个正方形,这就是为什么我提到“我没有看到实际绘图代码” – Sung

+0

@chohyunwook答案已更新。现在看来你从不使用OpenGL。您只需使用画布绘制文本和相机预览即可绘制每个相机框。两者都可以用OpenGL完成。但是,如果不需要,则不必使用OpenGL。 – Sung