Distortion on Draw2dImage [solved]

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Tex Pine
Posts: 5
Joined: Mon Nov 19, 2007 11:49 pm

Distortion on Draw2dImage [solved]

Post by Tex Pine »

HI,

We've been thinking a lot about this and debugging a lot before posting here, and I also checked for similar problems in the forum. I'm almost sure this is an Irrlicht bug, but I'd like to check with advanced users if they even ran on this problem.

First, a few things to keep in mind:

- We are using "power 2" sided textures, this is not a distortion because of the texture image dimensions.

- This is a problem with the overload Draw2dImage(texture, Rect dest, Rect source, color, alpha). It does not happen with Draw2dImage(texture, Pos2D, Rect source, Rect clip, color, alpha) overload. But as I'll explain, unfortunately we can't use this overload on our project.

OK, now, this is the image loaded in memory (512x512), and the area I marked with red is a slice that I'll render on a given point of the screen using Draw2dImage():

Image


Now, I will draw it using the following call:

IrrVideoDriver.Draw2DImage(tex, new Rect(290, 20, 502, 207), new Rect(300, 325, 512, 512), whitecolor, true);

The result is a draw with distortion on its pixels, where I marked red:


Image


But check the yellow mark above. This slice is rendered from the same texture, uses the same Draw2dImage() code, on the same class, but its pixels are fine! This distortion is actually random, and this another screen confirms it:

Image

So I guessed the engine was somehow missing the coordinates of the dest rect by one pixel. Then I forced a -1 subtraction on the first X and Y of the dest rect, as follows:

IrrVideoDriver.Draw2DImage(tex, new Rect(290-1, 20-1, 502, 207), new Rect(300, 325, 512, 512), whitecolor, true);


And it solved the problem... for a while:

Image


But I soon realized that this fix wouldn't work on all machines. I tested on another video card, and the distortion returned. And as the test continued, on each machine different slices were distorted or just rendered fine.

So I decided to try another overload, Draw2dImage(texture, Pos2D, Rect source, Rect clip, color, alpha). The call became:

IrrVideoDriver.Draw2DImage(tex, new Position2D(290, 20), new Rect(300, 325, 512, 512), new Rect(0, 0, 502, 207), whitecolor, true);


And it worked fine:

Image

Buuuuuuut....

But I have some scale animations on menu and characters that can't be reproduced with this overload. For example, the image below shows a scale transition for menus that can't be done with this last overload:

Image

So I really need to make the former overload, Draw2dImage(texture, Rect dest, Rect source, color, alpha), to work.

Does any one has a suggestion?

Thanks,
Last edited by Tex Pine on Wed Nov 21, 2007 1:35 pm, edited 1 time in total.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I'm not sure about the distortion, but I guess it's because of scaling...
Maybe AntiAlias could help ???
But I'm not really sure about this...

But what I see is that you count the bottom right corner of the image as 512,512...
Now as the upper left corner is 0,0 the bottom right corner is 511,511 !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Tex Pine
Posts: 5
Joined: Mon Nov 19, 2007 11:49 pm

Post by Tex Pine »

AntiAlias woudn't help, because I'm aiming at the more casual audience and DirectX 8 render. But you got a point about the 511x511... hummm.... I'll try that tomorrow then (could the solution be so simple?? ;))
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Looks like texture filtering is being turned off somewhere, so if the destination isn't exactly the same size as the source, they get all pixelated.
Does it also happen in D3D9?
What other things happen between the draw2dimage calls?
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Tex Pine
Posts: 5
Joined: Mon Nov 19, 2007 11:49 pm

Post by Tex Pine »

Yes, I'm using DX9. I didn't turn off any filtering by the way, just using the default stuff. Between the calls, I check for some internal events and game stuff.


Well, I tried things here and I came to the following conclusion:

- Adding +1,+1 to the start drawing position on dest rect compensate the 511x511 issue stated by Acki, but not on every video card. Don't know why, but it randomly distorts some draws (it is rare, of all our 2d images, just one got distorted on a GeForce 5700 LE).

- The overload Draw2dImage(tex, position2D, source rect, clip rect, color, alpha) NEVER distorts anything. But since it clips the image, obviously it doesn't allow scaling of the texture.

- But during scale draws the texture will be distorted anyway, and the scale transitions are going to be fast. So it was simply a matter of ensuring the full draw won't be distorted, and I came up with the following pseudocode:

Code: Select all


if (draw_scaled)
{

Draw2dImage(tex, dest_rect, source_rect, color, alpha);

}
else
{

Draw2dImage(tex, position2d, source_rect, clip rect, color, alpha);

}

Thanks for your tips. :)
Post Reply