3

I can't work out why this code is seg faulting:

AxesMarker::AxesMarker(float size)
    : size_(size), vbo_vertices_(0), vbo_elements_(0)
{
  Vertex vertices[6] = { 
      Vertex(Color4f::RED, Vector3f::ZERO, Vector3f::ZERO),
      Vertex(Color4f::RED, Vector3f::ZERO, Vector3f(size_, 0.0f, 0.0f)),
      Vertex(Color4f::BLUE, Vector3f::ZERO, Vector3f::ZERO),
      Vertex(Color4f::BLUE, Vector3f::ZERO, Vector3f(0.0f, size_, 0.0f)),
      Vertex(Color4f::GREEN, Vector3f::ZERO, Vector3f::ZERO),
      Vertex(Color4f::GREEN, Vector3f::ZERO, Vector3f(0.0f, size_, 0.0f)) };

  GLuint elements[6] = { 0, 1, 2, 3, 4, 5 };

  fprintf(stderr, "sizeof(vertices): %d, sizeof(Vertex): %d", (int) sizeof(vertices), (int) sizeof(Vertex));

  /* create buffers */
  glGenBuffers(1, &vbo_vertices_);
  glGenBuffers(1, &vbo_elements_);

  /* bind buffers */
  glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices_);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_elements_);

  /* buffer data */
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

  /* unbind buffers */
  glBindBuffer(GL_ARRAY_BUFFER, 0); 
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 
}

Compiles with no warnings, but appears to be seg faulting on the first call to glBufferData(). I can post more code if necessary, I'm not familiar enough with GL to know what might be relevant. Thanks!


  GLfloat vertices[60] = { 
     1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
     1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
     0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
     0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
     0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
     0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };

yields same seg fault.

Rhys van der Waerden
  • 3,526
  • 2
  • 27
  • 32

1 Answers1

5

Is your Vertex class a plain old data type? Does it have any virtual functions which might mean it also has a vtable? Can you try re-writing this code using an array of plain floats,(just to test your calls to glBufferData are working). From what I can tell though, it looks like you are using glBufferData correctly, but then again I might have missed something.

EDIT: Did you make absolutely sure that your OpenGL context is fully initialised before you call this code. Is this a global object, because it's constructor might be called before main?

Darcy Rayner
  • 3,385
  • 1
  • 23
  • 15