-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainScene.py
150 lines (134 loc) · 6.6 KB
/
MainScene.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
from Scene import Scene
import pygame
import Math3D
import math
from Math3D import *
import Math2D
import Texturing as TEX
from PIL import Image, ImageDraw
from numpy import asarray
with Image.open("alphazero.bmp") as renderimgbak:
renderimgbak=renderimgbak.copy()
print("initialized MainScene")
class MainScene(Scene):
def __init__(self, renderer):
xr=0
yr=0
zr=0
self.__renderer = renderer
MainScene.vertices = [[(20.0, 0.0, 0.0, 000, 000, 000, 0.0, 0.0, 0.0, 0, 0, xr, yr, zr), #x axis
(-20.0, 0.0, 0.0, 000, 000, 000, 0.0, 0.0, 0.0, 0, 0, xr, yr, zr), #x axis
(20.0, 0.0, 0.0, 000, 000, 000, 0.0, 0.0, 0.0, 0, 0, xr, yr, zr), #x axis
(0.0, 20.0, 0.0, 000, 000, 000, 0.0, 0.0, 0.0, 0, 0, xr, yr, zr), #y axis
(0.0, 20.0, 0.0, 000, 000, 000, 0.0, 0.0, 0.0, 0, 0, xr, yr, zr), #y axis
(0.0, -20.0, 0.0, 000, 000, 000, 0.0, 0.0, 0.0, 0, 0, xr, yr, zr), #y axis
(0.0, 0.0, 20.0, 000, 000, 000, 0.0, 0.0, 0.0, 0, 0, xr, yr, zr), #z axis
(0.0, 0.0, 20.0, 000, 000, 000, 0.0, 0.0, 0.0, 0, 0, xr, yr, zr), #z axis
(0.0, 0.0, -20.0, 000, 000, 000, 0.0, 0.0, 0.0, 0, 0, xr, yr, zr)]]
self.viewpos = [0.0, 0.0, -10.0]
self.rotation = [0.0, 0.0, 0.0]
self.delta = 0.0
self.last_frame_verts = []
self.clicking = False
self.start_click = []
self.render_surface = pygame.Surface((self.__renderer.screen_width, self.__renderer.screen_height))
def update(self, delta):
self.delta = delta
if self.clicking:
rel_pos = [pygame.mouse.get_pos()[1] - self.start_click[1], pygame.mouse.get_pos()[0] - self.start_click[0]]
self.rotation[0] = -rel_pos[0] / self.__renderer.screen_width
self.rotation[1] = -rel_pos[1] / self.__renderer.screen_height
def render(self, screen):
renderimg=renderimgbak.copy()
model = Mat4()
Math3D.translate(model, Vec3(1.0, 1.0, 1.0))
Math3D.rotate(model, 10.0 * self.rotation[0], Vec3(1.0, 0.0, 0.0))
Math3D.rotate(model, 10.0 * self.rotation[1], Vec3(0.0, 1.0, 0.0))
view = Mat4()
Math3D.translate(view, Vec3(-self.viewpos[0], -self.viewpos[1], self.viewpos[2]))
right = 12.0
top = 8.0
far = 100.0
near = 0.1
orth_proj = Mat4()
orth_proj.matrix[0][0] = 1 / right
orth_proj.matrix[1][1] = 1 / top
orth_proj.matrix[2][2] = -2 / (far - near)
orth_proj.matrix[2][3] = -((far + near) / (far - near))
world_space_tris = []
for obj in MainScene.vertices:
for i in range(0, len(obj), 3):
tri = []
rgb = None
for (x, y, z, r, g, b,X,Y,Z, tex, o, xr, yr, zr) in obj[i:i+3]:
point=Math2D.rotate3d(x,y,z,xr,yr,zr)
x=point[0]
y=point[1]
z=point[2]
vec = model * Vec4(x+X, y+Y, z+Z, 1.0)
tri.append(vec)
rgb = (r, g, b)
tex = [tex,o]
world_space_tris.append([tri, rgb, tex])
# sort the triangles so they get drawn in the right order. Sorted by mean depth of vertices in triangle
world_space_tris.sort(
key=lambda k: sum([vec.z for vec in k[0]]) / 3, reverse=True)
orthxview = orth_proj * view
transformed_vertices_tris = []
for tri, col, tex in world_space_tris:
new_tri = []
for v in tri:
vec = orthxview * v
vec.w = -vec.z
vec = Vec3(vec.x / vec.w, vec.y / vec.w, vec.z / vec.w)
new_tri.append(vec)
transformed_vertices_tris.append([new_tri, col, tex])
self.render_surface.fill((255, 255, 255))
for tri, col, tex in transformed_vertices_tris:
point1x = math.ceil(((tri[0].x + 1.0) / 2.0) * self.__renderer.screen_width)
point1y = math.ceil(((-tri[0].y + 1.0) / 2.0) * self.__renderer.screen_height)
point2x = math.ceil(((tri[1].x + 1.0) / 2.0) * self.__renderer.screen_width)
point2y = math.ceil(((-tri[1].y + 1.0) / 2.0) * self.__renderer.screen_height)
point3x = math.ceil(((tri[2].x + 1.0) / 2.0) * self.__renderer.screen_width)
point3y = math.ceil(((-tri[2].y + 1.0) / 2.0) * self.__renderer.screen_height)
#pygame.draw.polygon(self.render_surface, (col[0], col[1], col[2]), [[point1x, point1y], [point2x, point2y], [point3x, point3y]])
# pygame.draw.line(self.render_surface, (0, 0, 0), (point1x, point1y), (point2x, point2y))
# pygame.draw.line(self.render_surface, (0, 0, 0), (point2x, point2y), (point3x, point3y))
# pygame.draw.line(self.render_surface, (0, 0, 0), (point3x, point3y), (point1x, point1y))
# pygame.draw.circle(self.render_surface, (0, 0, 0), (point1x, point1y), 3)
# pygame.draw.circle(self.render_surface, (0, 0, 0), (point2x, point2y), 3)
# pygame.draw.circle(self.render_surface, (0, 0, 0), (point3x, point3y), 3)
if tex != [0,0]:
renderimg=TEX.texture((point1x,point1y),(point2x,point2y),(point3x,point3y),tex[0],tex[1], renderimg)
img = TEX.pilImageToSurface(renderimg)
self.render_surface.blit(img, img.get_rect())
screen.blit(self.render_surface, (0, 0))
def handle_input(self, events, pressed_keys):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
if self.clicking == False:
self.start_click = [pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]]
self.clicking = True
if event.type == pygame.MOUSEBUTTONUP:
self.clicking = False
self.start_click = [pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]]
if pressed_keys[pygame.K_UP]:
self.viewpos[1] += 0.1
if pressed_keys[pygame.K_DOWN]:
self.viewpos[1] -= 0.1
if pressed_keys[pygame.K_LEFT]:
self.viewpos[0] -= 0.1
if pressed_keys[pygame.K_RIGHT]:
self.viewpos[0] += 0.1
if pressed_keys[pygame.K_PAGEUP]:
self.viewpos[2] -= 0.1
if pressed_keys[pygame.K_PAGEDOWN]:
self.viewpos[2] += 0.1
if pressed_keys[pygame.K_e]:
self.rotation[1] += 0.01
if pressed_keys[pygame.K_q]:
self.rotation[1] -= 0.01
if pressed_keys[pygame.K_w]:
self.rotation[0] += 0.01
if pressed_keys[pygame.K_s]:
self.rotation[0] -= 0.01