Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: glutIdleFunc performance issue

  1. #1
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    52

    glutIdleFunc performance issue

    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:

    Code :
    [COLOR=#ca00a5]void[/COLOR] [COLOR=#438288]GameScene[/COLOR]::render()
    {
    [COLOR=#7d4725][COLOR=#000000]    [/COLOR][COLOR=#490085]glEnable[/COLOR][COLOR=#000000]([/COLOR]GL_DEPTH_TEST[COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#7d4725][COLOR=#000000]    [/COLOR][COLOR=#490085]glDisable[/COLOR][COLOR=#000000]([/COLOR]GL_CULL_FACE[COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#4700de][COLOR=#000000]    [/COLOR][COLOR=#490085]gluLookAt[/COLOR][COLOR=#000000]([/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]7.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]0.0f[COLOR=#000000], [/COLOR]1.0f[COLOR=#000000], -[/COLOR]1.0f[COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#008c00][COLOR=#000000]    [/COLOR]//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);[/COLOR]
     
    [COLOR=#438288][COLOR=#000000]    [/COLOR]selectedArena[COLOR=#000000]->[/COLOR][COLOR=#275a5e]render[/COLOR][COLOR=#000000]();[/COLOR][/COLOR]
     
    [COLOR=#7d4725][COLOR=#000000]    [/COLOR][COLOR=#490085]glEnable[/COLOR][COLOR=#000000]([/COLOR]GL_LIGHT0[COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#7d4725][COLOR=#000000]	[/COLOR][COLOR=#490085]glEnable[/COLOR][COLOR=#000000]([/COLOR]GL_LIGHTING[COLOR=#000000]);[/COLOR][/COLOR]
     
        [COLOR=#ca00a5]float[/COLOR] lightPos[] = {-[COLOR=#4700de]1.0[/COLOR], [COLOR=#4700de]1[/COLOR], [COLOR=#4700de]1[/COLOR], [COLOR=#4700de]0.0[/COLOR]};
    [COLOR=#7d4725][COLOR=#000000]    [/COLOR][COLOR=#490085]glLightfv[/COLOR][COLOR=#000000]([/COLOR]GL_LIGHT0[COLOR=#000000], [/COLOR]GL_POSITION[COLOR=#000000], lightPos);[/COLOR][/COLOR]
     
     
    [COLOR=#490085][COLOR=#000000]    [/COLOR]glPushMatrix[COLOR=#000000]();[/COLOR][/COLOR]
    [COLOR=#4700de][COLOR=#000000]    [/COLOR][COLOR=#490085]glScalef[/COLOR][COLOR=#000000]([/COLOR]0.06[COLOR=#000000], [/COLOR]0.06[COLOR=#000000], [/COLOR]0.06[COLOR=#000000]);[/COLOR][/COLOR]
        [COLOR=#490085]glRotatef[/COLOR]([COLOR=#4700de]90[/COLOR], [COLOR=#4700de]1.0[/COLOR], [COLOR=#4700de]0.0[/COLOR], [COLOR=#4700de]0.0[/COLOR]);
        [COLOR=#490085]glRotatef[/COLOR]([COLOR=#4700de]180[/COLOR], [COLOR=#4700de]0.0[/COLOR], [COLOR=#4700de]1.0[/COLOR], [COLOR=#4700de]0.0[/COLOR]);
     
        [COLOR=#490085]glTranslatef[/COLOR]([COLOR=#4700de]0.0[/COLOR], [COLOR=#4700de]0.0[/COLOR], [COLOR=#4700de]0.0[/COLOR]+[COLOR=#438288]spostamento[/COLOR]);
        [COLOR=#490085]glTranslatef[/COLOR]([COLOR=#4700de]0.0[/COLOR], [COLOR=#4700de]0.0[/COLOR], -[COLOR=#4700de]40.0[/COLOR]);
    [COLOR=#438288][COLOR=#000000]    [/COLOR]selectedCycle[COLOR=#000000]->[/COLOR][COLOR=#275a5e]drawCycle[/COLOR][COLOR=#000000]();[/COLOR][/COLOR]
    [COLOR=#490085][COLOR=#000000]    [/COLOR]glPopMatrix[COLOR=#000000]();[/COLOR][/COLOR]
     
    [COLOR=#008c00][COLOR=#000000]    [/COLOR]//glDisable(GL_BLEND);[/COLOR]
    [COLOR=#490085][COLOR=#000000]    [/COLOR]glDisable[COLOR=#000000]([/COLOR][COLOR=#7d4725]GL_LIGHT0[/COLOR][COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#7d4725][COLOR=#000000]    [/COLOR][COLOR=#490085]glDisable[/COLOR][COLOR=#000000]([/COLOR]GL_LIGHTING[COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#7d4725][COLOR=#000000]    [/COLOR][COLOR=#490085]glDisable[/COLOR][COLOR=#000000]([/COLOR]GL_CULL_FACE[COLOR=#000000]);[/COLOR][/COLOR]
     
        [COLOR=#275a5e]animate[/COLOR]();
     
     
    }

    movement task:

    Code :
    [COLOR=#ca00a5]void[/COLOR] [COLOR=#438288]GameScene[/COLOR]::animate()
    {
        [COLOR=#ca00a5]if[/COLOR]([COLOR=#438288]move[/COLOR]) {
    [COLOR=#7d4725][COLOR=#000000]        [/COLOR][COLOR=#438288]time[/COLOR][COLOR=#000000] = [/COLOR][COLOR=#490085]glutGet[/COLOR][COLOR=#000000]([/COLOR]GLUT_ELAPSED_TIME[COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#438288][COLOR=#000000]        [/COLOR]timediff[COLOR=#000000] = [/COLOR]time[COLOR=#000000] - [/COLOR]timeprec[COLOR=#000000];[/COLOR][/COLOR]
            [COLOR=#438288]timeprec[/COLOR] = [COLOR=#438288]time[/COLOR];
    [COLOR=#008c00][COLOR=#000000]        [/COLOR]//float xSpeed = 0.0f;[/COLOR]
     
    [COLOR=#438288][COLOR=#000000]        [/COLOR]xSpeed[COLOR=#000000] += ([/COLOR]selectedCycle[COLOR=#000000]->[/COLOR]accel[COLOR=#000000] * [/COLOR]timediff[COLOR=#000000]);[/COLOR][/COLOR]
    [COLOR=#438288][COLOR=#000000]        [/COLOR][COLOR=#ca00a5]if[/COLOR][COLOR=#000000]([/COLOR]xSpeed[COLOR=#000000] > [/COLOR]selectedCycle[COLOR=#000000]->[/COLOR]maxSpeed[COLOR=#000000]) [/COLOR]xSpeed[COLOR=#000000] = [/COLOR]selectedCycle[COLOR=#000000]->[/COLOR]maxSpeed[COLOR=#000000];[/COLOR][/COLOR]
    [COLOR=#438288][COLOR=#000000]        [/COLOR]spostamento[COLOR=#000000] += [/COLOR]xSpeed[COLOR=#000000] * [/COLOR]timediff[COLOR=#000000];[/COLOR][/COLOR]
            [COLOR=#7d1bae]cout[/COLOR] << [COLOR=#438288]xSpeed[/COLOR] << [COLOR=#490085]endl[/COLOR];
    [COLOR=#490085][COLOR=#000000]        [/COLOR]glutPostRedisplay[COLOR=#000000]();[/COLOR][/COLOR]
        }
     
     
    }

    I have two questions:
    1) actual implementation is correct or I'd use glutIdleFunc?
    2) 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

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,580
    It seems correct for both your questions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •