embedded-3dgfx 0.6.2

3D graphics rendering for embedded systems (fork of embedded-gfx by Kezii)
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Debug wireframe helpers (AABB / frustum) emitted as line draw primitives.
//!
//! Intended for `std` / simulator builds when tuning culling. All helpers are
//! `no_std`-safe and write into a caller-provided callback or command buffer.

use crate::bounds::Aabb;
use crate::camera::Camera;
use crate::primitive::DrawPrimitive;
use embedded_graphics_core::pixelcolor::Rgb565;
use nalgebra::{Matrix4, Point2, Point3, Vector3};

#[allow(unused_imports)]
use nalgebra::ComplexField;

const AABB_EDGES: [(usize, usize); 12] = [
    (0, 1),
    (1, 2),
    (2, 3),
    (3, 0),
    (4, 5),
    (5, 6),
    (6, 7),
    (7, 4),
    (0, 4),
    (1, 5),
    (2, 6),
    (3, 7),
];

/// Emit 12 wireframe edges for a model-space AABB transformed by `model_matrix`.
///
/// Vertices are projected with `vp * model`. Degenerate (behind-camera) edges
/// are skipped.
pub fn emit_aabb_wireframe<F>(
    aabb: &Aabb,
    model_matrix: &Matrix4<f32>,
    vp_matrix: &Matrix4<f32>,
    color: Rgb565,
    mut emit: F,
) where
    F: FnMut(DrawPrimitive),
{
    let mvp = vp_matrix * model_matrix;
    let corners = aabb.corners();
    let mut screen: [Option<Point2<i32>>; 8] = [None; 8];
    for (i, c) in corners.iter().enumerate() {
        let clip = mvp * nalgebra::Vector4::new(c.x, c.y, c.z, 1.0);
        if clip.w <= 0.0 {
            continue;
        }
        let inv_w = 1.0 / clip.w;
        // NDC → placeholder pixel space is caller-agnostic; use raw NDC*1000
        // style ints only if engine projection already baked screen scale.
        // Prefer transforming like the engine: assume vp already maps to pixels
        // when used with K3dengine (it does — see transform_point).
        screen[i] = Some(Point2::new(
            (clip.x * inv_w) as i32,
            (clip.y * inv_w) as i32,
        ));
    }
    for &(a, b) in &AABB_EDGES {
        if let (Some(pa), Some(pb)) = (screen[a], screen[b]) {
            emit(DrawPrimitive::Line([pa, pb], color));
        }
    }
}

/// Emit AABB wireframe using the engine's screen-space projection path.
pub fn emit_aabb_wireframe_projected<F>(
    aabb: &Aabb,
    model_matrix: &Matrix4<f32>,
    project: impl Fn([f32; 3]) -> Option<Point3<i32>>,
    color: Rgb565,
    mut emit: F,
) where
    F: FnMut(DrawPrimitive),
{
    let corners = aabb.corners();
    let mut screen: [Option<Point2<i32>>; 8] = [None; 8];
    for (i, c) in corners.iter().enumerate() {
        let world = model_matrix.transform_point(&Point3::new(c.x, c.y, c.z));
        screen[i] = project([world.x, world.y, world.z]).map(|p| p.xy());
    }
    for &(a, b) in &AABB_EDGES {
        if let (Some(pa), Some(pb)) = (screen[a], screen[b]) {
            emit(DrawPrimitive::Line([pa, pb], color));
        }
    }
}

