随笔 - 67,  文章 - 1,  评论 - 2,  阅读 - 56291
前言
  参考博客资源:
  
  <1> Qt 3D
  URL >> https://www.cnblogs.com/sunchaothu/p/11141212.html
  这里篇笔记记录的是实现部分,原理部分等整理后补充。
源码
/**
	\copyright 	Copyright (c) 2020-2022 chongqingBoshikang Corporation
	\contact   	https://www.cnblogs.com/shHome/
	\version   	v1.0
	\file      	global.h
	
	\brief     	导入依赖头文件
	\details   	导入依赖头文件,并定义依赖宏

    \include   	依赖文件的模块
    \li			<QPointer>
    \li			<QGuiApplication>
    \li			<Qt3DRender/qcamera.h>
    \li			<Qt3DCore/qentity.h>
    \li			<Qt3DRender/qcameralens.h>
    \li			<QtWidgets/QApplication>
    \li			<QtWidgets/QWidget>
    \li			<QtWidgets/QHBoxLayout>
    \li			<QtWidgets/QCheckBox>
    \li			<QtWidgets/QCommandLinkButton>
    \li			<QtGui/QScreen>
    \li			<Qt3DInput/QInputAspect>
    \li			<Qt3DExtras/qtorusmesh.h>
    \li			<Qt3DRender/qmesh.h>
    \li			<Qt3DRender/qtechnique.h>
    \li			<Qt3DRender/qmaterial.h>
    \li			<Qt3DRender/qeffect.h>
    \li			<Qt3DRender/qtexture.h>
    \li			<Qt3DRender/qrenderpass.h>
    \li			<Qt3DRender/qsceneloader.h>
    \li			<Qt3DRender/qpointlight.h>
    \li			<Qt3DCore/qtransform.h>
    \li			<Qt3DCore/qaspectengine.h>
    \li			<Qt3DRender/qrenderaspect.h>
    \li			<Qt3DExtras/qforwardrenderer.h>
    \li			<Qt3DExtras/qt3dwindow.h>
    \li			<Qt3DExtras/qfirstpersoncameracontroller.h>
    \li			<QPhongMaterial>
	
	\author    	sun
	\date      	2020/11/17
	\namespace 	nothing
	
	\attention 	注意事项 
	\par       	修改日志 
	<table>
	<tr><th>Date        <th>Version  	<th>Author    	<th>Description
	<tr><td>2020/11/17  	<td>1.0      	<td>sun 			<td>Create initial version
	</table>

*/

/**
    \brief  The header file precompiles the macro to prevent repeated inclusion.
            Refer to document name for specific definition form.
    \eg		_<ProjectName>_<ModuleNmae>_<FileName>_H_.
*/
#ifndef _QTGUIAPPLICATIONLOADOBJ__GLOBAL_H_
#define _QTGUIAPPLICATIONLOADOBJ__GLOBAL_H_


#include <QPointer>
#include <QGuiApplication>
#include <Qt3DRender/qcamera.h>
#include <Qt3DCore/qentity.h>
#include <Qt3DRender/qcameralens.h>
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QCommandLinkButton>
#include <QtGui/QScreen>
#include <Qt3DInput/QInputAspect>
#include <Qt3DExtras/qtorusmesh.h>
#include <Qt3DRender/qmesh.h>
#include <Qt3DRender/qtechnique.h>
#include <Qt3DRender/qmaterial.h>
#include <Qt3DRender/qeffect.h>
#include <Qt3DRender/qtexture.h>
#include <Qt3DRender/qrenderpass.h>
#include <Qt3DRender/qsceneloader.h>
#include <Qt3DRender/qpointlight.h>
#include <Qt3DCore/qtransform.h>
#include <Qt3DCore/qaspectengine.h>
#include <Qt3DRender/qrenderaspect.h>
#include <Qt3DExtras/qforwardrenderer.h>
#include <Qt3DExtras/qt3dwindow.h>
#include <Qt3DExtras/qfirstpersoncameracontroller.h>
#include <QPhongMaterial>

/** 声明类的别名 */
#define STATEMENT_CLASS_ALIAS_NAME(classname,aliasname) typedef classname aliasname

#endif  // !_QTGUIAPPLICATIONLOADOBJ__GLOBAL_H_
/**
      \brief      部件头文件,其实把 __initializateOn3DView(),void initializate(const QString&)的依赖代码提取出来就可以了
*/
#include <QtWidgets/QWidget>
#include "ui_QtGuiApplicationLoadObj.h"

