做軍事網(wǎng)站的項(xiàng)目背景圖片互聯(lián)網(wǎng)怎么打廣告推廣
文章目錄
- 1、簡介
- 2、Qt + QOpenGLWidget + gl函數(shù)
- 3、Qt + QOpenGLWidget + qt函數(shù)
- 4、Qt + QOpenGLWindow
- 5、Qt + glut
- 6、Qt + glfw
- 結(jié)語
1、簡介
Qt提供了與OpenGL實(shí)現(xiàn)集成的支持,使開發(fā)人員有機(jī)會(huì)在更傳統(tǒng)的用戶界面的同時(shí)顯示硬件加速的3D圖形。
Qt有兩種主要的UI開發(fā)方法:QtQuick和QtWidgets。它們的存在是為了支持不同類型的用戶界面,并建立在針對(duì)每種類型進(jìn)行了優(yōu)化的獨(dú)立圖形引擎上。
可以將在OpenGL圖形API中編寫的代碼與Qt中的這兩種用戶界面類型結(jié)合起來。當(dāng)應(yīng)用程序有自己的OpenGL相關(guān)代碼時(shí),或者當(dāng)它與基于OpenGL的第三方渲染器集成時(shí),這可能很有用。
Qt OpenGL模塊包含方便類,使這種類型的集成更容易、更快。
QOpenGLWidget提供了三個(gè)方便的虛擬函數(shù),您可以在子類中重新實(shí)現(xiàn)這些函數(shù)來執(zhí)行典型的OpenGL任務(wù):
- paintGL()-渲染OpenGL場(chǎng)景。每當(dāng)需要更新小部件時(shí)調(diào)用。
- resizeGL()-設(shè)置OpenGL視口、投影等。每當(dāng)小部件被調(diào)整大小時(shí)(以及當(dāng)它第一次顯示時(shí),因?yàn)樗行聞?chuàng)建的小部件都會(huì)自動(dòng)獲得調(diào)整大小事件),都會(huì)調(diào)用它。
- initializeGL()-設(shè)置OpenGL資源和狀態(tài)。在第一次調(diào)用resizeGL()或paintGL()之前調(diào)用一次。
最簡單的QOpenGLWidget子類可能如下所示:
class MyGLWidget : public QOpenGLWidget
{
public:MyGLWidget(QWidget *parent) : QOpenGLWidget(parent) { }protected:void initializeGL() override{// Set up the rendering context, load shaders and other resources, etc.:QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();f->glClearColor(1.0f, 1.0f, 1.0f, 1.0f);...}void resizeGL(int w, int h) override{// Update projection matrix and other size related settings:m_projection.setToIdentity();m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f);...}void paintGL() override{// Draw the scene:QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();f->glClear(GL_COLOR_BUFFER_BIT);...}};
或者,可以通過從QOpenGLFunctions派生來避免每個(gè)OpenGL調(diào)用的前綴:
class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{...void initializeGL() override{initializeOpenGLFunctions();glClearColor(...);...}...
};
2、Qt + QOpenGLWidget + gl函數(shù)
- untitled4.pro
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGSSOURCES += \main.cpp \qopenglwidgettest.cppHEADERS += \qopenglwidgettest.hFORMS += \qopenglwidgettest.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetRESOURCES += \res.qrc
- main.cpp
#include "qopenglwidgettest.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);QOpenGLWidgetTest w;w.show();return a.exec();
}
- qopenglwidgettest.h
#ifndef QOPENGLWIDGETTEST_H
#define QOPENGLWIDGETTEST_H#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>QT_BEGIN_NAMESPACE
namespace Ui { class QOpenGLWidgetTest; }
QT_END_NAMESPACEclass QOpenGLWidgetTest : public QOpenGLWidget, protected /*QOpenGLExtraFunctions*/QOpenGLFunctions_3_3_Core
{Q_OBJECTpublic:QOpenGLWidgetTest(QWidget *parent = nullptr);~QOpenGLWidgetTest();protected:virtual void initializeGL();virtual void resizeGL(int w, int h);virtual void paintGL();private:Ui::QOpenGLWidgetTest *ui;QOpenGLShaderProgram shaderProgram;
};
#endif // QOPENGLWIDGETTEST_H
- qopenglwidgettest.cpp
#include "qopenglwidgettest.h"
#include "ui_qopenglwidgettest.h"static GLuint VBO, VAO, EBO;QOpenGLWidgetTest::QOpenGLWidgetTest(QWidget *parent): QOpenGLWidget(parent), ui(new Ui::QOpenGLWidgetTest)
{ui->setupUi(this);
}QOpenGLWidgetTest::~QOpenGLWidgetTest()
{delete ui;glDeleteVertexArrays(1, &VAO);glDeleteBuffers(1, &VBO);glDeleteBuffers(1, &EBO);
}void QOpenGLWidgetTest::initializeGL(){this->initializeOpenGLFunctions();bool success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/new/prefix1/triangle.vert");if (!success) {qDebug() << "shaderProgram addShaderFromSourceFile failed!" << shaderProgram.log();return;}success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/new/prefix1/triangle.frag");if (!success) {qDebug() << "shaderProgram addShaderFromSourceFile failed!" << shaderProgram.log();return;}success = shaderProgram.link();if(!success) {qDebug() << "shaderProgram link failed!" << shaderProgram.log();}//VAO,VBO數(shù)據(jù)部分float vertices[] = {0.5f, 0.5f, 0.0f, // top right0.5f, -0.5f, 0.0f, // bottom right-0.5f, -0.5f, 0.0f, // bottom left-0.5f, 0.5f, 0.0f // top left};unsigned int indices[] = { // note that we start from 0!0, 1, 3, // first Triangle1, 2, 3 // second Triangle};glGenVertexArrays(1, &VAO);glGenBuffers(1, &VBO);glGenBuffers(1, &EBO);// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).glBindVertexArray(VAO);glBindBuffer(GL_ARRAY_BUFFER, VBO);glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //頂點(diǎn)數(shù)據(jù)復(fù)制到緩沖glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);//告訴程序如何解析頂點(diǎn)數(shù)據(jù)glEnableVertexAttribArray(0);glBindBuffer(GL_ARRAY_BUFFER, 0);//取消VBO的綁定, glVertexAttribPointer已經(jīng)把頂點(diǎn)屬性關(guān)聯(lián)到頂點(diǎn)緩沖對(duì)象了// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.glBindVertexArray(0); //取消VAO綁定
}void QOpenGLWidgetTest::resizeGL(int w, int h){glViewport(0, 0, w, h);
}void QOpenGLWidgetTest::paintGL(){glClearColor(0.5f, 0.5f, 0.5f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);shaderProgram.bind();glBindVertexArray(VAO);
// glDrawArrays(GL_TRIANGLES, 0, 6);glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);shaderProgram.release();
}
- triangle.vert
#version 330 core
layout(location = 0) in vec3 aPos;void main(){gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0f);
}
- triangle.frag
#version 330 core
out vec4 FragColor;void main(){FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
運(yùn)行如下:
3、Qt + QOpenGLWidget + qt函數(shù)
- qtfunctionwidget.h
#ifndef QTFUNCTIONWIDGET_H
#define QTFUNCTIONWIDGET_H#include <QOpenGLWidget>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
#include <QDebug>
#include <QOpenGLFunctions>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>class QtFunctionWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
public:QtFunctionWidget(QWidget *parent = nullptr);~QtFunctionWidget() Q_DECL_OVERRIDE;protected:virtual void initializeGL() Q_DECL_OVERRIDE;virtual void resizeGL(int w, int h) Q_DECL_OVERRIDE;virtual void paintGL() Q_DECL_OVERRIDE;private:QOpenGLShaderProgram shaderProgram;QOpenGLBuffer vbo, ebo;QOpenGLVertexArrayObject vao;
};#endif // QTFUNCTIONWIDGET_H
- qtfunctionwidget.cpp
#include "QtFunctionWidget.h"
#include <QFile>QtFunctionWidget::QtFunctionWidget(QWidget *parent) : QOpenGLWidget (parent),vbo(QOpenGLBuffer::VertexBuffer),ebo(QOpenGLBuffer::IndexBuffer)
{}QtFunctionWidget::~QtFunctionWidget(){makeCurrent();vbo.destroy();ebo.destroy();vao.destroy();doneCurrent();
}void QtFunctionWidget::initializeGL(){this->initializeOpenGLFunctions();bool success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/new/prefix1/triangle.vert");if (!success) {qDebug() << "shaderProgram addShaderFromSourceFile failed!" << shaderProgram.log();return;}success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/new/prefix1/triangle.frag");if (!success) {qDebug() << "shaderProgram addShaderFromSourceFile failed!" << shaderProgram.log();return;}success = shaderProgram.link();if(!success) {qDebug() << "shaderProgram link failed!" << shaderProgram.log();}//VAO,VBO數(shù)據(jù)部分GLfloat vertices[] = {0.7f, 0.5f, 0.0f, // top right0.5f, -0.6f, 0.0f, // bottom right-0.6f, -0.5f, 0.0f, // bottom left-0.5f, 0.7f, 0.0f // top left};unsigned int indices[] = { // note that we start from 0!0, 1, 3, // first Triangle1, 2, 3 // second Triangle};QOpenGLVertexArrayObject::Binder vaoBind(&vao);vbo.create();vbo.bind();vbo.allocate(vertices, sizeof(vertices));ebo.create();ebo.bind();ebo.allocate(indices, sizeof(indices));int attr = -1;attr = shaderProgram.attributeLocation("aPos");shaderProgram.setAttributeBuffer(attr, GL_FLOAT, 0, 3, sizeof(GLfloat) * 3);shaderProgram.enableAttributeArray(attr);vbo.release();
// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.
// ebo.release();
}void QtFunctionWidget::resizeGL(int w, int h){glViewport(0, 0, w, h);
}void QtFunctionWidget::paintGL(){glClearColor(0.2f, 0.2f, 0.0f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);shaderProgram.bind();{QOpenGLVertexArrayObject::Binder vaoBind(&vao);glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);}shaderProgram.release();
}
4、Qt + QOpenGLWindow
- untitled4.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = OpenGL
TEMPLATE = app
CONFIG += c++11SOURCES += \main.cpp \mywindow.cppHEADERS += \mywindow.hLIBS += -lopengl32\-lglu32
- main.cpp
#include <QApplication>
#include <MyWindow.h>int main(int argc, char *argv[])
{QApplication a(argc, argv);MyWindow w;w.setWidth(640);w.setHeight(480);w.setTitle(QString::fromLocal8Bit("愛看書的小沐"));w.show();return a.exec();
}
- mywindow.h
#ifndef WINDOW_H
#define WINDOW_H#include <QOpenGLWindow>
#include <QOpenGLFunctions>
#include <QTimer>class MyWindow : public QOpenGLWindow, protected QOpenGLFunctions
{Q_OBJECT
public:MyWindow();~MyWindow();protected:void initializeGL(); //初始化設(shè)置void resizeGL(int w, int h); //窗口尺寸變化響應(yīng)函數(shù)void paintGL(); //重繪響應(yīng)函數(shù)
private:GLfloat angle; //定義旋轉(zhuǎn)角度QTimer *timer; //定義新的定時(shí)器
};#endif // WINDOW_H
- mywindow.cpp
#include "mywindow.h"MyWindow::MyWindow()
{timer = new QTimer();angle = 0.0;connect(timer, SIGNAL(timeout()), this, SLOT(update()));timer->start(100);
}MyWindow::~MyWindow()
{}void MyWindow::initializeGL()
{initializeOpenGLFunctions();glClearColor(0.0,0.0,0.0,0.0);glClearDepth(1.0);
}void MyWindow::resizeGL(int w, int h)
{Q_UNUSED(w);Q_UNUSED(h);
}void MyWindow::paintGL()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glRotated(angle,0.0,1.0,0.0);glBegin(GL_TRIANGLES);glColor3f(1.0,0.0,0.0);glVertex3f(0.0,0.8,0.0);glColor3f(0.0,0.0,1.0);glVertex3f(0.5,0.0,0.0);glColor3f(0.0,1.0,0.0);glVertex3f(-0.5,0.0,0.0);glEnd();angle+=10.0;
}
程序運(yùn)行如下:
5、Qt + glut
https://freeglut.sourceforge.net/
freeglut是OpenGL實(shí)用工具工具包(GLUT)庫的免費(fèi)軟件/開源替代品。GLUT最初由Mark Kilgard編寫,用于支持OpenGL“紅皮書”第二版中的示例程序。從那時(shí)起,GLUT就被廣泛應(yīng)用于各種實(shí)際應(yīng)用中,因?yàn)樗唵?、可用性廣、便攜性強(qiáng)。
GLUT(以及freeglut)負(fù)責(zé)創(chuàng)建窗口、初始化OpenGL上下文和處理輸入事件所需的所有特定于系統(tǒng)的家務(wù),以實(shí)現(xiàn)真正可移植的OpenGL程序。
freeglut是在X-Consortium許可下發(fā)布的。
- untitled4.pro
LIBS += -L$$PWD\lib -lfreeglut
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
INCLUDEPATH += includeSOURCES += \main.cpp
- main.cpp
#include "GL/glut.h"void display(void)
{// clear all pixelsglClear(GL_COLOR_BUFFER_BIT);glColor3f(0.5, 0.1, 1.0);glBegin(GL_POLYGON);glVertex3f(0.20, 0.20, 0.0);glVertex3f(0.80, 0.20, 0.0);glVertex3f(0.80, 0.80, 0.0);glVertex3f(0.20, 0.80, 0.0);glEnd();glFlush();
}void init(void)
{// select clearing color: blueglClearColor(0.0, 1.0, 0.0, 0.0);// initialize viewing valuesglMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}int main(int argc, char *argv[])
{glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(640, 240);glutInitWindowPosition(480, 320);glutCreateWindow("愛看書的小沐");init();glutDisplayFunc(display);glutMainLoop();return 0;
}
程序運(yùn)行后:
6、Qt + glfw
https://www.glfw.org/
GLFW是一個(gè)開源、多平臺(tái)的庫,用于OpenGL、OpenGL ES和Vulkan在桌面上的開發(fā)。它提供了一個(gè)簡單的API,用于創(chuàng)建窗口、上下文和表面,接收輸入和事件。
GLFW是用C語言編寫的,支持Windows、macOS、Wayland和X11。
GLFW是根據(jù)zlib/libpng許可證獲得許可的。
- untitled4.pro
LIBS += -L$$PWD\lib -lglfw3 -lopengl32 -lGlU32 -luser32 -lkernel32 -lgdi32CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
INCLUDEPATH += includeSOURCES += \main.cpp
- main.cpp
#include <iostream>
#include "GLFW/glfw3.h"
using namespace std;int main(int argc, char *argv[])
{GLFWwindow* window;/* Initialize the library */if (!glfwInit())return -1;/* Create a windowed mode window and its OpenGL context */window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);if (!window){glfwTerminate();return -1;}/* Make the window's context current */glfwMakeContextCurrent(window);/* Loop until the user closes the window */while (!glfwWindowShouldClose(window)){/* Render here */glClear(GL_COLOR_BUFFER_BIT);/* Swap front and back buffers */glfwSwapBuffers(window);/* Poll for and process events */glfwPollEvents();}glfwTerminate();return 0;
}
程序運(yùn)行如下:
結(jié)語
如果您覺得該方法或代碼有一點(diǎn)點(diǎn)用處,可以給作者點(diǎn)個(gè)贊,或打賞杯咖啡;
╮( ̄▽ ̄)╭
如果您感覺方法或代碼不咋地
//(ㄒoㄒ)//,就在評(píng)論處留言,作者繼續(xù)改進(jìn);
o_O???
如果您需要相關(guān)功能的代碼定制化開發(fā),可以留言私信作者;
(????)
感謝各位童鞋們的支持!
( ′ ▽′ )ノ ( ′ ▽′)っ!!!