02-22-2023, 01:27 AM
Well all we need is to _underline PI and move the near not so much and it makes sense:
A more interesting question might be the reverse.
Code: (Select All)
Screen 12 'Set the screen mode to 640x480 16-color mode
Cls 'Clear the screen
' Set up the 3D coordinates of a cube
Dim x(8), y(8), z(8)
x(1) = -1: y(1) = -1: z(1) = 1
x(2) = 1: y(2) = -1: z(2) = 1
x(3) = 1: y(3) = 1: z(3) = 1
x(4) = -1: y(4) = 1: z(4) = 1
x(5) = -1: y(5) = -1: z(5) = -1
x(6) = 1: y(6) = -1: z(6) = -1
x(7) = 1: y(7) = 1: z(7) = -1
x(8) = -1: y(8) = 1: z(8) = -1
' Set up the camera position
cx = 0 ' Camera X coordinate
cy = 0 ' Camera Y coordinate
cz = -10 ' Camera Z coordinate
' Set up the projection parameters
near_plane = 2 ' Distance to near plane
far_plane = 100 ' Distance to far plane
fov = 90 ' Field of view in degrees
' Calculate the projection matrix
f = 1 / Tan(fov / 2 * _Pi / 180) ' Calculate focal length
a = f * 640 / 480 ' Calculate aspect ratio
proj_matrix(1, 1) = a: proj_matrix(2, 2) = f
proj_matrix(3, 3) = far_plane / (far_plane - near_plane)
proj_matrix(3, 4) = -far_plane * near_plane / (far_plane - near_plane)
proj_matrix(4, 3) = 1
' Apply the projection matrix to the 3D coordinates and convert to 2D screen coordinates
For i = 1 To 8
' Apply the projection matrix
x_proj = x(i) * proj_matrix(1, 1) + y(i) * proj_matrix(2, 1) + z(i) * proj_matrix(3, 1) + proj_matrix(4, 1)
y_proj = x(i) * proj_matrix(1, 2) + y(i) * proj_matrix(2, 2) + z(i) * proj_matrix(3, 2) + proj_matrix(4, 2)
w_proj = x(i) * proj_matrix(1, 4) + y(i) * proj_matrix(2, 4) + z(i) * proj_matrix(3, 4) + proj_matrix(4, 4)
' Convert to 2D screen coordinates
x_screen = 320 + x_proj / w_proj * 320 ' Center the X coordinate and scale to screen size
y_screen = 240 - y_proj / w_proj * 240 ' Center the Y coordinate and flip the Y axis
' Draw a point on the screen at the converted coordinates
PSet (x_screen, y_screen), 15
Next i
' Wait for the user to press a key
Do
Sleep
Loop Until InKey$ <> ""
End
A more interesting question might be the reverse.
b = b + ...