Python: 3D Rendering from Scratch (Projection and Rotation)

preview_player
Показать описание

this video is a part 1 of an upcoming the series which will cover matrix multiplication implemation in code and the projection and rotation matrix using python , we gonna try making our own 3d engine in python , the next video gonna cover the 4D hypercube(aka Tesseract)

for Support:

Please like and Subscribe

thanks For watching
like and Subscribe
Рекомендации по теме
Комментарии
Автор

i’ve recently started working on making a game engine from scratch. Thanks for giving me an understanding of how rendering can be achieved!

nordic
Автор

I need fellow programmers like yourself. If you ever need a friend for a project or someone to discuss your learning with I'd love to talk.

McTechnoPvP
Автор

I came here to learn about python 3d rendering, but experienced something else at the start of the video.

For the first time ever without any knowledge or i saw an cube backwards after extensive google searching it's found that it's an optical illusion, that was the case with me in this video, it's called "Necker cube", it spooked me out for few dozen minutes till i found out what it was, i was confused why your 3d object was warped and moved in weird directions, now i know it's a cube i just see it rotating but backwards making rotations entirely different than a normal cube when seen in that perspective.

Incredible experience something that i've never seen before but also the information provided which i needed for a personal 3d rendering project in python!

cybercreation
Автор

Were you using any kind of library please reply

.kartikbhardwaj
Автор

Where did you learn all this? I just started working my very own 3d modelling software like blender or c4d, and would like to know any resources their are, because my searches have come back lacking.

underrated
Автор

So cool ! How would you make it rotate based on the webcam of a computer ? Would you use AI to do that ?

valentinfontanger
Автор

Hey, that's awesome dude. I made my own program, that works just perfectly, but I still struggle to understand how you got those fancy rotations. I understand how to rotate the cube, but how do you make them beautiful, mine are just spinning to one same axis

brainloading
Автор

Ok so mine has a weird error, in the rotated_2d section in the for point loop. It says it can’t do the second matrix multiplication. Something about a nonetype value is not subscribable

codemill
Автор

how do i project oter things like a triangle or idk

wemes
Автор

How can I make it without arrays (matrixes)?

Jackson-yrih
Автор

why do you implement matrix multiplication when numpy offers it in a much faster way ? i wonder especially when making an engine you want theese multiplikations to be as fast as possible right ?

jayboy
Автор

Sure... this would give me the code, but how would I LEARN ANYTHING FROM THIS!!??

koolkd
Автор

Could you code a triangle filler for it?
(and make a video about it)

mushroomcraft
Автор

your code does not work line 60 for loop error

ishakidsworld
Автор

main py

import pygame

import os
import math
from matrix import matrix_multiplcation


width, height = 1920, 1080
black, white, blue = (20, 20, 20), (230, 230, 230), (0, 153, 255)

#pygame configurations
pygame.init()
cube projection")
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
fps = 60

angle = 0
cube_position = [width//2, height//2]
scale = 600
speed = 0.01
points = [n for n in range(8)]

points[0] = [[-1], [-1], [1]]
points[1] = [[1], [-1], [1]]
points[2] = [[1], [1], [1]]
points[3] = [[-1], [1], [1]]
points[4] = [[-1], [-1], [-1]]
points[5] = [[1], [-1], [-1]]
points[6] = [[1], [1], [-1]]
points[7] = [[-1], [1], [-1]]

def conect_point(i, j, k):
a = k[i]
b = k[j]
pygame.draw.line(screen, black, (a[0], a[1]), (b[0], b[1]), 4)

run = True
while run:
clock.tick(fps)
screen.fill(white)
for event in pygame.event.get():
if envent.type == pygame.OUIT:
run = False

index = 0
projected_points = [j for j in range(len(points))]

rotation_x = [[1, 0, 0],
[0, math.cos(angle), - math.sin(angle)],
[0, math.sin(angle), math.cos(angle)]]

rotation_y = [[math.cos(angle), 0, -math.sin(angle)],
[0, 1, 0],
[math.sin(angle), 0, math.cos(angle)]]

rotation_z =[[ [math.cos(angle), - math.sin(angle), 0],
[math.sin(angle), math.cos(angle), 0 ],
[0, 0, 1]]

for point in points:
rotated_2d = matrix_multiplucation(rotation_y, point)
rotated_2d = matrix_multiplucation(rotation_x, rotated_2d)
rotated_2d = matrix_multiplucation(rotation_z, rotated_2d)

distance = S

z = 1/(disstance = rotated_2d[2][0])
projection_matrix = [[z, 0, 0],
[0, z, 0]]
projected2d = matrix_multiplucation(projection-matrix, rotated_2d)

x = init(projected2d[0][0] * scale) + cube_position[0]
y = init(projected2d[1][0] * scale) + cube_position[1]

projected_points[index] = [x, y]
pygame.draw.circle(screen, blue, (x, y), 10)
index += 1


for m in range(4):
connect_point(m, (m+1)%4, projected_points)
connect_point(m+4, (m+1)%4+4, projected_points)
connect_point(m, m+4, projected_points)

angle += speed
pygame.display.update()

pygame.ouit()import pygame


matrix py

def matrix_multiplcation(a, b):
columns_a = len(a[0])
rows_a = len(a)
columns_b = len(b[0])
rows_b = len(b)

result_matrix = [[j for j in range(columns_b)]for i in range(rows_a)]
if columns_a == rows_b:
for x in range(rows_a):
for y in range(columns_b)
sum = 0
for k in range(columns_a):
sum += a[x][k] * b[k][y]
result_matrix[x][y] = sum
return result_matrix
else:
print("erro! the columns of the first matrix must be equal with the rows of the second matrix")
return None

ishakidsworld