persp_project(3alleg4)

πŸ’– persp_project β€” the Allegro manual, but make it cute ✨

okay bestie, so this is a page from the Allegro manual (yes, that Allegro β€” the game programming library πŸ‘‘). And it's about one specific little function that does one very specific little thing, and honestly? it eats. let's get into it 🫢


✨ NAME ✨

persp_project, persp_project_f β€” takes a 3d point and yeets it into 2d screen space πŸ¦‹

(also: Allegro game programming library, hi hello, that's the whole vibe)


🌸 SYNOPSIS 🌸

#include <allegro.h>

void persp_project(fixed x, fixed y, fixed z, fixed *xout, fixed *yout);

void persp_project_f(float x, float y, float z, float *xout, float *yout);

two flavors, same energy: one takes fixed, one takes float. pick your fighter πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

so what it does: it projects the 3d point (x, y, z) into 2d screen space, drops the result into (*xout, *yout), and it uses whatever scaling parameters you already set up when you called set_projection_viewport(). no cap, that's the whole trick β€” you gotta set the viewport first, this function just does the math with it ✨

and the math is coming from the normalized viewing pyramid, which sounds intimidating but is literally just:

  • the camera sits at the origin πŸŽ₯
  • the camera faces along the positive z axis
  • x runs left ↔ right
  • y runs up ↕ down
  • z increases as you go deeper into the screen (depth, bestie, it's giving perspective 🌈)

the camera has a 90 degree field of view, meaning: points sitting on the planes x=z and -x=z land on the left and right edges of the screen, and the planes y=z and -y=z land on the top and bottom of the screen. iconic geometry, honestly πŸ’œ

now, if you want a different field of view, or you want the camera somewhere else? that's not this function's job, bestie. you gotta transform all your objects with an appropriate viewing matrix. for example β€” and this is the classic one β€” to get the effect of panning the camera 10 degrees to the left, you rotate all your objects 10 degrees to the right. yes, it's backwards on purpose. that's just how it is πŸ™ˆ


πŸ¦‹ SEE ALSO πŸ¦‹

go read those next, they're the supporting cast and they also serve πŸ’…


and that's the page!! written against Allegro version 4.4.3, manual page persp_project(3alleg4) β€” mwah, love that for us πŸ’–βœ¨πŸŽ€πŸ¦‹