irrlicht——1

参考网址:https://zhuanlan.zhihu.com/p/52618929

源码下载地址:http://irrlicht.sourceforge.net/?page_id=10
http://irrlicht.sourceforge.net/docu/example001.html

this tutorial shows how to set up the IDE for using the irrlicht engine and how to write a simple helloworld program with it. the program will show how to use the basics of the videodriver, the GUIEnvironment, and the SceneManager. microsoft visual studio is used as an ide, but you will also be able to understand everything if you are using a different one or even another operating system than windows.

u have to incude the header file <irrlicht.h> in order to use the engine. the header file can be found in the irrlicht engine sdk directory include.
在这里插入图片描述

to let the compiler find this header file, the directory where it is located has to be specified. this is different for every IDE and compiler you use. let’s explain shortly how to do this in microsoft visual sudio:

  1. if you use version 6.0, select the menu extras->options.

after we have set up the IDE, the compiler will know where to find the irrlicht engine header files so we can include it now in our code.

#include <irrlicht.h>

in the irrlicht engine, everything can be found in the namespace ‘irr’. so if you want to use a class of the engine, you have to write irr:: before the name of the class. for example to ue the IrrlichtDevice write irr::IrrlichtDevice. to get rid of the irr:: in front of the engine of every class, we tell the compiler that we use that namespace from now on, and we will not have to write irr:: anymore.

   using namespace irr;

there are 5 sub namespaces in the irrlich engine. take a look at them, you can read a detailed description of them in the documentation by clicking on the top menu item ‘Namespace List’ or by using this link:http://irrlicht.sourceforge.net/docu/namespaces.html
在这里插入图片描述
like the irr namespace, we do not want these 5 sub namespaces now, to keep this example simple. hence, we tell the compiler again that we do not want always to write their names.

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

to be able to use the Irrlicht.DLL file, we need to link the Irrlicht.lib. we could set this option in the project settins, but to make it easy, we use a pragma comment lib for visualstudio. on windows platforms, we have to get rid of the console window, which pops up when starting a program with main(). this is done by the second pragma. we could also use the WinMain method, though losing platform independence then.

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

this is the main method. we can use main() on every platform.

int main()
{

the most important function of the engine is the createDevice() function. the IrrlichtDevice is created by it, which is the root object for doing anything with the engine. createDevice() has 7 parameters:
1/ deviceType: type of the device. this can currently be the Null-device, one of the two software renderers, D3D8, D3D9, or OpenGL. in this example we use EDT_SOFTWARD, but to try out, might want to change it to EDT_BURNINGSVIDEO, EDT_NULL, EDT_DIRECT3D8, EDT_DIRECT3D9, or EDT_OPENGL.

2/ windowSize: size of the window or screen in FullScreenMode to be created. in this example we use 640*480.

3/ bits: Amount of color bits per pixel. This should be 16 or 32. The parameter is often ignored when running in windowed mode.

4/ fullscreen: Specifies if we want the device to run in fullscreen mode or not.

5/ stencilbuffer: Specifies if we want to use the stencil buffer (for drawing shadows).

6/ vsync: Specifies if we want to have vsync enabled, this is only useful in fullscreen mode.

7/ eventReceiver: An object to receive events. We do not want to use this parameter here, and set it to 0.

always check the return value to cope with unsupported drivers, dimensions, etc.

  IrrlichtDevice *device =
        createDevice( video::EDT_SOFTWARE, dimension2d<u32>(640, 480), 16,
            false, false, false, 0);

    if (!device)
        return 1;

set the caption of the window to some nice text. note that there is an ‘L’ in front of the string. the irrlicht engine uses wide character strings when displaying text.

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

Get a pointer to the VideoDriver, the SceneManager and the graphical user interface environment, so that we do not always have to write device->getVideoDriver(), device->getSceneManager(), or device->getGUIEnvironment().

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

we add a hello world label to the window, using the GUI environment. the text is placed at the position (10,10) as top left corner and (260,22) as lower right corner.

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

to show something interesting, we load a Quake 2 model and display it. we only have to get the mesh from the scene manager with getMesh() and add a SceneNode to display the mesh with addAnimatedMeshSceneNode(). we check the return value of getMesh() to become aware of loading problems and other errors.

instead of writing the filename sydney.md2, it would also be possible to load a Maya object file (.obj), a complete Quake3 map (.bsp) or any other supported file format. By the way, that cool Quake 2 model called sydney was modelled by Brian Collins.

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

to let the mesh look a little bit nicer, we change its material. we disable lighting because we do not have a dynamic light in here, and the mesh would be totally black otherwise. then we set the frame loop, such that the predefined STAND animation(站立的动画) is used.

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

to look at the mesh, we place a camera into 3d space at the position (0,30,-40). the camera looks from there to (0,5,0), which is approximately the place where our md2 model is.

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

Ok, now we have set up the scene, lets draw everything: We run the device in a while() loop, until the device does not want to run any more. This would be when the user closes the window or presses ALT+F4 (or whatever keycode closes a window).

while(device->run())
{

Anything can be drawn between a beginScene() and an endScene() call. The beginScene() call clears the screen with a color and the depth buffer, if desired. Then we let the Scene Manager and the GUI Environment draw their content. With the endScene() call everything is presented on the screen.

 driver->beginScene(true, true, SColor(255,100,101,140));

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

        driver->endScene();
    }

after we are done with the render loop, we have to delete the Irrlicht Device created before with createDevice(). In the Irrlicht Engine, you have to delete all objects you created with a method or function which starts with ‘create’. The object is simply deleted by calling ->drop(). See the documentation at irr::IReferenceCounted::drop() for more information.

   device->drop();

    return 0;
}

that is it. compile and run.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值