Qt下使用OpenGL

       今天把之前在vs2015下的openGL写的代码移植到QtCreator上,把步骤和遇到的坑记录一下。

功能很简单只是绘制一个三角形和一个正方形。   

1. 首先建立工程,配置文件pro代码,这里需要注意的地方时,加入     

QT       += core gui opengl
win32: LIBS += -lOpenGL32
win32: LIBS += -lGlU32

否者编译器报错:

 OpenGLWindow.obj:-1: error: LNK2019: 无法解析的外部符号 __imp_glBegin,该符号在函数 

QT       += core gui opengl

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = FirstOpenGLTriangle
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

win32: LIBS += -lOpenGL32

win32: LIBS += -lGlU32

SOURCES += \
        main.cpp \
        mainwindow.cpp \
    OpenGLWindow.cpp


HEADERS += \
        mainwindow.h \
    OpenGLWindow.h


FORMS += \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

                  

2.新建c++类 :OpenGLWindow   头文件

#ifndef OPENGLWINDOW_H
#define OPENGLWINDOW_H

#include <QObject>
#include <QOpenGLWindow>
#include <QOpenGLWidget>
#include <QGLWidget>
#include <QGLFunctions>
#include <QtOpenGL>
#include <QOpenGLFunctions>
#include "gl/GLU.h"

class OpenGLWindow : public QOpenGLWindow, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit OpenGLWindow(QWindow *parent = Q_NULLPTR);
    ~OpenGLWindow();
protected:
    void initializeGL();
    void resizeGL(int width, int height);
    void paintGL();

};

#endif // OPENGLWINDOW_H

实现文件

#include "OpenGLWindow.h"


OpenGLWindow::OpenGLWindow(QWindow *parent)
//    : QOpenGLWindow(parent)
{

}

OpenGLWindow::~OpenGLWindow()
{

}

void OpenGLWindow::initializeGL()
{
    initializeOpenGLFunctions();
//    glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
    glColor3f(0.0f,0.7f,0.3f);
    glShadeModel(GL_SMOOTH);

    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void OpenGLWindow::resizeGL(int width, int height)
{
    glViewport(0, 0, (GLint)width, (GLint)height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)width / (GLfloat)height, 0.1, 1000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}

void OpenGLWindow::paintGL()
{
    glClearColor(0.0f,0.7f,0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(-1.5f, 0.0f, -6.0f);
    glBegin(GL_TRIANGLES);
        glColor3f(0.8f,1.0f,0.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);

        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);

        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();

    glTranslatef(3.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
        glColor3f(0.0f,0.7f,1.0f);
        glVertex3f(-1.0f, 1.0f, 0.0f);

        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f(1.0f, 1.0f, 0.0f);

        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f(1.0f, -1.0f, 0.0f);

        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
   glEnd();

   glPopMatrix();  //还原


}

3. main.cppda代码

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

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

    OpenGLWindow openGLWin;
    openGLWin.resize(QSize(400, 300));
    openGLWin.show();
    return a.exec();
}

运行效果如下

 

 源码下载地址:

https://download.csdn.net/download/aoxuestudy/12741516

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值