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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package my3d;
import java.util.Stack;
import javafx.scene.canvas.Canvas;
import javafx.scene.image.PixelWriter;
import javafx.scene.paint.Color;
import my3d._math3d.*;
import my3d._shape3d.*;
import my3d._shape3d2.*;
public class _viewport3d3 extends _viewport3d2 {
private int zbuf_w = (int)view_w, zbuf_h = (int)view_h;
private double[][] zbuf = new double[zbuf_h][zbuf_w];
private PixelWriter pw;
public _viewport3d3(Canvas view) {
super(view);
pw = gc.getPixelWriter();
for (int i = 0; i < na_buf.length; i++)
na_buf[i] = new _vector();
for (int i = 0; i < ia_buf.length; i++)
ia_buf[i] = new _point();
pick_material = new _material(Color.RED);
DEFAULT_MATERIAL = new _material(Color.WHITE);
ray_dx = 1 / view_w;
for (int i = 0; i < alloc_ld_buf.length; i++)
alloc_ld_buf[i] = new _line_data();
}
private boolean b_zbuf;
public boolean is_zbuffer_enable() { return b_zbuf; }
public void set_zbuffer_enable(boolean b) { b_zbuf = b; }
@Override
public void begin_render() {
super.begin_render();
if (b_zbuf) {
for (int y = 0; y < zbuf_h; y++)
for (int x = 0; x < zbuf_w; x++)
zbuf[y][x] = clip_back;
}
}
private _vector[] na_buf = new _vector[va_buf.length];
@Override
public void render_shape(_shape3d shape) {
if (pre_render_shape(shape)) return;
if (!b_zbuf &&
shading_mode == _shading_mode.Flat &&
!b_texture) {
super.render_shape(shape);
return;
}
_vector[] vertices = shape.vertices;
for (int i = 0; i < vertices.length; i++)
_math3d.mul(tf_curr, vertices[i], va_buf[i]);
_shape3d2 shape2 = (shape instanceof _shape3d2) ? (_shape3d2)shape : null;
_vector[] normals = (shape2 != null) ? shape2.normals : null;
if (shading_mode != _shading_mode.Flat && normals != null) {
_matrix m = tf_curr.orientation;
for (int i = 0; i < normals.length; i++) {
_math3d.mul(m, normals[i], na_buf[i]);
if (shading_mode == _shading_mode.Gouraud)
_math3d.normalize(na_buf[i], na_buf[i]);
}
}
for (_face face : shape.faces)
render_face(face, va_buf, na_buf, get_material(shape, face));
}
private _vector[] va3 = new _vector[3];
private _vector[] na3 = new _vector[3];
private _point[] pa3 = new _point[3];
private _point p_i1, p_i2, p_i3, p_i4, intensity = new _point();
private _vector v_n1, v_n2, v_n3, v_n = new _vector();
private _point p_t1, p_t2, p_t3, p_t4, p_t = new _point();
private _vector v_buf_ray = new _vector();
private _vector[] na = new _vector[va.length];
private _point[] ia_buf = new _point[va.length];
private _point[] ta;
private boolean b_corn;
private double stretch;
private boolean b_fill_intensity, b_fill_normal, b_fill_texture;
private void render_face(
_face face,
_vector[] vertices,
_vector[] normals,
_material material) {
int vertex_count = get_vertices(face.vertex_indices, vertices, va, v_buf_normal);
if (vertex_count == 0) return;
int n = get_polygon(va, vertex_count, pa_buf);
if (n == 0) return;
// 光源処理
int c_mat = _math3d.to_argb(material.color);
int diffuse = (int)(256 * get_diffuse(v_buf_normal));
int c_face = _math3d.mul(c_mat, diffuse);
// スムーズ・シェーディング
double shininess = material.shininess;
b_fill_intensity = false;
b_fill_normal = false;
b_corn = false;
_face2 face2 = (face instanceof _face2) ? (_face2)face : null;
int[] indices = (face2 != null) ? face2.normal_indices : null;
int normal_count;
if (normals != null && indices != null) {
normal_count = indices.length;
if (normal_count == 2) b_corn = true;
for (int i = 0; i < normal_count; i++)
na[i] = normals[indices[i]];
if (shading_mode == _shading_mode.Gouraud) {
_vector v;
if (b_corn) {
for (int i = 0; i < 4; i++) {
v = na[i / 2];
ia_buf[i].set(
get_diffuse(v),
get_specular(v, va[i % 3], shininess));
}
p_i4 = ia_buf[3];
} else {
for (int i = 0; i < indices.length; i++) {
v = na[i];
ia_buf[i].set(
get_diffuse(v),
get_specular(v, va[i], shininess));
}
}
b_fill_intensity = true;
} else if (shading_mode == _shading_mode.Phong) {
b_fill_normal = true;
}
}
// テクスチャ
_texture tex = (b_texture) ? material.texture : null;
ta = (tex == null || face2 == null) ?
null : face2.texture_coordinates;
b_fill_texture = (ta != null);
stretch = (b_fill_texture && face2 != null) ?
face2.stretch : 0;
boolean b_triangle = (vertex_count == 3);
boolean b_clip = is_clip();
_point[] pa;
if (b_clip) {
pa = pa_buf;
} else {
pa = pa3; n = 3;
}
int i1, i2, i3;
for (int i = 0; i < vertex_count - 2; i++) {
if (stretch != 0 && i == 1) {
stretch = 1 / stretch;
i1 = 2; i2 = 3; i3 = 0;
} else {
i1 = 0; i2 = i + 1; i3 = i + 2;
}
va3[0] = va[i1];
va3[1] = va[i2];
va3[2] = va[i3];
if (!b_clip) {
pa3[0] = pa_buf[i1];
pa3[1] = pa_buf[i2];
pa3[2] = pa_buf[i3];
} else if (!b_triangle) {
n = get_polygon(va3, 3, pa_buf);
if (n == 0) continue;
}
if (b_fill_intensity) {
p_i1 = ia_buf[i1];
p_i2 = ia_buf[i2];
p_i3 = ia_buf[i3];
} else if (b_fill_normal) {
v_n1 = na[i1];
v_n2 = na[i2];
v_n3 = na[i3];
}
if (b_fill_texture) {
p_t1 = ta[i1];
p_t2 = ta[i2];
p_t3 = ta[i3];
}
fill_polygon(pa, n, va3, v_buf_normal, v_buf_ray,
(t1, t2, ray) -> {
double t3 = 1 - t1 - t2;
if (b_fill_intensity) {
if (b_corn) _math3d.lerp(p_i1, p_i2, p_i3, p_i4, t1, t2, t3, intensity);
else _math3d.lerp(p_i1, p_i2, p_i3, t1, t2, t3, intensity);
}
if (b_fill_normal) {
if (b_corn) {
if (t1 > (1 - 10e-3)) _math3d.add(v_n1, v_n2, v_n);
_math3d.lerp(v_n1, v_n2, t2 / (1 - t1), v_n);
} else {
_math3d.lerp(v_n1, v_n2, v_n3, t1, t2, t3, v_n);
}
_math3d.normalize(v_n, v_n);
intensity.set(
get_diffuse(v_n),
get_specular(v_n, ray, shininess));
}
int c = c_mat;
if (b_fill_texture) {
if (stretch != 0) {
t3 /= stretch * t1 + (1 - t1);
t2 = 1 - t1 - t3;
}
_math3d.lerp(p_t1, p_t2, p_t3, t1, t2, t3, p_t);
c = _math3d.add(c, tex.get_color(p_t.x, p_t.y));
}
if (!b_fill_normal && !b_fill_intensity) {
return (b_fill_texture) ?
_math3d.mul(c, diffuse) : c_face;
}
return _math3d.mul_add(c, (int)(256 * intensity.x), (int)(256 * intensity.y));
});
}
}
private static interface _render_callback {
int get_pixel_color(double t1, double t2, _vector ray);
}
private static class _line_data {
int start_y, end_y;
double x, dx;
public void set(int sy, int ey, double x, double dx) {
start_y = sy; end_y = ey;
this.x = x; this.dx = dx;
}
}
// todo:
private _line_data[] alloc_ld_buf = new _line_data[12];
private int alloc_ld_pos;
private void alloc_ld_clear() { alloc_ld_pos = 0; }
private _line_data alloc_ld() { return alloc_ld_buf[alloc_ld_pos++]; }
private Stack<_line_data> ld_stack = new Stack<>();
private double ray_dx;
private void fill_polygon(
_point[] pa,
int n,
_vector[] va,
_vector normal,
_vector ray,
_render_callback rc) {
_point p_curr, p_next, p_tail;
int start_y, end_y, temp_y;
double l1, l2, l3;
_line_data ld;
p_tail = pa[n - 1];
p_next = p_tail;
ld_stack.clear();
alloc_ld_clear();
for (int i = n - 1; i >= 0; i--) {
p_curr = p_next;
p_next = (i >= 1) ? pa[i - 1] : p_tail;
start_y = (int)(p_curr.y + 0.5);
end_y = (int)(p_next.y + 0.5);
if (start_y == end_y)
continue;
if (start_y > end_y) {
temp_y = start_y;
start_y = end_y;
end_y = temp_y;
}
l1 = p_curr.y - (start_y + 0.5);
l2 = p_next.y - (start_y + 0.5);
l3 = l1 - l2;
ld = alloc_ld();
ld.set(start_y, end_y,
(p_next.x * l1 - p_curr.x * l2) / l3 + 0.5,
(start_y + 1 != end_y) ? (p_curr.x - p_next.x) / l3 : 0);
ld_stack.push(ld);
}
n = ld_stack.size();
if (n == 0) return;
int index = get_max_index(normal);
_vector v1 = va[0], v2 = va[1], v3 = va[2];
double s = get_square(v1, v2, v3, index);
double len = _math3d.dot(v1, normal);
ld_stack.sort((ld1, ld2) -> {
int sub = ld2.start_y - ld1.start_y;
if (sub != 0) return sub;
return (int)Math.signum(ld2.x - ld1.x);
});
end_y = ld_stack.get(0).end_y;
_line_data left = ld_stack.pop(), right = ld_stack.pop();
int start_x, end_x;
start_y = left.start_y;
for (int y = start_y; y < end_y; y++) {
if (y == left.end_y) left = ld_stack.pop();
if (y == right.end_y) right = ld_stack.pop();
start_x = (int)left.x; end_x = (int)right.x;
get_ray(start_x, y, ray);
for (int x = start_x; x < end_x; x++, ray.x += ray_dx) {
double ray_x = ray.x, ray_y = ray.y, ray_z = ray.z;
_math3d.mul(ray, len / _math3d.dot(ray, normal),
v_buf1);
if (b_zbuf) {
if (v_buf1.z > zbuf[y][x])
continue;
zbuf[y][x] = v_buf1.z;
}
pw.setArgb(x, y,
rc.get_pixel_color(
get_square(v_buf1, v2, v3, index) / s,
get_square(v_buf1, v3, v1, index) / s,
ray
)
);
}
left.x += left.dx; right.x += right.dx;
}
}
public _material pick_material;
private _material DEFAULT_MATERIAL;
private _material mat_buf = new _material(Color.WHITE);
private _material get_material(_shape3d shape, _face face) {
if (is_picked(shape, face)) return pick_material;
if (shape instanceof _shape3d2) {
_material mat;
if ((mat = ((_face2)face).material) != null) return mat;
if ((mat = ((_shape3d2)shape).material) != null) return mat;
} else {
Color color;
if ((color = face.get_color()) == null)
color = shape.get_color();
if (color != null) {
mat_buf.color = color;
return mat_buf;
}
}
return DEFAULT_MATERIAL;
}
// スムーズ・シェーディング
public static enum _shading_mode {
Flat, Gouraud, Phong
};
public _shading_mode shading_mode = _shading_mode.Flat;
private double get_specular(
_vector normal,
_vector ray,
double shininess) {
if (shininess == 0) return 0;
_math3d.add(ray,
_math3d.mul(normal, _math3d.dot(ray, normal) * -2, v_buf1), v_buf2);
double k = -_math3d.dot(v_sun, v_buf2) / _math3d.len(ray);
return (k <= 0) ?
0 : sun_light * Math.pow(k, shininess);
}
// テクスチャ
protected boolean b_texture;
public boolean is_texture_enable() { return b_texture; }
public void set_texture_enable(boolean b) { b_texture = b; }
}