0

我从open GL开始并且有一个小问题,我正在尝试用纯色(和随机)颜色绘制屏幕,​​但它显示出高刷新的颜色。我在 onDrawFrame 方法中插入了一个睡眠,以查看发生了什么,结果是:黑屏 - 彩色屏幕 - 黑屏 - 彩色屏幕......每秒刷新一次。我究竟做错了什么?那是我的代码:

package com.example.opengltest;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity implements Renderer {

    GLSurfaceView glView;
    GL10 gl;
    Object stateChanged = new Object();
    Random rand = new Random();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glView = new GLSurfaceView(this);
        glView.setRenderer(this);
        setContentView(glView);
    }

    private void paint() {
        Log.i("OPENGL","paint");
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glClearColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat(),
                1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(0, 320, 0, 480, 1, -1);

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * 2 * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        FloatBuffer vertices = byteBuffer.asFloatBuffer();
        vertices.put(new float[] { 0.0f, 0.0f,
        319.0f, 0.0f,
        160.0f, 479.0f });
        vertices.flip();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        glView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        glView.onResume();
    }

    public void onDrawFrame(GL10 gl) {
        Log.i("OPENGL","onDrawFrame");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        Log.i("OPENGL","onSurfaceChanged");

    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        this.gl = gl;
        Log.i("OPENGL","onSurfaceCreated");paint();

    }

}
4

1 回答 1

2

You are calling your paint() method in onSurfaceChanged whereas you should call some of its contents in onDrawFrame(), and never use the sleep function inside a GL thread, it will only cause black screens flickering all over.

In particular, try moving

gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // Clear the color buffer
gl.glClearColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat(), 1); // Clear it with a random color

to the onDrawFrame function, and I don't know exactly why you're calling glClear() twice in paint(), but that's a mistake, you're clearing what you just painted with a random color! So delete that second call to onClear() and I think you should be fine. Actually, I don't even think you need the first glClear() call, because you're already clearing it with glClearColor, although if you do introduce some alpha transparency then you're better off having both (or else they will sum up).

Let us know if that helped!

于 2012-08-01T10:34:36.543 回答