Generating IImages in memory

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Generating IImages in memory

Post by randomMesh »

I don't know if it is useful or not, but i felt like sharing

Code: Select all

#include <irrlicht.h>

#define BITDEPTH 4

/**
 * Determines if a point is inside a circle.
 * 
 * \param x The x coordinate of the point
 * \param y The y coordinate of the point
 * \param cx The x coordinate of the center of the circle
 * \param cy The y coordinate of the center of the circle
 * \param radius The radius of the circle
 * \return True if point is inside circle
 */
bool isInCircle(
	const irr::u32 x, const irr::u32 y,
	const irr::u32 cx, const irr::u32 cy,
	const irr::u32 radius) 
{
	const irr::u32 dx = x - cx; 
	const irr::u32 dy = y - cy; 
	const irr::u32 distance = dx*dx + dy*dy; 

	return distance < (radius*radius); 
}

irr::video::IImage* createCustomImage(
	irr::video::IVideoDriver* driver,
	const irr::core::dimension2di& size = irr::core::dimension2di(512, 512),
	const irr::video::SColor& foregroundColor = irr::video::SColor(255, 255, 255, 255),
	const irr::video::SColor& backgroundColor = irr::video::SColor(255, 255, 0, 0)
)
{
	irr::u8 imageData[size.Width*size.Height*BITDEPTH];

	const irr::u32 centerX = size.Width/2;
	const irr::u32 centerY = size.Height/2;

	irr::u32 radius = 0;
	if (centerX > centerY)
		radius = size.Height - centerY;
	else
		radius = size.Width - centerX;
	


	
	irr::s32 i;
	for(i = 0; i < size.Width*size.Height*BITDEPTH; i += BITDEPTH)
	{
		const irr::u32 x = (i / BITDEPTH) % size.Width;
		const irr::u32 y = i / (BITDEPTH * size.Width);
		
		if (isInCircle(x, y, centerX, centerY, radius))
		{
			//point is in circle, so draw in foreground color
			imageData[i] = foregroundColor.getBlue();
			imageData[i + 1] = foregroundColor.getGreen();
			imageData[i + 2] = foregroundColor.getRed();
			imageData[i + 3] = foregroundColor.getAlpha();
		}
		else //draw background
		{
			imageData[i] = backgroundColor.getBlue();
			imageData[i + 1] = backgroundColor.getGreen();
			imageData[i + 2] = backgroundColor.getRed();
			imageData[i + 3] = backgroundColor.getAlpha();				
		}
	}

	void* data = &imageData[0];
	return driver->createImageFromData(
		irr::video::ECF_A8R8G8B8,
		irr::core::dimension2di(size.Width, size.Height),
		data,
		false,
		true
	);
}


int main()
{
	irr::IrrlichtDevice* device = 0;
	device = irr::createDevice(irr::video::EDT_OPENGL);
	
	irr::video::IVideoDriver* driver = 0;
	driver = device->getVideoDriver();
	
	irr::scene::ISceneManager* smgr = 0;
	smgr = device->getSceneManager();


	irr::scene::ICameraSceneNode* camera = smgr->addCameraSceneNode();
	camera->setPosition(irr::core::vector3df(0.0f, 0.0f, -800.0f));
	
	
	//make texture out of an IImage
	irr::video::IImage* omfg = createCustomImage(driver);
	irr::video::ITexture* texture = driver->addTexture("Another texture", omfg);
	omfg->drop();

	irr::scene::ISceneNode* node = smgr->addCubeSceneNode(512);
	node->setMaterialFlag(irr::video::EMF_LIGHTING, false);
	node->setMaterialTexture(0, texture);
	
	irr::scene::ISceneNodeAnimator* anim = smgr->createRotationAnimator(irr::core::vector3df(0,1,0));
	node->addAnimator(anim);
	anim->drop();
	
	while (device->run())
	{
		if (device->isWindowActive())
		{
			driver->beginScene(true, true, irr::video::SColor(255, 128, 128, 128));
			smgr->drawAll();
			driver->endScene();
		}
	}
	
	device->drop();
	
	return 0;
}

Last edited by randomMesh on Tue Feb 19, 2008 10:05 pm, edited 1 time in total.
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Post by christianclavet »

Hi could you give more description on what this code generate?

From what I'Ve read so far, it produce a texture on a rotation cube.
Can this be used like a brush if we put the mouse coordinates?
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

christianclavet wrote:Hi could you give more description on what this code generate?
The code shows how to generate an IImage from a byte array.
christianclavet wrote: From what I'Ve read so far, it produce a texture on a rotation cube.
Can this be used like a brush if we put the mouse coordinates?
You can use it for whatever you can do with an IImage. Be creative.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Just a note about your using sqrt(). It is pretty expensive to call, and often you can avoid calling it entirely by squaring something else like so...

Code: Select all

bool isInCircle( 
   const irr::u32 x, const irr::u32 y, 
   const irr::u32 cx, const irr::u32 cy, 
   const irr::u32 radius) 
{ 
   const irr::u32 dx = x - cx; 
   const irr::u32 dy = y - cy; 
   const irr::u32 distance = dx*dx + dy*dy; 

   return distance < (radius*radius); 
} 
Travis
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Or, to be kind for those coming after you (or yourself later :P )

"Distancesquared"
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

vitek wrote:Just a note about your using sqrt(). It is pretty expensive to call, and often you can avoid calling it entirely by squaring something else like so...
Thanks for pointing this out. I updated my post.
Post Reply