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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// This file uses the old rendering system
// use crate::graphics::{image_rgba_to_u32, rgb_to_u32, u32_to_rgb};
// use crate::render::{uv_interpolate, vertex_3d_to_2d, Polygon};
// use std::mem;
// use image::{self, GenericImageView};
// pub fn draw_triangle(
// buffer: &mut Vec<u32>,
// width: usize,
// height: usize,
// point1: (u16, u16, f32, f32),
// point2: (u16, u16, f32, f32),
// point3: (u16, u16, f32, f32),
// texture: &image::DynamicImage,
// ) {
// // points: screen_x, screen_y, texture_x, texture_y
// let x1 = point1.0;
// let y1 = point1.1;
// let u1 = point1.2;
// let v1 = point1.3;
// let x2 = point2.0;
// let y2 = point2.1;
// let u2 = point2.2;
// let v2 = point2.3;
// let x3 = point3.0;
// let y3 = point3.1;
// let u3 = point3.2;
// let v3 = point3.3;
// let mut x_start;
// let mut x_end;
// let mut u_start;
// let mut u_end;
// let mut v_start;
// let mut v_end;
// let mut temp;
// let mut u;
// let mut v;
// let mut tex_x;
// let mut tex_y;
// let mut color;
// for y in y1..y3 + 1 {
// if y < y2 {
// x_start = uv_interpolate(
// y as f32, y1 as f32, x1 as f32, y2 as f32, x2 as f32,
// );
// x_end = uv_interpolate(
// y as f32 as f32,
// y1 as f32,
// x1 as f32,
// y3 as f32,
// x3 as f32,
// );
// u_start = uv_interpolate(
// y as f32, y1 as f32, u1 as f32, y2 as f32, u2 as f32,
// );
// u_end = uv_interpolate(
// y as f32, y1 as f32, u1 as f32, y3 as f32, u3 as f32,
// );
// v_start = uv_interpolate(
// y as f32, y1 as f32, v1 as f32, y2 as f32, v2 as f32,
// );
// v_end = uv_interpolate(
// y as f32, y1 as f32, v1 as f32, y3 as f32, v3 as f32,
// );
// } else {
// x_start = uv_interpolate(
// y as f32, y2 as f32, x2 as f32, y3 as f32, x3 as f32,
// );
// x_end = uv_interpolate(
// y as f32, y1 as f32, x1 as f32, y3 as f32, x3 as f32,
// );
// u_start = uv_interpolate(
// y as f32, y2 as f32, u2 as f32, y3 as f32, u3 as f32,
// );
// u_end = uv_interpolate(
// y as f32, y1 as f32, u1 as f32, y3 as f32, u3 as f32,
// );
// v_start = uv_interpolate(
// y as f32, y2 as f32, v2 as f32, y3 as f32, v3 as f32,
// );
// v_end = uv_interpolate(
// y as f32, y1 as f32, v1 as f32, y3 as f32, v3 as f32,
// );
// }
// if x_start > x_end {
// mem::swap(&mut x_start, &mut x_end);
// mem::swap(&mut u_start, &mut u_end);
// mem::swap(&mut v_start, &mut v_end);
// }
// for x in x_start.floor() as u16..(x_end.floor() + 1.0) as u16 {
// if x_start != x_end {
// temp = (x as f32 - x_start) / (x_end - x_start)
// } else {
// temp = 0.0
// }
// u = u_start + temp * (u_end - u_start);
// v = v_start + temp * (v_end - v_start);
// tex_x = top_clamp(
// (u * (texture.width() - 1) as f32) as u16,
// (texture.width() - 1) as u16,
// );
// tex_y = top_clamp(
// (v * (texture.height() - 1) as f32) as u16,
// (texture.width() - 1) as u16,
// );
// color = texture.get_pixel(tex_x as u32, tex_y as u32);
// set_pixel(
// buffer,
// width,
// height,
// x as usize,
// y as usize,
// image_rgba_to_u32(color),
// );
// }
// }
// }
// pub fn draw_polygon3d(
// buffer: &mut Vec<u32>,
// width: usize,
// height: usize,
// polygon: Polygon,
// texture: &image::DynamicImage,
// ) {
// let (point1_position_x, point1_position_y) =
// vertex_3d_to_2d(&polygon.point1, width, height);
// let (point2_position_x, point2_position_y) =
// vertex_3d_to_2d(&polygon.point2, width, height);
// let (point3_position_x, point3_position_y) =
// vertex_3d_to_2d(&polygon.point3, width, height);
// // print("Points1");
// // print(point1_position_x.to_string().as_str());
// // print(point1_position_y.to_string().as_str());
// // print("Points2");
// // print(point2_position_x.to_string().as_str());
// // print(point2_position_y.to_string().as_str());
// // print("Points3");
// // print(point3_position_x.to_string().as_str());
// // print(point3_position_y.to_string().as_str());
// draw_triangle(
// buffer,
// width,
// height,
// (
// point1_position_x,
// point1_position_y,
// polygon.point1.u,
// polygon.point1.v,
// ),
// (
// point2_position_x,
// point2_position_y,
// polygon.point2.u,
// polygon.point2.v,
// ),
// (
// point3_position_x,
// point3_position_y,
// polygon.point3.u,
// polygon.point3.v,
// ),
// texture,
// );
// }
// pub fn dbuffer(
// buffer: &mut Vec<u32>,
// width: usize,
// height: usize,
// texture_width: u16,
// texture_height: u16,
// texture_x: isize,
// texture_y: isize,
// texture: &image::DynamicImage,
// ) {
// let safe_width = texture.width() as f32 - 1.0;
// let safe_height = texture.height() as f32 - 1.0;
// // Clamping drawing bounds to the screen
// let start_x = texture_x.max(0) as u32;
// let start_y = texture_y.max(0) as u32;
// let end_x = (texture_x + texture_width as isize).min(width as isize) as u32;
// let end_y =
// (texture_y + texture_height as isize).min(height as isize) as u32;
// for x in start_x..end_x {
// let texture_uv_x = ((x as isize - texture_x) as f32
// / texture_width as f32)
// * safe_width;
// for y in start_y..end_y {
// let texture_uv_y = ((y as isize - texture_y) as f32
// / texture_height as f32)
// * safe_height;
// let clamped_uv_x = texture_uv_x.min(safe_width).max(0.0);
// let clamped_uv_y = texture_uv_y.min(safe_height).max(0.0);
// let pixel =
// texture.get_pixel(clamped_uv_x as u32, clamped_uv_y as u32);
// set_pixel(
// buffer,
// width,
// height,
// x as usize,
// y as usize,
// image_rgba_to_u32(pixel),
// );
// }
// }
// }
// fn buffer_to_image