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.

Ramin Zahedi
  • 455
  • 2
  • 6
  • 20

1 Answers1

1

Of course you should worry about multithreading. In particular, Android creates its own GLThread for rendering, when you attach a GLRenderer-derived class to a GLSurfaceView using the setRenderer() function.

In fact, multithreading can cause crashes (not only unexpected behavior) in your programs especially when you loop through arrays adding/removing objects and such.

Check if you are modifying the same data inside the onDrawFrame function of your GLRenderer and your own thread. If you are, try adding the following around the modified code (in both threads):

synchronize(variable) {
     modify(variable);
}

This will lock the variable throughout the modify() function until it ends the block. Try not to overuse it, though, only in places where you need it. One thread will block the other one until it's finished!

RedOrav
  • 953
  • 9
  • 21
  • I have not written any thread myself in the program. I just wrote code in onDrawFrame. I didn't use multithreading myself in the program – Ramin Zahedi Jul 15 '12 at 16:21
  • Could you post the other parts of the code where you have the same problem? – RedOrav Jul 15 '12 at 16:50
  • well it's a bit long and complicated and I completely confused by the unexpected results. Does codes execute from up to down or from down to up? funny question, yes? – Ramin Zahedi Jul 15 '12 at 17:55
  • Code executes from up to down, always in the same thread, unless you specify conditions for it not to do so (say, special goto instructions). I'm afraid I don't know how to answer your question without more information. Have you tried looking at your matrix contents using your IDE's debugger? – RedOrav Jul 15 '12 at 22:27
  • Don't bother RedOrav, this is the same problem he posted last week.. http://stackoverflow.com/questions/11467919/quaternion-rotation-do-not-works-as-excepted/11470663#11470663 – Matic Oblak Jul 18 '12 at 08:52