eric_the_animal
eric_the_animal

Reputation: 432

Smooth model movement in OpenGL 3D

When I translate my 3D model across the screen the movement does not look smooth. It jumps (no surprise as I am translating by a factor of 1) to each of the translated locations. My question is how can I get the model to move across the screen without showing this jerky/jumping animation effect. I have tried reducing the translation factor but that of course slows down the movement too much. The following code block is in the display(GLAutoDrawable drawable){} routine:

for (float x = -8;x < 8; x+=2){
    for (float z = -8; z < 8; z+=2){

        view2 = rotate(view,(float)20,(float)1,(float)0,0); //rotate 30deg on x

        //rotate the model. spin on Y. (rotate is a custom func - not using 
        //old glRotate)
        view2 = rotate(view2,(float)rotAngle,(float)0,(float)1,0); 

        //transX and transY are incremented/decremented by 1 every time
        //the KeyPressed event is fired.  Animator running at 60fps.
        //translate() is a simple matrix multiplier.  This is where
        //the model gets its new co-ordinates (that it jumps to...)
        view2 = translate(view2, (float)transX - x, 0, transZ - z);

        //pass transform to the vertex shader
        gl.glUniformMatrix4fv(iView, 1, false, view2, 0);

        //the projection matrix (static)
        gl.glUniformMatrix4fv(iProj, 1, false, projection, 0);

        gl.glDrawArrays(GL3.GL_TRIANGLES, 0, 36); //draw 12 triangles (36 vertices).

    }
}

Upvotes: 0

Views: 923

Answers (1)

datenwolf
datenwolf

Reputation: 162164

Redraw as fast as possible; you must achieve at least ~40 frames per second so that things appear to move smoothly; at slower frame rates you'd need motion blur to cover up the jerkiness.

Between drawing frames measure the time in between the same points in the drawing code of a given frame and move according to that. Note that

SwapBuffers returns almost immediately, regardless of V-Sync being enabled or not. It's the OpenGL pipeline that's going to stall eventually.

Upvotes: 2

Related Questions