通過 Windows 命令提示符(cmd)在桌面新建文件夾,并在該文件夾中編譯、運行一段 Java 程序段
861
2025-03-31
項目中需要加載簡單的3D場景。資深老前輩推薦使用開源小巧的引擎irrlicht。
關于irrlicht,來之百度百科
Irrlicht引擎是一個用C++書寫的高性能實時的3D引擎,可以應用于C++程序或者.NET語言中。通過使用Direct3D(Windows平臺),OpenGL 1.2或它自己的軟件著色程序,可以實現該引擎的完全跨平臺。盡管是開源的,該Irrlicht庫提供了可以在商業級的3D引擎上具有的藝術特性,例如動態的陰影,粒子系統,角色動畫,室內和室外技術以及碰撞檢測等。
具體信息?百度百科。
如何使用,
首先使用Qt建立工程,略過。
在Qt pro工程文件總中加入引擎頭文件路徑,和庫文件路徑。
#包含鬼火3D引擎需要的頭文件路勁
INCLUDEPATH +=D:\irrlicht-1.8.3\include
#連接開發需要用到的庫文件
LIBS +=D:\irrlicht-1.8.3\lib\Win32-gcc\libIrrlicht.a
如圖所示
剩下的就是一般的核心代碼部分了,
包含頭文件部分
#include
#include
#include
#include
使用命名空間部分
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
引擎初始化
void Irr_Device::init_Dev()
{
if(m_Device != NULL)
{
return;
}
SIrrlichtCreationParameters params;
params.AntiAlias = 0;
params.Bits = 32;
params.DeviceType = EIDT_BEST;
params.Doublebuffer = true;
params.DriverType = EDT_OPENGL;
params.EventReceiver = 0;
params.Fullscreen = false;
params.HighPrecisionFPU = false;
params.IgnoreInput = false;
params.LoggingLevel = ELL_INFORMATION;
params.Stencilbuffer = true;
params.Stereobuffer = false;
params.Vsync = false;
// Specify which window/widget to render to
// 指定哪個窗口小部件呈現
params.WindowId = reinterpret_cast
params.WindowSize.Width = width();
params.WindowSize.Height = height();
params.WithAlphaChannel = true;
params.ZBufferBits = 16;
// Create the Irrlicht Device with the previously specified parameters
// 創建Irrlicht設備的使用與前面指定的參數
m_Device = createDeviceEx(params);
/*
獲取視頻設備,場景管理器和用戶圖形環境的指針并存儲起來。
*/
video_Driver = m_Device->getVideoDriver();
scene_Msnsger = m_Device->getSceneManager();
guienv = m_Device->getGUIEnvironment();
//smgr->loadScene
qDebug()<< scene_Msnsger->loadScene("123.irr");
/*
Now we'll create a camera, and give it a collision response animator
that's built from the mesh nodes in the scene we just loaded.
*/
/*
現在我們將創建一個相機,給它一個碰撞響應動畫師 由網格節點的現場加載。
*/
m_Camera = scene_Msnsger->addCameraSceneNodeFPS(0,50.f,0.1f);
/*
Now we will find all the nodes in the scene and create triangle
selectors for all suitable nodes. Typically, you would want to make a
more informed decision about which nodes to performs collision checks
on; you could capture that information in the node name or Id.
*/
/*
現在我們將在現場找到的所有節點并創建三角形 選擇合適的節點。通常,您會想要 更明智的決定哪些節點執行碰撞檢查 ;你可以捕捉信息的節點名稱或Id。
*/
scene::IMetaTriangleSelector* meta = scene_Msnsger->createMetaTriangleSelector();
// core::array
// scene_Msnsger->getSceneNodeFromType(scene::ESNT_ANY, nodes);
core::array
scene_Msnsger->getSceneNodesFromType(scene::ESNT_ANY, nodes); // Find all nodes
for(u32 i =0;i { scene::ISceneNode* node = nodes[i]; scene::ITriangleSelector* selector =0; switch (node->getType()) { case scene::ESNT_CUBE: case scene::ESNT_ANIMATED_MESH: // Because the selector won't animate with the mesh, // and is only being used for camera collision, we'll just use an approximate // bounding box instead of ((scene::IAnimatedMeshSceneNode*)node)->getMesh(0) // 因為選擇器不會與網格動畫, // 和僅用于相機碰撞,我們只使用一個近似 // 邊界框代替((場景::IAnimatedMeshSceneNode *)節點)- > getMesh(0) selector = scene_Msnsger->createTriangleSelectorFromBoundingBox(node); break; case scene::ESNT_MESH: case scene::ESNT_SPHERE: selector = scene_Msnsger->createTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(),node); break; case scene::ESNT_TERRAIN: selector = scene_Msnsger->createTerrainTriangleSelector((scene::ITerrainSceneNode*)node); break; case scene::ESNT_OCTREE: selector = scene_Msnsger->createOctreeTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(),node); break; default: break; } if(selector) { meta->addTriangleSelector(selector); selector->drop(); } } /* Now that the mesh scene nodes have had triangle selectors created and added to the meta selector, create a collision response animator from that meta selector. */ /* 現在有三角形網格場景節點選擇器創建和添加 元選擇器,創建一個元的碰撞響應動畫選擇器。 */ scene::ISceneNodeAnimator* anim = scene_Msnsger->createCollisionResponseAnimator( meta,m_Camera,core::vector3df(5,5,5),core::vector3df(0,0,0)); meta->drop(); m_Camera->addAnimator(anim); anim->drop(); m_Camera->setPosition(core::vector3df(0.f,20.f,0.f)); scene::ISceneNode*cube = scene_Msnsger->getSceneNodeFromType(scene::ESNT_CUBE); if(cube) { m_Camera->setTarget(cube->getAbsolutePosition()); } m_Device->getCursorControl()->setVisible(false); connect(this,SIGNAL(sigUpdateIrrlicht(irr::IrrlichtDevice*)), this,SLOT(slotUpdateIrrlicht(irr::IrrlichtDevice*))); startTimer(0); } 保證實時刷新界面部分 void Irr_Device::slotUpdateIrrlicht(IrrlichtDevice *device) { if(device != 0) { if (isVisible() && isEnabled()/*&&device->isWindowActive()*/) { device->getTimer()->tick();//在沒有用IrrlichtDevice::run()的情況下,必須加上這句,否則鍵盤不響應 SColor color (255,100,100,140); device->getVideoDriver()->beginScene(true, true, color); device->getSceneManager()->drawAll(); device->getVideoDriver()->endScene(); } else { device->yield(); } } } void Irr_Device::timerEvent(QTimerEvent *event) { if (m_Device != NULL) { emit sigUpdateIrrlicht(m_Device); } event->accept(); } 這部分代碼暫時還不是很理解,也歡迎大神指出里面存在的問題。 軟件運行截圖如圖 Demo 鏈接:http://download.csdn.net/detail/z609932088/9504584 Qt 網絡
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。