简体   繁体   English

Android Opengles 2.0绘制字符串2D

[英]android opengles 2.0 draw string 2d

The Activity load the layout, which has a GLSurfaceView : Activity加载具有GLSurfaceView的布局:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mixed);

    Bundle bundle = getIntent().getExtras();
    if (bundle.containsKey(EXTRA_KEY_EMAIL)) {
        email = bundle.getString(EXTRA_KEY_EMAIL);
        TextView myTextView = (TextView) findViewById(R.id.myTextView);
        myTextView.setText(myTextView.getText() + " " + email);
    }
    myGLRenderer.setEmail(email);

    myGLSurfaceView = (GLSurfaceView) findViewById(R.id.myGLSurfaceView);

    // Create an OpenGL ES 2.0 context.
    myGLSurfaceView.setEGLContextClientVersion(2);

    // Set the Renderer for drawing on the GLSurfaceView        
    myGLSurfaceView.setRenderer(myGLRenderer);      

    // Render the view only when there is a change in the drawing data
    myGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);         
}

From here I took the example and fixed ( added the texture declaration and initialisation line ) 在这里,我以示例为例并进行了修复(添加了纹理声明和初始化行)

public class MyGLRenderer implements GLSurfaceView.Renderer {

    private static final String TAG = "MyGLRenderer";

    private String email;
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        Log.e(TAG, "onSurfaceCreated: " + config);
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);//gray this is done
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        Log.e(TAG, "onSurfaceChanged: width" + width + ",  height:" + height);
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        doPaintEmail1(gl);
    }

    private void doPaintEmail1(GL10 gl) {

        // TODO do draw all stuff here: paint the email
        Log.v(TAG, "doPaintEmail1: "+email);

        // Create an empty, mutable bitmap
        Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
        // get a canvas to paint over the bitmap
        Canvas canvas = new Canvas(bitmap);
        bitmap.eraseColor(0);

        // get a background image from resources
        // note the image format must match the bitmap format
        //Drawable background = context.getResources().getDrawable(R.drawable.background);
        //background.setBounds(0, 0, 256, 256);
        //background.draw(canvas); // draw the background to our bitmap

        // Draw the text
        Paint textPaint = new Paint();
        textPaint.setTextSize(12);
        textPaint.setAntiAlias(true);
        textPaint.setARGB(0xff, 0xFF, 0x00, 0x00);
        // draw the text centered
        canvas.drawText(email, 16,112, textPaint);

        int[] textures = new int[1];
        //Generate one texture pointer...
        gl.glGenTextures(1, textures, 0);
        //...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        //Create Nearest Filtered Texture
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

        //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

        //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        //Clean up
        bitmap.recycle();

    }

    public void setEmail(String email) {
        this.email = email;
    }

I am expecting to see my example email, but can't see it. 我希望看到我的示例电子邮件,但看不到。

Note I don't use vertexShaderCode , fragmentShaderCode , nor GLES20.glCreateProgram() . 注意我不使用vertexShaderCodefragmentShaderCodeGLES20.glCreateProgram() Maybe that is missinng, but can't understand why, where it needed. 也许是missinng,但是不知道为什么,在需要的地方。

How to fix this? 如何解决这个问题?

At Logcat I see the doPaintEmail1: foo@example message. 在Logcat上,我看到doPaintEmail1: foo@example消息。

在此处输入图片说明

Creating the vertex and fragment shader programs is required for any OpenGL ES 2.0 app to function. 要使任何OpenGL ES 2.0应用程序正常运行,都需要创建顶点和片段着色器程序。 They are not optional. 它们不是可选的。 You should take a look at the 2.0 examples in the Android SDK. 您应该看一下Android SDK中的2.0示例。 If you don't want to use shaders, then use OpenGL ES 1.1, instead. 如果您不想使用着色器,请改用OpenGL ES 1.1。

The third article here will guide you to the best examples in the SDK. 这里的第三篇文章将引导您找到SDK中的最佳示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM