近期用了款叫DesktopSpace,也想實現(xiàn)一下這款軟件實現(xiàn)的效果
具體實現(xiàn)步驟:
- 使用Qt3D繪制個旋轉(zhuǎn)的立方體(一)
- 使用快捷鍵控制立方體顯示面(二)
- 創(chuàng)建6個人虛擬桌面,截取不同虛擬桌面,顯示在不同的面上 (三)
- 在立方體上播放視頻
首先用Qt3D繪制一下桌面截圖旋轉(zhuǎn)的效果
Qt3D簡單介紹
Qt 3D 是一個用于創(chuàng)建交互式 3D 圖形應(yīng)用程序的框架。它提供了一組類和函數(shù),用于創(chuàng)建、渲染和操作 3D 場景。Qt 3D 在 Qt c++ 和 Qt Quick 應(yīng)用程序中支持 2D 和 3D 渲染,為近實時仿真系統(tǒng)提供了功能。
以下是 Qt 3D 中一些常用的類說明和代碼流程:
Qt3DCore:這是 Qt 3D 框架的核心模塊,包含了一些基本類和接口,用于構(gòu)建 3D 場景和實體。其中一些重要的類包括:
QEntity:表示 3D 場景中的實體,可以添加組件和子實體。
QTransform:表示實體的變換信息,用于控制實體的位置、旋轉(zhuǎn)和縮放等屬性。
Qt3DRender:這個模塊主要用于渲染圖形對象,包括定義材質(zhì)、紋理和著色器等。其中一些重要的類包括:
QMaterial:表示實體的材質(zhì),包含了表面屬性、紋理和渲染狀態(tài)等。
QEffect:定義了渲染效果的著色器程序。
QTexture:表示紋理,可以加載圖像并應(yīng)用于材質(zhì)。
Qt3DInput:這個模塊用于處理用戶輸入(例如鼠標和鍵盤事件),以及與用戶交互的功能。其中一些重要的類包括:
QMouseDevice:用于監(jiān)聽和處理鼠標事件。
QKeyboardDevice:用于監(jiān)聽和處理鍵盤事件。
下面是一個簡單的 Qt 3 D 代碼流程示例,展示了如何創(chuàng)建一個帶有實體、材質(zhì)和變換的場景:
#include <Qt3DCore/QEntity>
#include <Qt3DRender/QMaterial>
#include <Qt3DRender/QEffect>
#include <Qt3DRender/QTexture>
#include <Qt3DCore/QTransform>
int main(int argc, char** argv) {
QApplication app(argc, argv);
// 創(chuàng)建場景根實體
Qt3DCore::QEntity sceneRoot;
// 創(chuàng)建一個實體
Qt3DCore::QEntity* entity = new Qt3DCore::QEntity(&sceneRoot);
// 創(chuàng)建材質(zhì)
Qt3DRender::QMaterial* material = new Qt3DRender::QMaterial(entity);
// 創(chuàng)建紋理
Qt3DRender::QTexture2D* texture = new Qt3DRender::QTexture2D(material);
// 設(shè)置紋理屬性
// 創(chuàng)建效果
Qt3DRender::QEffect* effect = new Qt3DRender::QEffect();
// ...
}
這個示例只是 Qt 3 D 的一個簡單應(yīng)用,如果需要更深入了解和使用 Qt 3 D ,建議閱讀官方文檔和相關(guān)教程。
繪制旋轉(zhuǎn)立方體
主函數(shù)文章來源:http://www.zghlxwxcb.cn/news/detail-741736.html
#include "cubewidget.h"
#include <Qt3DExtras/QTextureMaterial>
#include <QImage>
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QLabel>
#include <QCheckBox>
#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QCuboidMesh>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DExtras/QForwardRenderer>
#include <QTimer>
#include <Qt3DRender/QCamera>
#include <Qt3DCore/QComponent>
#include <Qt3DRender/QTextureImage>
#include <Qt3DRender/QTexture>
#include <QMatrix3x3>
#include <QRenderSurfaceSelector>
#include <QImage>
#include "rotatorutil.h"
#include "rotator.h"
#include "hotkeymanager.h"
#include "mymaterialimage.h"
#include "lazyutil.h"
#include "animationutil.h"
CubeWidget::CubeWidget(QWidget *parent)
: QWidget{parent}
{
// 創(chuàng)建3D窗口
Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
view->defaultFrameGraph()->setClearColor(QColor(210, 210, 220, 0));
view->setTitle("Qt3D C++ example: rotate cube");
// 創(chuàng)建一個容器小部件和窗口布局
QWidget *container = QWidget::createWindowContainer(view);
container->setAttribute(Qt::WA_TranslucentBackground, true);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(container, 1);
// 創(chuàng)建一個用于窗口的小部件并設(shè)置其布局
setLayout(layout);
// 創(chuàng)建場景的根實體
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
// 創(chuàng)建相機實體并設(shè)置其位置和視圖中心
Qt3DRender::QCamera *cameraEntity = view->camera();
cameraEntity->setPosition(QVector3D(10.0f, 0, 0.0f));
cameraEntity->setViewCenter(QVector3D(0, 0, 0));
// 創(chuàng)建立方體實體并添加立方體網(wǎng)格組件和馮氏材質(zhì)組件
Qt3DCore::QEntity *cubeEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QCuboidMesh *cubeMesh = new Qt3DExtras::QCuboidMesh();
cubeMesh->setXExtent(5.1f);
cubeMesh->setYExtent(3.23f);
cubeMesh->setZExtent(5.1f);
// cubeMesh->setXYMeshResolution(QSize(300, 100));
// cubeMesh->setXZMeshResolution(QSize(300, 100));
// cubeMesh->setYZMeshResolution(QSize(100, 100));
auto *image = new MyQPaintedTextureImage;
image->setImage(LazyUtil::Instance()->grabScreenImage());
auto *cubeMaterial = new Qt3DExtras::QTextureMaterial;
cubeMaterial->texture()->addTextureImage(image);
#ifdef USE_OCV
QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, image, &MyQPaintedTextureImage::updateImage);
timer->start(50); // 1000毫秒刷新一次
#endif
// 將立方體網(wǎng)格組件和馮氏材質(zhì)組件添加到立方體實體
cubeEntity->addComponent(cubeMesh);
cubeEntity->addComponent(cubeMaterial);
// 創(chuàng)建一個變換組件并將其添加到立方體實體
Qt3DCore::QTransform *transform = new Qt3DCore::QTransform();
cubeEntity->addComponent(transform);
#if 1
// 定時旋轉(zhuǎn)
Rotator *rotator = new Rotator();
QObject::connect(rotator, &Rotator::rotationChanged, transform, &Qt3DCore::QTransform::setRotation);
#else
// 快捷鍵旋轉(zhuǎn)
RotatorUtil *rotator = new RotatorUtil();
QObject::connect(rotator, &RotatorUtil::rotationChanged, transform, &Qt3DCore::QTransform::setRotation);
QObject::connect(HotkeyManager::Instance(), &HotkeyManager::angleHorizontalChanged, rotator, &RotatorUtil::updateAngleHorizontal);
HotkeyManager::Instance()->registerLeftRotateHotkey(rotator);
HotkeyManager::Instance()->registerRightRotateHotkey(rotator);
QObject::connect(HotkeyManager::Instance(), &HotkeyManager::angleVerticalChanged, rotator, &RotatorUtil::updateAngleVertical);
HotkeyManager::Instance()->registerTopRotateHotkey(rotator);
HotkeyManager::Instance()->registerBottomRotateHotkey(rotator);
QObject::connect(HotkeyManager::Instance()->getWorker(), &RotateWorker::angleHorizontalChanged, rotator, &RotatorUtil::updateAngleHorizontal);
QObject::connect(HotkeyManager::Instance()->getWorker(), &RotateWorker::angleVerticalChanged, rotator, &RotatorUtil::updateAngleVertical);
#endif
// 設(shè)置窗口的根實體
view->setRootEntity(rootEntity);
setAttribute(Qt::WA_TranslucentBackground, true);
AnimationUtil::AnimationSmaller(this);
}
動畫控制類文章來源地址http://www.zghlxwxcb.cn/news/detail-741736.html
#ifndef ROTATOR_H
#define ROTATOR_H
#include <Qt3DCore/QComponent>
#include <QTimer>
#include <QVector3D>
#include <QQuaternion>
// A custom component that controls the rotation of an entity
class Rotator : public Qt3DCore::QComponent
{
Q_OBJECT
public:
Rotator(Qt3DCore::QNode *parent = nullptr)
: Qt3DCore::QComponent(parent)
, m_angle(0.0f)
{
// Create a timer to update the angle every 10 ms
QTimer* timer=new QTimer(this);
connect(timer, &QTimer::timeout, this, &Rotator::updateAngle);
timer->start(10);
}
// A function to get the current rotation as a quaternion
QQuaternion rotation() const
{
// Calculate the rotation axis as the cross product of the up vector and the forward vector
QVector3D axis = QVector3D(0.0f, 1.0f, 0.0f)
.crossProduct(QVector3D(0.0f, -1.0f, 0.0f),
QVector3D(-1.0f, 0.0f, 1.0f));
// Normalize the axis
axis.normalize();
// Return the rotation as a quaternion
return QQuaternion::fromAxisAndAngle(axis, m_angle);
}
private slots:
// A slot to update the angle by 1 degree
void updateAngle()
{
// Increment the angle by 1 degree
m_angle += 0.2f;
// Wrap the angle to [0, 360) range
if (m_angle >= 360.0f) {
m_angle -= 360.0f;
}
// Emit a signal to notify the change of rotation
emit rotationChanged(rotation());
}
signals:
void rotationChanged(const QQuaternion &rotation);
private:
float m_angle; // The angle of rotation in degrees
};
#endif // ROTATOR_H
到了這里,關(guān)于Qt3D繪制旋轉(zhuǎn)立方體的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!