How to load hires texture in lower resolution

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
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

How to load hires texture in lower resolution

Post by greenya »

I have very high quality textures (for example 1024x1024).
And I have a lot of them and all of them must be loaded.

I'm checking available memory in the system and deciding to load all of them with lower resolution.

Is there any ability to load certain texture with specified dimensions (for example 512x512 or 256x256) ?

Thank You.
hybrid
Admin
Posts: 14144
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I exposed copy-to-scaling in the IImage interface with Irrlicht 1.3.1. So you can create a new image or texture from it by scaling it down to the desired size. But this is not possible automatically.
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

would be nice if getTexture also takes an int that devides its size 8)
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

Halan wrote:would be nice if getTexture also takes an int that devides its size 8)
Yes, you right.
But anyway, I'm going to try hybrid's advice.
hybrid
Admin
Posts: 14144
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The software scaling can be quite time consuming, so it's probably best to keep it separated into two steps. If the GPU could scale it automatically it would be different. But both hardware APIs don't allow for, so we make it explicit in Irrlicht, too.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

For people how will search for the solution on this topic:

Here is the function, that will load any-dimension texture with necessary dimensions:

Code: Select all

video::ITexture*
LoadTextureFromImage(IrrlichtDevice *device,
				   const c8 *filename,
				   const core::dimension2d<s32> size,
				   const bool keepAspectRatio=false)
{
	video::ITexture *result_texture = NULL;
	video::IImage *im1, *im2;
	im1 = device->getVideoDriver()->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 = device->getVideoDriver()->addTexture(filename, im2);

					im2->drop();
				}

				free(im2buf);
			}
		}

		im1->drop();
	}

	return result_texture;
}
how does it works?
if keepAspectRatio is false then it will load texture scaled to size.Windth x size.Height, otherwise it will load textures with keeping aspect ration but width will not exceed size.Width and height will not exceed size.Height. If you have test.jpg with 3000x2000 dimensions and you want to scale it to 512, you can:

Code: Select all

LoadTextureFromImage(device,"test.jpg",core::dimension2d<s32>(512,512),true);
this code will load it with dim. 512x341 (512*2000/3000=~341). If you will execute the same code with keepAspectRatio false (which is default) you will get 512x512 texture.

Notes
- function returns NULL if loading failed.
- after executing "LoadTextureFromImage(test.jpg..." you can execute "video->getTexture(test.jpg)" as much as you want -- it will give you your previously loaded texture (so do not execute everywhere "LoadTextureFromImage(test.jpg...", but use it only at first-time loading).
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Post by Nadro »

Good work greenya:) Very helpfull stuff:)
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

cool, I'll move this thread to the code snippets forum so that it doesn't get buried in here :)
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
MolokoTheMole
Posts: 109
Joined: Tue Jan 09, 2007 1:18 pm

Post by MolokoTheMole »

Cool thanks.
Crimson Glory full Irrlicht game source code!
What I Do - my blog & dev log
Currently developing Link-Dead
Post Reply