当前位置:   article > 正文

Irrlicht学习笔记(11)--TerrainRender_irrlicht cskydomescenenode

irrlicht cskydomescenenode

说明:

本例程关于:
显示模式:网格,点云
材质模式:solid,detail mapped
天空盒与天空穹

源码:

#include <iostream>
#include <irrlicht.h>
#include "driverChoice.h"

using namespace irr;
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "irrlicht.lib")
//#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif


/**
*1.事件处理
*"W"网格模式
*"P"点云模式
*"D"solid和detail mapped材质切换
*"S"切换天空
*/
class MyEventReceiver:public IEventReceiver
{
private:
/*
*四个成员变量,
*地形节点
*天空盒节点
*圆顶天空节点
*showBox状态标记
*/
scene::ISceneNode* Terrain;
scene::ISceneNode* Skybox;
scene::ISceneNode* Skydome;
bool showBox;
public:
//构造函数,初始使用skybox
MyEventReceiver(scene::ISceneNode* terrain, scene::ISceneNode* skybox, scene::ISceneNode* skydome
):Terrain(terrain), Skybox(skybox), Skydome(skydome), showBox(true)
{
Skybox->setVisible(true);
Skydome->setVisible(false);
}
//事件处理函数
bool OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT&&!event.KeyInput.PressedDown)
{
switch (event.KeyInput.Key)
{
case irr::KEY_KEY_W://frame模式
Terrain->setMaterialFlag(video::EMF_WIREFRAME, !Terrain->getMaterial(0).Wireframe);
Terrain->setMaterialFlag(video::EMF_POINTCLOUD, false);
return true;
case irr::KEY_KEY_P://点云模式
Terrain->setMaterialFlag(video::EMF_POINTCLOUD, !Terrain->getMaterial(0).PointCloud);
Terrain->setMaterialFlag(video::EMF_WIREFRAME, false);
return true;
case irr::KEY_KEY_D://toggle detail map
Terrain->setMaterialType(Terrain->getMaterial(0).MaterialType == video::EMT_SOLID ?
video::EMT_DETAIL_MAP : video::EMT_SOLID);
return true;
case irr::KEY_KEY_S:
showBox = !showBox;
Skybox->setVisible(showBox);
Skydome->setVisible(!showBox);
return true;
default:
break;
}
}
return false;
}
};

int main(int argc, char** argv)
{
video::E_DRIVER_TYPE driverType = driverChoiceConsole();
IrrlichtDevice *Device =
createDevice(driverType, core::dimension2d<u32>(720, 455), 32,
false, true, false, 0);
if (!Device)
return 1;
Device->setWindowCaption(L"12TerrainRender");

video::IVideoDriver *driver = Device->getVideoDriver();
scene::ISceneManager *smgr = Device->getSceneManager();
gui::IGUIEnvironment *guiev = Device->getGUIEnvironment();
//2.准备
//logo,添加帮助,添加照相机
guiev->addImage(driver->getTexture("../media/irrlichtlogo2.png"), core::position2d<s32>(10, 10));
guiev->getSkin()->setFont(guiev->getFont("../media/fontlucida.png"));
guiev->addStaticText(
L"W:wire frame mode\nP:Point cloud\nD:toggle detailed map\nS:witch sky mode",
core::rect<s32>(10, 385, 250, 475), true, true, 0, -1, true);
scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0, 100.0f, 1.2f);
// camera->setPosition(core::vector3df(2700 * 2, 255 * 2, 2600 * 2));//?
camera->setPosition(core::vector3df(200,4100, 2000));
camera->setTarget(core::vector3df(2497 * 2, 243 * 2, 2700 * 2));//目标
camera->setFarValue(42000.0f);
Device->getCursorControl()->setVisible(false);

/*
*2.添加地形节点
*ISceneManager::addTerrainSceneNode()
*缩放(40,4.4,40)
*terrain-texture.jpg第一层,1次
*detailmap3.jpg纹理,20次
*/
scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
"../media/terrain-heightmap.bmp", 0, -1,
core::vector3df(0, 0, 0),/*position*/
core::vector3df(0, 0, 0),/*rotation*/
core::vector3df(400, 44, 400),/*scale*/
video::SColor(255, 255, 255, 255),/*vertexColor*/
5,/*maxLOD*/
scene::ETPS_17,/*patchSize*/
4/*smoothFactor*/
);
terrain->setMaterialFlag(video::EMF_LIGHTING, false);
terrain->setMaterialTexture(0, driver->getTexture("../media/terrain-texture.jpg"));
terrain->setMaterialTexture(1, driver->getTexture("../media/detailmap3.jpg"));
terrain->scaleTexture(1.0f, 20.0f);

/*
*3.添加碰撞检测
*三步:
*为地形创建三角形选择器
*为三角形选择器创建碰撞动画
*绑定到照相机
*/
scene::ITriangleSelector* selector =
smgr->createTerrainTriangleSelector(terrain, 0);
terrain->setTriangleSelector(selector);

scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(60, 100, 60),
core::vector3df(0, -9.8, 0),
core::vector3df(0, 50, 0));

selector->drop();
camera->addAnimator(anim);
anim->drop();


/*
*4.额外的
*获取地形数据,这里没有使用它
*/
scene::CDynamicMeshBuffer* buffer = new scene::CDynamicMeshBuffer(video::EVT_2TCOORDS,
video::EIT_16BIT);
terrain->getMeshBufferForLOD(*buffer, 0);
video::S3DVertex2TCoords* data = (video::S3DVertex2TCoords*)buffer->getVertexBuffer().getData();
//work

buffer->drop();

/*
*5.天空
*天空盒和天空穹
*/
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
scene::ISceneNode* skybox = smgr->addSkyBoxSceneNode(
driver->getTexture("../media/irrlicht2_up.jpg"),
driver->getTexture("../media/irrlicht2_dn.jpg"),
driver->getTexture("../media/irrlicht2_lf.jpg"),
driver->getTexture("../media/irrlicht2_rt.jpg"),
driver->getTexture("../media/irrlicht2_ft.jpg"),
driver->getTexture("../media/irrlicht2_bk.jpg"));
scene::ISceneNode* skydome = smgr->addSkyDomeSceneNode(
driver->getTexture("../media/skydome.jpg"),
16,/*horiRes*/
8,/*vertRes*/
0.95f,/*texturePercentange*/
2.0f/*spherePercentage*/);
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
MyEventReceiver receiver(terrain, skybox, skydome);
Device->setEventReceiver(&receiver);
int lastFPS = -1;
while (Device->run())
{
driver->beginScene(true, true, video::SColor(255, 100, 101, 140));

smgr->drawAll();
guiev->drawAll();

driver->endScene();

int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Campfire FX example [";
str += driver->getName();
str += "]FPS.",
str += fps;
Device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
Device->drop();

return 0;
}


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/133118?site
推荐阅读
相关标签
  

闽ICP备14008679号