0

I've begun doing a bit of OpenGL, and I've just discovered alternatives to the fixed function pipeline, so I went searching tutorials. Sadly, most good tutorials use C++, and I would like to stick to C if possible. So I wasn't able to just copy-paste stuff and try it.

Copy-pasting and merging code gave errors though, and I'm not sure how I should deal with them. The error in question was "Segmentation fault", and here is the code:

#include <GL/glfw.h>
#include <GL/glu.h>
#include <stdlib.h>

#define SCREEN_WIDTH 1600
#define SCREEN_HEIGHT 1000

void main(int argc, char **argv)
{
    float mouse_x, mouse_y;

    GLuint positionBufferObject;
    const float vertexPositions[] = {
    0.75f, 0.75f, 0.0f, 1.0f,
    0.75f, -0.75f, 0.0f, 1.0f,
    -0.75f, -0.75f, 0.0f, 1.0f,
};
    glGenBuffers(1, &positionBufferObject);

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    int running = GL_TRUE;
    // Initialize GLFW
    if( !glfwInit() )
    {
        exit( EXIT_FAILURE );
    }

    // Open an OpenGL window
    if( !glfwOpenWindow(SCREEN_WIDTH,SCREEN_HEIGHT, 8,8,8,8,8,8, GLFW_WINDOW ) )
    {
        glfwTerminate();
        exit( EXIT_FAILURE );
    }

    while (running)
    {
        glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

        glDrawArrays(GL_TRIANGLES, 0, 3);
    }
}

Of course, a lot of code is missing here, because it (apparently) didn't influence the error. Commenting out "glDrawArrays" fixes the error, and OpenGL runs normally (without drawing anything, logically).

Any help?

1 Answer 1

4

You should initialise gl before you start binding buffers and so on. The initialisation process fetches and assigns the pointers to these functions. Otherwise they're null.

Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't glfwInit initialize OpenGL? I tried putting that before. On this short code, it subsequently crashed my computer, and on the full code, still ended with a segmentation fault.
Well, apparently glfwInit is what you need to do first, before other stuff, so that at least is wrong in the code sample you've given here. Strange that you say removing DrawArrays fixes the error when there are quite a few other functions called before that that shouldn't work!
@Orpheon: No, glfwInit doesn't initialize OpenGL. It initializes GLFW. Before you can use OpenGL, you must initialize GLFW and open a window.
Possibly. I'm not sure what glfwinit actually is. I do know that you need to initialise GL before trying to give it commands, however :p.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.