#include "global.h"

class QtGuiApplicationLoadObj : public QWidget
{
    Q_OBJECT
private:
    STATEMENT_CLASS_ALIAS_NAME(Qt3DExtras::Qt3DWindow, Qt3DWindow);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DCore::QEntity, QtEntity);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DRender::QMaterial, QtMaterial);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DCore::QTransform, QtTransform);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DRender::QCamera, QtCamera);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DInput::QInputAspect, QtInputAspect);
public:
    QtGuiApplicationLoadObj(QWidget *parent = Q_NULLPTR);

    void initializate(const QString&);

protected:
    void __initializateOn3DView();
private:
    Ui::QtGuiApplicationLoadObjClass ui;

    struct date_struct 
    {
        QPointer<Qt3DWindow> m_windowOn3D;
        QPointer<QtEntity> m_rootEntity;
        QPointer<QtMaterial> m_material;
        QPointer<QtTransform> m_trans;
        QPointer<QtInputAspect> m_input;
        QPointer<QtCamera> m_camera;
    }db;
};
/**
      \brief      部件源文件
*/
#include "QtGuiApplicationLoadObj.h"

QtGuiApplicationLoadObj::QtGuiApplicationLoadObj(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    __initializateOn3DView();
}

void QtGuiApplicationLoadObj::initializate(const QString & obj_url)
{
    // 如果将3d放置在界面中,则需要创建一个widget来“装”它
    QWidget *container = QWidget::createWindowContainer(db.m_windowOn3D);
    // 设置场景大小
    QSize screenSize = db.m_windowOn3D->screen()->size();
    // 当你缩小对话框时,你希望它最小是多少
    container->setMinimumSize(QSize(200, 100));
    // 当你缩小对话框时,你希望它最大是多少
    container->setMaximumSize(screenSize);

    QHBoxLayout *hLayout = new QHBoxLayout(this);
    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->setAlignment(Qt::AlignTop);
    // 将3d场景veiw放到widget中
    hLayout->addWidget(container, 1);
    hLayout->addLayout(vLayout);

    // 设置3d场景的输入
    db.m_windowOn3D->registerAspect(db.m_input);

    // 设置相机属性
    // 观测视角
    db.m_windowOn3D->camera()->lens()->setPerspectiveProjection(45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);
    // 相机放哪里
    db.m_windowOn3D->camera()->setPosition(QVector3D(0, 0, 20.0f));
    db.m_windowOn3D->camera()->setUpVector(QVector3D(0, 1, 0));
    // 视角中心在哪里
    db.m_windowOn3D->camera()->setViewCenter(QVector3D(0, 0, 0));

    Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(db.m_rootEntity);
    Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
    light->setColor("white");
    light->setIntensity(1);
    lightEntity->addComponent(light);

    Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
    lightTransform->setTranslation(db.m_windowOn3D->camera()->position());
    lightEntity->addComponent(lightTransform);

    db.m_trans->setScale(0.01f);
    // 模型的缩放,我加载的模型比较大,这里要缩小显示 
    db.m_trans->setTranslation(QVector3D(.0f, 0.0f, 19.45f));

    Qt3DRender::QMesh *mesh = new Qt3DRender::QMesh();
    mesh->setSource(QUrl(obj_url));

    Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(db.m_rootEntity);

    db.m_rootEntity->addComponent(mesh);
    db.m_rootEntity->addComponent(material);
    db.m_rootEntity->addComponent(db.m_trans);

    Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(db.m_rootEntity);
    camController->setCamera(db.m_windowOn3D->camera());

    db.m_windowOn3D->setRootEntity(db.m_rootEntity);
}

void QtGuiApplicationLoadObj::__initializateOn3DView()
{
    db.m_windowOn3D = new Qt3DWindow();
    db.m_rootEntity = new QtEntity();
    db.m_material = new QtMaterial();
    db.m_trans = new QtTransform();
    db.m_input = new QtInputAspect();
}
#include "QtGuiApplicationLoadObj.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QtGuiApplicationLoadObj w;
    // 将自己的obj、stl文件路径传进去就可以渲染出来了
    w.initializate("file:///E:/PolyDataWriter.obj");
    w.show();
    w.resize(1200, 800);
    return a.exec();
}
posted on   怪小子  阅读(5446)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
              
点击右上角即可分享
微信分享提示