Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Draw bitmap as trapezoid

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Draw bitmap as trapezoid
ph03nix
Member #15,028
April 2013
avatar

I'm trying to draw a non-rotated bitmap as a trapezoid, where the top and bottom have different sizes.

I currently do this by drawing it line by line, scaling each row using linear interpolation, but it's a bit slow with larger bitmaps.

Is there some way I can do it more efficiently using allegro 5.0.10? Thanks

Arthur Kalliokoski
Second in Command
February 2005
avatar

You could simply "tilt" it as a 3D quad, A4 would probably do it fast enough.

They all watch too much MSNBC... they get ideas.

ph03nix
Member #15,028
April 2013
avatar

How exactly would I do that? I'm not using OpenGL.

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'm trying to find a ten year old example hidden somewhere in this terabyte drive, please stand by.

[EDIT]

Found it (and had to tweak it a bit), it's in the paperclip along with an image file.

They all watch too much MSNBC... they get ideas.

ph03nix
Member #15,028
April 2013
avatar

I just tried it and it works nicely, but I'm using 5.0.10, not 4. Is there a way I can use the triangle3D function in 5?

jmasterx
Member #11,410
October 2009

al_draw_prim lets you draw whatever shape you want by providing verticies and a texture. So you could map your texture onto 4 verticies where the 2 top ones go inward on the x axis to form a trapezoid.

https://www.allegro.cc/manual/5/al_draw_prim

Arthur Kalliokoski
Second in Command
February 2005
avatar

Holy carp! I read the original post as saying "Is there some way I can do it more efficiently other than using allegro 5.0.10?"

They all watch too much MSNBC... they get ideas.

SiegeLord
Member #7,827
October 2006
avatar

You can't get trapezoids like that using 2D coordinates only. al_draw_prim will work, but 5.0.10 does not have a good mechanism for the necessary perspective transformation... although maybe sticking a perspective transformation into al_use_transform will work.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Arthur Kalliokoski
Second in Command
February 2005
avatar

Come to think of it, he didn't specify what the trapezoid dimensions should be. Just rotating a quad would involve trigonometry to get the outside edge sizes at least.

They all watch too much MSNBC... they get ideas.

ph03nix
Member #15,028
April 2013
avatar

I programmed my own 3d coordinates that get converted to 2d coords to get the 4 points.

If the function lets me draw a shape in 3d I can reprogram it taking advantage of that, if it's 2D I can still draw it using my points and a trick to keep proper perspective but it won't be as efficient.

I'm trying al_draw_prim, but I'm having a bit of trouble getting it to render anything

Arthur Kalliokoski
Second in Command
February 2005
avatar

