Qt|C++-最简单的OpenGL(变颜色的三角形)

583 篇文章 127 订阅
40 篇文章 6 订阅

程序运行截图如下:
这里写图片描述

代码如下:
MyFrisstMSVCQt.h

#pragma once

#include <QtWidgets/QWidget>
#include "ui_MyFristMSVCQt.h"

class MyFristMSVCQt : public QWidget
{
    Q_OBJECT

public:
    MyFristMSVCQt(QWidget *parent = Q_NULLPTR);

    void timerEvent(QTimerEvent *event);

private:
    Ui::MyFristMSVCQtClass ui;
};

MyGLWidget.h

#pragma once

#include <QOpenGLWidget>
#include <QOpenGLFunctions>


class MyGLWidget:public QOpenGLWidget,protected QOpenGLFunctions
{
    Q_OBJECT
public:
    MyGLWidget(QWidget *parent = 0);
    ~MyGLWidget();
    void setRGB_red(const int value);
    void setRGB_blue(const int value);
    void setRGB_green(const int value);

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

protected:

    int m_redValue;
    int m_blueValue;
    int m_greenValue;
};

main.cpp

#include "MyFristMSVCQt.h"
#include <QtWidgets/QApplication>

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

    return a.exec();
}

MyFristMSVCQt.cpp

#include "MyFristMSVCQt.h"
#include "ui_MyFristMSVCQt.h"

#include <QTimer>
#include <QDebug>
#include <QTime>


MyFristMSVCQt::MyFristMSVCQt(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    this->setWindowTitle("MyFirstOPenGL");
    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
    startTimer(200);
}

void MyFristMSVCQt::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event)
    int numRed = qrand() % 256;
    int numGreen = qrand() % 256;
    int numBlue = qrand() % 256;
    ui.openGLWidget->setRGB_blue(numBlue);
    ui.openGLWidget->setRGB_green(numGreen);
    ui.openGLWidget->setRGB_red(numRed);
    ui.openGLWidget->update();
}

MyFLWidget.cpp

#include "MyGLWidget.h"
#include <gl/GLU.h>
#include <QDebug>


MyGLWidget::MyGLWidget(QWidget *parent)
    : QOpenGLWidget(parent)
{
}

MyGLWidget::~MyGLWidget()
{
}

void MyGLWidget::setRGB_blue(const int value)
{
    m_blueValue = value;
}

void MyGLWidget::setRGB_green(const int value)
{
    m_greenValue = value;
}

void MyGLWidget::setRGB_red(const int value)
{
    m_redValue = value;
}

void MyGLWidget::initializeGL()
{
    initializeOpenGLFunctions();
    glMatrixMode(GL_PROJECTION);
    gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void MyGLWidget::paintGL()
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
    glColor4ub(m_blueValue, m_greenValue, m_redValue, 255);
    glVertex3f(-0.2f, -0.2f, -0.5f);

    glColor4ub(m_redValue, m_greenValue, m_blueValue, 255);
    glVertex3f(0.2f, -0.2f, -0.5f);

    glColor4ub(m_greenValue, m_redValue, m_blueValue, 255);
    glVertex3f(0.0f, 0.2f, -0.5f);
    glEnd();
}

void MyGLWidget::resizeGL(int w, int h)
{
    Q_UNUSED(w)
    Q_UNUSED(h)
}
你可能打错了单词,我猜你的意思是使用 glBegin 函数而不是 glBeigin 函数。 在使用 glBegin 函数的情况下,实现渐三角形可以通过调用 glColor3f 函数来设置每个顶点的颜色,然后在 glBegin 和 glEnd 之间绘制三角形。 以下是使用 glBegin 函数的实现渐三角形的基本步骤: 1.在OpenGL上下文中启用混合(blending),以便颜色可以混合在一起。 ```c++ glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); ``` 2.创建一个QGradient对象,并将其设置为线性渐。 ```c++ QLinearGradient gradient(0, 0, 1, 1); gradient.setColorAt(0.0, Qt::red); gradient.setColorAt(0.5, Qt::green); gradient.setColorAt(1.0, Qt::blue); ``` 3.在 glBegin 和 glEnd 之间绘制三角形,并使用 glColor3f 函数在每个顶点上设置颜色。 ```c++ QColor color1 = gradient.colorAt(0.0); QColor color2 = gradient.colorAt(0.5); QColor color3 = gradient.colorAt(1.0); glBegin(GL_TRIANGLES); glColor3f(color1.redF(), color1.greenF(), color1.blueF()); glVertex3f(0.0, 0.5, 0.0); glColor3f(color2.redF(), color2.greenF(), color2.blueF()); glVertex3f(-0.5, -0.5, 0.0); glColor3f(color3.redF(), color3.greenF(), color3.blueF()); glVertex3f(0.5, -0.5, 0.0); glEnd(); ``` 完整的使用 glBegin 函数的渐三角形代码示例: ```c++ #include <QtOpenGL> #include <QLinearGradient> class GLWidget : public QGLWidget { public: GLWidget(QWidget *parent = 0) : QGLWidget(parent) {} protected: void initializeGL() { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } void paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QLinearGradient gradient(0, 0, 1, 1); gradient.setColorAt(0.0, Qt::red); gradient.setColorAt(0.5, Qt::green); gradient.setColorAt(1.0, Qt::blue); QColor color1 = gradient.colorAt(0.0); QColor color2 = gradient.colorAt(0.5); QColor color3 = gradient.colorAt(1.0); glBegin(GL_TRIANGLES); glColor3f(color1.redF(), color1.greenF(), color1.blueF()); glVertex3f(0.0, 0.5, 0.0); glColor3f(color2.redF(), color2.greenF(), color2.blueF()); glVertex3f(-0.5, -0.5, 0.0); glColor3f(color3.redF(), color3.greenF(), color3.blueF()); glVertex3f(0.5, -0.5, 0.0); glEnd(); } void resizeGL(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } }; ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值