56

My gdb backtrace gives:

(gdb) backtrace
#0  0x00000000 in ?? ()
#1  0x0804a211 in init () at example1.cpp:147
#2  0x0804a6bc in main (argc=1, argv=0xbffff3d4) at example1.cpp:283

Not very informative. Eclipse debugger at least lets me see that it stops on line 3 below:

// Create a vertex array object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

This is a very common block to see in gl programming, and I'm even running other code with the same block with no problem. So I'm baffled.

Build output from running make:

g++ -g -DFREEGLUT_STATIC -DGLEW_STATIC -I../../include example1.cpp ../../Commo/InitShader.o  -L/usr/lib/mesa -lGLEW -lglut -lGL -lX11  -lm  -o example1

Program containing the problem:

// rotating cube with two texture objects
// change textures with 1 and 2 keys

#include "Angel.h"

const int  NumTriangles = 12; // (6 faces)(2 triangles/face)
const int  NumVertices  = 3 * NumTriangles;
const int  TextureSize  = 64;

typedef Angel::vec4 point4;
typedef Angel::vec4 color4;

// Texture objects and storage for texture image
GLuint textures[2];

GLubyte image[TextureSize][TextureSize][3];
GLubyte image2[TextureSize][TextureSize][3];

// Vertex data arrays
point4  points[NumVertices];
color4  quad_colors[NumVertices];
vec2    tex_coords[NumVertices];

// Array of rotation angles (in degrees) for each coordinate axis
enum { Xaxis = 0, Yaxis = 1, Zaxis = 2, NumAxes = 3 };
int      Axis = Xaxis;
GLfloat  Theta[NumAxes] = { 0.0, 0.0, 0.0 };
GLuint   theta;

//----------------------------------------------------------------------------
int Index = 0;
void quad( int a, int b, int c, int d )
{
    point4 vertices[8] = {
        point4( -0.5, -0.5,  0.5, 1.0 ),
        point4( -0.5,  0.5,  0.5, 1.0 ),
        point4(  0.5,  0.5,  0.5, 1.0 ),
        point4(  0.5, -0.5,  0.5, 1.0 ),
        point4( -0.5, -0.5, -0.5, 1.0 ),
        point4( -0.5,  0.5, -0.5, 1.0 ),
        point4(  0.5,  0.5, -0.5, 1.0 ),
        point4(  0.5, -0.5, -0.5, 1.0 )
    };

    color4 colors[8] = {
        color4( 0.0, 0.0, 0.0, 1.0 ),  // black
        color4( 1.0, 0.0, 0.0, 1.0 ),  // red
        color4( 1.0, 1.0, 0.0, 1.0 ),  // yellow
        color4( 0.0, 1.0, 0.0, 1.0 ),  // green
        color4( 0.0, 0.0, 1.0, 1.0 ),  // blue
        color4( 1.0, 0.0, 1.0, 1.0 ),  // magenta
        color4( 0.0, 1.0, 1.0, 1.0 ),  // white
        color4( 1.0, 1.0, 1.0, 1.0 )   // cyan
    };

    quad_colors[Index] = colors[a];
    points[Index] = vertices[a];
    tex_coords[Index] = vec2( 0.0, 0.0 );
    Index++;

    quad_colors[Index] = colors[a];
    points[Index] = vertices[b];
    tex_coords[Index] = vec2( 0.0, 1.0 );
    Index++;

    quad_colors[Index] = colors[a];
    points[Index] = vertices[c];
    tex_coords[Index] = vec2( 1.0, 1.0 );
    Index++;

    quad_colors[Index] = colors[a];
    points[Index] = vertices[a];
    tex_coords[Index] = vec2( 0.0, 0.0 );
    Index++;

    quad_colors[Index] = colors[a];
    points[Index] = vertices[c];
    tex_coords[Index] = vec2( 1.0, 1.0 );
    Index++;

    quad_colors[Index] = colors[a];
    points[Index] = vertices[d];
    tex_coords[Index] = vec2( 1.0, 0.0 );
    Index++;
}

//----------------------------------------------------------------------------
void colorcube()
{
    quad( 1, 0, 3, 2 );
    quad( 2, 3, 7, 6 );
    quad( 3, 0, 4, 7 );
    quad( 6, 5, 1, 2 );
    quad( 4, 5, 6, 7 );
    quad( 5, 4, 0, 1 );
}

