glBufferData()的分段错误 [英] Segmentation fault with glBufferData()
问题描述
我无法弄清楚这段代码为什么会出错:
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);
}
编译时没有警告,但在第一次调用glBufferData()时似乎出现段错误. 如果需要,我可以发布更多代码,因为我对GL不够熟悉,无法知道可能有什么相关之处. 谢谢!
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 };
产生相同的段错误.
推荐答案
您的Vertex类是普通的旧数据类型吗?它是否具有任何虚拟功能,这可能意味着它还具有一个vtable?您是否可以尝试使用普通浮点数数组重写此代码(只是为了测试对glBufferData的调用是否正常).据我所知,看起来您正确使用了glBufferData,但是我可能又错过了一些东西.
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.
在调用此代码之前,您是否完全确定OpenGL上下文已完全初始化.这是一个全局对象,因为它的构造函数可能在main之前被调用?
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?
这篇关于glBufferData()的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!