[A5] Drawing a textured trapezoid (2D)
CrizerPL

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

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

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}

CrizerPL

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
CrizerPL said:

Thanks though, I guess my main priority now is to get Allegro 5.2 working :P

I offer binaries for MinGW 4.8.1 and Allegro 5.2 if you're interested. The A5.2 package includes all dependencies.

https://sourceforge.net/projects/unofficialmingw/files/mingw_4_8_1-4.tar.7z/download

https://sourceforge.net/projects/unofficialallegro5distribution/files/A52GITdistro2.tar.7z/download

Arthur Kalliokoski
CrizerPL

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

Thread #616402. Printed from Allegro.cc