如何在 QOpenGLWidget 中渲染三角形? [英] How do I render a triangle in QOpenGLWidget?

查看:21
本文介绍了如何在 QOpenGLWidget 中渲染三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 QOpenGLWidget 在 Qt 中使用 OpenGL,但我很难找到任何相关示例.我是 OpenGL 的新手,所以我正在尝试学习如何使用它,但是我发现的教程似乎不适用于 QOpenGLWidget.现在,我要做的就是渲染一个三角形开始.

I'm trying to use OpenGL inside of Qt using QOpenGLWidget, but I am having a hard time finding any relevant examples. I am new to OpenGL, so I am trying to learn how to use it, but the tutorials that I find don't seem to apply particularly well in QOpenGLWidget. Right now, all I want to do is render a triangle to start with.

这是我目前所拥有的.

标题:

namespace Ui {
class Widget;
}

class Widget : public QOpenGLWidget, protected QOpenGLFunctions
{
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected:
    void initializeGL();
    void resizeGL(int, int);
    void paintGL();

private:
    Ui::Widget *ui;
};

类:

Widget::Widget(QWidget *parent) :
    QOpenGLWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

void Widget::initializeGL()
{
    // Set up the rendering context, load shaders and other resources, etc.:
    initializeOpenGLFunctions();
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
}

void Widget::resizeGL(int w, int h)
{
    // Update projection matrix and other size-related settings:
}

void Widget::paintGL()
{
    // Draw the scene:
    glClear(GL_COLOR_BUFFER_BIT);
}

Widget::~Widget()
{
    delete ui;
}

有什么例子可以用来渲染一个基本的三角形吗?我从这里尝试了一个:https://www.khronos.org/assets/uploads/books/openglr_es_20_programming_guide_sample.pdf,但它抛出了很多我无法解决的错误.

Is there any example I could use to just render a basic triangle? I tried the one from here: https://www.khronos.org/assets/uploads/books/openglr_es_20_programming_guide_sample.pdf, but it threw a lot of errors that I couldn't work out.

我也不知道 OpenGL 上下文在 QOpenGLWidget 中是如何工作的.

I also don't know how OpenGL contexts work in QOpenGLWidget.

*所以事实证明,这些示例是我的发行版(Arch Linux)上的一个单独的包.我能够安装它们,看起来还有很多可以开始的地方.

* So it turns out that the examples were a separate package on my distro (Arch Linux). I was able to install them, and it looks like there is plenty there to get started.

感谢您的帮助!

推荐答案

如果你想使用 QOpenGLWidget 而不是 QGLWidget,那么可以这样做.

If you want to use QOpenGLWidget not QGLWidget, then this is the way to do it.

打开 Qt Creator 并选择 Qt Widgets Application.添加Widget和按钮如下

Open Qt Creator and choose Qt Widgets Application. Add Widget and push button as follows

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

现在添加新类并将其命名为 OGLWidget,它应该继承自 QOpenGLWidget

Now add New Class and name it OGLWidget which should be inherited from QOpenGLWidget

oglwidget.h

#ifndef OGLWIDGET_H
#define OGLWIDGET_H

#include <QWidget>
#include <QOpenGLWidget>
#include <gl/GLU.h>
#include <gl/GL.h>

class OGLWidget : public QOpenGLWidget
{
public:
    OGLWidget(QWidget *parent = 0);
    ~OGLWidget();

protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
};

#endif // OGLWIDGET_H

oglwidget.cpp

#include "oglwidget.h"

OGLWidget::OGLWidget(QWidget *parent)
    : QOpenGLWidget(parent)
{

}

OGLWidget::~OGLWidget()
{

}

void OGLWidget::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 OGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
        glColor3f(1.0, 0.0, 0.0);
        glVertex3f(-0.5, -0.5, 0);
        glColor3f(0.0, 1.0, 0.0);
        glVertex3f( 0.5, -0.5, 0);
        glColor3f(0.0, 0.0, 1.0);
        glVertex3f( 0.0,  0.5, 0);
    glEnd();
}

void OGLWidget::resizeGL(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, (float)w/h, 0.01, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,5,0,0,0,0,1,0);
}

现在返回表单并右键单击小部件.选择promoted widgets 并输入提升的类名OGLWidget.点击添加按钮,然后推广.现在单击背景并转到其属性并将 windowModality 更改为 ApplicationModel.

Now go back to the form and right click on the widget. Select promoted widgets and type in the promoted class name OGLWidget. Hit the add button and then promote. Now click on the background and go to its properties and change windowModality to ApplicationModel.

这是你应该得到的结果

.pro 文件是

#-------------------------------------------------
#
# Project created by QtCreator 2015-07-20T15:15:29
#
#-------------------------------------------------

QT       += core gui opengl

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test2
TEMPLATE = app


SOURCES += main.cpp
        mainwindow.cpp 
    oglwidget.cpp

HEADERS  += mainwindow.h 
    oglwidget.h

FORMS    += mainwindow.ui

这篇关于如何在 QOpenGLWidget 中渲染三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