//----------------------------------------------------------------------------
void init()
{
    colorcube();

    // Create a checkerboard pattern
    for ( int i = 0; i < 64; i++ ) {
        for ( int j = 0; j < 64; j++ ) {
            GLubyte c = (((i & 0x8) == 0) ^ ((j & 0x8)  == 0)) * 255;
            image[i][j][0]  = c;
            image[i][j][1]  = c;
            image[i][j][2]  = c;
            image2[i][j][0] = c;
            image2[i][j][1] = 0;
            image2[i][j][2] = c;
        }
    }

    // Initialize texture objects
    glGenTextures( 2, textures );

    glBindTexture( GL_TEXTURE_2D, textures[0] );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TextureSize, TextureSize, 0,
        GL_RGB, GL_UNSIGNED_BYTE, image );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );

    glBindTexture( GL_TEXTURE_2D, textures[1] );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TextureSize, TextureSize, 0,
        GL_RGB, GL_UNSIGNED_BYTE, image2 );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );

    glActiveTexture( GL_TEXTURE0 );
    glBindTexture( GL_TEXTURE_2D, textures[0] );

    // Create a vertex array object
    GLuint vao;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    // Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER,
        sizeof(points) + sizeof(quad_colors) + sizeof(tex_coords),
        NULL, GL_STATIC_DRAW );

    // Specify an offset to keep track of where we're placing data in our
    //   vertex array buffer.  We'll use the same technique when we
    //   associate the offsets with vertex attribute pointers.
    GLintptr offset = 0;
    glBufferSubData( GL_ARRAY_BUFFER, offset, sizeof(points), points );
    offset += sizeof(points);

    glBufferSubData( GL_ARRAY_BUFFER, offset,
        sizeof(quad_colors), quad_colors );
    offset += sizeof(quad_colors);

    glBufferSubData( GL_ARRAY_BUFFER, offset, sizeof(tex_coords), tex_coords );

    // Load shaders and use the resulting shader program
    GLuint program = InitShader( "vshader71.glsl", "fshader71.glsl" );
    glUseProgram( program );

    // set up vertex arrays
    offset = 0;
    GLuint vPosition = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( vPosition );
    glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
        BUFFER_OFFSET(offset) );
    offset += sizeof(points);

    GLuint vColor = glGetAttribLocation( program, "vColor" ); 
    glEnableVertexAttribArray( vColor );
    glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0,
        BUFFER_OFFSET(offset) );
    offset += sizeof(quad_colors);

    GLuint vTexCoord = glGetAttribLocation( program, "vTexCoord" );
    glEnableVertexAttribArray( vTexCoord );
    glVertexAttribPointer( vTexCoord, 2, GL_FLOAT, GL_FALSE, 0,
        BUFFER_OFFSET(offset) );

    // Set the value of the fragment shader texture sampler variable
    //   ("texture") to the the appropriate texture unit. In this case,
    //   zero, for GL_TEXTURE0 which was previously set by calling
    //   glActiveTexture().
    glUniform1i( glGetUniformLocation(program, "texture"), 0 );

    theta = glGetUniformLocation( program, "theta" );

    glEnable( GL_DEPTH_TEST );

    glClearColor( 1.0, 1.0, 1.0, 1.0 );
}

void display( void )
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glUniform3fv( theta, 1, Theta );

    glDrawArrays( GL_TRIANGLES, 0, NumVertices );

    glutSwapBuffers();
}

//----------------------------------------------------------------------------
void mouse( int button, int state, int x, int y )
{
    if ( state == GLUT_DOWN ) {
        switch( button ) {
        case GLUT_LEFT_BUTTON:    Axis = Xaxis;  break;
        case GLUT_MIDDLE_BUTTON:  Axis = Yaxis;  break;
        case GLUT_RIGHT_BUTTON:   Axis = Zaxis;  break;
        }
    }
}

//----------------------------------------------------------------------------
void idle( void )
{
    Theta[Axis] += 0.01;

    if ( Theta[Axis] > 360.0 ) {
        Theta[Axis] -= 360.0;
    }

    glutPostRedisplay();
}

//----------------------------------------------------------------------------
void keyboard( unsigned char key, int mousex, int mousey )
{
    switch( key ) {
    case 033: // Escape Key
    case 'q': case 'Q':
        exit( EXIT_SUCCESS );
        break;
    case '1':
        glBindTexture( GL_TEXTURE_2D, textures[0] );
        break;

    case '2':
        glBindTexture( GL_TEXTURE_2D, textures[1] );
        break;
    }

    glutPostRedisplay();
}

