c++ gui qt4的opengl 入门示例

本示例介绍如何使用Qt4.7.3和OpenGL在QtCreator2.2中创建并显示一个彩色正四面体。通过自定义testgl类继承QGLWidget,实现三维图形的初始化、缩放、绘制等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本示例使用qt4.7.3编制,qt creator 2.2版本。

先看pro文件,和exampl文件夹里的不一样,这个简单,没有依赖关系。

#-------------------------------------------------
#
# Project created by QtCreator 2019-06-06T22:22:03
#
#-------------------------------------------------

QT       += core gui
QT           += opengl
TARGET = mynewgl
TEMPLATE = app


SOURCES += main.cpp\
        widget.cpp \
    testgl.cpp

HEADERS  += widget.h \
    testgl.h

之后是main文件

#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.setWindowTitle(QObject::tr("Tetrahedron"));
    w.resize(500, 500);
    w.show();
    return a.exec();
}

首先生成widgets示例,之后show一下。

再看widget的头文件

#ifndef WIDGET_H
#define WIDGET_H

//#include <QtGui/QWidget>
#include <QWidget>
#include <QHBoxLayout>
#include "testgl.h"

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

private:
    testgl *mygl;
    QHBoxLayout *layout;
};

#endif // WIDGET_H

调用了testgl头文件,定义了构造函数,定了了两个私有指针。

再看widget实现文件

#include "widget.h"


// Constructor
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    // add mggl on layout, and add layout on widget
    mygl = new testgl;
    layout = new QHBoxLayout;
    layout->addWidget(mygl);
    setLayout(layout);

}

Widget::~Widget()
{

}

很简单,在构造函数里实现了一下testgl类,之后把它安装到layout上面。其实还可以更简单,直接显示testgl就得了。

在看testgl类的头文件

#ifndef TESTGL_H
#define TESTGL_H

#include <QGLWidget>

class testgl : public QGLWidget
{
    Q_OBJECT
public:
    explicit testgl(QWidget *parent = 0);

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

signals:

public slots:

private:
    void draw();
    GLfloat rotationX;
    GLfloat rotationY;
    GLfloat rotationZ;
    QColor faceColors[4];

};

#endif // TESTGL_H

这规定了testgl类的成员函数和私有变量

再看testgl的实现文件

#include <QtGui>
#include <QtOpenGL>
#include "testgl.h"

//构造函数
testgl::testgl(QWidget *parent) :
    QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer),parent)
{
    rotationX = -21.0;
    rotationY = -57.0;
    rotationZ = 0.0;
    faceColors[0] = Qt::red;
    faceColors[1] = Qt::green;
    faceColors[2] = Qt::blue;
    faceColors[3] = Qt::yellow;
}

void testgl::initializeGL()
{
    qglClearColor(Qt::gray);
//        qglClearColor(Qt::black);
    glShadeModel(GL_FLAT);
//    glEnable(GL_DEPTH_TEST);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glColor4f(1.0f,1.0f,1.0f,1.0f);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
//    glEnable(GL_CULL_FACE);
}

void testgl::resizeGL(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat x = GLfloat(width) / height;
    glFrustum(-x, +x, -1.0, +1.0, 4.0, 15.0);
    glMatrixMode(GL_MODELVIEW);
}


void testgl::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    draw();
}

void testgl::draw()
{
    static const GLfloat P1[3] = { 0.0, -1.0, +2.0 };
    static const GLfloat P2[3] = { +1.73205081, -1.0, -1.0 };
    static const GLfloat P3[3] = { -1.73205081, -1.0, -1.0 };
    static const GLfloat P4[3] = { 0.0, +2.0, 0.0 };

    static const GLfloat * const coords[4][3] = {
        { P1, P2, P3 }, { P1, P3, P4 }, { P1, P4, P2 }, { P2, P4, P3 }
    };

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -10.0);
    glRotatef(rotationX, 1.0, 0.0, 0.0);
    glRotatef(rotationY, 0.0, 1.0, 0.0);
    glRotatef(rotationZ, 0.0, 0.0, 1.0);

    for (int i = 0; i < 4; ++i) {
        glLoadName(i);
        glBegin(GL_TRIANGLES);
        qglColor(faceColors[i]);
        for (int j = 0; j < 3; ++j) {
            glVertex3f(coords[i][j][0], coords[i][j][1],
                       coords[i][j][2]);
        }
        glEnd();
    }
}




在三维空间里绘制了一个正四面体,并以不同颜色显示。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Intimes

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

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

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

打赏作者

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

抵扣说明:

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

余额充值