This is sort of a mixed CS/Math problem. Hopefully this is a good place to ask -- the question strikes me as primarily geometric. If not, please let me know how I might best find an answer to the problem. Also, hopefully the tags are helpful; I'm not 100% comfortable with what some of these terms mean, but they seemed the most relevant.
I'm a programmer, attempting to overlay a (seemingly) 3D image of a building over its footprint on a map (using Mapbox).
The basic idea is that you provide an image and the coordinates of its four corners, which isn't hard to get to line up correctly so that, from a certain angle, the building image looks like it's standing vertically in the space of the building.
Here's an example building image.
The trouble is, if someone alters the pitch or bearing of the map, things no longer line up.
Here's an example of the problem.
By modifying the corners of the image (bounded by the red line), it's possible to get the image to more or less line up correctly, though I haven't been able to achieve a 100% match, because changing any one corner tugs the rest of the image into misalignment, in a pretty much endless loop.
The question:
Is there a way to calculate, based on bearing (degrees of map rotation from facing pure north) and pitch (degrees of up or down rotation off of a direct vertical perspective), where the corners of the image should be to preserve realistic angles of the building walls in the image?
Some caveats/thoughts/additional questions:
Is this even possible? Or would I effectively need a different image for each bearing/pitch combo in order to effectively simulate a 3D structure from every angle.
The building is not necessarily a square (or even a parallelogram). The image is always a rectangle though.
Similar to the above, this solution has to be generalizable to an infinite number of differently shaped buildings (in rectangular images).
And just to state the hopefully obvious, I'm not trying to get others to do my "homework" and am happy to do the calculations on this myself. It's just been decades since I took even basic geometry, and I don't have any idea where to start on this one.