在QOpenGLWidget上启用抗锯齿功能

时间:2016-08-09 20:04:04

标签: c++ qt opengl

我正在尝试在QOpenGLWidget上启用antialising,但它无法在我的计算机上运行。我已经阅读了this,但它并没有帮助我达到预期的抗锯齿结果。我使用this旧教程编写代码。但是,我使用的是QOpenGLWidget而不是QGLWidget。 这是我班级的头文件:

#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QTimer>
#include <QMouseEvent>
#include <QWheelEvent>

class MyOpenGLWidget  : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    MyOpenGLWidget(QWidget *parent = 0);
    ~MyOpenGLWidget();
protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);
private:
    QOpenGLTexture *textures[6];
    QOpenGLShaderProgram *lightingProgram;
    QOpenGLShaderProgram *coloringProgram;
    QMatrix4x4 pMatrix;
    void makeObject();
    int numCubeVertices;
    QOpenGLBuffer cubeBuffer;
    GLuint cubeTexture;
    int numSpotlightVertices;
    QOpenGLBuffer spotlightBuffer;
    double lightAngle;
    double alpha;
    double beta;
    double distance;
    QPoint lastMousePosition;
private slots:
    void timeout();
};
#endif // MYOPENGLWIDGET_H

这是我的cpp文件的一部分。

#include "myopenglwidget.h"

MyOpenGLWidget::MyOpenGLWidget(QWidget *parent)
    : QOpenGLWidget(parent),
      lightingProgram(0),
      coloringProgram(0)
{
    QSurfaceFormat format;
    format.setSamples(8);    // Set the number of samples used for multisampling
    setFormat(format);

    memset(textures, 0, sizeof(textures));
    alpha = 25;
    beta = -25;
    distance = 2.5;
    lightAngle = 0;

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
    timer->start(20);
}

MyOpenGLWidget::~MyOpenGLWidget()
{}

void MyOpenGLWidget::initializeGL()
{
    initializeOpenGLFunctions();
    makeObject();

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glEnable(GL_MULTISAMPLE);

    QOpenGLShader *vshaderLighting = new QOpenGLShader(QOpenGLShader::Vertex, this);
    vshaderLighting->compileSourceFile(QString(":/shaders/lightingVertexShader.vsh"));
    QOpenGLShader *fshaderLighting = new QOpenGLShader(QOpenGLShader::Fragment, this);
    fshaderLighting->compileSourceFile(QString(":/shaders/lightingFragmentShader.fsh"));

    lightingProgram = new QOpenGLShaderProgram;
    lightingProgram->addShader(vshaderLighting);
    lightingProgram->addShader(fshaderLighting);
    lightingProgram->link();
    lightingProgram->bind();

    QOpenGLShader *vshaderColoring = new QOpenGLShader(QOpenGLShader::Vertex, this);
    vshaderColoring->compileSourceFile(QString(":/shaders/coloringVertexShader.vsh"));
    QOpenGLShader *fshaderColoring = new QOpenGLShader(QOpenGLShader::Fragment, this);
    fshaderColoring->compileSourceFile(QString(":/shaders/coloringFragmentShader.fsh"));
    coloringProgram = new QOpenGLShaderProgram;
    coloringProgram->addShader(vshaderColoring);
    coloringProgram->addShader(fshaderColoring);
    coloringProgram->link();
    coloringProgram->bind();
}

void MyOpenGLWidget::paintGL()
{
    glClearColor(0,0,127,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QMatrix4x4 mMatrix;
    QMatrix4x4 vMatrix;

    QMatrix4x4 cameraTransformation;
    cameraTransformation.rotate(alpha, 0, 1, 0);
    cameraTransformation.rotate(beta, 1, 0, 0);

    QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
    QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);

    vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);

    mMatrix.setToIdentity();

    QMatrix4x4 mvMatrix;
    mvMatrix = vMatrix * mMatrix;

    QMatrix3x3 normalMatrix;
    normalMatrix = mvMatrix.normalMatrix();

    QMatrix4x4 lightTransformation;
    lightTransformation.rotate(lightAngle, 0, 1, 0);

    QVector3D lightPosition = lightTransformation * QVector3D(0, 1, 1);

    lightingProgram->bind();

    lightingProgram->setUniformValue("mvpMatrix", pMatrix * mvMatrix);
    lightingProgram->setUniformValue("mvMatrix", mvMatrix);
    lightingProgram->setUniformValue("normalMatrix", normalMatrix);
    lightingProgram->setUniformValue("lightPosition", vMatrix * lightPosition);
    lightingProgram->setUniformValue("ambientColor", QColor(32, 32, 32));
    lightingProgram->setUniformValue("diffuseColor", QColor(128, 128, 128));
    lightingProgram->setUniformValue("specularColor", QColor(255, 255, 255));
    lightingProgram->setUniformValue("ambientReflection", (GLfloat) 1.0);
    lightingProgram->setUniformValue("diffuseReflection", (GLfloat) 1.0);
    lightingProgram->setUniformValue("specularReflection", (GLfloat) 1.0);
    lightingProgram->setUniformValue("shininess", (GLfloat) 100.0);
    lightingProgram->setUniformValue("texture", 0);

    textures[0]->bind();

    cubeBuffer.bind();

    int offset = 0;
    lightingProgram->enableAttributeArray("vertex");
    lightingProgram->setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0);

    offset += numCubeVertices * 3 * sizeof(GLfloat);
    lightingProgram->enableAttributeArray("normal");
    lightingProgram->setAttributeBuffer("normal", GL_FLOAT, offset, 3, 0);

    offset += numCubeVertices * 3 * sizeof(GLfloat);
    lightingProgram->setAttributeBuffer("textureCoordinate", GL_FLOAT, offset, 2, 0);
    lightingProgram->enableAttributeArray("textureCoordinate");

    cubeBuffer.release();

    glDrawArrays(GL_TRIANGLES, 0, numCubeVertices);

    lightingProgram->disableAttributeArray("vertex");
    lightingProgram->disableAttributeArray("normal");
    lightingProgram->disableAttributeArray("textureCoordinate");
    lightingProgram->release();

    mMatrix.setToIdentity();
    mMatrix.translate(lightPosition);
    mMatrix.rotate(lightAngle, 0, 1, 0);
    mMatrix.rotate(45, 1, 0, 0);
    mMatrix.scale(0.1);

    coloringProgram->bind();
    coloringProgram->setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);

    spotlightBuffer.bind();
    offset = 0;
    coloringProgram->setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0);
    coloringProgram->enableAttributeArray("vertex");
    offset += numSpotlightVertices * 3 * sizeof(GLfloat);
    coloringProgram->setAttributeBuffer("color", GL_FLOAT, offset, 3, 0);
    coloringProgram->enableAttributeArray("color");
    spotlightBuffer.release();

    glDrawArrays(GL_TRIANGLES, 0, numSpotlightVertices);

    coloringProgram->disableAttributeArray("vertex");
    coloringProgram->disableAttributeArray("color");
    coloringProgram->release();
}

我的代码出了什么问题?

0 个答案:

没有答案