-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsimple_3d_engine.py
195 lines (168 loc) · 6.62 KB
/
simple_3d_engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python3
import sys
import math
import time
# non std
import pygame
class Vec2d:
def __init__(self, x, y):
self.x = x
self.y = y
class Vec3d:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
class Vec4d:
def __init__(self, x, y, z, w):
self.x = x
self.y = y
self.z = z
self.w = w
class Triangle:
def __init__(self, v1, v2, v3):
self.v1 = v1
self.v2 = v2
self.v3 = v3
class Mesh:
def __init__(self, triangles):
self.triangles = triangles
def __getitem__(self, index):
return self.triangles[index]
class Matrix4x4:
def __init__(self, vecs):
self.vecs = vecs
def __mul__(self, vec3d):
vec4d = Vec4d(*[
vec3d.x * self.vecs[0][0] + vec3d.y * self.vecs[1][0] + vec3d.z * self.vecs[2][0] + self.vecs[3][0],
vec3d.x * self.vecs[0][1] + vec3d.y * self.vecs[1][1] + vec3d.z * self.vecs[2][1] + self.vecs[3][1],
vec3d.x * self.vecs[0][2] + vec3d.y * self.vecs[1][2] + vec3d.z * self.vecs[2][2] + self.vecs[3][2],
vec3d.x * self.vecs[0][3] + vec3d.y * self.vecs[1][3] + vec3d.z * self.vecs[2][3] + self.vecs[3][3]
])
if vec4d.w != 0.0:
vec4d.x /= vec4d.w
vec4d.y /= vec4d.w
vec4d.z /= vec4d.w
return vec4d
def draw_triangle(surface, color, triangle):
pygame.draw.line(surface, color, (triangle.v1.x, triangle.v1.y), (triangle.v2.x, triangle.v2.y), 1)
pygame.draw.line(surface, color, (triangle.v2.x, triangle.v2.y), (triangle.v3.x, triangle.v3.y), 1)
pygame.draw.line(surface, color, (triangle.v3.x, triangle.v3.y), (triangle.v1.x, triangle.v1.y), 1)
if __name__=='__main__':
try:
surface = pygame.display.set_mode((600,600))
pygame.init()
# constants
FPS = 20
WHITE = (255, 255, 255) # white
clock = pygame.time.Clock()
width = surface.get_width()
height = surface.get_height()
theta = 90.0 # sixtee degree
z_near = 0.1
z_far = 1000.0
# some well known terms
fov = 1 / math.tan(theta / 2) # field of view
aspect = width / height
q = z_far / (z_far - z_near) # z projection
# help:
# x = aspect * fov * x / z
# y = fov * y / z
# z = z * (q - q * z_near)
projection_matrix = Matrix4x4([
[ aspect * fov, 0 , 0 , 0],
[ 0 , fov, 0 , 0],
[ 0 , 0 , q , 1],
[ 0 , 0 , -z_near * q, 0]
])
# define cube, all triangle in same direction
cube = Mesh([
Triangle(Vec3d(0, 0, 0), Vec3d(0, 1, 0), Vec3d(1, 1, 0)), # south
Triangle(Vec3d(1, 1, 0), Vec3d(1, 0, 0), Vec3d(0, 0, 0)), # south
Triangle(Vec3d(1, 0, 0), Vec3d(1, 1, 0), Vec3d(1, 1, 1)), # east
Triangle(Vec3d(1, 1, 1), Vec3d(1, 0, 1), Vec3d(1, 0, 0)), # east
Triangle(Vec3d(0, 0, 1), Vec3d(0, 1, 1), Vec3d(0, 1, 0)), # west
Triangle(Vec3d(0, 1, 0), Vec3d(0, 0, 0), Vec3d(0, 0, 1)), # west
Triangle(Vec3d(1, 0, 1), Vec3d(1, 1, 1), Vec3d(0, 0, 1)), # north
Triangle(Vec3d(0, 0, 1), Vec3d(0, 1, 1), Vec3d(1, 1, 1)), # north
Triangle(Vec3d(0, 0, 1), Vec3d(0, 0, 0), Vec3d(1, 0, 0)), # bottom
Triangle(Vec3d(1, 0, 0), Vec3d(1, 0, 1), Vec3d(0, 0, 1)), # bottom
Triangle(Vec3d(0, 1, 0), Vec3d(0, 1, 1), Vec3d(1, 1, 1)), # top
Triangle(Vec3d(1, 1, 1), Vec3d(1, 1, 0), Vec3d(0, 1, 0)) # top
])
while True:
clock.tick(FPS)
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit(0)
keyinput = pygame.key.get_pressed()
if keyinput is not None:
# print keyinput
if keyinput[pygame.K_ESCAPE]:
sys.exit(1)
surface.fill(0)
# create rotation matrix around x and z axis
f_theta = time.time()
rot_x = Matrix4x4([
[ 1, 0, 0, 0 ],
[ 0, math.cos(f_theta), -math.sin(f_theta), 0 ],
[ 0, math.sin(f_theta), math.cos(f_theta), 0 ],
[ 0, 0, 0, 1 ]
])
rot_y = Matrix4x4([
[ math.cos(f_theta), 0, math.sin(f_theta), 0 ],
[ 0, 1, 0, 0 ],
[ -math.sin(f_theta), 0, math.cos(f_theta), 0 ],
[ 0, 0, 0, 1 ]
])
rot_z = Matrix4x4([
[ math.cos(f_theta * 0.5), -math.sin(f_theta * 0.5), 0, 0 ],
[ math.sin(f_theta * 0.5), math.cos(f_theta * 0.5), 0, 0 ],
[ 0, 0, 1, 0 ],
[ 0, 0, 0, 1 ]
])
# do projection
for triangle in cube:
# rotate z
t_z = Triangle(*[
rot_z * triangle.v1,
rot_z * triangle.v2,
rot_z * triangle.v3
])
# rotate x
t_x = Triangle(*[
rot_x * t_z.v1,
rot_x * t_z.v2,
rot_x * t_z.v3
])
# translate into Z
t_t = Triangle(*[
Vec3d(t_x.v1.x, t_x.v1.y, t_x.v1.z + 3.0),
Vec3d(t_x.v2.x, t_x.v2.y, t_x.v2.z + 3.0),
Vec3d(t_x.v3.x, t_x.v3.y, t_x.v3.z + 3.0)
])
# project
t_p = Triangle(*[
projection_matrix * t_t.v1,
projection_matrix * t_t.v2,
projection_matrix * t_t.v3
])
# shift to positive values
t_p.v1.x += 1.0
t_p.v1.y += 1.0
t_p.v2.x += 1.0
t_p.v2.y += 1.0
t_p.v3.x += 1.0
t_p.v3.y += 1.0
# scale by half the screen
t_p.v1.x *= width / 2
t_p.v1.y *= height / 2
t_p.v2.x *= width/2
t_p.v2.y *= height / 2
t_p.v3.x *= width/2
t_p.v3.y *= height / 2
draw_triangle(surface, WHITE, t_p)
pygame.display.flip()
except KeyboardInterrupt:
print('shutting down')