Half drawn 2d images

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
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Half drawn 2d images

Post by Morgawr »

Hi everyone... I've just started programming with Irrlicht and, since i Love 2d games, I wanted to do some practice with 2d programming and 2d games..

I just wanted to draw a sprite from a bmp file on my screen and I followed parts of the tutorial for 2d graphics... The program works fine, even with transparency and everything but the image is drawn only for a quarter, I can't see the whole image...

Where am I wrong? The image size is 90x145 pixels

This is the important part in the main:

Code: Select all

IrrlichtDevice *device = createDevice(video::EDT_OPENGL,
            core::dimension2d<s32>(800,600));

    if(device == 0)
     return 1;

    device->setResizeAble(true);

    video::IVideoDriver *driver = device->getVideoDriver();
    video::ITexture *img = driver->getTexture("f618e9.bmp");
    driver->makeColorKeyTexture(img,core::position2d<s32>(0,0));


    while(device->run())
    {
        driver->beginScene(true,false,video::SColor(255,255,255,255));
        driver->draw2DImage(img,core::position2d<s32>(100,100),
                        core::rect<s32>(0,0,90,145),0,
                        video::SColor(255,255,255,255),true);
        driver->endScene();
    }
    device->drop();
Am i missing something? thanks for your help ^^
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

your texture (image) is 90 x 145 ???
that's the fault, a texture must have dimensions in power of 2 (2^0, 2^1, 2^2, 2^3, ... ) !!!
So enlarge your image to (for example) 128 x 256 and draw the picture into the upper left corner, that should work... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
TomiZ
Posts: 40
Joined: Wed Aug 29, 2007 6:02 am
Location: Poland
Contact:

Post by TomiZ »

I used that code, and it's work fine.

Texture dimensions don't have to be in power of 2. It will be scaled by engine. But in draw2DImage source coordination will be scaled from 0 to 1 not with new dimensions , but with original dimensions of texture.

EDIT: I'm using Irrlicht 1.4, where getSize/getOriginalSize for OpenGL has been fixed. If you are using Irrlicht 1.3.1 try to use direct3D instead of OpenGl.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Or exchange the method bodies of getSize and getOriginalSize in COpenGLTexture.cpp.
mb
Posts: 1
Joined: Thu Aug 30, 2007 9:31 pm

Hello all

Post by mb »

It worked fine for me too after changing the dimensions.

Now my question is how to draw animated meshes keeping the 2D image? meshes shoud be such that they dont get overlapped. I wish to do it using IvideoDriver not by scenemanager draw all command.

Thanks
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

Acki wrote:your texture (image) is 90 x 145 ???
that's the fault, a texture must have dimensions in power of 2 (2^0, 2^1, 2^2, 2^3, ... ) !!!
So enlarge your image to (for example) 128 x 256 and draw the picture into the upper left corner, that should work... ;)
WOW thanks a lot mate, this works now, it's awesome :P

And yep, I'm using irrlicht 1.3.1 (also can't use DirectX since I'm under Ubuntu environment now, my xp pc is broken D: )
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

TomiZ wrote:Texture dimensions don't have to be in power of 2. It will be scaled by engine.
That's exactly the problem, if it's not in power of 2 it will be scaled and getting destored !!! ;)
So if you don't want the scaling it'll have to be 2^n !!!
But I also have to say that this probably depends on the gfx card, AFAIK it could be that some cards don't need 2^n dimensions... ;)
But you cannot assume that every user has such a card so to be on the save side always use dimensions in 2^n !!! :P
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
TomiZ
Posts: 40
Joined: Wed Aug 29, 2007 6:02 am
Location: Poland
Contact:

Post by TomiZ »

Acki wrote:That's exactly the problem, if it's not in power of 2 it will be scaled and getting destored !!! ;)
There was bug in 1.3.1 with OpenGL. Look in source code at Draw2DImage. Texture coordinates are scaled - every texture coordinate is divided by original texture size, not by new size (power of 2). But as hybrid said in 1.3.1 you have to replace getSize and getOriginalSize for OpenGL driver.

So if you have texture 200x200. It will be scaled to 256x256. Now if you want to draw rect(0,0,100,100) it will be scaled to (0/200, 0/200, 100/200, 100/200) and yow will get (0, 0, 0.5, 0.5). With those coordinates you will copy pixels from this rect (0*256, 0*256, 0.5*256, 0.5*256) = (0, 0, 128, 128); But this don't work in 1.3.1 with OpenGL because of bug.

Anyway it has been fixed in 1.4 so you can use all textures, not only with size x^2 in new version. :)
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

TomiZ wrote:
Acki wrote:That's exactly the problem, if it's not in power of 2 it will be scaled and getting destored !!! ;)
There was bug in 1.3.1 with OpenGL. Look in source code at Draw2DImage. Texture coordinates are scaled - every texture coordinate is divided by original texture size, not by new size (power of 2). But as hybrid said in 1.3.1 you have to replace getSize and getOriginalSize for OpenGL driver.

So if you have texture 200x200. It will be scaled to 256x256. Now if you want to draw rect(0,0,100,100) it will be scaled to (0/200, 0/200, 100/200, 100/200) and yow will get (0, 0, 0.5, 0.5). With those coordinates you will copy pixels from this rect (0*256, 0*256, 0.5*256, 0.5*256) = (0, 0, 128, 128); But this don't work in 1.3.1 with OpenGL because of bug.

Anyway it has been fixed in 1.4 so you can use all textures, not only with size x^2 in new version. :)
Mmm I guess I'll just stick with 1.3.1 for the moment, it was such a pain to set up that I'm scared I might screw up my IDE with setting up 1.4 ^^
and 1.3.1 seems very nice as it is at the moment so no big need to change after all
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

TomiZ wrote:Anyway it has been fixed in 1.4 so you can use all textures, not only with size x^2 in new version. :)
Just to correct you, it's 2^x (correctly 2^n) and not x^2 (n^2)... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply