Android onDrawFrame、requestRender和渲染线程?|OpenGLES2.0
我在OpenGL ES 2.0中使用游戏循环的外部线程运行我的应用程序时遇到问题 这是我的初级课程中的onCreate:Android onDrawFrame、requestRender和渲染线程?|OpenGLES2.0,android,multithreading,opengl-es,opengl-es-2.0,game-loop,Android,Multithreading,Opengl Es,Opengl Es 2.0,Game Loop,我在OpenGL ES 2.0中使用游戏循环的外部线程运行我的应用程序时遇到问题 这是我的初级课程中的onCreate: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLView = new MyGLView(this); System.out.println(running); setContentV
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new MyGLView(this);
System.out.println(running);
setContentView(mGLView);
loop = new GameLoop();
//loop.run(); <- this is supposed to run the loop but when its called,
// nothing really appears on screen. just title of the app.
}
所以基本上,如果我不调用loop.run()代码>我得到一个静态图片,基本上是onDrawFrame的一个周期,完成后,logCat上不会弹出任何其他内容
接下来发生的事情是,如果我调用loop.run()
,基本上循环会一直进行下去,但屏幕上没有显示任何内容。我只看到一个标题屏幕,而不是glView
我做错了什么?还有其他方法更新屏幕吗?线程必须用start()而不是run()启动。哇,我真不敢相信这么简单!谢谢如果start确实启动了线程,那么run做什么呢?
private final static int maxFPS = 30;
private final static int maxFrameSkips = 5;
private final static int framePeriod = 1000 / maxFPS;
public static boolean running;
public final static String TAG = "test";
public GameLoop() {
}
@Override
public void run() {
running = StartPoint.isRunning();
long beginTime;
long timeDiff;
int sleepTime;
int framesSkipped;
sleepTime = 0;
while (running) {
running = StartPoint.isRunning();
beginTime = System.currentTimeMillis();
framesSkipped = 0;
StartPoint.mGLView.requestRender(); // <- supposed to re-render?
timeDiff = System.currentTimeMillis() - beginTime;
sleepTime = (int) (framePeriod - timeDiff);
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
}
}
while (sleepTime < 0 && framesSkipped < maxFrameSkips) {
sleepTime += framePeriod;
framesSkipped++;
Log.d(TAG, "Frames Skipped");
}
}
}
private static final String TAG = "Renderer";
BoundsSquare square;
private final float[] mMVPMatrix = new float[16];
private final float[] mProjMatrix = new float[16];
private final float[] mVMatrix = new float[16];
private final float[] mRotationMatrix = new float[16];
private int camWidth,camHeight;
Camera2D cam;
Vector3 vec;
public long cycle = 0; // used this to determine how many cycles
//went through, it is stuck on cycle 0, nothing else happens after first render
@Override
public void onDrawFrame(GL10 nope) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
//set camera position
cam.setFrustum(mProjMatrix, mVMatrix, mMVPMatrix);
square.draw(mMVPMatrix);
//square.animate(tick);
//square.setBounds(vec);
System.out.println("Cycle " + cycle + " Ended");
cycle++;
}
@Override
public void onSurfaceChanged(GL10 nope, int width, int height) {
cam.setRatio(width, height);
GLES20.glViewport(0, 0, width, height);
}
public static int loadShader(int type, String shaderCode) {
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
@Override
public void onSurfaceCreated(GL10 gl,
javax.microedition.khronos.egl.EGLConfig config) {
GLES20.glClearColor(0.0f, 0.0f, 1.0f, 0.5f);
camWidth=480;camHeight=320;
cam= new Camera2D(0, 0, camHeight, camWidth);
// initialize a square
vec = new Vector3 (10,10,40,90);
square = new BoundsSquare(vec);
System.out.println("Surface Created");
}