jeudi 1 octobre 2020

Painter not active on inherited QOpenGLWidget class

I would like to draw very efficiently basic 2D shapes (squares) in Qt, hence I inherited QOpenGLWidget as the following:

WatorOpenGLWidget.hpp

#include <QOpenGLWidget>
#include <QPainter>
#include "Environment.hpp"

#ifndef WATOROPENGLWIDGET_HPP
#define WATOROPENGLWIDGET_HPP


class WatorOpenGLWidget : public QOpenGLWidget
{
private:
    Environment* environment;
public:
    WatorOpenGLWidget(QWidget* widget, Environment* environment);
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int w, int h) override;
    void setEnvironment(Environment *e) {this->environment = e;};
};

#endif // WATOROPENGLWIDGET_HPP

WatorOpenGLWidget.cpp

#include "WatorOpenGLWidget.hpp"
#include <iostream>

WatorOpenGLWidget::WatorOpenGLWidget(QWidget* widget, Environment* e): QOpenGLWidget(widget), environment(e){
}
void WatorOpenGLWidget::initializeGL() {
    glClearColor(0, 0, 0, 1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);
}

void WatorOpenGLWidget::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QPainter painter(this);
    painter.setBrush(QBrush(Qt::blue));
    painter.drawRect(0, 0, 3 * environment->getWidth(), 3 * environment->getHeight());

    std::vector<Agent*> agents = environment->getAgents();
    for(uint32_t i = 0; i < agents.size(); i++){
        painter.setBrush(agents[i]->getColor());
        painter.drawRect(agents[i]->getX() * 3, agents[i]->getY() * 3, 3, 3);
    }
}

void WatorOpenGLWidget::resizeGL(int w, int h){
    glViewport(0, 0, w, h);
}

And I create my custom widget, WatorOpenGLWidget from the mainwindow class :

#include "Mainwindow.hpp"
#include "ui_mainwindow.h"
#include "Environment.hpp"
#include "Fish.hpp"
#include "Shark.hpp"
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateEnvironment()));
    timer->start(20);
    tick = 0;

    initializeEnvironment();
    opengl_widget = new WatorOpenGLWidget(nullptr, environment);
    opengl_widget->setFixedSize(3 * environment->getWidth(), 3 * environment->getHeight());
    ui->scrollAreaWidgetContents->layout()->addWidget(opengl_widget);
    updateGUI();
}

void MainWindow::initializeEnvironment(){
    this->environment = new Environment(100, 100, 0);

    std::vector<Agent*> agents;
    for(uint32_t i = 0; i < 5000; i++){
        agents.push_back(new Fish());
    }

    for(uint32_t i = 0; i < 1000; i++){
        agents.push_back(new Shark());
    }

    this->environment->placeAgents(agents);
}

void MainWindow::updateEnvironment(){
    initializeEnvironment();
    std::vector<Agent*> agents = environment->getAgents();
    for(uint32_t i = 0; i < agents.size(); i++){
        agents[i]->decide();
    }

    updateGUI();
}

void MainWindow::updateGUI(){
    opengl_widget->setEnvironment(environment);
    opengl_widget->paintGL();
    tick++;
    std::string tick_text = "tick " + std::to_string(tick);
    ui->tickLabel->setText(QString::fromStdString(tick_text));
}

MainWindow::~MainWindow()
{
    delete timer;
    delete environment;
    delete ui;
}

The drawing works well every time the timer triggers it, however the following output is generated during runtime:

QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setBrush: Painter not active
QPainter::drawRects: Painter not active
QPainter::setBrush: Painter not active
QPainter::drawRects: Painter not active
QPainter::setBrush: Painter not active
QPainter::drawRects: Painter not active

What should I do to prevent this ? Also is using a QPainter object more CPU consuming than using OpenGL native functions ?

Aucun commentaire:

Enregistrer un commentaire