我从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();
}
}