0
我開始與開放GL,有一個小問題,我試圖繪製一個固體(和隨機)顏色的屏幕,但它顯示了一個高顏色刷新。我在onDrawFrame方法中插入了一個睡眠,以查看發生了什麼,結果是:黑屏 - 彩色屏幕 - 黑屏 - 彩色屏幕...每秒刷新一次。我究竟做錯了什麼?這就是我的代碼:框架之間的Android OpenGL黑色屏幕
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();
}
}
你爲什麼介紹睡眠線程? – VinceFR 2012-08-01 10:11:59
這將是看看問題出在他的'Renderer.onDrawFrame()'或其他地方。它似乎在別的地方。 – tolgap 2012-08-01 10:16:00
好吧,所以我不明白(也許我不是唯一的一個)你的意思是'它顯示了高的刷新顏色' – VinceFR 2012-08-01 10:17:41