1

I am trying to enable antialising on QOpenGLWidget, but it is not working on my computer. I've read this, but it didn't help me to achieve the expected antialias result. I used this old tutorial for writing my code. However, I used QOpenGLWidget instead of QGLWidget. Here is the header file of my class:

#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

Here is part of my cpp file.

#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();
}

What is wrong with my code?

Community
  • 1
  • 1
Leonardo
  • 76
  • 11
  • Maybe also specify depth and stencil in your format? Run into `apitrace` and check the FBO creation? – peppe Aug 09 '16 at 20:35
  • How do you know it's not working? what are the symptoms? Have you checked that the driver isn't overriding the multisampling setting? – 1stCLord Aug 09 '16 at 20:53
  • @peppe how can I specify depth and stencil? Do you have some snippet? – Leonardo Aug 09 '16 at 23:52
  • @TheSombreroKid The textures are looking awkward. The lines in the textures are smooth. This is the symptom that bothers me. Also, how do I check if the driver isn't overriding the multisampling setting? – Leonardo Aug 09 '16 at 23:56
  • 2
    MSAA doesn't affect sampling textures (unless those textures are a MSAA render buffer). It's possible on an nvidia card that FXAA is forced on in the NVIDIA Control Panel, under Manage 3D Settings -> Antialiasing - FXAA – 1stCLord Aug 10 '16 at 00:11
  • @Leonardo: `format.setDepthBufferSize(24)`, `setStencilBufferSize(8)` – peppe Aug 10 '16 at 05:59
  • @TheSombreroKid Thanks! I think you are right. I will look for mipmapping. – Leonardo Aug 10 '16 at 13:59
  • 1
    @TheSombreroKid: MSAA still won't affect texture sampling in that scenario. You can only fetch multi-sampled textures without filtering. The AA part of MSAA is a filter. You have to do the filter (resolve) yourself by doing multiple fetches, or blit from a multi-sampled rendertarget into a single-sampled one. – Andon M. Coleman Aug 10 '16 at 14:37

0 Answers0