Sign in to follow this  
armond

Help with glut example(modified)

Recommended Posts

Hi, I tried to modify a program from the red book(i.e. the one with the rotating square). Here's the code...
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <stdlib.h>

static GLfloat spin = 0.0;
static bool rotate = false;

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(spin, 0.0, 0.0, 1.0);
    glColor3f(1.0, 1.0, 1.0);
    glRectf(-25.0, -25.0, 25.0, 25.0);
    glPopMatrix();
    glutSwapBuffers();
}

void spinDisplay(void)
{
    spin = spin + 2.0;
    
    if (spin > 360.0)
    {
        spin = spin - 360.0;
    }    
    
    glutPostRedisplay();
    
}    

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
    switch (button)
    {
        case GLUT_LEFT_BUTTON:
            if (state == GLUT_DOWN)
            {
                if (rotate == true)
                {
                    rotate = false;
                }
                else
                {
                    rotate = true;
                }        
            }
            else
            {
                if (rotate == true)
                {
                    glutIdleFunc(spinDisplay);
                }
                else
                {
                    glutIdleFunc(NULL);
                }        
            }           
      
        default:
            break;
        /*
        case GLUT_LEFT_BUTTON:
            if (state == GLUT_DOWN)
            {
                glutIdleFunc(spinDisplay);
            }
            break;
        case GLUT_MIDDLE_BUTTON:
            if (state == GLUT_DOWN)
            {
                glutIdleFunc(NULL);
            }
            break;
        default:
            break;          
       */  
    }   
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(250, 250);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    
    init();
    
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);    
    glutMainLoop();
    return 0;
}


It displays the square/retangle... But when I click on it and it starts rotating it crashes my system... The only weird thing is that I was able to make this work until lately... This is really preventing me from going any further... Any ideas? Thanks!

Share this post


Link to post
Share on other sites
Works fine for me.

==EDIT==
I've just had a look at what the program is actually doing and it seems to be hogging my CPU. By the looks of your code you're not telling the program to close and destroy itself when the window is closed, which it should do.

Share this post


Link to post
Share on other sites
Armond,

The code is fine and works fine on my system. However the idle callback calling glutPostRedisplay() is probably not a good idea and may be causing your system to hang.

Try patching the following lines:


// glutIdleFunc(spinDisplay);
glutTimerFunc(100,spinDisplay,0);

// void spinDisplay(void)
void spinDisplay(int t)
{
...

glutTimerFunc(100,spinDisplay,0);
glutPostRedisplay();
}



This will use the timer callback instead of idle.

Share this post


Link to post
Share on other sites
Hi again, I tried your suggestions... But it made my code slower and still freezes my computer... Here's what I've done...



#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <stdlib.h>

static GLfloat spin = 0.0;
static bool rotate = false;

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
}

void spinDisplay(int someValue)
{
spin = spin + 2.0;

if (spin > 360.0)
{
spin = spin - 360.0;
}

glutTimerFunc(100, spinDisplay, 0);
glutPostRedisplay();

}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
if (rotate == true)
{
rotate = false;
}
else
{
rotate = true;
}
}
else
{
if (rotate == true)
{
//glutIdleFunc(spinDisplay);
glutTimerFunc(100, spinDisplay, 0);
}
else
{
glutIdleFunc(NULL);
}
}

default:
break;
/*
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
glutIdleFunc(spinDisplay);
}
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
{
glutIdleFunc(NULL);
}
break;
default:
break;
*/

}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);

init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}



I hope you could help me make improvements to it. Thanks! :D

Share this post


Link to post
Share on other sites
Yes I could. As long as it's not animated. I could run the program for a few seconds though... There's a trick here... I try to monitor my CPU... If it's something less thatn 30% I run the program... When the program starts it would start eating up my CPU and thus I need to close/stop the rotation and thus I save my system from hanging. But that could be pretty annoying right? It's not even 3D yet...

Share this post


Link to post
Share on other sites
Hehe, I had that problem. It's rather frustrating. I think it is glut doing it. I got so frustrated I gave up on learning OpenGL. I use C++ Builder and since then found OpenGLPanel which basically does what glut does. That application worked fine for me this time around. It was the imaging subset that crashed all over instead :)

Not using glut can be challenging if you don't know windows programming. If you are familar and comfortable with windows programming through there really isn't much to setting up the rendering context. So that would be an alternative for you. You could fairly easily setup a base application that creates the rendering context and calls init, resize, draw, keyboard and mouse which are the only routines I've seen the redbook use so far and I'm about done. If you don't know how to write a windows program and you're working in a windows environment then I would think that should come before OpenGL on a practical basis.

Share this post


Link to post
Share on other sites
Aaaargh that's very frustrating to hear! I'm planning on upgrading for a higher processor (about 933 Mhz I think). I used glut so I could save myself the time of learning the Windows API. Also... I chose OpenGL so I could make platform independent programs... If I use windows API with it then I'm generally killing my objective... Any other alternatives? Thanks!

Share this post


Link to post
Share on other sites
You could program in Java => platform independent.
You'd need jogl.dll.
The problem is that everybody else should have this dll to be able to run the program.
There is a way around this with executable jar's. (dll is included, and you dont't have to start the program in dos)
Java is also a bit slower.

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