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
package my3d; import java.util.ArrayList; import java.util.Stack; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; import my3d._math3d.*; import my3d._shape3d.*; public class _viewport3d { protected GraphicsContext gc; public GraphicsContext get_graphics_context() { return gc; } protected double view_w, view_h; private static final int RENDER_SHAPE_BUF_SIZE = 1000; private static final int RENDER_FACE_BUF_SIZE = 50; private static final int ALLOC_TF_BUF_SIZE = 50; private Color DEFAULT_COLOR = Color.WHITE; public _viewport3d(Canvas view) { gc = view.getGraphicsContext2D(); view_w = view.getWidth(); view_h = view.getHeight(); scene_transform = new _transform(new _vector(0, 0, 5)); for (int i = 0; i < va_buf.length; i++) va_buf[i] = new _vector(); for (int i = 0; i < pa_buf.length; i++) pa_buf[i] = new _point(); for (int i = 0; i < alloc_v_buf.length; i++) alloc_v_buf[i] = new _vector(); // 光源処理 amb_light = 0.1; head_light = 0.2; sun_light = 1.0; sun_vector = new _vector(-1, -3, 2); // クリッピング clip_front = 10e-3; clip_back = 10e3; clip_x = 0.5; clip_y = clip_x * view_h / view_w; // 座標の親子関係 ray_cx = view_w / 2 - 0.5; ray_cy = view_h / 2 - 0.5; for (int i = 0; i < alloc_tf_buf.length; i++) alloc_tf_buf[i] = new _transform(); } public _transform scene_transform; public void begin_render() { gc.clearRect(0, 0, view_w, view_h); // 光源処理 _math3d.normalize(_math3d.mul( scene_transform.orientation, sun_vector, v_sun), v_sun); // 座標の親子関係 tf_stack.clear(); alloc_tf_clear(); tf_curr = scene_transform; } public void end_render() {} protected Color get_color(_shape3d shape, _face face) { Color color; if ((color = face.get_color()) != null) return color; if ((color = shape.get_color()) != null) return color; return DEFAULT_COLOR; } protected _vector[] va_buf = new _vector[RENDER_SHAPE_BUF_SIZE]; public void render_shape(_shape3d shape) { _vector[] vertices = shape.vertices; for (int i = 0; i < vertices.length; i++) _math3d.mul(tf_curr, vertices[i], va_buf[i]); for (_face face : shape.faces) render_face(face, va_buf, get_color(shape, face)); } protected _vector[] va = new _vector[RENDER_FACE_BUF_SIZE]; protected _vector v_buf_normal = new _vector(); protected _point[] pa_buf = new _point[RENDER_FACE_BUF_SIZE]; protected void render_face(_face face, _vector[] vertices, Color color) { int n = get_vertices(face.vertex_indices, vertices, va, v_buf_normal); if (n == 0) return; n = get_polygon(va, n, pa_buf); if (n == 0) return; // 光源処理 color = _math3d.mul(color, get_diffuse(v_buf_normal)); fill_polygon(pa_buf, n, color); } protected _vector v_buf1 = new _vector(), v_buf2 = new _vector(); protected int get_vertices( int[] indices, _vector[] vertices, _vector[] va, _vector normal) { // 頂点 int n = indices.length; for (int i = 0; i < n; i++) va[i] = vertices[indices[i]]; // 法線 _math3d.sub(va[1], va[0], v_buf1); _math3d.sub(va[2], va[0], v_buf2); _math3d.cross(v_buf1, v_buf2, normal); _math3d.normalize(normal, normal); // 陰面処理 if (_math3d.dot(normal, va[0]) >= (-10e-3 * _math3d.len(va[0]))) return 0; return n; } private void project(_vector v, _point po) { po.x = view_w / 2 + (v.x / v.z) * view_w; po.y = view_h / 2 - (v.y / v.z) * view_w; } protected void fill_polygon(_point[] polygon, int n, Color color) { gc.beginPath(); for (int i = 0; i < n; i++) { if (i == 0) gc.moveTo(polygon[i].x, polygon[i].y); else gc.lineTo(polygon[i].x, polygon[i].y); } gc.closePath(); gc.setFill(color); gc.fill(); gc.stroke(); } // 光源処理 public _vector sun_vector; protected double amb_light, head_light, sun_light; protected _vector v_sun = new _vector(); public void set_light_balance(double amb, double head, double sun) { amb_light = amb; head_light = head; sun_light = sun; } public double[] get_light_balance() { return new double[] { amb_light, head_light, sun_light }; } protected double get_diffuse(_vector normal) { return Math.min( amb_light + Math.max(-normal.z * head_light, 0) + Math.max(-_math3d.dot(normal, v_sun) * sun_light, 0), 1); } // クリッピング protected double clip_front, clip_back, clip_x, clip_y; private double clip(_vector v, int index) { switch (index) { case 0: return v.z - clip_front; case 1: return clip_back - v.z; case 2: return clip_x * v.z + v.x; case 3: return clip_x * v.z - v.x; case 4: return clip_y * v.z + v.y; default: return clip_y * v.z - v.y; } } private _vector[] alloc_v_buf = new _vector[6 * 2]; private int alloc_v_pos = 0; private void alloc_v_clear() { alloc_v_pos = 0; } private _vector alloc_v() { return alloc_v_buf[alloc_v_pos++]; } protected boolean is_clip() { return alloc_v_pos != 0; } private ArrayList<_vector> v_list_clip = new ArrayList<>(); protected int get_polygon(_vector[] va, int n, _point[] pa) { _vector v_curr, v_next, v_tail; double l1, l2; _vector v; v_list_clip.clear(); alloc_v_clear(); for (int i = 0; i < n; i++) v_list_clip.add(va[i]); for (int i = 0; i < 6; i++) { v_tail = v_list_clip.get(n - 1); v_next = v_tail; for (int j = n - 1; j >= 0; j--) { v_curr = v_next; v_next = (j >= 1) ? v_list_clip.get(j - 1) : v_tail; l1 = clip(v_curr, i); l2 = clip(v_next, i); if (l1 < 0) v_list_clip.remove(j); if (l1 < 0 != l2 < 0) { v = alloc_v(); _math3d.lerp(v_curr, v_next, l2 / (l2 - l1), v); v_list_clip.add(j, v); } } n = v_list_clip.size(); if (n < 3) return 0; } for (int i = 0; i < n; i++) project(v_list_clip.get(i), pa[i]); return n; } // 座標の親子関係 private _transform[] alloc_tf_buf = new _transform[ALLOC_TF_BUF_SIZE]; private int alloc_tf_pos = 0; private void alloc_tf_clear() { alloc_tf_pos = 0; } private _transform alloc_tf() { return alloc_tf_buf[alloc_tf_pos++]; } protected _transform tf_curr = new _transform(); private Stack<_transform> tf_stack = new Stack<>(); public void push_transform(_transform tf) { tf_stack.push(tf_curr); tf_curr = _math3d.mul(tf_curr, tf, alloc_tf()); } public void pop_transform() { tf_curr = tf_stack.pop(); } private double ray_cx, ray_cy; public _vector get_ray(double x, double y, _vector vo) { vo.x = (x - ray_cx) / view_w; vo.y = (ray_cy - y) / view_w; vo.z = 1; return vo; } }