views:

218

answers:

4

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

A: 

The above code runs fine on my laptop -- 32bit Lucid 10.04 with freeglut, version 2.6.0-0ubuntu2. Silly questions:

1.. Do you have an X server running?

1a) Which display drivers do you have installed?

1b) If you run ldd on the compiled object, which version of libGL.so are you linking against?

2.. Are you passing in any command line arguments which you haven't listed above?

3.. Is your segfault occuring on the line with:

glutDisplayFunc(display);
M. Tibbits
M. Tibbits
I dont know what an X server is, so probably not?Does the virtual machine have its own display drivers installed or would it be using the ones from my Win 7 64 bit which is running the virtual machine because I have ATI catalyst version 2010.0527.1242.20909 installed on Win 7 for my Radeon 5570, the VM just says auto detect, unless I'm missing something.
will
1b, libGL.so.1 => /usr/lib/fglrx/libGL.so.1 (0x00007ffaca65e000)2, no3, pretty sure its the line after that
will
From your comments, you have an X-server, you're running Linux on top of Win 7. Eclipse and netbeans are running as GUI's inside of Linux and this environment is driven by an X-server. Which version of the display drivers are installed in Linux? (Sorry to keep the conversation going in two places).
M. Tibbits
M. Tibbits
A: 

When you run the executable, what arguments are you passing?

Pimpl
none and that shouldn't matter because its only using argv[0] as the name of the window which should be the name of the program, since thats generally what argv[0] is.
will
A: 

Do you have hardware acceleration inside the virtual machine? Check using glxinfo. The crash inside the DRI suggests that you do not.

Matias Valdenegro
name of display: :0.0Segmentation faultthats all glxinfo tells me
will
I apparently have Accelerate 3D graphics checked in the virtual machine settings, so I think i do?
will
The VM might have HW acceleration, but you need your drivers installed in the OS inside the VM.
Matias Valdenegro
I'd make sure that your version of the flgrx matches (or is as close as possible) to the version of Windows drivers. According to this http://developer.amd.com/drivers/ccc/Pages/default.aspx it appears that you have Catalyst 10.6 drivers installed under Win 7. I'd update both Windows 7 and Ubuntu to 10.9. Check out http://support.amd.com/us/gpudownload/Pages/index.aspx
M. Tibbits
M. Tibbits
A: 

My hunch is that if you compile the following pared down example, you'll still get a segmentation fault:

#include <stdlib.h>
#include <GL/freeglut.h>
#include <stdio.h>

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(250,250);
    glutInitWindowPosition(100,100);
    glutCreateWindow("test window");
    return 0;
}

Because your segmentation fault is occurring in:

fgCreateWindow() at freeglut_structure.c:106 0x7ffff7948f62 glutCreateWindow()

In other words, it can't create a window (doesn't matter what's in it). My guess, along the lines of what @Matias replied is that you need to enable 3D acceleration within your VM? But it might be a display driver issue, 32 vs 64 bit, or something slightly more sinister such as a mis-match between freeglut & your version of OpenGl -- hence my previous barrage of questions. Could you re-compile this pared down version and post the results?

M. Tibbits
Yes that still segfaults
will
Ahh - and I see from your comments that you're running Ubuntu 10.04 x64 in MS Virtual PC from Win 7 x64? My guess is that this is another instance of the bugs related to the way they changed the display drivers loading in Win 7. Did you modify the boot parameters for the install - as "noreplace-paravirt"? See for instance http://ubuntuforums.org/showthread.php?t=1466888 or http://nemesisv.blogspot.com/2009/04/installing-ubuntu-904-on-microsoft.html
M. Tibbits