glGenBuffers crashing with Segmentation fault [clo

2019-09-21 04:22发布

I can't figure out why it keeps crashing on glGenBuffers

GDB backtrace gives me this

#0  0x00000000 in ?? ()
#1  0x0808701d in setupBuffers () at main_scene.cc:250
#2  0x08088cf8 in main (argc=1, argv=0xbffff3c4) at main_scene.cc:694

Here is my main()

int main(int argc, char **argv) {

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutInitWindowPosition(20, 20);

    glutCreateWindow("New Window");

    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        /* glewInit failed, something is seriously wrong */
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    }
    setupBuffers();

    ...

    /* set callback functions  */
    glutReshapeFunc (reshape);
    glutDisplayFunc (display);
    glutKeyboardFunc(input);
    glutIdleFunc    (idle);

    glutMainLoop();
    return 0;
}

Here is my setupBuffers() method

void setupBuffers() {
    GLenum errorCode = 0;

    // Normal
    size_t normal_buffer_size = 0; // Calc buffer size needed
    for (size_t i = 0; i < shapes.size(); i++) {
        normal_buffer_size += sizeof(float) * shapes[i].mesh.normals.size();
    }

    glGenBuffers(1, &normal_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, normal_buffer);
    glBufferData(GL_ARRAY_BUFFER, normal_buffer_size, NULL, GL_STATIC_DRAW);
    normal_buffer_size = 0; 
    for (size_t i = 0; i < shapes.size(); i++) {
        glBufferSubData(GL_ARRAY_BUFFER, normal_buffer_size, sizeof(float) * shapes[i].mesh.normals.size(), &shapes[i].mesh.normals[0]); // Add the buffer data
        normal_buffer_size += sizeof(float) * shapes[i].mesh.normals.size();
    }
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    ...

    // draw multiple objects with one draw call
    glGenVertexArrays(1, &vertex_array_object);
    glBindVertexArray(vertex_array_object);
    glBindBuffer(GL_ARRAY_BUFFER, normal_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
    glBindVertexArray(0);

    errorCode = glGetError();
    if (errorCode != 0) {
        fprintf(stderr, "Error data: %s, code %d\n", glewGetErrorString(errorCode), errorCode);
    }
}

Heres the global variables I'm using

GLuint vertex_buffer;
GLuint index_buffer;
GLuint normal_buffer;
GLuint texture_buffer;
GLuint vertex_array_object;

Output of glxinfo(in case you need it)

name of display: localhost:11.0
display: localhost:11  screen: 0
direct rendering: No (If you want to find out why, try setting LIBGL_DEBUG=verbose)
server glx vendor string: SGI
server glx version string: 1.4
server glx extensions:
GLX_ARB_multisample, GLX_EXT_import_context, GLX_EXT_visual_info,
GLX_EXT_visual_rating, GLX_MESA_copy_sub_buffer, GLX_OML_swap_method,
GLX_SGI_make_current_read, GLX_SGI_swap_control, GLX_SGIS_multisample,
GLX_SGIX_fbconfig, GLX_SGIX_pbuffer
client glx vendor string: ATI
client glx version string: 1.4
client glx extensions:
GLX_ARB_create_context, GLX_ARB_create_context_profile,
GLX_ARB_get_proc_address, GLX_ARB_multisample, GLX_EXT_import_context,
GLX_EXT_swap_control, GLX_EXT_visual_info, GLX_EXT_visual_rating,
GLX_MESA_allocate_memory, GLX_MESA_copy_sub_buffer, GLX_MESA_swap_control,
GLX_MESA_swap_frame_usage, GLX_NV_swap_group, GLX_OML_swap_method,
GLX_SGI_make_current_read, GLX_SGI_swap_control, GLX_SGI_video_sync,
GLX_SGIS_multisample, GLX_SGIX_fbconfig, GLX_SGIX_pbuffer,
GLX_SGIX_swap_barrier, GLX_SGIX_swap_group, GLX_SGIX_visual_select_group,
GLX_EXT_texture_from_pixmap, GLX_EXT_framebuffer_sRGB,
GLX_ARB_fbconfig_float, GLX_AMD_gpu_association, GLX_EXT_buffer_age
GLX version: 1.4
GLX extensions:
GLX_ARB_get_proc_address, GLX_ARB_multisample, GLX_EXT_import_context,
GLX_EXT_visual_info, GLX_EXT_visual_rating, GLX_MESA_copy_sub_buffer,
GLX_OML_swap_method, GLX_SGI_make_current_read, GLX_SGI_swap_control,
GLX_SGIS_multisample, GLX_SGIX_fbconfig, GLX_SGIX_pbuffer
OpenGL vendor string: ATI Technologies Inc.
OpenGL renderer string: AMD Radeon HD 7800 Series
OpenGL version string: 1.4 (4.4.12967 Compatibility Profile Context 14.200.1004.0)
OpenGL extensions:
GL_ARB_depth_texture, GL_ARB_draw_buffers, GL_ARB_fragment_program,
GL_ARB_fragment_program_shadow, GL_ARB_imaging, GL_ARB_multisample,
GL_ARB_multitexture, GL_ARB_occlusion_query, GL_ARB_point_parameters,
GL_ARB_point_sprite, GL_ARB_shadow, GL_ARB_shadow_ambient,
GL_ARB_texture_border_clamp, GL_ARB_texture_compression,
GL_ARB_texture_cube_map, GL_ARB_texture_env_add,
GL_ARB_texture_env_combine, GL_ARB_texture_env_crossbar,
GL_ARB_texture_env_dot3, GL_ARB_texture_mirrored_repeat,
GL_ARB_texture_non_power_of_two, GL_ARB_texture_rectangle,
GL_ARB_transpose_matrix, GL_ARB_vertex_buffer_object,
GL_ARB_vertex_program, GL_ARB_window_pos, GL_ARB_shader_objects,
GL_ARB_vertex_shader, GL_ARB_fragment_shader, GL_ARB_framebuffer_object,
GL_ARB_geometry_shader4, GL_EXT_abgr, GL_EXT_bgra, GL_EXT_blend_color,
GL_EXT_blend_equation_separate, GL_EXT_blend_func_separate,
GL_EXT_blend_minmax, GL_EXT_blend_subtract, GL_EXT_copy_texture,
GL_EXT_draw_range_elements, GL_EXT_fog_coord, GL_EXT_framebuffer_object,
GL_EXT_multi_draw_arrays, GL_EXT_packed_pixels, GL_EXT_point_parameters,
GL_EXT_rescale_normal, GL_EXT_secondary_color,
GL_EXT_separate_specular_color, GL_EXT_shadow_funcs, GL_EXT_stencil_wrap,
GL_EXT_subtexture, GL_EXT_texture3D, GL_EXT_texture_compression_s3tc,
GL_EXT_texture_edge_clamp, GL_EXT_texture_env_add,
GL_EXT_texture_env_combine, GL_EXT_texture_env_dot3,
GL_EXT_texture_filter_anisotropic, GL_EXT_texture_lod,
GL_EXT_texture_lod_bias, GL_EXT_texture_mirror_clamp,
GL_EXT_texture_object, GL_EXT_texture_rectangle, GL_EXT_vertex_array,
GL_EXT_histogram, GL_EXT_framebuffer_blit, GL_EXT_framebuffer_multisample,
GL_EXT_geometry_shader4, GL_EXT_gpu_shader4, GL_ATI_draw_buffers,
GL_ATI_texture_env_combine3, GL_ATI_texture_float,
GL_ATI_texture_mirror_once, GL_ATIX_texture_env_combine3,
GL_IBM_texture_mirrored_repeat, GL_INGR_blend_func_separate,
GL_SGIS_generate_mipmap, GL_SGIS_texture_border_clamp,
GL_SGIS_texture_edge_clamp, GL_SGIS_texture_lod, GL_SGIX_shadow_ambient,
GL_SUN_multi_draw_arrays, GL_ARB_texture_float

Oh btw I'm using a static glew library.

2条回答
smile是对你的礼貌
2楼-- · 2019-09-21 05:07

You're not properly dealing with the case that GLEW doesn't initialize. After printing a message you just go on with your program, which may run in a crash then.

But worse: You don't check if a particular feature or extension you want to use is actually available. glGenBuffers is actually a function of OpenGL-1-5, while you only get a OpenGL-1.4 context, at least that's what the version string says (which is weird, because apparently your GPU can do OpenGL-4). You must check for extension's availability before using them. And it goes without saying that you can't use functions of a OpenGL version profile that's beyond what the OpenGL implementation gave you for a context. GL_ARB_vertex_buffer_object is supported by your OpenGL implementation, but if you want to use that you must use glGenBuffersARB.

Update I think the drivers on your system are not installed of functioning properly. The server string reports "vendor SGI", which doesn't match your GPU drivers.

查看更多
甜甜的少女心
3楼-- · 2019-09-21 05:24

From you question it is not quite clear which operating system you are using for development, but based on gdb usage I will make a guess that you are using Linux.

Your video card hardware supports OpenGL 4.2 while you are getting OpenGL 1.4 compatibility mode (probably software rendering stack). This usually happens when the video card drivers are not installed (in Linux you may be using open-source driver instead of vendor provided closed-source driver), but it can also happen if you try running your OpenGL program in a remote session. If you are trying to do that, installing VirtualGL might help (http://www.virtualgl.org/).

查看更多
登录 后发表回答