Irrlicht code snippets

Tuesday, October 6, 2009

WRITE IMAGE TO FILE:

Code:

IImage* image = Device.driver->createScreenShot();

if (image)
{
Device.driver->writeImageToFile(image, "c:\\images\\dump.jpeg");
Device.driver->writeImageToFile(image, "c:\\images\\dump.png");
Device.driver->writeImageToFile(image, "c:\\images\\dump.bmp");
Device.driver->writeImageToFile(image, "c:\\images\\dump.tga");
Device.driver->writeImageToFile(image, "c:\\images\\dump.jpg");
image->drop();
}

LOAD IMAGE (256X256) FROM FILE, CHANGE PIXEL AND SAVE:
Code:

video::ITexture * image1 = driver->getTexture("d:\\x\\map.bmp");
//driver->makeColorKeyTexture(image1, core::position2d(0,0));
u8 * ff=(u8 *)image1->lock();
IImage* im = driver->createImageFromData(image1->getColorFormat(), dimension2d(256,256), ff);
im->setPixel(100,100,SColor(255,255,0,0));
driver->writeImageToFile(im,"D:\\x\\map.bmp");
im->drop();

CHANGE CAPTION FUNCTION:
Code:

void showCaptionVar(std::string txt)
{
std::string gio1=txt;
char gio2[512];
strcpy_s(gio2, gio1.c_str());
core::stringw gio3 = gio2;
device->setWindowCaption(gio3.c_str());
}

EVENTS:
Code:

class mvm:public IEventReceiver
{
public:
bool OnEvent(const SEvent &e)
{
static bool pwheel=false;
if (e.EventType==EEVENT_TYPE::EET_MOUSE_INPUT_EVENT)
{
if (e.MouseInput.Wheel==-1)
{
}else if (e.MouseInput.Wheel==1)
{
}
if (e.MouseInput.Event==irr::EMIE_RMOUSE_PRESSED_DOWN)
{
mox=e.MouseInput.X;
moy=e.MouseInput.Y;
}
if (e.MouseInput.Event==irr::EMIE_RMOUSE_LEFT_UP)
{
}
if (e.MouseInput.Event==irr::EMIE_MOUSE_MOVED)
{
}
}
if(e.MouseInput.Wheel)
{
if (pwheel==false)
{
//middle button down event
pwheel=true;
}else{
//middle button up event
pwheel=false;
}
}
if (e.EventType==irr::EEVENT_TYPE::EET_GUI_EVENT)
{
s32 id = e.GUIEvent.Caller->getID();
switch (e.GUIEvent.EventType)
{
case gui::EGUI_EVENT_TYPE::EGET_BUTTON_CLICKED:
{
if (id==2)
{

}
break;
}
case gui::EGUI_EVENT_TYPE::EGET_ELEMENT_FOCUSED
{
if (id==2)
{
//gui::
z=z+10;
}
break;
}
}
}
if (e.EventType==EET_KEY_INPUT_EVENT)
{
switch (e.KeyInput.Key)
{
case KEY_KEY_W:
{
wasd[0]=e.KeyInput.PressedDown;
break;
}
case KEY_ESCAPE:
{
device->closeDevice();
break;
}
return true;
}
return false;
}
return false;
}
};

BASIC VARIABLES:
Code:

#include "irrlicht.h"
#include
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif

IrrlichtDevice * device;
video::IVideoDriver * driver;
scene::ISceneManager * smgr;
scene::ICameraSceneNode * cam;

irr::SIrrlichtCreationParameters pars;
pars.EventReceiver=&receiver;
pars.AntiAlias=true;
pars.Fullscreen=true;
pars.DriverType=video::EDT_DIRECT3D9;
pars.WindowSize=core::dimension2d(1024,768);
pars.Bits=32;
device = createDeviceEx(pars);
driver = device->getVideoDriver();
driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);

smgr = device->getSceneManager();
cam= smgr->addCameraSceneNode();
cam->setFarValue(50000.0f);

COLLISION DETECTION WITH BOUNDING BOXES
Code:

bool collision(ISceneNode* one, ISceneNode* two) {
aabbox3d b1, b2;

b1 = one->getBoundingBox ();
b2 = two->getBoundingBox ();

one->getRelativeTransformation().transformBoxEx( b1 );
two->getRelativeTransformation().transformBoxEx( b2 );
return b1.intersectsWithBox( b2 );
}
//OTHER WAY
//bool collision(ISceneNode* one, ISceneNode* two) {
//if(one->getTransformedBoundingBox().intersectsWithBox(two->getTransformedBoundingBox())) {
//return (one->getTransformedBoundingBox().intersectsWithBox(two->getTransformedBoundingBox()));
//}
//return false;
//}

SHOW BOUNDING BOX (debugging)
Code:

node->setDebugDataVisible(scene::EDS_BBOX);

MOVE NODE
Code:

core::vector3df v=node->getPosition();
v.X+=1.0f;
node->setPosition(vector3df(v));

TEXTURES
Code:

irr::video::ITexture * texture[1];
texture[0]=driver->getTexture("c:/x/0.bmp");
texture[1]=driver->getTexture("c:/x/1.bmp");
node->setMaterialTexture(0,texture[0]);
node->setMaterialTexture(1,texture[1]);
node->setMaterialFlag(EMF_LIGHTING, false);
node->setMaterialType(video::EMT_REFLECTION_2_LAYER);

REMOVE CONSOLE WINDOW
Code:

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
int main()
{
return 0;
}

ATTACH TO BONE (Add Child to Joint)
Code:

ISceneNode * bone=node->getJointNode("Bone1");
bone->addChild(node2);

CREATE BILLBOARD
Code:

IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
bill->setMaterialType(EMT_TRANSPARENT_ADD_COLOR );
bill->setMaterialTexture(0, driver->getTexture("c:/x/b1.bmp"));
bill->setMaterialFlag(video::EMF_LIGHTING, false);
bill->setSize(dimension2d(10.0f, 10.0f));
bill->setPosition(vector3df(1.0f, 0.0f, 110.0f));

ADD ANIMATOR
Code:

irr::scene::ISceneNodeAnimator * anim = smgr->createFlyCircleAnimator (core::vector3df(0,55,110),5.0f, 0.01,core::vector3df(1,0,1));
bill->addAnimator(anim);
anim->drop();

ADD LIGHT
Code:

scene::ILightSceneNode* nodeLight = smgr->addLightSceneNode(0, core::vector3df(100, 100, 100),
video::SColorf(1.0f,1.0f,1.0f,1.0f),
20000.0f);
video::SLight light;
light.Direction = core::vector3df(100, 100, 0);
light.Type = video::ELT_DIRECTIONAL;
light.AmbientColor = video::SColorf(0.3f,0.3f,0.3f,1);
light.SpecularColor= video::SColorf(0.4f,0.4f,0.4f,1);
light.DiffuseColor = video::SColorf(1.0f,1.0f,1.0f,1);
light.CastShadows = false;

nodeLight->setLightData(light);

CREATE A PARTICLE SYSTEM
Code:

// create a particle system
scene::IParticleSystemSceneNode* ps = smgr->addParticleSystemSceneNode(false);

scene::IParticleEmitter* em = ps->createBoxEmitter(
core::aabbox3d(-7,0,-7,7,1,7), // emitter size
core::vector3df(0.0f,-0.06f,0.0f), // initial direction
10,100, // emit rate
video::SColor(0,0,0,255), // darkest color
video::SColor(0,255,255,255), // brightest color
80,20000,0, // min and max age, angle
core::dimension2df(5.f,5.f), // min size
core::dimension2df(20.f,20.f)); // max size

ps->setEmitter(em); // this grabs the emitter
em->drop(); // so we can drop it here without deleting it

scene::IParticleAffector* paf = ps->createFadeOutParticleAffector();

ps->addAffector(paf); // same goes for the affector
paf->drop();

ps->setPosition(core::vector3df(10,450,0));
ps->setScale(core::vector3df(100,100,100));
ps->setMaterialFlag(video::EMF_LIGHTING, false);
ps->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
ps->setMaterialTexture(0, driver->getTexture("c:/x/b1.bmp"));
ps->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);

CREATE WATER (addWaterSurfaceSceneNode)
Code:

IAnimatedMesh* plane = smgr->addHillPlaneMesh("floor", // Name of mesh
core::dimension2d(1,1), // Size of a tile of the mesh.
core::dimension2d(100,100), //tileCount - Specifies how much tiles there will be. If you specifiy for example that a tile has the size (10.0f, 10.0f) and the tileCount is (10,10), than you get a field of 100 tiles which has the dimension 100.0fx100.0f.
0, //material
0, //hillheight
core::dimension2d(10,10), //countHills
core::dimension2d(100,100)); // texturerepeat

ISceneNode* sea = smgr->addWaterSurfaceSceneNode(plane->getMesh(0), 2.0f, 350.0f, 8.0f,0,-1,vector3df(0,0,0),vector3df(10,0,0),vector3df(1,1,1));
sea->setMaterialTexture(0, driver->getTexture("c:/x/sand.png"));
sea->setMaterialTexture(1, driver->getTexture("c:/x/water.png"));
sea->setMaterialFlag(EMF_LIGHTING, true);
sea->setMaterialType(video::EMT_REFLECTION_2_LAYER);

GET DISTANCE BETWEEN NODES (addWaterSurfaceSceneNode)
Code:

ISceneNode* one;
ISceneNode* two;
one->getAbsolutePosition().getDistanceFrom(two->getAbsolutePosition());

NODE ANIMATION
Code:

myNode->setFrameLoop(1,15);
myNode->setAnimationSpeed(25);

CREATE FOG
Code:
driver->setFog(SColor(0,0, 0, 0),true, 150,220,0.01,false, false);
node->setMaterialFlag(video::EMF_FOG_ENABLE, true);

CREATE GUI ELEMENT
Code:
irr::gui::IGUIEnvironment *ige=smgr->getGUIEnvironment();

irr::gui::IGUIMeshViewer * gm=ige->addMeshViewer(rect(0,0,100,100),0,-1,0);
scene::IAnimatedMesh * mesh=smgr->getMesh("c:/x/cube.x");
gm->setMesh(mesh);

irr::gui::IGUIEditBox * txt=ige->addEditBox(L"giorgi",core::rect(150,150,300,200),true,0,-1);

GET NODE AT SCREEN 2D POSITION №1
Code:
scene::ISceneNode *node = smgr->getSceneCollisionManager()-> getSceneNodeFromScreenCoordinatesBB(position2d(mox,moy),0x1,false);

GET NODE AT SCREEN 2D POSITION №2
Code:
core::line3d line;
line=smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(position2d(mox,moy),cam);
scene::ISceneNode *nodeline = smgr->getSceneCollisionManager()->getSceneNodeFromRayBB(line,0x1,false);

GET NODE AT SCREEN 2D POSITION №3 (exact selection without bounding box)
Code:
int idd=0;
int curid=0;
scene::ITriangleSelector * sel[100];

void gio::add()
{
idd++;
scene::IAnimatedMesh * mesha=smgr->getMesh("c:/x/apple/apple.x");
scene::IAnimatedMeshSceneNode *nd=smgr->addAnimatedMeshSceneNode(mesha,0,idd,core::vector3df(xx, yy, zz));
sel[idd] = smgr->createOctTreeTriangleSelector(mesha, nd, 128);
//If you wish to select animated node you can use:
//sel[idd] = smgr->createTriangleSelectorFromBoundingBox(nd);
nd->setTriangleSelector(sel[idd]);
}
core::line3d line;
core::vector3df intersection;
core::triangle3df tri;
line=smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(position2d(mox,moy),cam);

int k=0;
for(k=0; k<=idd; k++)
{
if (smgr->getSceneCollisionManager()->getCollisionPoint(line, sel[k], intersection, tri))
{
curid=k;
break;
}
}
scene::IAnimatedMeshSceneNode *curnode=smgr->getSceneNodeFromId(curid);

SELECTOR - INTERSECTION POINT WITH 1) CAMERA VIEW - 2) MOUSE 2D COORDINATES
Code:
scene::IAnimatedMesh * ter = smgr->getMesh("c:/x/ter.3ds");
scene::ISceneNode * ternode=smgr->addOctTreeSceneNode(ter,0);
ternode->setMaterialFlag(video::EMF_LIGHTING,false);

scene::ITriangleSelector * selector = smgr->createOctTreeTriangleSelector(ter, ternode, 128);
ternode->setTriangleSelector(selector);


IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
bill->setMaterialType(EMT_TRANSPARENT_ADD_COLOR );
bill->setMaterialTexture(0, driver->getTexture("c:/x/b1.bmp"));
bill->setMaterialFlag(video::EMF_LIGHTING, false);
bill->setSize(dimension2d(10.0f, 10.0f));
bill->setPosition(vector3df(1.0f, 0.0f, 110.0f));


core::line3d line;

//1) Intersection with camera:
line.start = cam->getPosition();
line.end = line.start + (cam->getTarget() - line.start).normalize() * 1000.0f;

//2) intersection with mousecoordinates:
//line=smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(position2d(mox,moy),cam);



core::vector3df intersection;
core::triangle3df tri;

if (smgr->getSceneCollisionManager()->getCollisionPoint(line, selector, intersection, tri))
{
bill->setPosition(intersection);
}
Source: http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=34385

0 comments:

Post a Comment

backlink

Free Automatic Backlink

Followers

  © Blogger template On The Road by Ourblogtemplates.com 2009

Back to TOP