wrongusername
wrongusername

Reputation: 18918

Segmentation fault in OpenGL under Linux

I am following some tutorials and came up with the following code:

// rendering.cpp

#include "rendering.h"
#include <GL/gl.h>
#include <GL/freeglut.h>

void DrawGLScene()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
}

int InitGL(int argc, char** argv)
{
    /*glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);*/

    glutInit(&argc, argv);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutDisplayFunc(DrawGLScene);
    glutCreateWindow("Swimming Simulation");
    glutMainLoop(); // Enter GLUT's main loop

    return true;
}

My main function is very simple and only calls that function:

#include "rendering.h"

int main(int argc, char** argv)
{
    InitGL(argc, argv);
    return 0;
}

I am compiling with this command:

g++ -Wall -g swim.cpp rendering.cpp -lglut -lGLU -o swim

Running swim creates a window as expected. However, if I uncomment the lines in InitGL, then I get a segmentation fault when running the program:

(gdb) r
Starting program: <dir> 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x000000335ca52ca7 in glShadeModel () from /usr/lib64/libGL.so.1
Missing separate debuginfos, use: debuginfo-install freeglut-2.6.0-6.fc15.x86_64 glibc-2.14.90-24.fc16.6.x86_64 libX11-1.4.3-1.fc16.x86_64 libXau-1.0.6-2.fc15.x86_64 libXdamage-1.1.3-2.fc15.x86_64 libXext-1.3.0-1.fc16.x86_64 libXfixes-5.0-1.fc16.x86_64 libXi-1.4.5-1.fc16.x86_64 libXxf86vm-1.1.1-2.fc15.x86_64 libdrm-2.4.33-1.fc16.x86_64 libgcc-4.6.3-2.fc16.x86_64 libstdc++-4.6.3-2.fc16.x86_64 libxcb-1.7-3.fc16.x86_64 mesa-libGL-7.11.2-3.fc16.x86_64 mesa-libGLU-7.11.2-3.fc16.x86_64
(gdb) backtrace
#0  0x000000335ca52ca7 in glShadeModel () from /usr/lib64/libGL.so.1
#1  0x0000000000401d67 in InitGL (argc=1, argv=0x7fffffffe198)
    at rendering.cpp:25
#2  0x0000000000401c8c in main (argc=1, argv=0x7fffffffe198) at swim.cpp:37

What should I be doing here to get my program to run without crashing?

Upvotes: 0

Views: 3708

Answers (3)

James Mitch
James Mitch

Reputation: 549

Get rid of glut and use something better like GLFW and also a lot of those functions are deprecated so use a modern tutorial like http://www.opengl-tutorial.org/ or http://ogldev.atspace.co.uk/

Upvotes: 3

datenwolf
datenwolf

Reputation: 162317

You fell into a tricky pitfall of GLUT. GLUT is sort of a state machine like OpenGL (it's not part of OpenGL). And the callback functions must be set after creating or selecting a window. In your case move the call of glutDisplayFunc (and any other callback setters) after the call of glutCreateWindow.

Upvotes: 5

Mārtiņš Možeiko
Mārtiņš Možeiko

Reputation: 12927

OpenGL functions can be called only when there is OpenGL context - after glutCreateWindow function call if you use GLUT.

But they shouldn't crash the application though...

Upvotes: 1

Related Questions