irrlicht 3d游戏编程1第三人称摄像机

近来有空,打算写点游戏编程的文章。

今天第一篇,关于第三人称摄像机的。

先看效果


按下鼠标中键,左右可旋转,上下可倾斜。


下面是代码

#include   <windows.h>

#include <string.h>  // strcpy_s




#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif



#include "my3rdcamera.h"

My3rdCamera* myCamera;

class EventReceiver: public irr::IEventReceiver { 
        public: 
                
		virtual      bool OnEvent (const irr::SEvent& e){
				
			if(myCamera) myCamera->OnEvent(e); //传入事件到camera里处理
			return false; 
		}
};

 EventReceiver e;


int _tmain(int argc, _TCHAR* argv[])
{
 
	// create device

	IrrlichtDevice *device = createDevice(EDT_OPENGL,
			core::dimension2d<u32>(640, 480), 16, false);
		
	if (device == 0)
		return 1; // could not create selected driver.

	device->setEventReceiver(&e);

	device->setWindowCaption(L"Irrlicht Engine Demo");

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();






	scene::IAnimatedMesh* roomMesh = smgr->getMesh("plane.ms3d");
	scene::ISceneNode* room = 0; 

	if (roomMesh)
	{
		room = smgr->addMeshSceneNode(roomMesh);

		room->setScale(vector3df(1000,1000,1000));
 

	 	room->setMaterialFlag(video::EMF_LIGHTING, false);
 
 
	}



	scene::IAnimatedMeshSceneNode* node = 0;
		video::SMaterial material;
		// Add an MD2 node, which uses vertex-based animation.
	node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("faerie.md2"),
						0 );
	node->setPosition(core::vector3df(0,22,0)); // Put its feet on the floor.
	node->setScale(core::vector3df(1.6f)); // Make it appear realistically scaled
	node->setMD2Animation(scene::EMAT_POINT);
	node->setAnimationSpeed(20.f);
	material.setTexture(0, driver->getTexture("faerie2.bmp"));
	material.Lighting = false;
	material.NormalizeNormals = true;
	node->getMaterial(0) = material;




	 // setup the camera 
	myCamera=new My3rdCamera(device,node); //传入device和要跟随的角色节点

	//// add fps camera
	//scene::ICameraSceneNode* fpsCamera = smgr->addCameraSceneNodeFPS();
	//fpsCamera->setPosition(core::vector3df(-50,50,-150));


   
	u32 frames=0;
	while(device->run())
	{
		driver->beginScene(true, true, video::SColor(0,100,100,100));
		
		myCamera->update(); //处理camera

		smgr->drawAll();

		driver->endScene();
		if (++frames==100)
		{
			core::stringw str = L"Irrlicht Engine [";
			str += driver->getName();
			str += L"] FPS: ";
			str += (s32)driver->getFPS();

			device->setWindowCaption(str.c_str());
			frames=0;
		}
	}

	device->drop();
	
	return 0;
}


My3rdCamera 在github 有, https://github.com/344717871/3drpgdemo/tree/master/client/My3rdCamera.h  

https://github.com/344717871/3drpgdemo/tree/master/client/My3rdCamera.cpp


faerie.md2 是irr自带的3d模型,plane.ms3d就是一个矩形平面,作为游戏的地面。
相关文章
相关标签/搜索