Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Drawing a textured trapezoid (2D)

Credits go to Edgar Reynaldo, jmasterx, and SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
[A5] Drawing a textured trapezoid (2D)
CrizerPL
Member #16,496
July 2016

Hello,

I've been recently trying to draw a textured trapezoid using al_draw_prim() so I can make these pseudo-3D buildings like in GTA 1 possible.
And I ran into an obvious issue:
{"name":"t.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/7\/0710027359231792b6461c324445e409.png","w":303,"h":251,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/7\/0710027359231792b6461c324445e409"}t.png
Is there any way to make it look properly? I'm using Allegro 5.0.4.

Here's the code, just in case:

#SelectExpand
1 int bmp_w = al_get_bitmap_width(textures.GetBitmapResource("Brickwall"))-1; 2 int bmp_h = al_get_bitmap_height(textures.GetBitmapResource("Brickwall"))-1; 3 ALLEGRO_VERTEX v[] = 4 { 5 {200, 200, 0, 0, 0, al_map_rgb(255,255,255)}, 6 {350, 400, 0, bmp_w, bmp_h, al_map_rgb(255,255,255)}, 7 {250, 400, 0, 0, bmp_h, al_map_rgb(255,255,255)}, 8 9 {200, 200, 0, 0, 0, al_map_rgb(255,255,255)}, 10 {400, 200, 0, bmp_w, 0, al_map_rgb(255,255,255)}, 11 {350, 400, 0, bmp_w, bmp_h, al_map_rgb(255,255,255)} 12 }; 13 al_draw_prim(v, NULL, textures.GetBitmapResource("Brickwall"), 0, 6, ALLEGRO_PRIM_TRIANGLE_LIST);

jmasterx
Member #11,410
October 2009

First, define your definition of 'looks right' (has screenshot)?

The main issue is that shingles do not naturally warp inwards like that.

You need to play with your UV coordinates such that either:

Your bottom UV coordinates form at straight line with the top ones:

eg:

*              *




*              *

Or:
You make the UV parellel eg:

*             *




     *             *

Right now you have the UV like a trapezoid because you have a 1 to 1 mapping with your texture, so the shingles will warp of course.

If we imagine your cords are currently:

0,0                        1,0



0,1                        1,1

Then you want to solve for the UV coord that forms a right triangle with your trapezoid.

SiegeLord
Member #7,827
October 2006
avatar

Basically, as jmasterx is explaining, this transformation is non-linear, so you can't do it without introducing some non-linearity. The typical way of accomplishing this is to use a projection transform (i.e. literally draw your rectangle in 3D and use perspective to accomplish what you want). Unfortunately, the feature to do that is only in Allegro 5.2. Here's some sample code (I forget how I came up with the constants, I'll think about it some more later):

#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_use_projection_transform(&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]

CrizerPL
Member #16,496
July 2016

This is what I'm looking for, if it wasn't clear.
{"name":"bw.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/a\/3aecb940a35bebc8b17fbb3fa434d6f5.png","w":256,"h":256,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/a\/3aecb940a35bebc8b17fbb3fa434d6f5"}bw.png
Thanks though, I guess my main priority now is to get Allegro 5.2 working :P

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Arthur Kalliokoski
Second in Command
February 2005
avatar

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

CrizerPL
Member #16,496
July 2016

It works! Thanks to all of you :)

{"name":"finally.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/1\/c1f0941ed0cc3251c2df118f1cd16f73.png","w":1219,"h":680,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/1\/c1f0941ed0cc3251c2df118f1cd16f73"}finally.png

Go to: