onDrawFrame、requestRender 和渲染线程? | OpenGL ES 2.0
Posted
技术标签:
【中文标题】onDrawFrame、requestRender 和渲染线程? | OpenGL ES 2.0【英文标题】:onDrawFrame, requestRender, and render thread? | OpenGL ES 2.0 【发布时间】:2013-05-01 00:59:01 【问题描述】:我在使用 OpenGL ES 2.0 中的游戏循环的外部线程运行我的应用程序时遇到问题。
这是我在开始类中的 onCreate:
@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.
我的 GameLoop 类
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");
所以基本上发生的情况是,如果我不调用loop.run();
,我会得到一张静态图片,这基本上是一个 onDrawFrame 循环,在完成此操作后,logCat 上不会弹出任何其他内容。
接下来发生的事情是如果我打电话给loop.run()
,基本上循环会永远循环下去,但屏幕上什么也没有。我只看到一个标题屏幕,而不是 glView。
我做错了什么?还有其他方法可以更新屏幕吗?
【问题讨论】:
【参考方案1】:线程必须以 start() 而不是 run() 开始。
【讨论】:
哇,我不敢相信就这么简单!谢谢!如果 start 确实启动了线程,那么 run 做了什么?以上是关于onDrawFrame、requestRender 和渲染线程? | OpenGL ES 2.0的主要内容,如果未能解决你的问题,请参考以下文章
Android:了解 OnDrawFrame、FPS 和 VSync (OpenGL ES 2.0)