I am looking to how to make a 3D projection to a 2D screen, or, in other words, take a 3D point with 3 coordinates (x, y, z), and get the corresponding 2D point to draw on the screen (x, y).
I have found this answer, which I liked, and I implemented it in my code, but I would like to be able to change the camera angle. I also found this answer, which I understood very partially.
I currently can only change the camera position in 3D space, but not the angle. For as to what my code is, here it is, it is written in Python and I am coding on the Casio Graph 90+E calculator, so I only have access to a couple of functions (I also made a small 2D graphics module with simple functions for drawing primitives shapes, but dont care about it, since I'm just looking for simple 3D points) :
- set_pixel(x, y, RGB color), which sets a specific pixel on the screen to the RGB color specified
- get_pixel(x, y), which returns the color of the pixel of the screen (I dont think it will be useful here)
- show_screen(), which updates the screen
- and clear_screen(), which clears the screen in white
from casioplot import * #this is the calculator's graphics library
points = [
(-5, -5, 5),
(5, -5, 5),
(5, 5, 5),
(-5, 5, 5),
(-5, -5, 15),
(5, -5, 15),
(5, 5, 15),
(-5, 5, 15)
] #simple square corners coordinates
def threeDToTwoD(point, cam):
f = point[2] - cam[2]
x = (point[0] - cam[0]) * (f / point[2]) + cam[0]
y = (point[1] - cam[1]) * (f / point[2]) + cam[1]
return (round(x), round(y)) #the rounding is because I need integer coordinates for setting the pixel
cam = [0, 0, 0]
while True:
clear_screen()
for point in points:
x, y = threeDToTwoD(point, cam)
set_pixel(191 + x, 96 - y, (0, 0, 0))
show_screen()
cam[2] -= 1 #to move towards the points