![]() |
|
[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. Here's the code, just in case: 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: * * * * 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. Agui GUI API -> https://github.com/jmasterx/Agui |
SiegeLord
Member #7,827
October 2006
![]() |
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): 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 |
CrizerPL
Member #16,496
July 2016
|
This is what I'm looking for, if it wasn't clear. |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
CrizerPL said:
Thanks though, I guess my main priority now is to get Allegro 5.2 working 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 My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
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"} |
|