Last active
April 20, 2022 08:33
-
-
Save matisiekpl/53191e147dc7a92d49f9eb9bf69cbfce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Napisane przez: Mateusz Woźniak (20 kwi 2021) | |
# Program to działający silnik renderowania 3d, który używa tylko podobieństwa trójkątów | |
import threading | |
from tkinter import Tk, Canvas, Frame, BOTH | |
# Delaracja obiektu w 3d | |
pyramid = [ | |
[-500, -500, 1000], | |
[500, -500, 1000], | |
[500, -500, 2000], | |
[-500, -500, 2000], | |
[0, 3000, 2000], | |
] | |
fps = 144 | |
class Engine(Frame): | |
def __init__(self): | |
super().__init__() | |
self.master.title("Engine") | |
self.pack(fill=BOTH, expand=1) | |
self.canvas = Canvas(self) | |
# Rzeczy związane z animacją | |
self.translated = pyramid | |
self.x_translation = 10 | |
self.y_translation = 10 | |
self.render() | |
def render(self): | |
self.canvas.delete("all") | |
coords = [] | |
lines = [] | |
fov = 200 | |
# Rysowanie odcinków od każdego do każdego punktu | |
for p1 in self.translated: | |
for p2 in self.translated: | |
lines.append([p1, p2]) | |
# Projekcja odcinków z trójwymiaru na dwuwymiarowy ekran na podstawie podobieństwa trójkątów | |
for figure in lines: | |
for point in figure: | |
# wzór wyznaczony ze stosunku długości boków trójkąta | |
x = point[0] * fov / point[2] | |
y = point[1] * fov / point[2] | |
# wrzucenie każdego punktu do tablicy | |
coords.append((1920 / 2) + x) | |
coords.append((1080 / 2) - y) | |
# narysowanie linii na ekranie | |
self.canvas.create_line(*coords) | |
# | |
if self.translated[0][0] > 750 or self.translated[0][0] < -750: | |
self.x_translation = -self.x_translation | |
if self.translated[0][1] > 750 or self.translated[0][1] < -750: | |
self.y_translation = -self.y_translation | |
dup = self.translated.copy() | |
self.translated = [] | |
for point in dup: | |
self.translated.append([point[0] + self.x_translation, point[1] + self.y_translation, point[2]]) | |
self.canvas.pack(fill=BOTH, expand=1) | |
threading.Timer(1 / fps, self.render).start() | |
def main(): | |
root = Tk() | |
root.resizable(False, False) | |
root.geometry("1920x1080") | |
Engine() | |
root.mainloop() | |
if __name__ == '__main__': | |
main() | |
Author
matisiekpl
commented
Jan 11, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment