Wikipedia:Reference desk/Archives/Computing/2008 May 4

Computing desk
< May 3 << Apr | May | Jun >> May 5 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


May 4

edit

opengl program crash

edit

hey I am a newbie at opengl programming in fact just started.. I got this program to compile under VS2008 but it crashes at glClear():-

i have included gl.h, glut.h and defined GLUT_DISABLE_ATEXIT_HACK

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_POLYGON);
        glVertex3f (0.25, 0.25, 0.0);
        glVertex3f (0.75, 0.25, 0.0);
        glVertex3f (0.75, 0.75, 0.0);
        glVertex3f (0.25, 0.75, 0.0);
    glEnd();
    glFlush ();
}

void init (void) 
{
    glClearColor (0.5, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize (250, 250); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init ();
    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;   
}

it crashes at glutMainLoop() while doing a callback to display(). Commenting that line produces a crash at glFlush(). I am using windows vista with ATI drivers. Bobatnet (talk) 12:12, 4 May 2008 (UTC)[reply]

It looks like all you need to do is swap the buffers. Opengl works by filling one buffer while the other is displayed and then switching them. So, your display method should look something like this:
void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_POLYGON);
        glVertex3f (0.25, 0.25, 0.0);
        glVertex3f (0.75, 0.25, 0.0);
        glVertex3f (0.75, 0.75, 0.0);
        glVertex3f (0.25, 0.75, 0.0);
    glEnd();
    glFlush ();
    glutSwapBuffers ( );
}

If you forget to do this, your window won't display what you want, and it will look like it crashed. Leeboyge (talk) 23:54, 4 May 2008 (UTC)[reply]

Tried that but it still crashes. Actually, it stays on screen for some seconds and then it crashes. Also, the code is copied from the red book as I am just learning opengl. So, may be the code is right ?!! Bobatnet (talk) 04:47, 5 May 2008 (UTC)[reply]

That's really strange. I am assuming that it compiled correctly with no errors. If so, it may just be a Visual Studios thing. I am actually not using VS. I am using Cygwin to compile with gcc, and I am using the GLUT library directly. Here is all of the code I used to test yours:
#include <windows.h>
#include <stdlib.h>
#include <unistd.h>
#include <GL/glut.h> 
#include <iostream>
using namespace std;

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_POLYGON);
        glVertex3f (0.25, 0.25, 0.0);
        glVertex3f (0.75, 0.25, 0.0);
        glVertex3f (0.75, 0.75, 0.0);
        glVertex3f (0.25, 0.75, 0.0);
    glEnd();
    glFlush ();
    glutSwapBuffers ( );
}
 
void init (void) 
{
    glClearColor (0.5, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize (250, 250); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init ();
    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;   
}

I compiled with this:

g++ -g -Wall test.cpp -l glut32 -l glu32 -l opengl32 -o test

This gives me a white square centered on a dark red background. If yours is crashing, I can only guess that it may be something to do with linking the libraries or with Visual Studio. You should have no problem running on XP or Vista. I am running on Vista right now, and it works just fine. Leeboyge (talk) 06:32, 5 May 2008 (UTC)[reply]