I can't get it to work either. :'(

They all watch too much MSNBC... they get ideas.

SiegeLord
Member #7,827
October 2006
avatar

I tried for a bit, and I can't get it to work with 5.0.10 in pure Allegro. Works like a charm in 5.1 though. I'd either upgrade, or hack it up using OpenGL.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Arthur Kalliokoski
Second in Command
February 2005
avatar

SiegeLord said:

Works like a charm in 5.1 though.

Please post the code, I'm using 5.1.7. I just stuck the example in the docs in, and tried altering the z if there was a viewing frustum I didn't know about and couldn't get it to work.

They all watch too much MSNBC... they get ideas.

SiegeLord
Member #7,827
October 2006
avatar

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3 4int main() 5{ 6 al_init(); 7 al_init_primitives_addon(); 8 ALLEGRO_DISPLAY* d = al_create_display(800, 600); 9 10 ALLEGRO_VERTEX vtx[4]; 11 vtx[0].x = -400; 12 vtx[0].y = -200; 13 vtx[0].z = -400; 14 vtx[0].color = al_map_rgb_f(0, 0, 1); 15 16 vtx[1].x = -400; 17 vtx[1].y = 200; 18 vtx[1].z = -800; 19 vtx[1].color = al_map_rgb_f(0, 1, 0); 20 21 vtx[2].x = 400; 22 vtx[2].y = 200; 23 vtx[2].z = -800; 24 vtx[2].color = al_map_rgb_f(1, 0, 1); 25 26 vtx[3].x = 400; 27 vtx[3].y = -200; 28 vtx[3].z = -400; 29 vtx[3].color = al_map_rgb_f(1, 1, 1); 30 31 ALLEGRO_TRANSFORM t; 32 al_identity_transform(&t); 33 al_perspective_transform(&t, -400, -300, 400, 400, 300, 10000); 34 al_set_projection_transform(d, &t); 35 36 al_clear_to_color(al_map_rgb_f(0, 0, 0)); 37 al_draw_prim(vtx, NULL, NULL, 0, 4, ALLEGRO_PRIM_TRIANGLE_FAN); 38 al_flip_display(); 39 al_rest(1.0); 40}

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Arthur Kalliokoski
Second in Command
February 2005
avatar

Yeah, that worked, I didn't have the transforms and stuff. Thanks!

They all watch too much MSNBC... they get ideas.

ph03nix
Member #15,028
April 2013
avatar

Works beautifully for me as well. The thing is I'm using features in 5.0.10 that aren't available in 5.1.7.

Is it possible to take the primitives addon from 5.1.7 and use it with 5.0.10?

SiegeLord
Member #7,827
October 2006
avatar

ph03nix said:

The thing is I'm using features in 5.0.10 that aren't available in 5.1.7

I'm not sure what you mean by this... what features would those be? Is 5.1.7 behind 5.0.10 somehow? EDIT: Oh I guess it's possible, 5.1.7 is from May and 5.0.10 is from June...

Quote:

Is it possible to take the primitives addon from 5.1.7 and use it with 5.0.10?

It's not the primitives addon that's at "fault", it's the core. The function that you need (al_set_projection_transform) is part of the core.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

ph03nix
Member #15,028
April 2013
avatar

Whoops, I thought some of the blend modes weren't available in the 5.1 branch for some reason... ;D

Anyway, is there a chance al_set_projection_transform and the rest will be added to the stable branch any time in the near future?

SiegeLord
Member #7,827
October 2006
avatar

It's not impossible (quite a few of the 5.1 features were backported to 5.0) but it seems unlikely... but who knows. It's really up to Peter Wang who does the backporting.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

ph03nix
Member #15,028
April 2013
avatar

Does al_draw_prim do nothing in 5.0.10? Could it be possible that it requires some type of transformation to make it visible on screen?

SiegeLord
Member #7,827
October 2006
avatar

As I said before, this has nothing to do with the primitives addon and everything to do with Allegro's core. Maybe it's possible to hack something up using the transformation utilities available in 5.0.10... but I wouldn't bet on it.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Arthur Kalliokoski
Second in Command
February 2005
avatar

In other words, if you used your own 4x4 matrices, and used them to mathematically transform the vertices, it'd work?

They all watch too much MSNBC... they get ideas.

SiegeLord
Member #7,827
October 2006
avatar

5.0.10 has an unchangeable orthographic transform for the projection matrix labeled P, and a user customizable transformation T. The vertices are transformed by a product of P and T. The goal is to create a perspective projection matrix J by modifying T. So, it's just a matter of solving this matrix equation, J = T P for T. I don't feel like writing up a 4x4 matrix inversion necessary to do that... the reason why we add features to 5.1 at all is to make this stuff easier... finding workabouts for this in 5.0 kind of defeats the point, eh?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'd like to know how and why stuff works from first principles, rather than rules of thumb. It was almost a rhetorical question.

[EDIT]

It makes no difference if you come up with an algorithm to solve the answer to life, the universe, and everything if nobody knows about it, e.g. isn't covered in the documentation. I tried the example in the docs with copy and paste, and it didn't work.

They all watch too much MSNBC... they get ideas.

SiegeLord
Member #7,827
October 2006
avatar

I'd like to know how and why stuff works from first principles, rather than rules of thumb.

There are no rules of thumb in my post, just simple linear algebra.

Quote:

I tried the example in the docs with copy and paste, and it didn't work.

Which example is that? al_draw_prim one works just fine.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

 1   2 


Go to: