Cannot get even a simple hello world triangle program running

EDIT2: Solved, solution at end of post.

So I’ve been stuck for the last couple of days trying to run a hello world program.

The code will compile without any errors however when it is run I get: segmentation fault (core dumped) ./a.out

I was initially following a tutorial using shaders (arcsynthesis tutorial) but switched to a more simple example just trying to get something to work. Running the code through Code::Blocks works so I’m guessing this isn’t an environment issue.

I’ve also tried gdb debugging however I’m still not that experienced with using it.

If I comment all the buffer object initialization + usage in the display function the window initializes and does not crash immediately.

I’ve included my code below, any help would be appreciated if anything I’m doing wrong jumps out as obvious.


#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>

void display();
void reshape(int, int);
void keyboard(unsigned char, int, int);

GLuint vertexBuffer;
GLuint VertexArrayID;

static const GLfloat 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,
};

void init()
{
  glGenVertexArrays(1, &VertexArrayID);
  glBindVertexArray(VertexArrayID);

  glGenBuffers(1, &vertexBuffer);
  glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
}

void display()
{
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT);

  glEnableVertexAttribArray(0);
  glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

  glDrawArrays(GL_TRIANGLES, 0, 3);

  glDisableVertexAttribArray(0);

  glutSwapBuffers();
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH | GLUT_STENCIL);
  glutInitContextVersion(3, 3);
  glutInitContextProfile(GLUT_CORE_PROFILE);

  glutInitWindowSize(500, 500);
  glutInitWindowPosition(300, 200);

  glutCreateWindow("triangle");

  init();

  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyboard);
  glutMainLoop();
  return 0;
}

void reshape(int w, int h)
{
  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
}

void keyboard(unsigned char key, int x, int y)
{
  switch (key)
  {
    case 27:
      glutLeaveMainLoop();
      return;
  }
}

Also, if it helps I compile the code with the following command:


g++ main.cpp -lGLEW -lglut -lGL -lGLU

Thanks!

EDIT: Thought I would add some more info. By following other examples of debugging errors in general and specifically segmentation errors using gdb I had no success. The result of running the program and backtrace is…


0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x0000000000400e64 in init () at main2.cpp:29
#2  0x0000000000400fc4 in main (argc=1, argv=0x7fffffffe258) at main2.cpp:66

… so it seems to be failing when generating the vao?

Also, my os is Linux if it helps.

EDIT2: Turns out I needed these two lines, so perhaps it was an environment issue after all? Any explanation could be cool.


glewExperimental = GL_TRUE;
glewInit();

Would post SO link though it seems I am not allowed.