0

我的目标是用 QOpenGLBuffers(索引和顶点)渲染一个正方形。但是在查看了所有的导师和文档之后,我错过了一些东西。现在它什么也不渲染。

这是这个缓冲区的 init():

initializeOpenGLFunctions();

glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);

m_vao.create();
m_vao.bind();

size_t size = 12; // test value. 5 points * 3 coordinates
m_primitives = new GLfloat[size];

m_primitives[0]  = 0.0f;
m_primitives[1]  = 0.0f;
m_primitives[2]  = 0.0f;
m_primitives[3]  = 0.5f;
m_primitives[4]  = 0.5f;
m_primitives[5]  = 0.0f;
m_primitives[6]  = 0.0f;
m_primitives[7]  = 0.5f;
m_primitives[8]  = 0.0f;
m_primitives[9]  = 0.5f;
m_primitives[10] = 0.0f;
m_primitives[11] = 0.0f;

m_indices = new GLint[6];
m_indices[0] = 0;
m_indices[1] = 1;
m_indices[2] = 2;
m_indices[3] = 0;
m_indices[4] = 3;
m_indices[5] = 1;

m_vertexBuffer = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
m_vertexBuffer->create();
m_vertexBuffer->bind();
m_vertexBuffer->allocate(m_primitives, 12 * sizeof(GLfloat));
m_vertexBuffer->setUsagePattern(QOpenGLBuffer::StreamDraw);

m_indexBuffer = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
m_indexBuffer->create();
m_indexBuffer->bind();
m_indexBuffer->setUsagePattern(QOpenGLBuffer::StaticDraw);
m_indexBuffer->allocate(m_indices, 6 * sizeof(GLint));

m_program = new QOpenGLShaderProgram();
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/LabelShader.vs");
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/LabelShader.fs");

m_program->link();

QImage i = QGLWidget::convertToGLFormat( m_fontTexture );
m_pijeltexture = new QOpenGLTexture(m_fontTexture, QOpenGLTexture::GenerateMipMaps);
m_pijeltexture->setWrapMode(QOpenGLTexture::ClampToEdge);

这是 render() 方法:

 m_program->bind();
m_vao.bind();
glActiveTexture(GL_TEXTURE0);
m_pijeltexture->bind(0);
m_vertexBuffer->bind();
m_indexBuffer->bind();

int vertexLocation =  m_program->attributeLocation("Vertex");
m_program->enableAttributeArray(vertexLocation);
m_program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3);

glDrawElements(GL_TRIANGLES, 6, GL_INT, 0);

m_vao.release();
m_vertexBuffer->release();
m_indexBuffer->release();
m_program->disableAttributeArray(0);
m_program->release();

Mybe有人遇到过这样的问题吗?

4

1 回答 1

0

感谢帮助。使用 glGetError 后,我发现我使用了错误的枚举。我错过了索引可能只有 3 种类型:GL_UNSIGNED_BYTE、GL_UNSIGNED_SHORT 或 GL_UNSIGNED_INT。

所以问题出在这一行: glDrawElements(GL_TRIANGLES, 6, GL_INT, 0);

我改为:glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
现在一切都很好。

于 2017-04-13T12:26:33.343 回答