//----------------------------------------------------------------------------
int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize( 512, 512 );
    glutInitContextVersion( 3, 2 );
    glutInitContextProfile( GLUT_CORE_PROFILE );
    glutCreateWindow( "Color Cube" );

    glewInit();

    init();

    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );
    glutMouseFunc( mouse );
    glutIdleFunc( idle );

    glutMainLoop();
    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Rooster
  • 1,267
  • 3
  • 15
  • 23
  • Does the third line happen to be line 147 of example1.cpp? – Marcelo Cantos Nov 28 '11 at 21:34
  • 6
    Sorry; this isn't enough information to even begin working on this problem. How do you [load your OpenGL function pointers](http://www.opengl.org/wiki/Load_OpenGL_Functions)? Do you use a [library](http://www.opengl.org/wiki/OpenGL_Loading_Library) or do you do it manually? If it's a library, did you initialize it? What library is it? If you do it manually, what pointer value did you get back for `glGenVertexArrays`? – Nicol Bolas Nov 28 '11 at 21:37
  • @Marcelo Line 147 is init(), in which that block resides. There's more in init() besides that. – Rooster Nov 28 '11 at 21:40
  • 1
    Post a complete, minimal program that demonstrates the problem. – genpfault Nov 28 '11 at 21:47
  • @NicolBolas I added make output; tell me if this answers your questions. This isn't my code, it's a published example that should be working. So I have limited knowledge of what going on. – Rooster Nov 28 '11 at 21:47
  • 2
    @bbarre: That doesn't change the fact that it's still not enough information to go on. At the very least, you could link to the example. – Nicol Bolas Nov 28 '11 at 21:48
  • What's the output of `glxinfo | egrep "OpenGL (vendor|renderer|version|shading)"`? – genpfault Nov 28 '11 at 21:51
  • 1
    @genpfault The output: OpenGL vendor string: NVIDIA Corporation OpenGL renderer string: GeForce GT 240/PCI/SSE2 OpenGL version string: 3.3.0 NVIDIA 275.28 OpenGL shading language version string: 3.30 NVIDIA via Cg compiler OpenGL extensions: – Rooster Nov 28 '11 at 21:54
  • Ok I'll put the example here. Sorry if it's big, a simpler example I have contains that block but doesn't have the problem. – Rooster Nov 28 '11 at 21:58
  • [This](http://www.cs.unm.edu/~angel/BOOK/INTERACTIVE_COMPUTER_GRAPHICS/SIXTH_EDITION/CODE/include/Angel.h) Angel.h? – genpfault Nov 28 '11 at 22:10
  • Thanks a lot, You saved me from sitting for hours in front of my computer! – MathNerd Sep 01 '15 at 20:03

4 Answers4

134
glewExperimental = GL_TRUE; 
glewInit();

Should do the magic


Experimental Drivers

GLEW obtains information on the supported extensions from the graphics driver. Experimental or pre-release drivers, however, might not report every available extension through the standard mechanism, in which case GLEW will report it unsupported. To circumvent this situation, the glewExperimental global switch can be turned on by setting it to GL_TRUE before calling glewInit(), which ensures that all extensions with valid entry points will be exposed.

KoKuToru
  • 4,055
  • 2
  • 20
  • 21
  • I thank you very much for this, I'll look into the reason this fix works but right I'm just glad I solved my problem ! – lollancf37 Apr 21 '12 at 04:23
  • To clarify: this is an OSX-specific problem, which is why the code worked for genpfault below. – cjcurrie Jan 28 '13 at 01:10
  • 2
    No it's not. Had exactly the same problem under Linux 64-bit and glewExperimental = GL_TRUE solved it. – Plankalkül Mar 02 '13 at 18:44
  • 2
    No it's not. Had exactly the same problem under Windows 7 64-bit and glewExperimental = GL_TRUE solved it. – Korchkidu Jan 14 '14 at 11:51
  • Definitely not just an OSX-specific problem. In 2014 January, I had exactly the same problem under Debian unstable with linux-3.12-1-amd64, libglew-1.10, and the proprietary nvidia-driver-319.82. Setting glewExperimental = GL_TRUE fixed the problem for me. – tevaughan Jan 17 '14 at 18:47
  • Thank you. killed 2 weeks trying to find problem, after last NVIDIA drivers install on ubuntu and got SEGFAULT. Now everything works. – demensdeum Jul 29 '18 at 14:36
1

Works fine for me:

screenshot

GL_VERSION  : 4.1.10750 Compatibility Profile Context
GL_VENDOR   : ATI Technologies Inc.
GL_RENDERER : AMD Radeon HD 6500 Series

EDIT: I'm using the latest versions of FreeGLUT (2.8.0 RC2) and GLEW (1.7.0), which may make a difference if you're relying on distro-supplied versions.

genpfault
  • 51,148
  • 11
  • 85
  • 139
0

Ubuntu 10.04 for example comes with glew 1.50 which glGenVertexArrays doesn't work without the glewExperimental flag. so it is glew version dependent

vallentin
  • 23,478
  • 6
  • 59
  • 81
Nadim Farhat
  • 446
  • 3
  • 11
0

Have you tried testing on other systems with different graphics cards? If your code meets the OpenGL spec and it mysteriously crashes inside a function that is correctly called with valid parameters, it could well be a driver bug. If it's a driver bug, you're reduced to guesswork, making shotgun changes, and gradually building up a healthy exasperation that a huge corporation with billions of dollars produce absolutely crap excuses for a graphics card driver. Good luck!

AshleysBrain
  • 22,335
  • 15
  • 88
  • 124
  • I haven't. But is it even worth trying since I can call the same function just fine in a different program on this same machine? – Rooster Nov 28 '11 at 22:18
  • Yes, it's definitely worth testing, because driver bugs can be totally nonsensical and random. Trust me, I've spent many an hour bashing my head on my desk over OpenGL driver bugs. The standard of drivers is terrible, even nVidia and ATI, and don't get me started on Intel. – AshleysBrain Nov 28 '11 at 22:20
  • Wow. Well so far OpenGL programming has been....fun. And I mean that in the most sarcastic way possible. – Rooster Nov 28 '11 at 22:23