From 9112b3c297736aa3683fd37dedddb5dcef0e1209 Mon Sep 17 00:00:00 2001 From: Looki2000 Date: Tue, 21 Mar 2023 00:21:56 +0100 Subject: [PATCH] I have no idea what I'm doing lol --- .gitignore | 2 ++ main_scene.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ raytracer.py | 24 ++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 .gitignore create mode 100644 main_scene.py create mode 100644 raytracer.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dada4b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +renders/* diff --git a/main_scene.py b/main_scene.py new file mode 100644 index 0000000..db6c119 --- /dev/null +++ b/main_scene.py @@ -0,0 +1,48 @@ +import pygame +import numpy as np + +import raytracer # raytracer.py + +##### CONFIG ##### +window_size = (1280, 720) + +# raytracer config +vertical_fov = 90 # fov in degrees + +################## + + +# convert fov to radians +vertical_fov = np.radians(vertical_fov) + + +# pygame init +pygame.init() +window = pygame.display.set_mode(window_size) +clock = pygame.time.Clock() + + +# raytracer init +focal_length = 1 / np.tan(vertical_fov / 2) +rt = raytracer.Raytracer(window_size, focal_length) + +rt.update_cam_rot((np.radians(70), np.radians(-27), 0)) + + + + +# main loop +while True: + # events + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + quit() + + window.fill((0, 0, 0)) + + + + # update + pygame.display.update() + clock.tick(60) \ No newline at end of file diff --git a/raytracer.py b/raytracer.py new file mode 100644 index 0000000..99fffc9 --- /dev/null +++ b/raytracer.py @@ -0,0 +1,24 @@ +import numpy as np + +# Z up + +class Raytracer: + def __init__(self, window_size, focal_length): + self.window_size = window_size + self.focal_length = focal_length + + self.cam_pos = np.zeros(3, dtype=np.float32) + self.cam_rot = np.zeros(3, dtype=np.float32) + self.cam_dir = np.zeros(3, dtype=np.float32) + + def update_cam_rot(self, cam_rot): # pitch (down - top) | yaw (left - right) | roll + self.cam_rot = np.array(cam_rot, dtype=np.float32) + + #### calculate camera direction #### + self.pitch_sin = np.sin(self.cam_rot[0]) + + self.cam_dir[0] = -np.sin(self.cam_rot[1]) * self.pitch_sin # from yaw and pitch + self.cam_dir[1] = np.cos(self.cam_rot[1]) * self.pitch_sin # from yaw and pitch + self.cam_dir[2] = -np.cos(self.cam_rot[0]) # from pitch + + print(self.cam_dir) \ No newline at end of file