Damit der Einstieg in die Irrlicht Engine gelingt, folgt hier eine detaillierte Anweisung und ein konkretes Beispiel:
include
-Verzeichnis der Irrlicht-Engine zu "Externe
Includeverzeichnisse" hinzu (z.B. E:\Projects\irrlicht-1.8.5\include
).
lib
-Verzeichnis der Irrlicht-Engine zu "Zusätzliche
Bibliotheksverzeichnisse" hinzu (z.B.
E:\Projects\irrlicht-1.8.5\lib\Win64-visualStudio
).Irrlicht.lib
zu den "Zusätzlichen Abhängigkeiten" hinzu.
main.cpp
main.cpp
ein:
Lichtquelle hinzufügen:
Animiertes Mesh laden:
sydney.md2
)
wird geladen. Sie müssen die Datei
sydney.md2
und die entsprechende Textur
sydney.bmp
in Ihrem
Projektverzeichnis haben.Animierten Mesh-Szenenknoten erstellen:
Kamera hinzufügen:
Mauszeiger ausblenden:
Hauptschleife:
Nun kommt etwas sehr Wichtiges:
In ihr Release-Verzeichnis mit
der ausführbaren Datei müssen Sie die Datei Irrlicht.dll und die
Resourcen-Dateien sydney.bmp und sydney.md2 manuell hinzufügen.
Sie finden diese Dateien hier:
...\irrlicht-1.8.5\bin\Win64-VisualStudio
...\irrlicht-1.8.5\media
Experimentieren Sie mit diesem Code, damit Sie die
Engine gut kennenlernen.Sie können andere Mesh-Dateien und Texturen verwenden,
wenn Sie möchten. Stellen Sie sicher, dass die Pfade zu den Dateien korrekt
sind. Um weitere Animationen zu verwenden, können Sie die
setMD2Animation
-Methode
mit verschiedenen Animationstypen (EMAT_*
)
aufrufen.
Im Verzeichnis ...\irrlicht-1.8.5\examples
finden eine Menge Beispiele für den Einstieg.
Dr. Erhard Henkes 07.09.2008 (unverändert)
<?xml version="1.0"?> <config> <!--This is a config file for the Irrlicht Engine.--> <settings width = "400" height = "300" fullscreen = "0" vsync = "0"> </settings> </config> |
#include "stdafx.h" #include <iostream> #include <limits> #include <irrlicht.h> using namespace irr; using namespace core; using namespace scene; using namespace video; using namespace io; using namespace gui; using namespace std; #pragma comment(lib, "Irrlicht.lib") void wait() // prohibits "unexpected shutdown" of console { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.get(); } void drawScene(IVideoDriver* driver, ISceneManager* smgr, IGUIEnvironment* guienv) { driver->beginScene(true, true, SColor(0,200,200,200)); smgr->drawAll(); guienv->drawAll(); driver->endScene(); } s32 main() { s32 screenWidth, screenHeight, fullScreen, vSync; IrrXMLReader* xml = createIrrXMLReader("settings.xml"); while(xml && xml->read()) { switch(xml->getNodeType()) { case EXN_ELEMENT: { if (!strcmp("settings", xml->getNodeName())) { screenWidth = xml->getAttributeValueAsInt("width"); screenHeight = xml->getAttributeValueAsInt("height"); fullScreen = xml->getAttributeValueAsInt("fullscreen"); vSync = xml->getAttributeValueAsInt("vsync"); //... } } break; } } delete xml; IrrlichtDevice* device = createDevice( EDT_DIRECT3D9, // Type of the device. dimension2d<s32>(screenWidth,screenHeight), // Size of the window or the video mode in fullscreen mode. 16, // Bits per pixel in fullscreen mode. Ignored if windowed mode. fullScreen, // fullscreen false, // stencilbuffer vSync, // vsync 0); // IEventReceiver* IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IGUIEnvironment* guienv = device->getGUIEnvironment(); ICursorControl* cursor = device->getCursorControl(); ILogger* logger = device->getLogger(); u32 scene = 0; while(device->run()) { if(device->isWindowActive()) { stringw strc = "Scene: "; strc += (++scene); device->setWindowCaption(strc.c_str()); drawScene(driver, smgr, guienv); stringc logstr = "Scene: "; logstr += (scene); logstr += " FPS: "; logstr += driver->getFPS(); logger->log(logstr.c_str()); if( scene >= 10000 ) break; } else device->yield(); } device->drop(); cout << "GAME OVER - Press ENTER (twice), please. Thank you for playing this game." << endl; wait(); } |
//... ILogger* logger = device->getLogger(); IGUIStaticText* text = guienv->addStaticText(L"", rect<int>(10,10,200,22), true); u32 scene = 0; while(device->run()) { if(device->isWindowActive()) { stringw strc = "Scene: "; strc += (++scene); device->setWindowCaption(strc.c_str()); text->setText(strc.c_str()); drawScene(driver, smgr, guienv); //... |
IGUIStaticText* text = guienv->addStaticText(L"", rect<int>(10,10,200,22), true); ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(); // First Person Shooter Camera ITextSceneNode* nodeText = smgr->addTextSceneNode(guienv->getFont("bigfont.png"),L"hello, world"); nodeText->setTextColor(SColor(255,255,0,0)); nodeText->setPosition(vector3df(0,0,0)); u32 scene = 0; while(device->run()) |
// IrrSpiel.cpp from www.HenkesSoft.de // STEP 1 // // Irrlicht types cf. irrTypes.h, e.g. // s32: __int32, u32: unsigned __int32, // f32: float, f64: double #include "stdafx.h" // MS VisualC++ #include <iostream> // cout, cin, endl #include <limits> // wait() #include <irrlicht.h> // grafic engine (free) #include <irrklang.h> // sound system (free for private use) // irrlicht namespace using namespace irr; using namespace core; using namespace scene; using namespace video; using namespace io; using namespace gui; using namespace std; // linker commands #pragma comment(lib, "irrlicht.lib") #pragma comment(lib, "irrklang.lib") void wait() // prohibits "unexpected shutdown" of console { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.get(); } void drawScene(IVideoDriver* driver, ISceneManager* smgr, IGUIEnvironment* guienv) { driver->beginScene(true, true, SColor(0,200,200,200)); smgr->drawAll(); guienv->drawAll(); driver->endScene(); } // entry point of program s32 main() { // start Irrlicht IrrlichtDevice* device = createDevice( EDT_DIRECT3D9, dimension2d<s32>(800,600),32,0,0,0,0); IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IGUIEnvironment* guienv = device->getGUIEnvironment(); device->getCursorControl()->setVisible(false); // no mouse cursor u32 scene = 0; u32 fps = 0; /********************* "device->run()" with FPS and Info ********************/ while(device->run()) { if(device->isWindowActive()) { // draw scene drawScene(driver, smgr, guienv); ++scene; // deliver information in window caption fps = driver->getFPS(); stringw str = L"HenkesSoft \"MUHAHA Code1\" FPS: "; str += fps; str += " Scene: "; str += scene; device->setWindowCaption(str.c_str()); if( scene >= 10000 ) break; }//if }//while device->drop(); // delete object cout << "GAME OVER - Press ENTER (twice), please. Thank you for playing this game." << endl; wait(); // windows console specialty ;-) }//main |
IrrSpiel - STEP1 |
driver->getFPS() besorgt die Frames per Second. Diese Information ist sehr wichtig, um den Bewegungsfluss einheitlich steuern zu können.
while(device->run())
{
if(device->isWindowActive())
{
// draw scene
// further actions
}
}
ist ein typisches Vorgehen, um Darstellungen auf den Bildschirm zu bringen.
Im Window-Modus kann man die Titelzeile (caption) ebenfalls zur Ausgabe von Informationen nutzen.
fps = driver->getFPS();
stringw str = L"HenkesSoft \"MUHAHA Code1\" FPS: ";
str += fps;
str += " Scene: ";
str += scene;
device->setWindowCaption(str.c_str());
s32 main() { // start Irrlicht IrrlichtDevice* device = createDevice( EDT_DIRECT3D9, dimension2d<s32>(800,600),32,0,0,0,0); IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IGUIEnvironment* guienv = device->getGUIEnvironment(); device->getCursorControl()->setVisible(false); // no mouse cursor |
// "Skybox" six times "black sky with some stars" (512*512 pixel) ISceneNode* SkyBox = smgr->addSkyBoxSceneNode( driver->getTexture("../skybox/top.jpg" ), driver->getTexture("../skybox/bottom.jpg" ), driver->getTexture("../skybox/left.jpg" ), driver->getTexture("../skybox/right.jpg" ), driver->getTexture("../skybox/front.jpg" ), driver->getTexture("../skybox/back.jpg" )); // Camera ICameraSceneNode* cam = smgr->addCameraSceneNode(0, vector3df(0,1000,0), vector3df(0,0,0)); |
u32 scene = 0; u32 fps = 0; |
IrrSpiel - STEP2 |
// IrrSpiel.cpp from www.HenkesSoft.de // STEP 2 // // Irrlicht types cf. irrTypes.h, e.g. // s32: __int32, u32: unsigned __int32, // f32: float, f64: double #include "stdafx.h" // MS VisualC++ #include <ctime> // clock, time #include <cstdlib> // srand, rand #include <iostream> // cout, cin, endl #include <limits> // wait() #include <irrlicht.h> // grafic engine (free) #include <irrklang.h> // sound system (free for private use) // irrlicht namespace using namespace irr; using namespace core; using namespace scene; using namespace video; using namespace io; using namespace gui; using namespace std; // linker commands #pragma comment(lib, "irrlicht.lib") #pragma comment(lib, "irrklang.lib") void wait() // prohibits "unexpected shutdown" of console { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.get(); } void drawScene(IVideoDriver* driver, ISceneManager* smgr, IGUIEnvironment* guienv) { driver->beginScene(true, true, SColor(0,200,200,200)); smgr->drawAll(); guienv->drawAll(); driver->endScene(); } // entry point of program s32 main() { // start Irrlicht IrrlichtDevice* device = createDevice( EDT_DIRECT3D9, dimension2d<s32>(800,600),32,0,0,0,0); IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IGUIEnvironment* guienv = device->getGUIEnvironment(); device->getCursorControl()->setVisible(false); // no mouse cursor // "Skybox" six times "black sky with some stars" (512*512 pixel) ISceneNode* SkyBox = smgr->addSkyBoxSceneNode( driver->getTexture("../skybox/top.jpg" ), driver->getTexture("../skybox/bottom.jpg" ), driver->getTexture("../skybox/left.jpg" ), driver->getTexture("../skybox/right.jpg" ), driver->getTexture("../skybox/front.jpg" ), driver->getTexture("../skybox/back.jpg" )); ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(); // SpaceShip //http://www.turbosquid.com/FullPreview/Index.cfm/ID/261430 (free) const vector3df STARTPOSSPACESHIP = vector3df(-200,0,0); // Texture SMaterial matSpaceShip; matSpaceShip.setTexture(0, driver->getTexture("../media/orange.jpg")); matSpaceShip.Lighting = false; // Mesh IAnimatedMeshSceneNode* nodeSpaceShip; IAnimatedMesh* meshSpaceShip = smgr->getMesh("../media/spaceship.3ds"); if (meshSpaceShip) { nodeSpaceShip = smgr->addAnimatedMeshSceneNode(meshSpaceShip); nodeSpaceShip->setPosition(STARTPOSSPACESHIP); nodeSpaceShip->getMaterial(0) = matSpaceShip; nodeSpaceShip->setMaterialFlag(EMF_LIGHTING, false); } u32 scene = 0; u32 fps = 0; /********************* "device->run()" with FPS and Info ********************/ while(device->run()) { if(device->isWindowActive()) { // draw scene drawScene(driver, smgr, guienv); ++scene; // deliver information in window caption fps = driver->getFPS(); stringw str = L"HenkesSoft \"MUHAHA Code1\" FPS: "; str += fps; str += " Scene: "; str += scene; device->setWindowCaption(str.c_str()); if( scene >= 10000 ) /**/; //damit wir mehr als 10000 "scenes" haben. } }//while device->drop(); // delete object cout << "GAME OVER - Press ENTER (twice), please. Thank you for playing this game." << endl; wait(); // windows console specialty ;-) }//main |
// IrrSpiel.cpp from www.HenkesSoft.de
|
static clock_t last_t; if( clock() > ( last_t + CLOCKS_PER_SEC / MAXSHOOTS_PER_SEC) ) { // action last_t = clock(); } |
// 3DViewer.cpp from www.HenkesSoft.de // // Irrlicht types cf. irrTypes.h, e.g. // s32: __int32, u32: unsigned __int32, // f32: float, f64: double #include "stdafx.h" // MS VisualC++ #include <iostream> // cout, cin, endl #include <limits> // wait() #include <irrlicht.h> // grafic engine (free) // irrlicht namespace using namespace irr; using namespace core; using namespace scene; using namespace video; using namespace io; using namespace gui; // C++ namespace using namespace std; // linker commands #pragma comment(lib, "irrlicht.lib") #pragma comment(lib, "irrklang.lib") //void wait(){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cin.get(); } // Global Objects and variables IrrlichtDevice* device = 0; IGUIEnvironment* guienv = 0; void drawScene(IVideoDriver* driver, ISceneManager* smgr, IGUIEnvironment* guienv){ driver->beginScene(true, true, SColor(0,255,255,255)); smgr->drawAll(); guienv->drawAll(); driver->endScene(); } void setCaption( IrrlichtDevice* device, IVideoDriver* driver, ICameraSceneNode* nodeCam) { vector3df camPos = nodeCam->getAbsolutePosition(); stringw str = L"HenkesSoft \"3D-Viewer 0.91\" ["; str += driver->getName(); str += "] FPS: "; str += driver->getFPS(); str += " X: "; str += s32(camPos.X); str += " Y: "; str += s32(camPos.Y); str += " Z: "; str += s32(camPos.Z); device->setWindowCaption(str.c_str()); } // Event Handler class MyEventReceiver : public IEventReceiver { public: virtual bool OnEvent(const SEvent& event) { if (event.EventType == EET_KEY_INPUT_EVENT) KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown; if (event.EventType == EET_MOUSE_INPUT_EVENT) MouseEvent = FetchMouseInput(event); if (event.EventType == EET_GUI_EVENT) { s32 id = event.GUIEvent.Caller->getID(); IGUIEnvironment* guienv = device->getGUIEnvironment(); switch(event.GUIEvent.EventType) { case EGET_BUTTON_CLICKED: return true; break; default: break; } } return false; } SEvent::SMouseInput GetMouseInput(){return MouseEvent;} virtual bool IsKeyDown(EKEY_CODE keyCode) const {return KeyIsDown[keyCode];} MyEventReceiver(){for(u32 i=0; i<KEY_KEY_CODES_COUNT; ++i) KeyIsDown[i] = false;} private: bool KeyIsDown[KEY_KEY_CODES_COUNT]; SEvent::SMouseInput FetchMouseInput(const SEvent& event){return event.MouseInput;} SEvent::SMouseInput MouseEvent; }; s32 main() { // ************ Setup Irrlicht *************** // let user select driver type E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9; cout << "Please select the driver you want for this example:\n"\ " (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\ " (d) Software Renderer\n (e) Burning's Software Renderer\n"\ " (f) NullDevice\n (otherKey) exit\n\n" << endl; char i; std::cin >> i; switch(i) { case 'a': driverType = EDT_DIRECT3D9; break; case 'b': driverType = EDT_DIRECT3D8; break; case 'c': driverType = EDT_OPENGL; break; case 'd': driverType = EDT_SOFTWARE; break; case 'e': driverType = EDT_BURNINGSVIDEO; break; case 'f': driverType = EDT_NULL; break; default: return 0; } MyEventReceiver receiver; device = createDevice(driverType, dimension2d<s32>(1024,768),32,0,0,0,&receiver); IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); guienv = device->getGUIEnvironment(); device->getCursorControl()->setVisible(false); // mouse cursor // GUI IGUISkin* skin = guienv->getSkin(); //IGUIFont* font = guienv->getFont("../media/fonthaettenschweiler.bmp"); //if (font) skin->setFont(font); //skin->setFont(guienv->getBuiltInFont(), EGDF_TOOLTIP); // Camera ICameraSceneNode* nodeCam = smgr->addCameraSceneNodeMaya(); vector3df camPos = vector3df(-500, -2000, 0); vector3df camTar = vector3df(-500, -2000, 0); nodeCam->setPosition(camPos); nodeCam->setTarget(camTar); // general light ILightSceneNode* light0 = smgr->addLightSceneNode(0, vector3df(-1000, 0, 0), SColorf(1.0f, 1.0f, 1.0f, 1.0f), 10000.0f); ILightSceneNode* light1 = smgr->addLightSceneNode(0, vector3df( 1000, 0, 0), SColorf(1.0f, 1.0f, 1.0f, 1.0f), 10000.0f); ILightSceneNode* light2 = smgr->addLightSceneNode(0, vector3df( 0, 0,-1000), SColorf(1.0f, 1.0f, 1.0f, 1.0f), 10000.0f); ILightSceneNode* light3 = smgr->addLightSceneNode(0, vector3df( 0, 0, 1000), SColorf(1.0f, 1.0f, 1.0f, 1.0f), 10000.0f); ILightSceneNode* light4 = smgr->addLightSceneNode(0, vector3df( 0, 1000, 0), SColorf(1.0f, 1.0f, 1.0f, 1.0f), 10000.0f); ILightSceneNode* light5 = smgr->addLightSceneNode(0, vector3df( 0,-1000, 0), SColorf(1.0f, 1.0f, 1.0f, 1.0f), 10000.0f); // add animated character IAnimatedMesh* mesh = smgr->getMesh("test.x"); IAnimatedMeshSceneNode* nodeCharacter = smgr->addAnimatedMeshSceneNode(mesh); nodeCharacter->setPosition( vector3df(0,-800,0) ); nodeCharacter->setAnimationSpeed(15); nodeCharacter->addShadowVolumeSceneNode(); // add shadow smgr->setShadowColor(SColor(150,0,0,0)); nodeCharacter->setScale(vector3df(1,1,1)); // scale nodeCharacter->setRotation(vector3df(0,0,0)); // rotation nodeCharacter->setMaterialFlag(EMF_NORMALIZE_NORMALS, true); // normalize for correct lighting // general preparations u32 scene = 0; u32 fps = 0; /********************* "device->run()" with FPS and Info ********************/ while(device->run()) { if(device->isWindowActive()) { // key input if (receiver.IsKeyDown(KEY_KEY_1)){/*...*/} // mouse input SEvent::SMouseInput emi = receiver.GetMouseInput(); // LEFTMOUSE // if (emi.Event == EMIE_LMOUSE_PRESSED_DOWN) {} // draw scene drawScene(driver, smgr, guienv); // deliver information in window caption setCaption(device, driver, nodeCam); } }//while device->drop(); // delete object //cout << "Press ENTER (twice), please. Thank you for using this program." << endl; //wait(); // windows console specialty ;-) }//main |