Segmentation fault with glBufferData()

强颜欢笑 提交于 2019-12-14 01:32:38

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/7054772/segmentation-fault-with-glbufferdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!