glDrawArrays 导致内存不足
glDrawArrays cause out of memory
我正在使用 VAO 和 VBO 使用 OpenGL 构建 Qt 应用程序。我得到了一个简单的参考网格,我想用下面的代码绘制
void ReferenceGrid::initialize()
{
// Buffer allocation and initialization
Float3Array vertices;
for (float pos = -GridSide; pos <= GridSide; pos += 1.0) {
// X line
vertices.push_back(Float3(pos, -GridSide, 0.0f));
vertices.push_back(Float3(pos, GridSide, 0.0f));
// Y line
vertices.push_back(Float3(-GridSide, pos, 0.0f));
vertices.push_back(Float3( GridSide, pos, 0.0f));
LineCount += 2;
}
s_gridVao.create();
s_gridVao.bind();
s_gridBuffer.create();
s_gridBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
s_gridBuffer.allocate(vertices.data(), vertices.memorySize());
// Shader allocation and initialization
s_gridShader.create();
if (!s_gridShader.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/Grid.vert")) {
qWarning() << "Cannot grid vertex shader";
}
if (!s_gridShader.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/Grid.frag")) {
qWarning() << "Cannot grid fragment shader";
}
if (!s_gridShader.link()) {
qWarning() << "Cannot link grid shader";
}
s_gridBuffer.bind();
s_gridShader.enableAttributeArray("vertexPosition");
s_gridShader.setAttributeBuffer("vertexPosition", GL_FLOAT, 0, 3);
s_gridBuffer.release();
s_gridShader.release();
s_gridVao.release();
}
void ReferenceGrid::draw()
{
s_gridVao.bind();
s_gridShader.bind();
s_gridBuffer.bind();
glfuncs->glDrawArrays(GL_LINES, 0, LineCount);
// Return GL_OUT_OF_MEMORY
assert(glfuncs->glGetError() == GL_NO_ERROR);
s_gridBuffer.release();
s_gridShader.release();
s_gridVao.release();
}
问题是在调用 glDrawArrays 后返回错误 (GL_OUT_OF_MEMORY)。我不明白这是怎么回事。
有没有人遇到过这个问题并且有解决办法?
我忘记在分配之前绑定缓冲区。我以为 Qt 会自动执行此操作,但我错了。所以正确的做法是:
s_gridBuffer.create();
s_gridBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
s_gridBuffer.bind();
s_gridBuffer.allocate(vertices.data(), vertices.memorySize());
我找到了关于该主题的很好的教程,http://www.kdab.com/opengl-in-qt-5-1-part-2/
我正在使用 VAO 和 VBO 使用 OpenGL 构建 Qt 应用程序。我得到了一个简单的参考网格,我想用下面的代码绘制
void ReferenceGrid::initialize()
{
// Buffer allocation and initialization
Float3Array vertices;
for (float pos = -GridSide; pos <= GridSide; pos += 1.0) {
// X line
vertices.push_back(Float3(pos, -GridSide, 0.0f));
vertices.push_back(Float3(pos, GridSide, 0.0f));
// Y line
vertices.push_back(Float3(-GridSide, pos, 0.0f));
vertices.push_back(Float3( GridSide, pos, 0.0f));
LineCount += 2;
}
s_gridVao.create();
s_gridVao.bind();
s_gridBuffer.create();
s_gridBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
s_gridBuffer.allocate(vertices.data(), vertices.memorySize());
// Shader allocation and initialization
s_gridShader.create();
if (!s_gridShader.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/Grid.vert")) {
qWarning() << "Cannot grid vertex shader";
}
if (!s_gridShader.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/Grid.frag")) {
qWarning() << "Cannot grid fragment shader";
}
if (!s_gridShader.link()) {
qWarning() << "Cannot link grid shader";
}
s_gridBuffer.bind();
s_gridShader.enableAttributeArray("vertexPosition");
s_gridShader.setAttributeBuffer("vertexPosition", GL_FLOAT, 0, 3);
s_gridBuffer.release();
s_gridShader.release();
s_gridVao.release();
}
void ReferenceGrid::draw()
{
s_gridVao.bind();
s_gridShader.bind();
s_gridBuffer.bind();
glfuncs->glDrawArrays(GL_LINES, 0, LineCount);
// Return GL_OUT_OF_MEMORY
assert(glfuncs->glGetError() == GL_NO_ERROR);
s_gridBuffer.release();
s_gridShader.release();
s_gridVao.release();
}
问题是在调用 glDrawArrays 后返回错误 (GL_OUT_OF_MEMORY)。我不明白这是怎么回事。
有没有人遇到过这个问题并且有解决办法?
我忘记在分配之前绑定缓冲区。我以为 Qt 会自动执行此操作,但我错了。所以正确的做法是:
s_gridBuffer.create();
s_gridBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
s_gridBuffer.bind();
s_gridBuffer.allocate(vertices.data(), vertices.memorySize());
我找到了关于该主题的很好的教程,http://www.kdab.com/opengl-in-qt-5-1-part-2/