main.cpp
Code: Select all
#include <irrlicht.h>
#include <iostream>
#include "driver.cpp"
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
video::E_DRIVER_TYPE askDriver();
int main()
{
video::E_DRIVER_TYPE driverType = askDriver();
// create device and exit if creation failed
IrrlichtDevice *device =
createDevice(driverType, core::dimension2d<s32>(1024, 768));
if (device == 0)
return 1; // could not create selected driver.
/*
Get a pointer to the video driver and the SceneManager so that
we do not always have to write device->getVideoDriver() and
device->getSceneManager().
*/
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
/*
To display the Quake 3 map, we first need to load it. Quake 3 maps
are packed into .pk3 files wich are nothing other than .zip files.
So we add the .pk3 file to our FileSystem. After it was added,
we are able to read from the files in that archive as they would
directly be stored on disk.
*/
device->getFileSystem()->addZipFileArchive("media/map-20kdm2.pk3");
/*
Now we can load the mesh by calling getMesh(). We get a pointer returned
to a IAnimatedMesh. As you know, Quake 3 maps are not really animated,
they are only a huge chunk of static geometry with some materials
attached. Hence the IAnimated mesh consists of only one frame,
so we get the "first frame" of the "animation", which is our quake level
and create an OctTree scene node with it, using addOctTreeSceneNode().
The OctTree optimizes the scene a little bit, trying to draw only geometry
which is currently visible. An alternative to the OctTree would be a
AnimatedMeshSceneNode, which would draw always the complete geometry of
the mesh, without optimization. Try it out: Write addAnimatedMeshSceneNode
instead of addOctTreeSceneNode and compare the primitives drawed by the
video driver. (There is a getPrimitiveCountDrawed() method in the
IVideoDriver class). Note that this optimization with the Octree is only
useful when drawing huge meshes consiting of lots of geometry.
*/
scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
scene::ISceneNode* node = 0;
if (mesh)
node = smgr->addOctTreeSceneNode(mesh->getMesh(0));
/*
Because the level was modelled not around the origin (0,0,0), we translate
the whole level a little bit.
*/
scene::ITriangleSelector* selector = 0;
if (node)
{
node->setPosition(core::vector3df(-1300,-144,-1249));
selector = smgr->createOctTreeTriangleSelector(
mesh->getMesh(0), node, 128);
node->setTriangleSelector(selector);
selector->drop();
}
/*
Now we only need a Camera to look at the Quake 3 map.
And we want to create a user controlled camera. There are some
different cameras available in the Irrlicht engine. For example the
Maya Camera which can be controlled compareable to the camera in Maya:
Rotate with left mouse button pressed, Zoom with both buttons pressed,
translate with right mouse button pressed. This could be created with
addCameraSceneNodeMaya(). But for this example, we want to create a
camera which behaves like the ones in first person shooter games (FPS).
*/
scene::ICameraSceneNode* camera =
camera = smgr->addCameraSceneNodeFPS(0,100.0f,300.0f);
/* The follow code adds the collision detection for the camera. */
scene::ISceneNodeAnimator* anim =
smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(30,50,30),
core::vector3df(0,-3,0),
core::vector3df(0,50,0));
camera->addAnimator(anim);
anim->drop();
/*
The mouse cursor needs not to be visible, so we make it invisible.
*/
device->getCursorControl()->setVisible(false);
/*
We have done everything, so lets draw it. We also write the current
frames per second and the drawn primitives to the caption of the
window. The 'if (device->isWindowActive())' line is optional, but
prevents the engine render to set the position of the mouse cursor
after task switching when other program are active.
*/
int lastFPS = -1;
while(device->run())
if (device->isWindowActive())
{
driver->beginScene(true, true, video::SColor(0,200,200,200));
smgr->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Irrlicht Engine - Quake 3 Map example [";
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
/*
In the end, delete the Irrlicht device.
*/
device->drop();
return 0;
}
driver.cpp
Code: Select all
#include <iostream>
#include <irrlicht.h>
using namespace irr;
video::E_DRIVER_TYPE askDriver()
{
printf("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) Apfelbaum Software Renderer\n"\
" (f) NullDevice\n (otherKey) exit\n\n");
char i;
std::cin >> i;
video::E_DRIVER_TYPE driverPick;
switch(i)
{
case 'a': driverPick = video::EDT_DIRECT3D9;break;
case 'b': driverPick = video::EDT_DIRECT3D8;break;
case 'c': driverPick = video::EDT_OPENGL; break;
case 'd': driverPick = video::EDT_SOFTWARE; break;
case 'e': driverPick = video::EDT_SOFTWARE2;break;
case 'f': driverPick = video::EDT_NULL; break;
default: return driverPick = video::EDT_DIRECT3D9;
}
return driverPick;
}
Just me modifying the Quake 3 Level Loader example.