/// Approximate frustum corner rays at `near` and `far` along the view axes,
/// then emit the 12 edges of the view frustum wireframe.
///
/// Uses the camera's FOV/aspect/near/far rather than extracting planes from
/// `vp_matrix`, which keeps the math small and deterministic on MCU.
pub fn emit_frustum_wireframe<F>(
    camera: &Camera,
    project: impl Fn([f32; 3]) -> Option<Point3<i32>>,
    color: Rgb565,
    mut emit: F,
) where
    F: FnMut(DrawPrimitive),
{
    let aspect = camera.get_aspect_ratio();
    // Reconstruct eye-space frustum corners from near/far and a synthetic FOV.
    // Camera stores fov privately; approximate from projection by sampling.
    // Use a fixed vertical FOV estimate via near-plane height from vp if needed.
    // Fallback: 90° vertical FOV when we cannot read fov — Camera exposes no getter.
    // We use look direction + orthonormal basis from the view matrix.
    let view = camera.view_matrix;
    // view maps world→eye; inverse rotation is view^T for orthonormal view.
    let r = view.fixed_view::<3, 3>(0, 0);
    let right = Vector3::new(r[(0, 0)], r[(0, 1)], r[(0, 2)]);
    let up = Vector3::new(r[(1, 0)], r[(1, 1)], r[(1, 2)]);
    let forward = -Vector3::new(r[(2, 0)], r[(2, 1)], r[(2, 2)]); // camera looks down -Z in eye space

    // Derive half-angles from the camera's vertical FOV.
    let half_v = (camera.fovy() * 0.5).tan();
    let half_h = half_v * aspect;

    let eye = camera.position.coords;
    let mut corners = [Vector3::zeros(); 8];
    for (i, &z) in [camera.near, camera.far].iter().enumerate() {
        let center = eye + forward * z;
        let h = half_h * z;
        let v = half_v * z;
        let base = i * 4;
        corners[base] = center - right * h - up * v;
        corners[base + 1] = center + right * h - up * v;
        corners[base + 2] = center + right * h + up * v;
        corners[base + 3] = center - right * h + up * v;
    }

    let mut screen: [Option<Point2<i32>>; 8] = [None; 8];
    for (i, c) in corners.iter().enumerate() {
        screen[i] = project([c.x, c.y, c.z]).map(|p| p.xy());
    }
    for &(a, b) in &AABB_EDGES {
        if let (Some(pa), Some(pb)) = (screen[a], screen[b]) {
            emit(DrawPrimitive::Line([pa, pb], color));
        }
    }
}

/// Emit a wireframe grid on the XZ plane centered at `center`.
///
/// * `center`: World-space center point of the grid.
/// * `cell_size`: Distance between adjacent grid lines.
/// * `half_count`: Number of grid cells in positive and negative directions (total lines: `2 * half_count + 1` per axis).
/// * `grid_color`: Line color for standard grid lines.
/// * `axis_color`: Optional highlight color for the primary center axes passing through `center`.
/// * `project`: World-to-screen projection closure.
/// * `emit`: Callback receiving emitted line primitives.
pub fn emit_ground_grid<F>(
    center: Point3<f32>,
    cell_size: f32,
    half_count: i32,
    grid_color: Rgb565,
    axis_color: Option<Rgb565>,
    project: impl Fn([f32; 3]) -> Option<Point3<i32>>,
    mut emit: F,
) where
    F: FnMut(DrawPrimitive),
{
    let extent = half_count as f32 * cell_size;
    let min_x = center.x - extent;
    let max_x = center.x + extent;
    let min_z = center.z - extent;
    let max_z = center.z + extent;
    let y = center.y;

    // Lines parallel to Z (varying X)
    for i in -half_count..=half_count {
        let x = center.x + i as f32 * cell_size;
        let color = if i == 0 {
            axis_color.unwrap_or(grid_color)
        } else {
            grid_color
        };

        if let (Some(p0), Some(p1)) = (project([x, y, min_z]), project([x, y, max_z])) {
            emit(DrawPrimitive::Line([p0.xy(), p1.xy()], color));
        }
    }

    // Lines parallel to X (varying Z)
    for j in -half_count..=half_count {
        let z = center.z + j as f32 * cell_size;
        let color = if j == 0 {
            axis_color.unwrap_or(grid_color)
        } else {
            grid_color
        };

        if let (Some(p0), Some(p1)) = (project([min_x, y, z]), project([max_x, y, z])) {
            emit(DrawPrimitive::Line([p0.xy(), p1.xy()], color));
        }
    }
}

