我正在尝试在 Qt 5 中使用实例化渲染,但遇到了一些麻烦。我想我的问题可以追溯到我对 VAO 的使用。我是一位经验丰富的程序员,知道这些问题几乎总是我的错,但我偶尔会遇到 Qt 错误,所以我想在提交错误报告之前我可以使用一些帮助。
我整理了一个简单的例子,它不使用实例化(出于某种原因,我无法请求最新版本的 OpenGL,即使我在我正在开发的“真实”软件中没有这个问题) .
在初始化时,我创建了一个 VAO 和两个 VBO。一个 VBO 填充了一些数据,并保持绑定;另一个已分配,但已释放(我从未真正使用它:我计划最终将矩阵写入它以进行实例化,但没有走得那么远)。
我添加了一些预处理器指令来简化打开或关闭 VAO 或第二个 VBO 的使用。如果我同时使用 VAO 和 VBO,则会出现分段错误。如果我摆脱了第二个 VBO 或 VAO,则没有 seg 错误并且绘图看起来正确(它应该是在彩色三角形后面绘制的白色三角形)。
这是代码:
glbuffertest.cpp:
######################################################################
# Automatically generated by qmake (3.0) Fri May 16 09:49:41 2014
######################################################################
TEMPLATE = app
TARGET = glbuffertest
INCLUDEPATH += .
CONFIG += qt debug
DEFINES += USE_VAO
DEFINES += USE_MATBO
# Input
SOURCES += glbuffertest.cpp glwindow.cpp
HEADERS += glwindow.h
glbuffertest.cpp:
#include <QGuiApplication>
#include <QSurfaceFormat>
#include "glwindow.h"
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
GLWindow window;
window.resize(400, 400);
window.show();
return app.exec();
}
glwindow.cpp:
#include "glwindow.h"
#include <QColor>
#include <QMatrix4x4>
#include <QVector>
#include <QVector3D>
#include <QVector4D>
#include <QDebug>
GLWindow::GLWindow(QWindow *parent)
: QWindow(parent)
, _vbo(QOpenGLBuffer::VertexBuffer)
, _matbo(QOpenGLBuffer::VertexBuffer)
, _context(0)
{
setSurfaceType(QWindow::OpenGLSurface);
create();
}
GLWindow::~GLWindow()
{}
void GLWindow::initGL()
{
if (_context) // already initialized
return;
_context = new QOpenGLContext(this);
QSurfaceFormat format(requestedFormat());
format.setDepthBufferSize(24);
_context->setFormat(format);
_context->create();
_context->makeCurrent(this);
setupShaders();
_program->bind();
_positionAttr = _program->attributeLocation("position");
_colourAttr = _program->attributeLocation("colour");
_matrixUnif = _program->uniformLocation("matrix");
_program->release();
initializeOpenGLFunctions();
QVector<QVector3D> triangles;
triangles << QVector3D(-0.5, 0.5, 1) << QVector3D(-0.5, -0.5, 1) << QVector3D(0.5, -0.5, 1);
triangles << QVector3D(0.5, 0.5, 0.5) << QVector3D(-0.5, -0.5, 0.5) << QVector3D(0.5, -0.5, 0.5);
QVector<QVector3D> colours;
colours << QVector3D(1, 0, 0) << QVector3D(0, 1, 0) << QVector3D(0, 0, 1);
colours << QVector3D(1, 1, 1) << QVector3D(1, 1, 1) << QVector3D(1, 1, 1);
#ifdef USE_VAO
_vao.create();
_vao.bind();
#endif
_vbo.create();
_vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
_vbo.bind();
size_t positionSize = triangles.size() * sizeof(QVector3D);
size_t colourSize = colours.size() * sizeof(QVector3D);
_vbo.allocate(positionSize + colourSize);
_vbo.write(0, triangles.constData(), positionSize);
_vbo.write(positionSize, colours.constData(), colourSize);
_colourOffset = positionSize;
#ifdef USE_MATBO
_matbo.create();
_matbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
_matbo.bind();
_matbo.allocate(4 * sizeof(QVector4D));
qDebug() << "matrix buffer size:" << _matbo.size() << "requested:" << 4 * sizeof(QVector4D);
_matbo.release();
#endif
#ifdef USE_VAO
glVertexAttribPointer(_positionAttr, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(_colourAttr, 3, GL_FLOAT, GL_FALSE, 0, (void*)(_colourOffset));
glEnableVertexAttribArray(_positionAttr);
glEnableVertexAttribArray(_colourAttr);
_vao.release();
#else
_vbo.release();
#endif
resizeGL(width(), height());
}
void GLWindow::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void GLWindow::paintGL()
{
if (! _context) // not yet initialized
return;
_context->makeCurrent(this);
QColor background(Qt::black);
glClearColor(background.redF(), background.greenF(), background.blueF(), 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 matrix;
matrix.perspective(60, 4.0/3.0, 0.1, 100.0);
matrix.translate(0, 0, -2);
_program->bind();
_program->setUniformValue(_matrixUnif, matrix);
#ifdef USE_VAO
_vao.bind();
#else
_vbo.bind();
glVertexAttribPointer(_positionAttr, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(_colourAttr, 3, GL_FLOAT, GL_FALSE, 0, (void*)(_colourOffset));
glEnableVertexAttribArray(_positionAttr);
glEnableVertexAttribArray(_colourAttr);
#endif
glEnable(GL_DEPTH_TEST);
glDrawArrays(GL_TRIANGLES, 0, 6);
#ifdef USE_VAO
_vao.release();
#else
glDisableVertexAttribArray(_positionAttr);
glDisableVertexAttribArray(_colourAttr);
_vbo.release();
#endif
_program->release();
_context->swapBuffers(this);
_context->doneCurrent();
}
static const char *vertexShaderSource =
"attribute highp vec4 position;\n"
"attribute lowp vec4 colour;\n"
"uniform highp mat4 matrix;\n"
"varying lowp vec4 col;\n"
"void main() {\n"
" col = colour;\n"
" gl_Position = matrix * position;\n"
"}\n";
static const char *fragmentShaderSource =
"varying lowp vec4 col;\n"
"void main() {\n"
" gl_FragColor = col;\n"
"}\n";
void GLWindow::setupShaders()
{
_program = new QOpenGLShaderProgram(this);
_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
_program->link();
}
void GLWindow::exposeEvent(QExposeEvent *event)
{
Q_UNUSED(event);
if (isExposed())
{
if (! _context)
initGL();
paintGL();
}
}
glwindow.h:
#ifndef GL_WINDOW_H
#define GL_WINDOW_H
#include <QExposeEvent>
#include <QSurfaceFormat>
#include <QWindow>
#include <QOpenGLBuffer>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
class GLWindow : public QWindow, protected QOpenGLFunctions
{
Q_OBJECT
public:
GLWindow(QWindow * = 0);
virtual ~GLWindow();
void initGL();
void paintGL();
void resizeGL(int, int);
protected:
virtual void exposeEvent(QExposeEvent *);
private:
void setupShaders();
QOpenGLBuffer _vbo;
QOpenGLBuffer _matbo;
QOpenGLContext *_context;
QOpenGLShaderProgram *_program;
QOpenGLVertexArrayObject _vao;
GLuint _positionAttr;
GLuint _colourAttr;
GLuint _matrixUnif;
size_t _colourOffset;
} ;
#endif
谁能发现我在做什么愚蠢的事情?