【问题标题】:glDrawArrays cause out of memoryglDrawArrays 导致内存不足
【发布时间】:2015-03-24 11:33:11
【问题描述】:

我正在使用 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)。我无法理解发生了什么。

有没有人遇到过这个问题并有解决方案?

【问题讨论】:

标签: c++ qt opengl vbo vao


【解决方案1】:

我忘记在分配缓冲区之前绑定它。我以为 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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2013-08-02
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多