/// Emit 3D RGB coordinate axes (Red = +X, Green = +Y, Blue = +Z) at `origin`.
///
/// * `origin`: World-space pivot point.
/// * `model_matrix`: Model-to-world transform matrix.
/// * `length`: Length of each axis line.
/// * `project`: World-to-screen projection closure.
/// * `emit`: Callback receiving emitted line primitives.
pub fn emit_transform_axes<F>(
    origin: Point3<f32>,
    model_matrix: &Matrix4<f32>,
    length: f32,
    project: impl Fn([f32; 3]) -> Option<Point3<i32>>,
    mut emit: F,
) where
    F: FnMut(DrawPrimitive),
{
    let red = Rgb565::new(31, 0, 0);
    let green = Rgb565::new(0, 63, 0);
    let blue = Rgb565::new(0, 0, 31);

    let p_orig = model_matrix.transform_point(&origin);
    let p_x = model_matrix.transform_point(&(origin + Vector3::new(length, 0.0, 0.0)));
    let p_y = model_matrix.transform_point(&(origin + Vector3::new(0.0, length, 0.0)));
    let p_z = model_matrix.transform_point(&(origin + Vector3::new(0.0, 0.0, length)));

    if let (Some(s0), Some(sx)) = (
        project([p_orig.x, p_orig.y, p_orig.z]),
        project([p_x.x, p_x.y, p_x.z]),
    ) {
        emit(DrawPrimitive::Line([s0.xy(), sx.xy()], red));
    }
    if let (Some(s0), Some(sy)) = (
        project([p_orig.x, p_orig.y, p_orig.z]),
        project([p_y.x, p_y.y, p_y.z]),
    ) {
        emit(DrawPrimitive::Line([s0.xy(), sy.xy()], green));
    }
    if let (Some(s0), Some(sz)) = (
        project([p_orig.x, p_orig.y, p_orig.z]),
        project([p_z.x, p_z.y, p_z.z]),
    ) {
        emit(DrawPrimitive::Line([s0.xy(), sz.xy()], blue));
    }
}

/// Emit 2D screen-space text lines using the Hershey simplex stroke font.
///
/// * `text`: ASCII string (characters 32..=126).
/// * `origin`: Screen-space top-left coordinate.
/// * `scale`: Scaling factor applied to glyphs (height ≈ `21.0 * scale` pixels).
/// * `color`: Line color.
/// * `emit`: Callback receiving emitted line primitives.
pub fn emit_stroke_text_2d<F>(
    text: &str,
    origin: Point2<i32>,
    scale: f32,
    color: Rgb565,
    mut emit: F,
) where
    F: FnMut(DrawPrimitive),
{
    use crate::simplex_stroke_font::{SIMPLEX_CAP_HEIGHT, SIMPLEX_STROKE_FONT};

    let mut cursor_x = origin.x as f32;
    let baseline_y = origin.y as f32 + SIMPLEX_CAP_HEIGHT * scale;

    for c in text.chars() {
        if c == ' ' {
            cursor_x += SIMPLEX_STROKE_FONT.advance as f32 * scale;
            continue;
        }

        if let Some((advance, stroke_range)) = SIMPLEX_STROKE_FONT.get_glyph(c) {
            for s in stroke_range {
                let point_range = SIMPLEX_STROKE_FONT.strokes[s].clone();
                if point_range.len() < 2 {
                    continue;
                }

                let mut prev: Option<Point2<i32>> = None;
                for p_idx in point_range {
                    let [px, py] = SIMPLEX_STROKE_FONT.positions[p_idx];
                    let sx = (cursor_x + px as f32 * scale) as i32;
                    let sy = (baseline_y - py as f32 * scale) as i32;
                    let curr = Point2::new(sx, sy);

                    if let Some(p) = prev {
                        emit(DrawPrimitive::Line([p, curr], color));
                    }
                    prev = Some(curr);
                }
            }
            cursor_x += advance as f32 * scale;
        } else {
            cursor_x += SIMPLEX_STROKE_FONT.advance as f32 * scale;
        }
    }
}

