-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.py
152 lines (121 loc) · 5.92 KB
/
Renderer.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
from PyQt5.QtGui import QPainter, QColor, QPen, QPicture
from PyQt5.QtCore import Qt
from Model import Model
import MatrixShop
import numpy as np
from ZBufferMethod import ZBuffer
class Renderer:
@staticmethod
def _render_all(models, project_matrix, model_matrix, context):
transformation_matrix = transform_matrix_init(project_matrix, model_matrix, context)
# преобразовать вершины умножив на матрицу
transform_vertices = []
for model in models:
transform_vertices.append([*map(lambda vert: vert @ transformation_matrix, model.vertices)])
#z-buffer work
x_size, y_size = context.width(), context.height()
transformed_models = []
for transf_verts, model in zip(transform_vertices, models):
new_model = Model().init_like(transf_verts, model.faces, model.color)
transformed_models.append(new_model)
z_buff = ZBuffer(transformed_models)
z_buf, color_mask = z_buff.go(back_color=Qt.lightGray, x_size=x_size, y_size=y_size)
return z_buf, color_mask
@staticmethod
def render_all_color(models, project_matrix, model_matrix, context):
_, color_mask = Renderer._render_all(models, project_matrix, model_matrix, context)
Renderer.render_from_color_mask(color_mask, context)
@staticmethod
def render_all_z_mask(models, project_matrix, model_matrix, context):
z_buf, _ = Renderer._render_all(models, project_matrix, model_matrix, context)
Renderer.render_from_z_mask(z_buf, context)
@staticmethod
def render_one(model: Model, project_matrix, model_matrix, context):
transformation_matrix = transform_matrix_init(project_matrix, model_matrix, context)
# преобразовать вершины умножив на матрицу
transform_vertices = [*map(lambda vert: vert @ transformation_matrix, model.vertices)]
#z-buffer work
x_size, y_size = context.width(), context.height()
z_buff = ZBuffer([Model().init_like(transform_vertices, model.faces, model.color)])
z_buff_mask, color_mask = z_buff.go(back_color=Qt.lightGray, x_size=x_size, y_size=y_size)
# рисовать
Renderer.render_from_color_mask(color_mask, context)
@staticmethod
def prerender_all(models, project_matrix, model_matrix, context):
transformation_matrix = transform_matrix_init(project_matrix, model_matrix, context)
# преобразовать вершины умножив на матрицу
transform_vertices = []
for model in models:
transform_vertices.append([*map(lambda vert: vert @ transformation_matrix, model.vertices)])
#z-buffer work
x_size, y_size = context.width(), context.height()
transformed_models = []
for transf_verts, model in zip(transform_vertices, models):
new_model = Model().init_like(transf_verts, model.faces, model.color)
transformed_models.append(new_model)
z_buff = ZBuffer(transformed_models)
generator = z_buff.step_by_step(back_color=Qt.lightGray, x_size=x_size, y_size=y_size)
return generator
@staticmethod
def render_from_z_mask(z_buf_mask, context):
z_buf_mask = convert_deep_to_alpha(z_buf_mask)
# рисовать
qp = QPainter()
picture = QPicture()
qp.begin(picture)
for x in range(len(z_buf_mask)):
for y in range(len(z_buf_mask[0])):
# a = color_mask[x][y]
a = QColor(Qt.black)
a.setAlpha(z_buf_mask[x][y])
qp.setPen(a)
qp.drawPoint(x, y)
qp.end() # painting done
picture.save("drawing.pic") # save picture
picture = QPicture()
picture.load("drawing.pic") # load picture
qp = QPainter()
qp.begin(context) # paint in myImage
qp.drawPicture(0, 0, picture) # draw the picture at (0,0)
qp.end()
@staticmethod
def render_from_color_mask(color_mask, context):
qp = QPainter()
picture = QPicture()
qp.begin(picture)
for x in range(len(color_mask)):
for y in range(len(color_mask[0])):
a = color_mask[x][y]
qp.setPen(a)
qp.drawPoint(x, y)
qp.end() # painting done
picture.save("drawing.pic") # save picture
picture = QPicture()
picture.load("drawing.pic") # load picture
qp = QPainter()
qp.begin(context) # paint in myImage
qp.drawPicture(0, 0, picture) # draw the picture at (0,0)
qp.end()
def prepeare_to_screen(context) :
scale = min(context.width(), context.height()) / 1.5
scale_matrix = MatrixShop.dilation(scale, scale, scale)
invert_matrix = MatrixShop.reflectionY()
center_matrix = MatrixShop.translation(context.width() / 2, context.height() / 2, 0)
return scale_matrix @ invert_matrix @ center_matrix
def convert_deep_to_alpha(z_buf):
tmp = map((lambda line: list(filter(lambda n: not np.isinf(n), line))), z_buf)
tmp = list(filter(lambda line: line != [], tmp))
z_min = min(map(min, tmp))
z_max = max(map(max, tmp))
z_zero = z_min - 1
z_buf = (map(lambda n: n if n != -np.inf else z_zero, line) for line in z_buf)
to_procent = lambda x: int((x - z_zero) * 255 / (z_max - z_zero))
z_buf = (map(to_procent, line) for line in z_buf)
z_buf = list(map(list, z_buf))
return z_buf
def transform_matrix_init(project_matrix, model_matrix, context):
project_matrix[2,2] = 1
transformation_matrix = model_matrix @ project_matrix # module viue projection matrix
prep_to_scr = prepeare_to_screen(context)
transformation_matrix = transformation_matrix @ prep_to_scr
return transformation_matrix