Controlling apps

Hi, I use glut to make my programs and evertime I run my program my CPU usage goes up to 100%. Is there a way in glut to keep this from happening?

All replies are greatly appreciated

What are you drawing?
Rotations, translations, scalings and lighting involves lots of calculations.
And how fast is you’re CPU?
These things affect the cpu usage, I don’t think GLUT is caussing the problem.

Do not render in the idle callback.

ok,

I’m creating an entire scene that you can roam around in using a first person camera. Although this may seem to be the problem all of my apps take 100% CPU usage to try and get as many frames per second as possible. And I dont render in the idle function, I use glutDisplayFunc for rendering my scene.

Can you throw us a bone? :confused:

It’s a good idea to wrap suspect code with profilers, so you’ll always have a good idea where the time is spent. This way you can isolate problem areas within your app without guessing.

“try and get as many frames per second as possible”

Well, that’s a perfectly valid reason to get 100% CPU usage. If you continuously render (in whatever callback) you burn as much as your system can give.

My program is running as many frames per seconds as it can on the machine it is running on. A simple window has the same problem. Here’s the code for just a window, but it will use 100% CPU

#include<glut.h>

void init()
{
glClearColor(0,0,0,0);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glFlush();
glutPostRedisplay();
glutSwapBuffers();
}

void main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitWindowPosition(10,20);
glutInitWindowSize(1000,700);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow(argc[0]);
init();
glutDisplayFunc(display);
glutMainLooop();
}

This program itself will run using 100% CPU power on any machine, I would like to know if there is a way to keep it from happening.
}

Yes, don’t do glutPostRedisplay() in the display routine.
Do it when you need a repaint. If the image doesn’t change, don’t repaint it.
If it changes all the time, you’re stuck.
You could limit the framerate (and CPU load) by sending the update from a timer callback.

If that’s all not feasible for your case, I would at least put the glutPostRedisplay() at the end of that function for peace of mind. :wink:

The glFlush is useless and only needed for single buffered pixelformats. Remove it for double buffered ones.

ok, Thanks for all your help