当前位置:网站首页>QT OpenGL makes objects move under care to form animation
QT OpenGL makes objects move under care to form animation
2022-07-27 21:04:00 【wb175208】
Look at the effect :
Refer to the previous article :Qt OPenGL Diffuse reflection of light
Add a timer :
#pragma once
#include <QOpenGLWindow>
#include <QOpenGLExtraFunctions>
#include <QDebug>
#include <QOpenGLTexture>
#include <QElapsedTimer>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
class DiffuseLightWnd : public QOpenGLWindow {
Q_OBJECT
public:
DiffuseLightWnd();
~DiffuseLightWnd();
protected:
void initializeGL()override;
void paintGL()override;
void keyPressEvent(QKeyEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void resizeGL(int w, int h) Q_DECL_OVERRIDE;
private:
void drawObj();
private slots:
void slotTimeout();
private:
GLuint _VBO, _VAO;
class QOpenGLFunctions_3_3_Core* _openGLCore;
QOpenGLShaderProgram _shaderProgram;// Shader program , All shaders in the system
QMatrix4x4 model, view, projection;
class DiffuseLightCamera* _diffuseLightCamera = nullptr;
bool _lBtnDown = false;
QPoint _lBtnDownPos;
class QTimer* _timer;
float _rotateModel = 45.0;
};
#include "DiffuseLightWnd.h"
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QTimer>
#include <QOpenGLFunctions_3_3_Core>
#include "DiffuseLightCamera.h"
DiffuseLightWnd::DiffuseLightWnd() {
_diffuseLightCamera = new DiffuseLightCamera(QVector3D(0.0, 0.0, -4.0), 0.0, 90.0, QVector3D(0.0, 1.0, 0.0));
_timer = new QTimer;
connect(_timer, SIGNAL(timeout()), this, SLOT(slotTimeout()));
_timer->start(50);
}
DiffuseLightWnd::~DiffuseLightWnd() {
}
void DiffuseLightWnd::initializeGL() {
_openGLCore = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
// Open depth test
_openGLCore->glEnable(GL_DEPTH_TEST);
_openGLCore->glDepthFunc(GL_LESS);
// Vertex data ( Texture coordinates are not used )
float vertices[] = {
// positions // normal vector // texture
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
// 2
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
// 3
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
// 4
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
// 5
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
// 6
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
};
// 1. Create objects
_openGLCore->glGenVertexArrays(1, &_VAO);
_openGLCore->glGenBuffers(1, &_VBO);
// 2. Bound object
_openGLCore->glBindVertexArray(_VAO);
_openGLCore->glBindBuffer(GL_ARRAY_BUFFER, _VBO);
// 3. Data storage
_openGLCore->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// 4. Parsing data
_openGLCore->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
_openGLCore->glEnableVertexAttribArray(0);
// 5. Vertex normal vector
_openGLCore->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
_openGLCore->glEnableVertexAttribArray(1);
// 6. Unbundling
_openGLCore->glBindBuffer(GL_ARRAY_BUFFER, 0);
_openGLCore->glBindVertexArray(0);
// 7. Shader program
_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, "E:/Projects/QtGuiTest/OPenGLApp/DiffuseLight/DiffuseLight.vert");
_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, "E:/Projects/QtGuiTest/OPenGLApp/DiffuseLight/DiffuseLight.frag");
_shaderProgram.link();
}
void DiffuseLightWnd::paintGL() {
_openGLCore->glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
_openGLCore->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_openGLCore->glBindVertexArray(_VAO);
drawObj();
update();
}
void DiffuseLightWnd::keyPressEvent(QKeyEvent *event) {
_diffuseLightCamera->keyPress(event->key());
}
void DiffuseLightWnd::mouseMoveEvent(QMouseEvent *event) {
if (_lBtnDown) {
float xpos = static_cast<float>(event->pos().x());
float ypos = static_cast<float>(event->pos().y());
float xoffset = _lBtnDownPos.x() - xpos;
float yoffset = _lBtnDownPos.y() - ypos;
_lBtnDownPos = event->pos();
_diffuseLightCamera->mouseMove(xoffset, yoffset);
}
}
void DiffuseLightWnd::wheelEvent(QWheelEvent *event) {
_diffuseLightCamera->wheel(event->delta());
update();
}
void DiffuseLightWnd::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
_lBtnDown = true;
_lBtnDownPos = event->pos();
}
}
void DiffuseLightWnd::mouseReleaseEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
_lBtnDown = false;
}
}
void DiffuseLightWnd::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
}
void DiffuseLightWnd::drawObj() {
_shaderProgram.bind();
QMatrix4x4 model;
model.rotate(_rotateModel, QVector3D(1.0, 1.0, 1.0));
model.scale(0.5);
QMatrix4x4 view = _diffuseLightCamera->getViewMatrix();
QMatrix4x4 projection;
projection.perspective(_diffuseLightCamera->getZoom(), width() / height(), 0.1, 100);
_shaderProgram.setUniformValue("model", model);
_shaderProgram.setUniformValue("view", view);
_shaderProgram.setUniformValue("projection", projection);
_shaderProgram.setUniformValue("objectColor", 1.0f, 0.5f, 0.31f);
_shaderProgram.setUniformValue("lightColor", 1.0f, 1.0f, 1.0f);
_shaderProgram.setUniformValue("lightPos", 0.0f, 0.0f, -4.0f);
_openGLCore->glDrawArrays(GL_TRIANGLES, 0, 36);
}
void DiffuseLightWnd::slotTimeout() {
_rotateModel++;
}
aaa
边栏推荐
猜你喜欢

VI working mode (3 kinds) and mode switching (conversion)

Things about stack migration

User login switching case

Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture

IOU 目标跟踪其一:IOU Tracker

hcip第五天
![[numpy] broadcast mechanism](/img/1f/8d61ac7b35a82067bc0b77426590eb.png)
[numpy] broadcast mechanism

Face recognition 5.1- insightface face face detection model training practice notes

一文读懂Plato&nbsp;Farm的ePLATO,以及其高溢价缘由

如何查看蓝牙耳机的蓝牙版本
随机推荐
Vant component library
Sscanf caused the address to be out of bounds
一文读懂Plato&nbsp;Farm的ePLATO,以及其高溢价缘由
[Numpy] 数组索引和切片
How does the industrial switch enter the web management interface?
LeetCode-209-长度最小的子数组
vant组件库
用户登录切换案例
Introduction to rk3399 platform introduction to proficient series (Introduction) 21 day learning challenge
力扣解法汇总592-分数加减运算
Automatic test solution based on ATX
Beijing / Shanghai / Guangzhou / Shenzhen dama-cdga/cdgp data governance certification registration conditions
NATAPP内网穿透工具外网访问个人项目
Recommend a powerful search tool listary
征服所有程序员的3件IT装备 →
DJI内推码(一码一用,2022年7月26日更新)
Express WEB服务器的简单使用
How to talk to CIO / CTO
认识网络模型TCPIP模型
IOU 目标跟踪其二:VIOU Tracker