Hi,
in my program I have a 3D model, it starts moving pressing one button in towards only one direction (movement = speed *Dt, speed increase till it reaches max speed).
At first I call the movement task throught glutIdleFunc but I’ve noticed a performance little decrease (framerate lower).
So I implement movement task inside render task calling glutPostRedisplay:
void GameScene::render()
{
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
gluLookAt(0.0f, 0.0f, 7.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
selectedArena->render();
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
float lightPos[] = {-1.0, 1, 1, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glPushMatrix();
glScalef(0.06, 0.06, 0.06);
glRotatef(90, 1.0, 0.0, 0.0);
glRotatef(180, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, 0.0+spostamento);
glTranslatef(0.0, 0.0, -40.0);
selectedCycle->drawCycle();
glPopMatrix();
//glDisable(GL_BLEND);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE);
animate();
}
movement task:
void GameScene::animate()
{
if(move) {
time = glutGet(GLUT_ELAPSED_TIME);
timediff = time - timeprec;
timeprec = time;
//float xSpeed = 0.0f;
xSpeed += (selectedCycle->accel * timediff);
if(xSpeed > selectedCycle->maxSpeed) xSpeed = selectedCycle->maxSpeed;
spostamento += xSpeed * timediff;
cout << xSpeed << endl;
glutPostRedisplay();
}
}
I have two questions:
- actual implementation is correct or I’d use glutIdleFunc?
- If I use glutIdleFunc the movement task is the same, I have to call “glutPostRedisplay” at the end of movement func, isn’t it?
Thank you