GLUT 100% CPU Issue

Recommended Posts

Strider007    122
Hey im hoping someone could hopefully help me out with an issue i have. Basically ive created a GLUT application and every time i compile and run it im hitting 100% cpu usage. Strange and confusing thing is that it works fine on any other PC and if i run any other GLUT application on my PC it just maxes out my CPU. Any Ideas?

Share on other sites
Morpheus011    309
It's impossible to tell what your bottleneck is without seeing some code. I would recommend profiling it or posting some likely points of interest.

As a side note, run your code with your SwapBuffers() call commented out and see what your CPU runs at. It won't draw anything but it will tell you how much time the SwapBuffers() is eating up.

Share on other sites
If you can see your Windows message pump, it should say PeekMessage instead of GetMessage. I'm betting thats exactly what it is.

Share on other sites
Strider007    122
Commented out glswapbuffers() and had no effect.

Heres my main.cpp file.

#include <windows.h>
#include <stdio.h>
#include <glut.h>
#include <gl\gl.h>
#include <math.h>
#include "Texture.h"
#include "Collision.h"
#pragma comment( lib, "Glu32.lib")
#pragma comment( lib, "glaux.lib")
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

Texture texture[2];

{
int Status=FALSE;

{
Status=TRUE;

for (int loop=0; loop<2; loop++)
{
glGenTextures(1, &texture[loop].texID); glBindTexture(GL_TEXTURE_2D, texture[loop].texID);
glTexImage2D(GL_TEXTURE_2D, 0, texture[loop].bpp / 8, texture[loop].width, texture[loop].height, 0, texture[loop].type, GL_UNSIGNED_BYTE, texture[loop].imageData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

if (texture[loop].imageData)
{
free(texture[loop].imageData);
}
}
}
return Status;
}

void renderScene() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(-90,0.0,0.0,1.0);
glBindTexture(GL_TEXTURE_2D, texture[0].texID);

glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f,-1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(-1.0f,1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f,1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex2f(1.0f,-1.0f);
glEnd();

glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();

}

void InitOpenGL(void)
{
glOrtho(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glEnable(GL_TEXTURE_2D);
glClear(GL_COLOR_BUFFER_BIT);
}

void vars(void)
{
float speed=0.0f;
speed += 0.05;
glutPostRedisplay();
}

void main(int argc, char **argv) {

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(768,576);
glutCreateWindow("Carrom Board");

InitOpenGL();

glutDisplayFunc(renderScene);
glutIdleFunc(vars);

glEnable(GL_DEPTH_TEST);
glutMainLoop();

}

Share on other sites
Strider007    122
Quote:
 Original post by dpadam450If you can see your Windows message pump, it should say PeekMessage instead of GetMessage. I'm betting thats exactly what it is.

Not entirely sure what you mean.

Share on other sites
bakery2k1    712
Quote:
 Original post by dpadam450If you can see your Windows message pump, it should say PeekMessage instead of GetMessage. I'm betting thats exactly what it is.

Completely irrelevant.

Quote:
 Original post by Strider007void renderScene() { ... glutPostRedisplay();}

This is why you have 100% CPU usage. As soon as your renderScene function completes, you have requested for it to be called again.

Share on other sites
Strider007    122
Quote:
Original post by bakery2k1
Quote:
 Original post by dpadam450If you can see your Windows message pump, it should say PeekMessage instead of GetMessage. I'm betting thats exactly what it is.

Completely irrelevant.

Quote:
 Original post by Strider007void renderScene() { ... glutPostRedisplay();}

This is why you have 100% CPU usage. As soon as your renderScene function completes, you have requested for it to be called again.

Removing glutPostRedisplay(); has no effect either. The thing is the code works fine on other computers just not mine. Is there anything other than the code that could be affecting the performance?

Share on other sites
bakery2k1    712
Notice you have a glutPostRedisplay in "vars" as well.

Share on other sites
Strider007    122
ignore the vars function ive deleted that, i stuck that in hoping that would solve the issue which i got from a thread but that had no effect.

Share on other sites
wyled    127
It will show the program using 100% unless you are throttling it using sleeping commands after you draw the screne..

After you draw the scene do this:

      include: <windows.h>      include: <winbase.h>.........Sleep(100); // sleep 100ms

Share on other sites
Guest Anonymous Poster
Why do you care if you CPU is at 100% usage !? All this means is your application is hogging the cpu, which in most cases for ages (what this site is about), is not a problem!!

Share on other sites
RuneLancer    253
Quote:
 Original post by Anonymous PosterWhy do you care if you CPU is at 100% usage !? All this means is your application is hogging the cpu, which in most cases for ages (what this site is about), is not a problem!!

Temperature rises with CPUs that run at full load. Some cores have a hard time staying at full load for excessive periods of time, and not everyone likes to go out and buy uber-l33t puppy-blood cooled nitrogene systems to play a game during a blisteringly hot summer.

Also, tabbing out of a game to go read a faq or do some other mundane task is awful when it takes half an hour to just switch windows.

Share on other sites
Quote:
 Also, tabbing out of a game to go read a faq or do some other mundane task is awful when it takes half an hour to just switch windows.

That's caused by excessive memory usage, not CPU usage. The operating system can take CPU time whenever it wants.

Quote:
 Removing glutPostRedisplay(); has no effect either.

Your idle function is getting called constantly. Instead, you can use glutTimerFunc to register a function to be called after a certain number of milliseconds has passed.

Quote:
 The thing is the code works fine on other computers just not mine. Is there anything other than the code that could be affecting the performance?

What is the % usage you observed on the other machines? If they have HyperThreading, Windows would report 50% utilization if the program is taking up 100% of the processor.

Share on other sites
Strider007    122
Other Machines are registering a 2-5% cpu usage. Ive sort of dug deeper into the problem and ive noticed its not just GLUT that causes the cpu to max out but any OpenGL application just crushes my CPU.

I havent got a clue on how to solve this issue.

Share on other sites
DaBono    1496
Your video driver probably has VSync switched off. When VSync is on, your application will render in sync with the monitors refresh rate. When it's off, it will render as fast as it can, which will take 100% CPU-load.

If you have a NVidia-card, you can turn this behaviour off by going to Control Panel->Display->Advanced->Geforce XXX->Performance settings. Then set 'Vertical sync' to either on, or application controlled.
Similar settings will probably exist for other video cards as well.

Share on other sites
Guest Anonymous Poster
Probably because opengl cant find the right rendering hardware and switches to software emulation.

Share on other sites
Strider007    122
Quote:
 Original post by Anonymous PosterProbably because opengl cant find the right rendering hardware and switches to software emulation.

If thats the case how would i be able to rectify it?

Share on other sites
RuneLancer    253
If you have an ATI, get Catalyst ASAP. You'll have a hard time running OpenGL apps until you upgrade your drivers.

My game used to get pathetic framerates with my betatesters (sometimes in the single digit range, whereas I'd get a few hundred easily with my card at the time.) After recommanding driver updates and the use of Catalyst, framerates rose drastically.