0

I have got some strange and unexpected results from my program in OpenGL ES for android for example in the code below:

matrix = ThisRot.get();
gl.glMultMatrixf(matrix, 0);
currentRotation.toMatrix(matrix);
temp.set(matrix);

I set the matrix value before I use it as an argument for gl.glMultMatrixf and after that I change the value of matrix and use it for another purpose, but it has effect an the way the object rotate so it should have effect on gl.glMultMatrixf(). and that's not the only one, some other places in my code I had this unexpected results. so I have thought maybe these happen due to mutual exclusion and multitreading and those kind of things. am I right? should we worry about multithreading when we code in Opengl ES for android? How can I avoid these kind of problems.

4

1 回答 1

1

当然,您应该担心多线程。特别是,当您使用 setRenderer() 函数将 GLRenderer 派生类附加到 GLSurfaceView 时,Android 会创建自己的 GLThread 进行渲染。

事实上,多线程可能会导致程序崩溃(不仅是意外行为),尤其是当您循环通过数组添加/删除对象等时。

检查您是否在 GLRenderer 的 onDrawFrame 函数和您自己的线程中修改相同的数据。如果是,请尝试在修改后的代码周围添加以下内容(在两个线程中):

synchronize(variable) {
     modify(variable);
}

这将在整个 modify() 函数中锁定变量,直到它结束块。不过,尽量不要过度使用它,只在你需要它的地方。一个线程将阻塞另一个线程,直到它完成!

于 2012-07-15T16:06:21.060 回答