1al_perspective_transform(3)                        al_perspective_transform(3)
2
3
4

NAME

6       al_perspective_transform - Allegro 5 API
7

SYNOPSIS

9              #include <allegro5/allegro.h>
10
11              void al_perspective_transform(ALLEGRO_TRANSFORM *trans,
12                 float left, float top, float n,
13                 float right, float bottom, float f)
14

DESCRIPTION

16       Like  al_orthographic_transform(3)  but  honors perspective.  If every‐
17       thing is at a z-position of -near it will look the same as with an  or‐
18       thographic transformation.
19
20       To use a specific horizontal field of view you can use the relation:
21
22              tan(hfov / 2) = (right - left) / 2 / near
23
24       and therefore
25
26              near = (right - left) / 2 / tan(hfov / 2)
27
28       Example 1:
29
30                 float w = 800, h = 450; // assume our display is 800 x 450
31                 float fov = tan(90 * ALLEGRO_PI / 180 / 2); // 90 degree field of view
32
33                 // Our projection goes from 0/0 to w/h with the near parameter set
34                 // for a 90 degree horizontal viewing angle.
35                 ALLEGRO_TRANSFORM projection;
36                 al_identity_transform(&projection);
37                 al_perspective_transform(&projection, 0, 0,
38                  w / 2 / fov,
39                  w, h,
40                  2000);
41                 al_use_projection_transform(&projection);
42
43                 // Set the camera z to +400 (which is exactly the near distance)
44                 ALLEGRO_TRANSFORM camera;
45                 al_build_camera_transform(&camera, 0, 0, 400, 0, 0, 0, 0, 1, 0);
46                 al_use_transform(&camera);
47
48                 // This will draw two rectangles at the left and right edge of the
49                 // screen and vertically centered. The x distance between them is 800
50                 // units, but the camera transform moves them 400 along z, so with
51                 // a 90° viewing angle both are visible.
52                 al_draw_filled_rectangle(0, 200, 50, 250, red;
53                 al_draw_filled_rectangle(750, 200, 800, 250, red);
54
55       Example 2:
56
57                 float w = 800, h = 450; // assume our display is 800 x 450
58                 float fov = tan(90 * ALLEGRO_PI / 180 / 2); // 90 degree field of view
59                 float aspect = h / w;
60                 float zoom = 2; // enlarge x 2
61
62                 // This projection is very different from the one in the first example.
63                 // Here we map the left and right edge of the screen to -1 and +1. And
64                 // the y axis goes from -1 at the bottom to +1 at the top, scaled by
65                 // the aspect ratio. We also add a zoom parameter so we can control
66                 // the visible portion of the scene independent of the field of view.
67                 ALLEGRO_TRANSFORM projection;
68                 al_identity_transform(&projection);
69                 al_perspective_transform(&projection,
70                    -1 / zoom, aspect / zoom,
71                    1 / fov,
72                    1 / zoom, -aspect / zoom,
73                    2000);
74                 al_use_projection_transform(&projection);
75
76                 // Moves everything by -4 in the z direction.
77                 ALLEGRO_TRANSFORM camera;
78                 al_build_camera_transform(&camera, 0, 0, 4, 0, 0, 0, 0, 1, 0);
79                 al_use_transform(&camera);
80
81                 // At a z distance of 4 with a 90° hfov everything would be scaled
82                 // down to 25%. However we also zoom 2-fold, so the final scaling is
83                 // 50%. This rectangle therefore will appear at a size of 400 x 225
84                 // pixel (assuming the display is 800 x 450).
85                 al_draw_filled_rectangle(-1, -1, 1, 1, red);
86

SINCE

88       5.1.3
89

SEE ALSO

91       al_use_projection_transform(3), al_orthographic_transform(3)
92
93
94
95Allegro reference manual                           al_perspective_transform(3)
Impressum