Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » al_draw_rotated_bitmap() giving distorted edges

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
al_draw_rotated_bitmap() giving distorted edges
Thinkal VB
Member #16,859
May 2018

I was just running an animation using frames. But when I changed the rotation the edged became distorted but for rotation = 0 degrees it's crystal clear and fine. I am using #define PI 3.141592653589793f for converting the degree to radian. The data type used to store the value in radians is a float.

What would be the potential cause of this problem?
All the graphical results are attached to this post. For visual analysis.

float Position::getRadRotX() const
{
return((rotX * PI) / 180);
}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Something you can do is turn on multi-sampling.

You have to enable it before creating your display :

al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS , 1 , ALLEGRO_SUGGEST);
al_set_new_display_option(ALLEGRO_SAMPLES , 4 , ALLEGRO_SUGGEST);

ALLEGRO_DISPLAY* d = al_create_display(...);
if (al_get_display_option(d , ALLEGRO_SAMPLE_BUFFERS) != 1) {
   cout << "Failed to enable multisampling on the display." << std::endl;
}
cout << "There are " << al_get_display_option(d , ALLEGRO_SAMPLES) << " sample buffers in use." << std::endl;

Thinkal VB
Member #16,859
May 2018

Thank you, Edgar Reynaldo for the replay

std::cout<< says:-
Failed to enable multisampling on the display.
There are 0 sample buffers in use.
And it's the same as before, blurred edges.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thinkal VB
Member #16,859
May 2018

Windows 10 64bit. Visual Studio 2017
Intel(R) Pentium(R) CPU N3710 @ 1.60GHz 1.60 GHz
Intel HD Graphics 405
Shader version 5.0
Open GL version 4.4
Open CL version 1.2
Direct X runtime version 12.0
hardware supported version 11.1

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thinkal VB
Member #16,859
May 2018

void debug()
{
ALLEGRO_DISPLAY *display;
ALLEGRO_DISPLAY_MODE dispData;

al_init();
al_init_primitives_addon();
al_init_image_addon();
al_get_display_mode(al_get_num_display_modes() - 1, &dispData);

al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST);

display = al_create_display(dispData.width, dispData.height);
if (al_get_display_option(display, ALLEGRO_SAMPLE_BUFFERS) != 1) {
std::cout << "Failed to enable multisampling on the display." << std::endl;
}
std::cout << "There are " << al_get_display_option(display, ALLEGRO_SAMPLES) << " sample buffers in use." << std::endl;

}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

That's really weird, because I'm running mostly the same setup as you, and I know I have a working multisampling example around here somewhere. Let me find it.

Run a debugging build, and see what allegro.log has to say about the display.

EDIT
Try the OpenGL driver and see if it enables multi sampling. Could be a DX issue.

Thinkal VB
Member #16,859
May 2018

Thank you Edgar Reynaldo for helping out,
The log file is attached above.
Allegro produce log's in debugging mode only or it's the same in the release mode too?. I never noticed it before ::)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thinkal VB
Member #16,859
May 2018

Don't know how to switch to open GL in windows 10.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thinkal VB
Member #16,859
May 2018

Thank you, Edgar Reynaldo for your kind support.
It's little better, and the sampling worked ....
But not fluid as the 0-degree pic.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thinkal VB
Member #16,859
May 2018

I replaced the value of PI with const float PI = 104348 / 33215;
Now it seems better. Still some little distortions here and there.

I switched back to d3d and used the same code as "const float PI = 104348 / 33215;" and the result was the same as when the OpenGL was rendering with 4 buffers. ???

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Are you drawing your sprite on the back buffer or another bitmap? If you're drawing it on another bitmap it won't be multi-sampled unless you enable multi-sampling on the bitmap as well.

al_set_new_bitmap_samples(8);
al_create_bitmap(w,h);

As for your float problem. int/int always gives you int. You need to specify a floating point literal value as such by using .0f on the end of it. Otherwise you get integer division, which discards the remainder.

Here's multi-sampling enabled through al_set_new_bitmap_samples :

https://www.allegro.cc/files/attachment/611523

Actually, 8 samples looks the best.

Thinkal VB
Member #16,859
May 2018

Thank you Edgar Reynaldo, for bearing with me ...
I am drawing to the back buffer (not to any bitmap). I removed the entire code for sampling and I got the same result as before with the sampling enabled. It's little weird

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Okay, I'm thinking that MultiSampling doesn't do anything for sprite drawing. Try this instead : Before you load your sprites, call the following code :

al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MIPMAP);

ALLEGRO_MIPMAP won't do anything if the width and height of the image are not a power of two.

You can also try ALLEGRO_MAG_LINEAR, but it can make sprites blurry when enlarging them.

Thinkal VB
Member #16,859
May 2018

Thank you Edgar Reynaldo,
I think it's little better than before - just little distortion especially at the tip of the gun. For a starting, it won't be a problem. Thank you for helping out.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Well, I'm glad it's improved somewhat. Could you show a screenshot of the sprite with ALLEGRO_MIN_LINEAR enabled? Is it really any smoother?

I've never had this problem before, but I haven't been rotating sprites much. I tried out rotation on multi-sampled primitives and enabled down scaling and ALLEGRO_MIN_LINEAR.

While it doesn't do much for full size sprites, it looks pretty good when actually scaling down.

You might try increasing the resolution of your graphics and scaling them down in your game, but whether that is worth it or not is up to you.

I will agree, there are some rather jagged edges when rotating sprites, even multi-sampled primitive sprites.

Thinkal VB
Member #16,859
May 2018

Thank you Edgar Reynaldo,
My previous post had the pic's attached to it with these flags enabled " al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MIPMAP); ". But however the width and height of the png's are not a power of two, and the pic shows an unscaled version of the character. I will try to use HD images - and downscale it as per need if required.

One more thing - Should the actual bitmap have to be in the power of two or just the sub-bitmaps have to be in power of two.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

If you want mipmaps, the source image has to be power of two in width and height (not necessarily the same though). mipmaps are really only good if you change the scale of your sprite often at runtime. That's what mipmaps are, pre-scaled versions of your image.

I will play with this some more over the next few days. Could you attach the image you're using for your player? I'd like to try some things.

Thinkal VB
Member #16,859
May 2018

Thank you Edgar Reynaldo,
So the source image has to be a power of two :'( that's bad.......
I took all the game assets from open game arts https://opengameart.org/content/animated-top-down-survivor-player
To test the code I uploaded - I will attach the combined animation sprite with the custom script I am using to this post.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Note for the future - if you're going to attach multiple files, like a game project, it's best to put it in a .zip file so there's only one file to download. I will take a look at the assets.

You only need power of two textures if you want mipmaps. mipmaps are only good if you're planning to zoom in and out on your images.

EDIT
I made a small demo of your survivor guy being rotated and scaled with different combinations of ALLEGRO_MIN_LINEAR, ALLEGRO_MAG_LINEAR, and ALLEGRO_MIPMAP. Mipmapping didn't seem to help at all, and resulted in a faded image when scaling down. Mipmapping doesn't do anything when scaling up. Shrinking with ALLEGRO_MIN_LINEAR and growing with ALLEGRO_MAG_LINEAR really helped though, when not drawing at actual size.

Here's the demo - MinMag.zip.

For some reason, mipmapping made the images fade when drawn at less than actual size.

https://www.allegro.cc/files/attachment/611584

Rotate around the center with the mouse. Mouse wheel changes zoom.

Thinkal VB
Member #16,859
May 2018

Thank you, Edgar Reynaldo, for your kind support.
I will look into the code.
Yes, next time I will take special care in zipping all the contents before attaching it.
Thank you once again....

 1   2 


Go to: