I just started trying to use OpenGL in C++ for a class(I have previously used it in Java a fair amount). And I started off trying to write something substantial, I couldn't get that to stop Seg faulting so I wrote this piddly little piece of code which is nearly a line for line copy from an example in the first chapter of the red book. It also Seg faults. My question is why. I have tried both eclipse, and netbeans, I have the glut.h library linked in my projects in both, I am running 64 bit ubuntu 10.4, on a virtual machine using VMWare, gcc and freeglut are both installed, Both netbeans and eclipse will run regular (non OpenGL) C++ code I write without seg faulting.
Anyway here is the code:
#include <stdlib.h>
#include <GL/freeglut.h>
#include <stdio.h>
void init(){
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void display(){
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();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250,250); //if I comment out this line,
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]); //this line,
init(); //this line and the glut main loop line it runs without any errors, but why wouldn't it? It's not doing anything now!
glutDisplayFunc(display);
glutMainLoop(); //if I comment out just this line I get illegal instruction instead of segfault but I need this line
return 0;
}
Thread [1] 28944 (Suspended : Signal : SIGSEGV:Segmentation fault)
XF86DRIQueryVersion() at 0x7ffff7e7412e
XF86DRIQueryExtension() at 0x7ffff7e742c9
0x7ffff7e73c70
0x7ffff7e53ff8
glXGetFBConfigs() at 0x7ffff7e4c71e
glXChooseFBConfigSGIX() at 0x7ffff7e4cd97
fgChooseFBConfig() at freeglut_window.c:205 0x7ffff794a8c7
fgOpenWindow() at freeglut_window.c:768 0x7ffff794aac8
fgCreateWindow() at freeglut_structure.c:106 0x7ffff7948f62
glutCreateWindow() at freeglut_window.c:1,183 0x7ffff794a2a2
main() at Thread [1] 28944 (Suspended : Signal : SIGSEGV:Segmentation fault)
XF86DRIQueryVersion() at 0x7ffff7e7412e
XF86DRIQueryExtension() at 0x7ffff7e742c9
0x7ffff7e73c70
0x7ffff7e53ff8
glXGetFBConfigs() at 0x7ffff7e4c71e
glXChooseFBConfigSGIX() at 0x7ffff7e4cd97
fgChooseFBConfig() at freeglut_window.c:205 0x7ffff794a8c7
fgOpenWindow() at freeglut_window.c:768 0x7ffff794aac8
fgCreateWindow() at freeglut_structure.c:106 0x7ffff7948f62
glutCreateWindow() at freeglut_window.c:1,183 0x7ffff794a2a2
main() at (project stuff here):54 0x40100b