0
\$\begingroup\$

I am trying to run an example from the redbook 8th edition and I am receiving a segmentation fault in line:

glCreateBuffers( NumBuffers, Buffers );

file.cpp

//////////////////////////////////////////////////////////////////////////////
//
//  Triangles.cpp
//
//////////////////////////////////////////////////////////////////////////////

#include "vgl.h"
#include "LoadShaders.h"

enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };

GLuint  VAOs[NumVAOs];
GLuint  Buffers[NumBuffers];

const GLuint  NumVertices = 6;

//----------------------------------------------------------------------------
//
// init
//

void
init( void )
{
    glGenVertexArrays( NumVAOs, VAOs );
    glBindVertexArray( VAOs[Triangles] );

    GLfloat  vertices[NumVertices][2] = {
        { -0.90f, -0.90f }, {  0.85f, -0.90f }, { -0.90f,  0.85f },  // Triangle 1
        {  0.90f, -0.85f }, {  0.90f,  0.90f }, { -0.85f,  0.90f }   // Triangle 2
    };

    glCreateBuffers( NumBuffers, Buffers );
    glBindBuffer( GL_ARRAY_BUFFER, Buffers[ArrayBuffer] );
    glBufferStorage( GL_ARRAY_BUFFER, sizeof(vertices), vertices, 0);

    ShaderInfo  shaders[] =
    {
        { GL_VERTEX_SHADER, "triangles.vert" },
        { GL_FRAGMENT_SHADER, "triangles.frag" },
        { GL_NONE, NULL }
    };

    GLuint program = LoadShaders( shaders );
    glUseProgram( program );

    glVertexAttribPointer( vPosition, 2, GL_FLOAT,
                           GL_FALSE, 0, BUFFER_OFFSET(0) );
    glEnableVertexAttribArray( vPosition );
}

//----------------------------------------------------------------------------
//
// display
//

void
display( void )
{
    static const float black[] = { 0.0f, 0.0f, 0.0f, 0.0f };

    glClearBufferfv(GL_COLOR, 0, black);

    glBindVertexArray( VAOs[Triangles] );
    glDrawArrays( GL_TRIANGLES, 0, NumVertices );
}

//----------------------------------------------------------------------------
//
// main
//


int main( int argc, char** argv )
{
    if ( !glfwInit() )
    {
        exit(EXIT_FAILURE);
    }

    //glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    GLFWwindow* window = glfwCreateWindow(300, 300, "Triangles", NULL, NULL);
    if ( !window )
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwMakeContextCurrent(window);
    glewInit();

    init();

    while (!glfwWindowShouldClose(window))
    {
        display();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
}

I am compiling :

g++ -O2 -g -o run opengl.cpp LoadShaders.cpp -lGL -lGLU -lGLEW -lglut -lglfw -lX11 -lm

\$\endgroup\$
  • \$\begingroup\$ Does your LoadShaders check if the shader compilation and linking fails? \$\endgroup\$ – SurvivalMachine Oct 21 '16 at 14:40
1
\$\begingroup\$

glCreateBuffers is an OpenGL 4.5 function but you are using OpenGL 2.0 context, you need to use glGenBuffers instead.

\$\endgroup\$
  • \$\begingroup\$ Hmm..I didn't know it!Thanks! It works without seg fault.But it shows me a black window even though I am loading GL_VERTEX_SHADER, and FRAGMENT_SHADER. \$\endgroup\$ – George Oct 21 '16 at 14:36
  • \$\begingroup\$ glCreateBuffers is also intended to be used with the DSA API so if you're using it you definitely should not be using glBindBuffer or glBufferStorage. \$\endgroup\$ – Maximus Minimus Oct 21 '16 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.