Sign in to follow this  
Strider007

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 this post


Link to post
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 this post


Link to post
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\"" )

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

}


Share this post


Link to post
Share on other sites
Strider007    122
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.

Share this post


Link to post
Share on other sites
bakery2k1    712
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.

Share this post


Link to post
Share on other sites
Strider007    122
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?

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
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 this post


Link to post
Share on other sites
RuneLancer    253
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.

Share this post


Link to post
Share on other sites
igni ferroque    415
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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
Strider007    122
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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this