2009年4月3日 星期五

IrrLicht Example Learn: 01.HelloWorld


環境: VS 2005


01.HelloWord Example Learn:

1.
創建引擎

.createDevice( video::EDT_SOFTWARE, dimension2d(640, 480), 16,
false, false, false, 0);

IRRLICHT_API IrrlichtDevice* IRRCALLCONV createDevice(

video::E_DRIVER_TYPE deviceType = video::EDT_SOFTWARE,
const core::dimension2d< s32 > & windowSize = (core::dimension2d< s32 >(640, 480)),
u32 bits = 16,
bool fullscreen = false,
bool stencilbuffer = false,
bool vsync = false,
IEventReceiver * receiver = 0)


Creates an Irrlicht device. The Irrlicht device is the root object for using the engine.

If you need more parameters to be passed to the creation of the Irrlicht Engine device, use the createDeviceEx() function.

Parameters:

deviceType: Type of the device. This can currently be video::EDT_NULL, video::EDT_SOFTWARE,
video::EDT_BURNINGSVIDEO, video::EDT_DIRECT3D8, video::EDT_DIRECT3D9 and
video::EDT_OPENGL.
windowSize: Size of the window or the video mode in fullscreen mode.
bits: Bits per pixel in fullscreen mode. Ignored if windowed mode.
fullscreen: Should be set to true if the device should run in fullscreen. Otherwise the device runs in
windowed mode.
stencilbuffer: Specifies if the stencil buffer should be enabled. Set this to true, if you want the engine
be able to draw stencil buffer shadows. Note that not all devices are able to use the stencil buffer.
If they don’t no shadows will be drawn.
vsync: Specifies vertical syncronisation: If set to true, the driver will wait for the vertical retrace
period, otherwise not.
receiver: A user created event receiver.

Returns:

Returns pointer to the created IrrlichtDevice or null if the device could not be created.


2.
設置窗口標題

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

3.取得顯示指標, 場景管理指標, GUI介面指標.

IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();


4. 顯示文字在rect(10,10,100,22) 位置.

guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
rect(10,10,100,22), true);

第三個參數表示顯示文字邊框

5. 加載一個md2模型, 掛載到場景節點.

IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
if (!mesh)
return 1;
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );

virtual IAnimatedMeshSceneNode* irr::scene::ISceneManager::addAnimatedMeshSceneNode (

IAnimatedMesh * mesh,
ISceneNode * parent = 0,
s32 id = -1,
const core::vector3df & position = core::vector3df(0, 0, 0),
const core::vector3df & rotation = core::vector3df(0, 0, 0),
const core::vector3df & scale = core::vector3df( 1.0f , 1.0f , 1.0f ),
bool alsoAddIfMeshPointerZero = false)


Adds a scene node for rendering an animated mesh model.

Parameters:

mesh: Pointer to the loaded animated mesh to be displayed.
parent: Parent of the scene node. Can be NULL if no parent.
id: Id of the node. This id can be used to identify the scene node.
position: Position of the space relative to its parent where the scene node will be placed.
rotation: Initital rotation of the scene node.
scale: Initial scale of the scene node.
alsoAddIfMeshPointerZero: Add the scene node even if a 0 pointer is passed.


Returns:

Pointer to the created scene node. This pointer should not be dropped. See IReferenceCounted::drop() for more information.

6. 設定模型材質

if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
node->setMD2Animation(scene::EMAT_STAND);
node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") );
}

首先是關閉了Material的動態光源. (這部份目前還是不太清楚用法)
再來設定播放MD2的站立動畫, 這裡EMAT_STAND 是只MD2模型中的站立部分動畫.
一個完整的MD2模型應該包含以下的動作.

enum EMD2_ANIMATION_TYPE
{
EMAT_STAND = 0,
EMAT_RUN,
EMAT_ATTACK,
EMAT_PAIN_A,
EMAT_PAIN_B,
EMAT_PAIN_C,
EMAT_JUMP,
EMAT_FLIP,
EMAT_SALUTE,
EMAT_FALLBACK,
EMAT_WAVE,
EMAT_POINT,
EMAT_CROUCH_STAND,
EMAT_CROUCH_WALK,
EMAT_CROUCH_ATTACK,
EMAT_CROUCH_PAIN,
EMAT_CROUCH_DEATH,
EMAT_DEATH_FALLBACK,
EMAT_DEATH_FALLFORWARD,
EMAT_DEATH_FALLBACKSLOW,
EMAT_BOOM,

//! Not an animation, but amount of animation types.
EMAT_COUNT
};

PS:

要播放跑步的動畫則程式應改為: node->setMD2Animation(scene::EMAT_RUN);
也可以直接指定要播放第幾張到第幾張:
node->setFrameLoop(0, 310);


然後是設定模型的第0層紋理.

void irr::scene::ISceneNode::setMaterialTexture (u32 textureLayer, video::ITexture *
texture)


textureLayer
指設定貼圖的紋理層. (關於textureLayer 的應用需要再學習).

7.設置照相機的位置

smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));


設定攝影機的位置, 方向.父節點.

virtual ICameraSceneNode* irr::scene::ISceneManager::addCameraSceneNode(

ISceneNode * parent = 0,
const core::vector3df & position = core::vector3df(0, 0, 0),
const core::vector3df & lookat = core::vector3df(0, 0, 100),
s32 id = -1)


Adds a camera scene node to the scene graph and sets it as active camera.
This camera does not react on user input like for example the one created with
addCameraSceneNodeFPS(). If you want to move or animate it, use animators or the ISceneNode::setPosition(), ICameraSceneNode::setTarget() etc methods.

Parameters:

position: Position of the space relative to its parent where the camera will be placed.
lookat: Position where the camera will look at. Also known as target.
parent: Parent scene node of the camera. Can be null. If the parent moves, the camera will move too.
id: id of the camera. This id can be used to identify the camera.

Returns:

Pointer to interface to camera if successful, otherwise 0. This pointer should not be dropped. See IReferenceCounted::drop() for more information.

8. 主循環

while(device->run())
{
driver->beginScene(true, true, SColor(255,100,101,140));

smgr->drawAll();
guienv->drawAll();

driver->endScene();
}

.

9. 刪除引擎

device->drop();





沒有留言: