Scaling textures?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
TomTim
Posts: 72
Joined: Mon Aug 16, 2010 5:32 pm
Location: NRW, Germany

Scaling textures?

Post by TomTim »

I got a texture loaded with Irrlicht, but it's go he wrong size. It's squarish, and I need it streched to each end of the screen. How I get the size of the screen I already know, but I have no idea of scaling the texture to the right size.

Oh, and is there a way to create new GUI-skins with some textures I already got?
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

Here is a function from my old sources.
I used it to load any image and return ITexture of desired size back.
Its able to load image like 4999x2999 size (non power-of-two & bigger that actual max-texture-size supported by current video driver).
It will return null if anything went wrong, like file not found, or not enough memory or failed to create texture.

Note: Game.Video is type of IVideoDriver*.

Code: Select all

video::ITexture*
GameLoadTextureFromImage(const c8 *filename,
					    const core::dimension2d<s32> size,
					    bool keepAspectRatio)
{
	video::ITexture *result_texture = NULL;
	video::IImage *im1, *im2;

	im1 = Game.Video->createImageFromFile(filename);

	if (im1)
	{
		core::dimension2di im1size = im1->getDimension();
		if (im1size.Width>0 && im1size.Height>0)
		{
			core::dimension2di im2size = size;
			if (keepAspectRatio)
			{
				if (im1size.Width>im1size.Height)
				{
					im2size.Height = (im1size.Height*size.Width)/im1size.Width;
					im2size.Width = size.Width;
				}
				else
				{
					im2size.Width = (im1size.Width*size.Height)/im1size.Height;
					im2size.Height = size.Height;
				}
			}

			s32 *im2buf = (s32*) malloc(sizeof(s32)*im2size.Width*im2size.Height);
			if (im2buf)
			{
				im2 = Game.Video->createImageFromData(video::ECF_A8R8G8B8, core::dimension2di(im2size.Width,im2size.Height), im2buf);
				if (im2)
				{
					im1->copyToScaling(im2);
					result_texture = Game.Video->addTexture(filename, im2);

					im2->drop();
				}

				free(im2buf);
			}
		}

		im1->drop();
	}

	return result_texture;
}
TomTim
Posts: 72
Joined: Mon Aug 16, 2010 5:32 pm
Location: NRW, Germany

Post by TomTim »

greenya wrote:Here is a function from my old sources.
I used it to load any image and return ITexture of desired size back.
Its able to load image like 4999x2999 size (non power-of-two & bigger that actual max-texture-size supported by current video driver).
It will return null if anything went wrong, like file not found, or not enough memory or failed to create texture.

Note: Game.Video is type of IVideoDriver*.

Code: Select all

video::ITexture*
GameLoadTextureFromImage(const c8 *filename,
					    const core::dimension2d<s32> size,
					    bool keepAspectRatio)
{
	video::ITexture *result_texture = NULL;
	video::IImage *im1, *im2;

	im1 = Game.Video->createImageFromFile(filename);

	if (im1)
	{
		core::dimension2di im1size = im1->getDimension();
		if (im1size.Width>0 && im1size.Height>0)
		{
			core::dimension2di im2size = size;
			if (keepAspectRatio)
			{
				if (im1size.Width>im1size.Height)
				{
					im2size.Height = (im1size.Height*size.Width)/im1size.Width;
					im2size.Width = size.Width;
				}
				else
				{
					im2size.Width = (im1size.Width*size.Height)/im1size.Height;
					im2size.Height = size.Height;
				}
			}

			s32 *im2buf = (s32*) malloc(sizeof(s32)*im2size.Width*im2size.Height);
			if (im2buf)
			{
				im2 = Game.Video->createImageFromData(video::ECF_A8R8G8B8, core::dimension2di(im2size.Width,im2size.Height), im2buf);
				if (im2)
				{
					im1->copyToScaling(im2);
					result_texture = Game.Video->addTexture(filename, im2);

					im2->drop();
				}

				free(im2buf);
			}
		}

		im1->drop();
	}

	return result_texture;
}
OK, that helped me a lot. Thanks. :D

But what's about the skins?
TomTim
Posts: 72
Joined: Mon Aug 16, 2010 5:32 pm
Location: NRW, Germany

Post by TomTim »

Just because you ignore the thread, the problem does not get solved. ;)

How can I create new GUI-Skins for Irrlicht?
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

TomTim wrote:How can I create new GUI-Skins for Irrlicht?
Implement IGUISkin and set the skin via IGUIEnvironment::setSkin()
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Post Reply