Rendering time independent object animation [SOLVED]

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Rendering time independent object animation [SOLVED]

Postby teo2k » Sun Jul 05, 2009 8:59 pm

Hi All,

I'm doing some experiments with OpenGL using the GLSurfaceView introduced in 1.5

In my Renderer I keep track of the time spent to render the last frame and pass this value to the method that update my model. The goal is to keep objects movements on the screen independent from the rendering time.

Now... this seems to work perfectly in the emulator, but on the real device is at least 4/5 times slower!!!

Has anyone experienced the same problem? Or, anyone has a clue of what I'm doing wrong?

Here's pieces of the code I'm using:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /* Renderer */
  2.  
  3. public void onDrawFrame(GL10 gl) {
  4.  
  5.     mRefTime = System.currentTimeMillis();     
  6.  
  7.  
  8.  
  9.     // Update the game state before rendering
  10.  
  11.     if(mLastFrameTime > 0) {
  12.  
  13.         mGameEngine.update(mLastFrameTime);
  14.  
  15.     }
  16.  
  17.  
  18.  
  19.     gl.glMatrixMode(GL10.GL_MODELVIEW);
  20.  
  21.     gl.glLoadIdentity();
  22.  
  23.                    
  24.  
  25.     for (int i = 0; i < mRenderables.length; i++) {
  26.  
  27.         mRenderables[i].draw(gl);
  28.  
  29.     }
  30.  
  31.                    
  32.  
  33.     mLastFrameTime = System.currentTimeMillis() - mRefTime;
  34.  
  35. }
  36.  
  37.  
  38.  
  39. /* Update method */
  40.  
  41. public void update(long lastFrameTimeMS) {
  42.  
  43.     int i, j;
  44.  
  45.     float moveOffset = mGameVelocity * UNIT_VELOCITY_MS * lastFrameTimeMS;
  46.  
  47.  
  48.  
  49.     for(i=0; i<ROW_COUNT; i++) {
  50.  
  51.         for(j=0; j<ROW_SIZE; j++) {
  52.  
  53.             mObj[i][j].x += moveOffset;
  54.  
  55.         }
  56.  
  57.     }
  58.  
  59. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
teo2k
Freshman
Freshman
 
Posts: 4
Joined: Thu Jul 02, 2009 4:22 pm

Top

Postby teo2k » Wed Jul 08, 2009 3:05 pm

I solved it just by adding a Thread.sleep(10) between the update and the rendering.
teo2k
Freshman
Freshman
 
Posts: 4
Joined: Thu Jul 02, 2009 4:22 pm

Postby _Auron_ » Fri Jul 10, 2009 8:45 am

Personally I do something roughly like the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onDrawFrame(GL10 gl) {
  2.  
  3.                 Global.time = SystemClock.uptimeMillis();
  4.  
  5.  
  6.  
  7.                 // Update game objects no faster than 60fps.
  8.  
  9.                 // 1000ms / 60.0fps = 16.667ms/frame - rounded down to 16ms
  10.  
  11.                
  12.  
  13.                 if (Global.time >= (Global.oldTime + 16)) {
  14.  
  15.                         Global.deltaTime        = Global.time - Global.oldTime;
  16.  
  17.                         Global.deltaMod = (float)(1.0f / 16.0f * Global.deltaTime);
  18.  
  19.                         Global.oldTime          = Global.time;
  20.  
  21.                 }
  22.  
  23. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Any value that needs to get updated (rotations, x/y values, etc) I multiply by Global.deltaMod; or divide, depending.
_Auron_
Freshman
Freshman
 
Posts: 9
Joined: Fri Jul 10, 2009 7:50 am

Postby teo2k » Fri Jul 10, 2009 6:17 pm

Thanks, I'll try that.
teo2k
Freshman
Freshman
 
Posts: 4
Joined: Thu Jul 02, 2009 4:22 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests