SMART TUTORIAL

A forum to store posts deemed exceptionally wise and useful
Post Reply
geronika2004
Posts: 11
Joined: Mon Jan 22, 2007 5:42 pm
Location: Tbilisi, Georgia, Eastern Europe
Contact:

SMART TUTORIAL

Post by geronika2004 »

I have put everything together, maybe this smart tutorial will be helpful :)

WRITE IMAGE TO FILE:

Code: Select all

   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: Select all

video::ITexture * image1 = driver->getTexture("d:\\x\\map.bmp");
//driver->makeColorKeyTexture(image1, core::position2d<s32>(0,0));
u8 * ff=(u8 *)image1->lock();
IImage* im = driver->createImageFromData(image1->getColorFormat(), dimension2d<s32>(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: Select all

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: Select all

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: Select all

#include "irrlicht.h"
#include <SIrrCreationParameters.h>
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<s32>(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: Select all

bool collision(ISceneNode* one, ISceneNode* two) { 
   aabbox3d<f32> 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: Select all

node->setDebugDataVisible(scene::EDS_BBOX);
MOVE NODE

Code: Select all

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

Code: Select all

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: Select all

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
int main()
{
return 0;
}
ATTACH TO BONE (Add Child to Joint)

Code: Select all

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

Code: Select all

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<f32>(10.0f, 10.0f)); 
bill->setPosition(vector3df(1.0f, 0.0f, 110.0f));
ADD ANIMATOR

Code: Select all

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: Select all

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: Select all

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

scene::IParticleEmitter* em = ps->createBoxEmitter(
    core::aabbox3d<f32>(-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: Select all

IAnimatedMesh* plane = smgr->addHillPlaneMesh("floor", // Name of mesh
core::dimension2d<f32>(1,1), //   Size of a tile of the mesh.
core::dimension2d<u32>(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<f32>(10,10), //countHills
core::dimension2d<f32>(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: Select all

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

Code: Select all

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

Code: Select all

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: Select all

irr::gui::IGUIEnvironment *ige=smgr->getGUIEnvironment();

irr::gui::IGUIMeshViewer * gm=ige->addMeshViewer(rect<s32>(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<s32>(150,150,300,200),true,0,-1);
GET NODE AT SCREEN 2D POSITION №1

Code: Select all

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

Code: Select all

core::line3d<f32> line;
line=smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(position2d<s32>(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: Select all

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<f32> line;
core::vector3df intersection;
core::triangle3df tri;
line=smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(position2d<s32>(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: Select all

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<f32>(10.0f, 10.0f)); 
bill->setPosition(vector3df(1.0f, 0.0f, 110.0f));


core::line3d<f32> 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<s32>(mox,moy),cam);



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

if (smgr->getSceneCollisionManager()->getCollisionPoint(line, selector, intersection, tri))
{
	bill->setPosition(intersection);
}
Last edited by geronika2004 on Fri Sep 11, 2009 9:38 am, edited 2 times in total.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The set caption method is using a wrong approach with far too many low-level things. Just use "core::stringw caption(string1.c_str())" to create the wide char string from your std::string.
Also make sure that all your pragmas are encapsulated in "#ifdef MSC_VER ... #endif" to avoid warnings under other OS.
And the "basic variables" seem to suggest to use global variables. While this is done in the examples (for brevity of the code examples) it's not a good idea to suggest this as a general pattern.
Besides that, the code looks ok. Do you miss any of those hints in the examples, i.e. do you see a need to extend the examples set?
geronika2004
Posts: 11
Joined: Mon Jan 22, 2007 5:42 pm
Location: Tbilisi, Georgia, Eastern Europe
Contact:

Post by geronika2004 »

hybrid
Thank you for your comment.
Of course, if it is helpful I will extend examples set and will apply necessary correctings :)
[/quote]
kryton9
Posts: 20
Joined: Sun Feb 22, 2009 7:26 pm

Thanks

Post by kryton9 »

Thanks geronika2004, this is a great idea! I am pretty new to Irrlicht and this is such a great way to wrap ones head around all that is in irrlicht!
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: SMART TUTORIAL

Post by randomMesh »

geronika2004 wrote:

Code: Select all

void showCaptionVar(std::string txt)
Please use a constant reference for heaven's sake, e.g

Code: Select all

void showCaptionVar(const std::string& txt)
This will speed things up.
"Whoops..."
stevebondy
Posts: 20
Joined: Thu Aug 13, 2009 5:47 pm
Location: British Columbia, Canada

More please

Post by stevebondy »

This is a great idea to help beginners like me. Please post more.
I could use some help with GUI stuff at the moment, particularly placing GUI elements (text and lines) over the rendered scene for HUD type effects.
thiefbug
Posts: 8
Joined: Sun Aug 16, 2009 9:05 pm

Post by thiefbug »

this is more helpfully than the other tuts I've seen. thank you =D
geronika2004
Posts: 11
Joined: Mon Jan 22, 2007 5:42 pm
Location: Tbilisi, Georgia, Eastern Europe
Contact:

Post by geronika2004 »

I have added my new smart tutorials :) ,
Later I will add new things :wink:
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Great collection of code snippets 8)
I will totally add some of them to my classes.
Thank You :wink:

Post some more of them :)
Working on game: Marrbles (Currently stopped).
Post Reply