Adam Van Oijen
Adam Van Oijen

Reputation: 545

opengl segmentation fault on initializing

This is a pretty nasty question, but I am getting a segmentation fault every time the program tries to run 'init()' and I have no idea why. The libraries i am using are: glew, glu, glfw3, and a shader loader from here http://www.opengl-redbook.com/.

#include <GL/glew.h>
#include <GL/glu.h>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include "LoadShaders.h"
#include <iostream>
//g++ -c main.cpp -l glfw -l GLEW -l GL -l GLU -c LoadShaders.cpp 
//g++ -o run main.o LoadShaders.o -l GLEW -l glfw -l GL -l GLU

GLuint VAOs[1];
GLuint Buffers[1];
enum Attrib_IDs {vPosition = 0};
void init (void){

    glGenVertexArrays(1, VAOs);
    glBindVertexArray(VAOs[0]);

    GLfloat vertices[6][2] ={
        {-0.90, -0.90},
        {0.85, -0.90},
        {-0.90, 0.85},
        {0.90, -0.85},
        {0.90, 0.90},
        {-0.85, 0.90}
    };

    glGenBuffers(1, Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, Buffers[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

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

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

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

int main(){
    GLFWwindow* window;

    /*initialize the glfw library*/
    if (!glfwInit())
    {
        return -1;
    }

    window = glfwCreateWindow(640, 480, "Hello world!", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    init();
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {

        glClear(GL_COLOR_BUFFER_BIT);

        glBindVertexArray(VAOs[0]);
        glDrawArrays(GL_TRIANGLES, 0, 6);

        glFlush();
        /* Swap front and back buffers */
        glfwSwapBuffers(window);
        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;

}

Upvotes: 0

Views: 971

Answers (1)

Adam Van Oijen
Adam Van Oijen

Reputation: 545

turns out I only forgot to initialize glew, using glewInit(). Everything works well after doing that

Upvotes: 1

Related Questions