/// Emit 3D text in world space projected to screen lines using the Hershey simplex stroke font.
///
/// Glyphs are placed on the XY plane in world space (advancing along +X, with +Y up)
/// starting at `origin`, and projected to screen space.
///
/// * `text`: ASCII string (characters 32..=126).
/// * `origin`: World-space position of first glyph baseline.
/// * `scale`: World scale factor for glyphs (height in world units ≈ `21.0 * scale`).
/// * `color`: Line color.
/// * `project`: World-to-screen projection closure.
/// * `emit`: Callback receiving emitted line primitives.
pub fn emit_stroke_text_projected<F>(
    text: &str,
    origin: Point3<f32>,
    scale: f32,
    color: Rgb565,
    project: impl Fn([f32; 3]) -> Option<Point3<i32>>,
    mut emit: F,
) where
    F: FnMut(DrawPrimitive),
{
    use crate::simplex_stroke_font::SIMPLEX_STROKE_FONT;

    let mut cursor_x = origin.x;

    for c in text.chars() {
        if c == ' ' {
            cursor_x += SIMPLEX_STROKE_FONT.advance as f32 * scale;
            continue;
        }

        if let Some((advance, stroke_range)) = SIMPLEX_STROKE_FONT.get_glyph(c) {
            for s in stroke_range {
                let point_range = SIMPLEX_STROKE_FONT.strokes[s].clone();
                if point_range.len() < 2 {
                    continue;
                }

                let mut prev: Option<Point2<i32>> = None;
                for p_idx in point_range {
                    let [px, py] = SIMPLEX_STROKE_FONT.positions[p_idx];
                    let wx = cursor_x + px as f32 * scale;
                    let wy = origin.y + py as f32 * scale;
                    let wz = origin.z;

                    let curr = project([wx, wy, wz]).map(|p| p.xy());

                    if let (Some(p), Some(c)) = (prev, curr) {
                        emit(DrawPrimitive::Line([p, c], color));
                    }
                    prev = curr;
                }
            }
            cursor_x += advance as f32 * scale;
        } else {
            cursor_x += SIMPLEX_STROKE_FONT.advance as f32 * scale;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use embedded_graphics_core::pixelcolor::WebColors;

    #[test]
    fn aabb_emits_up_to_twelve_lines() {
        let aabb = Aabb::from_min_max(Vector3::new(-1.0, -1.0, -1.0), Vector3::new(1.0, 1.0, 1.0));
        let mut count = 0;
        emit_aabb_wireframe_projected(
            &aabb,
            &Matrix4::identity(),
            |p| Some(Point3::new(p[0] as i32, p[1] as i32, 1)),
            Rgb565::CSS_LIME,
            |_| count += 1,
        );
        assert_eq!(count, 12);
    }

    #[test]
    fn test_emit_ground_grid() {
        let mut line_count = 0;
        emit_ground_grid(
            Point3::new(0.0, 0.0, 0.0),
            1.0,
            2,
            Rgb565::CSS_GRAY,
            Some(Rgb565::CSS_WHITE),
            |p| Some(Point3::new(p[0] as i32, p[2] as i32, 1)),
            |_| line_count += 1,
        );
        // 2 * half_count + 1 = 5 lines per axis * 2 axes = 10 lines
        assert_eq!(line_count, 10);
    }

    #[test]
    fn test_emit_transform_axes() {
        let mut line_count = 0;
        emit_transform_axes(
            Point3::new(0.0, 0.0, 0.0),
            &Matrix4::identity(),
            1.0,
            |p| Some(Point3::new(p[0] as i32, p[1] as i32, 1)),
            |_| line_count += 1,
        );
        assert_eq!(line_count, 3);
    }

    #[test]
    fn test_emit_stroke_text_2d() {
        let mut line_count = 0;
        emit_stroke_text_2d(
            "BEVY 3D",
            Point2::new(10, 10),
            1.0,
            Rgb565::CSS_WHITE,
            |_| line_count += 1,
        );
        assert!(line_count > 10);
    }

    #[test]
    fn test_emit_stroke_text_projected() {
        let mut line_count = 0;
        emit_stroke_text_projected(
            "HI",
            Point3::new(0.0, 0.0, 0.0),
            0.1,
            Rgb565::CSS_YELLOW,
            |p| Some(Point3::new((p[0] * 100.0) as i32, (p[1] * 100.0) as i32, 1)),
            |_| line_count += 1,
        );
        assert!(line_count > 0);
    }
}