Skip to main content
GameDev.net gamedev.net
🔒 Locked

GLUT 100% CPU Issue

Started by Strider007 Apr 21, 2006 at 5:00 PM 16 replies 6k views
Original Post
Strider007
Strider007
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?
Morpheus011
Morpheus011
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.
dpadam450
dpadam450
If you can see your Windows message pump, it should say PeekMessage instead of GetMessage. I'm betting thats exactly what it is.
NBA2K, Madden, Maneater, Killing Floor, Sims 
Strider007
Strider007
Commented out glswapbuffers() and had no effect.

Heres my main.cpp file.




#include
#include
#include
#include
#include
#include "Texture.h"
#include "Collision.h"
#pragma comment( lib, "Glu32.lib")
#pragma comment( lib, "glaux.lib")
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

bool LoadTGA(Texture *, char *);
Texture texture[2];




int LoadGLTextures()
{
int Status=FALSE;

if (LoadTGA(&texture[0], "Media/Background.tga"))
(LoadTGA(&texture[1], "Media/Striker.tga"));
{
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);

glBegin(GL_QUADS);
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);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
}



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");

LoadGLTextures();
InitOpenGL();

glutDisplayFunc(renderScene);
glutIdleFunc(vars);

glEnable(GL_DEPTH_TEST);
glutMainLoop();

}


Strider007
Strider007
Quote:
Original post by dpadam450
If 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.
bakery2k1
bakery2k1
Quote:
Original post by dpadam450
If 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 Strider007
void 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.
Strider007
Strider007
Quote:
Original post by bakery2k1
Quote:
Original post by dpadam450
If 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 Strider007
void 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?
bakery2k1
bakery2k1
Notice you have a glutPostRedisplay in "vars" as well.
Strider007
Strider007
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.
wyled
wyled
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
---John Josef, Technical DirectorGlass Hat Software Inc
RuneLancer
RuneLancer
Quote:
Original post by 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!!


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.
igni ferroque
igni ferroque
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.
Free Mac Mini (I know, I'm a tool)
Strider007
Strider007
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.
DaBono
DaBono
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.
Strider007
Strider007
Quote:
Original post by Anonymous Poster
Probably 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?
RuneLancer
RuneLancer
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.