asciirend 0.3.1

no_std compatible ascii rendering core
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
//! # asciirend
//!
//! ```text
//!     .  .  .  ....   . . .   .  ..   . ...
//!  .   ..    ...   .  . .  .   .. .    ..  .
//!  .. .#+===+========+=+=============+=. ..
//!    ..  %#######===============++==++.... .
//! .   ..  ######%#...........=+==++== ..... .
//!  .    ...#######.asciirend.==++=+.  ..
//! ..     .   %%###...........=++++..     .
//!   .      .. #%########+++=+=+== ..     .  .
//!   ..   . . ...########==+=+==...  . . . .
//! . .  ..     .. ..####%+=++=. .... .  ...
//!      . .... ..... .###++=.  .      .. .. .
//! ```
//!
//! ## Generic ascii renderer
//!
//! `asciirend` is a `no_std` compatible 3D rendering core. This crate renders objects in several
//! stages:
//!
//! - Primitive shading (similar to vertex shading, albeit works on whole primitives).
//! - Fragment shading.
//! - Additional text pass.
//!
//! Fragments are shaded in full sRGB float color space, which then is quantized to characters with a
//! dithering algorithm. This allows the image to have clearer color transitions, as opposed to direct
//! character shading. This also means the rendering backend is agnostic to the available color space
//! (so long as it's not HDR!), which is important for a generic `no_std` compatible renderer.
//!
//! Entrypoint to rendering is the [`Renderer`] struct.
//!
//! With `std` feature, you may also incorporate `crossterm` crate for out-of-the-box colorful 3D
//! rendering.
//!
//! ## Example
//!
//! Please see [`src/bin/example.rs`](src/bin/example.rs) for usage sample.

#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

use alloc::{sync::Arc, vec::Vec};
use core::cmp::Ordering;

use nalgebra as na;

pub mod dithering;
use dithering::Dithering;
pub mod color;
use color::{PixelText, QuantizePixel};
pub mod extra;
pub mod material;
use material::*;

pub type Transform = na::Transform3<f32>;
pub type Vector2 = na::Vector2<f32>;
pub type Vector3 = na::Vector3<f32>;
pub type Vector4 = na::Vector4<f32>;
pub type Matrix4 = na::Matrix4<f32>;
pub type Matrix<const R: usize, const C: usize> =
    na::Matrix<f32, na::Const<R>, na::Const<C>, na::ArrayStorage<f32, R, C>>;

fn clip_to_ndc(clip_space: Vector4) -> Vector3 {
    Vector3::new(
        clip_space.x / clip_space.w,
        clip_space.y / clip_space.w,
        clip_space.z / clip_space.w,
    )
}

fn ndc_to_screen(mut p: Vector3, w: usize, h: usize) -> Vector3 {
    let half_w = (w - 1) as f32 / 2.0;
    let half_h = (h - 1) as f32 / 2.0;
    p.x = (p.x + 1.0) * half_w;
    p.y = (1.0 - p.y) * half_h;
    p.z = (p.z + 1.0) / 2.0;
    p
}

fn edge_function(a: Vector2, b: Vector2, c: Vector2) -> f32 {
    (c.x - a.x) * (b.y - a.y) - (c.y - a.y) * (b.x - a.x)
}

#[allow(unused)]
fn float_sort(a: f32, b: f32) -> Ordering {
    if a > b {
        Ordering::Greater
    } else if a < b {
        Ordering::Less
    } else {
        Ordering::Equal
    }
}

/// Computes bounding box
///
/// # Panics
///
/// If empty list is passed
fn bounding_box<const R: usize, const C: usize>(
    v: &[Matrix<R, C>],
) -> (Matrix<R, C>, Matrix<R, C>) {
    let mut min = v[0];
    let mut max = v[0];

    for v in v {
        for (v, (min, max)) in v.iter().zip(min.iter_mut().zip(max.iter_mut())) {
            *min = v.min(*min);
            *max = v.max(*max);
        }
    }

    (min, max)
}

/// Returns the assumed aspect ratio of terminal characters.
///
/// Currently returns (1, 2), indicating that characters are twice as tall, compared to their
/// width.
pub fn term_char_aspect() -> (usize, usize) {
    (1, 2)
}

/// Describes background parameters.
///
/// These parameters need to be passed at the start of each frame, and are used to clear the
/// background with specified color.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Background {
    pub color: Vector3,
}

impl Default for Background {
    fn default() -> Self {
        Self {
            color: Vector3::new(0.05, 0.23, 0.4),
        }
    }
}

/// Immediate mode renderer.
///
/// This object allows one to render graphics into arbitrary vectors, holding any [`QuantizePixel`]
/// types. The typical flow of each frame is as follows:
///
/// 1. Clear the framebuffer with [`Renderer::clear_screen`].
/// 2. First pass object rendering with [`Renderer::render`].
/// 3. Second pass text rendering with [`Renderer::text_pass`].
///
/// The end result may look something like this:
///
/// ```text
///    ...   .   .   ..    ...   .  . .  .   .. .    ..  .    .
///  ..    .  . .. ... . . ...  ==. . .  .  .     . . ..   ..  .
///   .      ..   ..   ==+===================..  ...... . .   ..
///  .      .  .   ..##=++==========+=+====+=...   ..... . ..  .
///       ..    .    .####===.........+=====.. ..  ..        ...
/// .  .  .......     .###%%#.Example.=====......     .      . .
/// . .  . .     .      ####%.........=+== .   ..     .  .    .
///     .  . ..  ..   . .##########+=====.. ...  . . . .  .  . .
/// .  ..  . . . .  ..     ########==+==.=+ .... .  ...    . .
/// .    . .  .     . .... ..######===. .  +==    .. .. .     ..
///   ..  ..    ....    .  ..  ###=== .    .  +++.    .. ..   ..
///    .    ... .... .  .. .    .#= .          ..++=... ...  . .
/// .   .    .  .. ..  .. .. ....  .       ....    .=++..  ... .
///  . ...      ...  . ...      ...  . ...      ...  . ++.
/// ```
#[derive(Default, Debug)]
pub struct Renderer {
    vertex_state: VertexState,
    fragment_state: RasterState,
}

#[derive(Debug, Clone, Copy)]
struct PrimitiveId {
    mat_idx: usize,
    obj_idx: usize,
    pri_idx: usize,
}

#[derive(Default, Debug)]
struct VertexState {
    /// (Primitive, (mat_idx, obj_idx, pri_idx))
    ///
    /// pri_idx will be passed to material at mat_idx, in order to shade fragments using correct
    /// material.
    primitives: Vec<(Primitive, PrimitiveId)>,
    /// Centers of objects in screen space coordinates
    ///
    /// This is not necessary per se, but it is used in text rendering to have stable centering of
    /// text.
    obj_clip_center: Vec<Vector4>,
}

fn plane_intersect(inside: Vector4, outside: Vector4, dim: usize, clip: f32) -> Vector4 {
    let t = (clip - inside[dim]) / (outside[dim] - inside[dim]);
    let ret = inside + t * (outside - inside);
    ret
}

impl VertexState {
    pub fn reset(&mut self) {
        self.primitives.clear();
        self.obj_clip_center.clear();
    }

    pub fn clip_and_push_primitive(&mut self, primitive: Primitive, id: PrimitiveId) {
        match primitive {
            Primitive::Triangle(t) => self.clip_and_push_triangle(t, id),
            Primitive::Line(l) => self.clip_and_push_line(l, id),
        }
    }

    pub fn clip_and_push_line(&mut self, mut line: Line, id: PrimitiveId) {
        // TODO: clip the line on all axis, and both ends of the coord space.
        // Currently the lines may not render if none of the points is within the screen.
        for dim in 2..3 {
            let Line { start, end } = line;
            let clip = [start, end].map(|v| v[dim] / libm::fabsf(v.w) < 0.0);
            let clip_cnt = clip.iter().filter(|v| **v).count();

            line = if clip_cnt == 0 {
                // No vertices clipped - push as is
                Line { start, end }
            } else if clip_cnt == 1 {
                // 1 vertice clipped - find intersection point and push
                if clip[0] {
                    Line {
                        start: plane_intersect(end, start, dim, 0.0),
                        end,
                    }
                } else {
                    Line {
                        start,
                        end: plane_intersect(start, end, dim, 0.0),
                    }
                }
            } else {
                // All vertices clipped - don't push anything
                return;
            };
        }

        self.primitives.push((Primitive::Line(line), id));
    }

    /// Performs near plane clipping and pushes the triangle on stack.
    ///
    /// This may lead into an additional triangle being created, but it will share both mat_idx and
    /// pri_idx with the original one.
    pub fn clip_and_push_triangle(&mut self, Triangle { a, b, c }: Triangle, id: PrimitiveId) {
        let dim = 2;

        let clip = [a, b, c].map(|v| v[dim] / libm::fabsf(v.w) < 0.0);
        let clip_cnt = clip.iter().filter(|v| **v).count();

        if clip_cnt == 2 {
            // 2 verts clipped, we just bring all vertices to be within bounds
            let unclipped_idx = clip.iter().enumerate().find(|(_, v)| !**v).unwrap().0;
            let mut cnt = 0;
            let verts = [a, b, c];
            let [a, b, c] = verts.map(|v| {
                let i = cnt;
                cnt += 1;
                if i == unclipped_idx {
                    v
                } else {
                    plane_intersect(verts[unclipped_idx], v, dim, 0.0)
                }
            });
            self.primitives
                .push((Primitive::Triangle(Triangle { a, b, c }), id));
        } else if clip_cnt == 1 {
            // 1 vert clipped, we get 2 intersection points, and create 2 triangles out of them
            let clipped_idx = clip.iter().enumerate().find(|(_, v)| **v).unwrap().0;
            let verts = [a, b, c];

            let (i1, i2) = if clipped_idx == 0 {
                (1, 2)
            } else {
                (0, 1 + (clipped_idx - 1) % 2)
            };

            let c1 = plane_intersect(verts[i1], verts[clipped_idx], dim, 0.0);
            let c2 = plane_intersect(verts[i2], verts[clipped_idx], dim, 0.0);

            {
                let mut verts1 = verts;
                verts1[clipped_idx] = c1;
                let [a, b, c] = verts1;
                self.primitives
                    .push((Primitive::Triangle(Triangle { a, b, c }), id));
            }

            {
                let mut verts2 = verts;
                verts2[i1] = c1;
                verts2[clipped_idx] = c2;
                let [a, b, c] = verts2;
                self.primitives
                    .push((Primitive::Triangle(Triangle { a, b, c }), id));
            }
        } else if clip_cnt == 0 {
            self.primitives
                .push((Primitive::Triangle(Triangle { a, b, c }), id));
        }
    }
}

#[derive(Default, Debug)]
struct RasterOutput {
    obj_bb: Vec<Option<(usize, usize, usize, usize)>>,
}

#[derive(Default, Debug)]
struct RasterState {
    w: usize,
    h: usize,
    /// Used for depth testing
    depth: Vec<f32>,
    /// Used as a stencil for text drawing
    objs: Vec<usize>,
    /// Used for additional passes (like text rendering)
    output: RasterOutput,
}

impl RasterState {
    pub fn ndc_to_screen(&self, p: Vector3) -> Vector3 {
        ndc_to_screen(p, self.w, self.h)
    }

    fn clear_screen<T: QuantizePixel>(
        &mut self,
        bg: &Background,
        conv_params: &T::Params,
        dithering: &mut impl Dithering,
        buf: &mut Vec<T>,
        w: usize,
        h: usize,
    ) {
        self.w = w;
        self.h = h;

        let len = w * h;
        buf.clear();
        dithering.new_frame(w, h);

        for y in 0..h {
            for x in 0..w {
                buf.push(T::quantize_color(conv_params, bg.color, dithering, x, y));
            }
        }

        self.depth.clear();
        self.depth.resize(len, 1f32);
        self.objs.resize(len, !0usize);
    }

    fn rasterize<T: QuantizePixel, M: Material + ?Sized>(
        &mut self,
        vs: &VertexState,
        mats: &mut [impl AsMut<M>],
        max_obj: usize,
        conv_params: &T::Params,
        dithering: &mut impl Dithering,
        buf: &mut Vec<T>,
    ) {
        let len = self.w * self.h;
        assert_eq!(buf.len(), len);

        self.output.obj_bb.clear();
        self.output.obj_bb.resize(max_obj, None);

        for (
            p,
            PrimitiveId {
                mat_idx,
                obj_idx,
                pri_idx,
            },
        ) in vs.primitives.iter()
        {
            let mat = mats[*mat_idx].as_mut();

            let mut shade_pixel = |x, y, depth| {
                assert!(x < self.w);
                assert!(y < self.h);
                let bidx = y * self.w + x;

                // Update bounding box before depth checking, and before making sure the fragment
                // was emitted. This is so that holes can be represented within the bounds.
                if let Some(bb) = self.output.obj_bb.get_mut(*obj_idx) {
                    if let Some((min_x, min_y, max_x, max_y)) = bb.as_mut() {
                        *min_x = core::cmp::min(*min_x, x);
                        *min_y = core::cmp::min(*min_y, y);
                        *max_x = core::cmp::max(*max_x, x);
                        *max_y = core::cmp::max(*max_y, y);
                    } else {
                        *bb = Some((x, y, x, y));
                    }
                }

                if depth >= 0.0 && self.depth[bidx] >= depth {
                    if let Some(color) = mat.fragment_shade(
                        *pri_idx,
                        Vector2::new((x as f32) / self.w as f32, (y as f32) / self.h as f32),
                        depth,
                    ) {
                        self.depth[bidx] = depth;
                        self.objs[bidx] = *obj_idx;
                        // Currently we only support cutout, although, that could be changed with
                        // RGB buffer rendering.
                        if color.w >= 0.5 {
                            buf[bidx] =
                                T::quantize_color(conv_params, color.xyz(), dithering, x, y);
                        }
                    }
                }
            };

            match p {
                Primitive::Triangle(t) => {
                    let t = [t.a, t.b, t.c]
                        .map(clip_to_ndc)
                        .map(|v| ndc_to_screen(v, self.w, self.h));

                    let (bbmin, bbmax) = bounding_box(&t);

                    let [a, b, c] = t;

                    let area = edge_function(a.xy(), b.xy(), c.xy());

                    if area <= 0.0 {
                        continue;
                    }

                    for y in
                        (bbmin.y.max(0.) as usize)..(libm::ceilf(bbmax.y.min(self.h as _)) as usize)
                    {
                        for x in (bbmin.x.max(0.) as usize)
                            ..(libm::ceilf(bbmax.x.min(self.w as _)) as usize)
                        {
                            let p = Vector2::new(x as f32, y as f32);

                            let wa = edge_function(b.xy(), c.xy(), p) / area;
                            let wb = edge_function(c.xy(), a.xy(), p) / area;
                            let wc = edge_function(a.xy(), b.xy(), p) / area;

                            if wa >= 0.0 && wb >= 0.0 && wc >= 0.0 {
                                let depth = wa * a.z + wb * b.z + wc * c.z;
                                shade_pixel(x, y, depth);
                            }
                        }
                    }
                }
                Primitive::Line(l) => {
                    let l = [l.start, l.end]
                        .map(clip_to_ndc)
                        .map(|v| ndc_to_screen(v, self.w, self.h));

                    let [a, b] = l;

                    fn plot_line_low(
                        w: usize,
                        h: usize,
                        mut x0: usize,
                        y0: usize,
                        x1: usize,
                        y1: usize,
                        mut plot: impl FnMut(usize, usize),
                    ) {
                        let dx = x1 as isize - x0 as isize;
                        let mut dy = y1 as isize - y0 as isize;
                        let mut yi = 1;

                        if dy < 0 {
                            yi = -1;
                            dy = -dy;
                        }

                        let mut d = (2 * dy) - dx;
                        let mut y = y0;

                        while x0 <= x1 && x0 < w && y < h {
                            plot(x0, y);
                            if d > 0 {
                                y = y.saturating_add_signed(yi);
                                d += 2 * (dy - dx);
                            } else {
                                d += 2 * dy;
                            }
                            x0 += 1;
                        }
                    }

                    fn plot_line_high(
                        w: usize,
                        h: usize,
                        x0: usize,
                        mut y0: usize,
                        x1: usize,
                        y1: usize,
                        mut plot: impl FnMut(usize, usize),
                    ) {
                        let mut dx = x1 as isize - x0 as isize;
                        let dy = y1 as isize - y0 as isize;
                        let mut xi = 1;

                        if dx < 0 {
                            xi = -1;
                            dx = -dx;
                        }

                        let mut d = (2 * dx) - dy;
                        let mut x = x0;

                        while y0 <= y1 && y0 < h && x < w {
                            plot(x, y0);
                            if d > 0 {
                                x = x.saturating_add_signed(xi);
                                d += 2 * (dx - dy);
                            } else {
                                d += 2 * dx;
                            }
                            y0 += 1;
                        }
                    }

                    fn plot_line(
                        w: usize,
                        h: usize,
                        x0: usize,
                        y0: usize,
                        x1: usize,
                        y1: usize,
                        plot: impl FnMut(usize, usize),
                    ) {
                        if y1.abs_diff(y0) < x1.abs_diff(x0) {
                            if x0 > x1 {
                                plot_line_low(w, h, x1, y1, x0, y0, plot);
                            } else {
                                plot_line_low(w, h, x0, y0, x1, y1, plot);
                            }
                        } else {
                            if y0 > y1 {
                                plot_line_high(w, h, x1, y1, x0, y0, plot);
                            } else {
                                plot_line_high(w, h, x0, y0, x1, y1, plot);
                            }
                        }
                    }

                    plot_line(
                        self.w,
                        self.h,
                        libm::roundf(a.x) as usize,
                        libm::roundf(a.y) as usize,
                        libm::roundf(b.x) as usize,
                        libm::roundf(b.y) as usize,
                        |x, y| {
                            // Compute how close we are to both segments (in NDC 2D);
                            let da = (a.xy() - Vector2::new(x as f32, y as f32)).magnitude();
                            let db = (b.xy() - Vector2::new(x as f32, y as f32)).magnitude();
                            let total = da + db;
                            let lerp = da / total;
                            let depth = a.z + (b.z - a.z) * lerp;
                            shade_pixel(x, y, depth);
                        },
                    );
                }
            }
        }
    }
}

/// Describes camera point of view.
///
/// The camera is ought to use right handed coordinate system, where `Z` is up, and `Y` is forward.
/// Feel free to use [`extra::create_transform`] function to construct the transformation. While
/// continuous control of the camera can be set up with
/// [`extra::camera_controller::CameraController`].
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Camera {
    pub transform: Transform,
    pub proj: na::Projective3<f32>,
}

impl Default for Camera {
    fn default() -> Self {
        Self::new(na::Perspective3::new(1f32, 90f32.to_radians(), 0.1, 500.0).to_projective())
    }
}

impl Camera {
    pub fn new(proj: na::Projective3<f32>) -> Self {
        Self {
            transform: Transform::default(),
            proj,
        }
    }
}

/// Properties for a renderable object.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Object {
    /// Tranformation matrix.
    ///
    /// Create one using [`extra::create_transform`] or do it yourself.
    pub transform: Transform,
    /// Material ID.
    ///
    /// This will correspond to the slice index of materials that are passed to the renderer.
    pub material: usize,
    /// Object type.
    pub ty: ObjType,
    /// Optional text to be drawn.
    ///
    /// If [`Renderer::text_pass`] is called, this text will be placed in the middle of the object
    /// (based on transform world origin), and the text will only be drawn within the pixels of the
    /// object, i.e. it will not overlap any other objects.
    pub text: Option<Arc<str>>,
}

/// Describes an object shape.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ObjType {
    Cube { size: Vector3 },
    Primitive(Primitive),
}

impl ObjType {
    fn gen(
        &self,
        proj: Matrix4,
        model: Matrix4,
        state: &mut VertexState,
        material: &mut (impl Material + ?Sized),
        obj_idx: usize,
        mat_idx: usize,
    ) {
        //let mat = proj * model;
        match self {
            Self::Cube { size, .. } => {
                const CUBE_VERTICES: [Vector4; 8] = [
                    Vector4::new(-0.5, -0.5, -0.5, 1.0),
                    Vector4::new(0.5, -0.5, -0.5, 1.0),
                    Vector4::new(0.5, 0.5, -0.5, 1.0),
                    Vector4::new(-0.5, 0.5, -0.5, 1.0),
                    Vector4::new(-0.5, -0.5, 0.5, 1.0),
                    Vector4::new(0.5, -0.5, 0.5, 1.0),
                    Vector4::new(0.5, 0.5, 0.5, 1.0),
                    Vector4::new(-0.5, 0.5, 0.5, 1.0),
                ];

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

                for [a, b, c] in CUBE_INDICES.map(|v| {
                    v.map(|v| {
                        //println!("{:?} - {:?}", CUBE_VERTICES[v], CUBE_VERTICES[v].component_mul(&Vector4::new(size.x, size.y, size.z, 1.0)));
                        //println!("{:?} - {:?}", mat * CUBE_VERTICES[v], mat * CUBE_VERTICES[v].component_mul(&Vector4::new(size.x, size.y, size.z, 1.0)));
                        CUBE_VERTICES[v].component_mul(&Vector4::new(size.x, size.y, size.z, 1.0))
                    })
                }) {
                    let triangle = Triangle { a, b, c };
                    let (pri_idx, primitive) =
                        material.primitive_shade(Primitive::Triangle(triangle), proj, model);
                    state.clip_and_push_primitive(
                        primitive,
                        PrimitiveId {
                            mat_idx,
                            obj_idx,
                            pri_idx,
                        },
                    );
                }
            }
            Self::Primitive(primitive) => {
                let (pri_idx, primitive) = material.primitive_shade(*primitive, proj, model);
                state.clip_and_push_primitive(
                    primitive,
                    PrimitiveId {
                        mat_idx,
                        obj_idx,
                        pri_idx,
                    },
                );
            }
        }
    }
}

/// General rendering primitives.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Primitive {
    Triangle(Triangle),
    Line(Line),
}

/// A triangle.
///
/// Described rather oddly, in homogeneous coordinates, but oh well. Deal with it. Or if you don't
/// know how, just keep `w` component set to `1.0` and it should all be good.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Triangle {
    pub a: Vector4,
    pub b: Vector4,
    pub c: Vector4,
}

/// A line.
///
/// Described rather oddly, in homogeneous coordinates, but oh well. Deal with it. Or if you don't
/// know how, just keep `w` component set to `1.0` and it should all be good.
#[derive(Debug, Clone, Copy, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Line {
    pub start: Vector4,
    pub end: Vector4,
}

impl Renderer {
    /// Clears the screen with specified background.
    ///
    /// This function must be called before every [`Renderer::render`] call, because it sets
    /// internal buffers up with specified dimensions.
    ///
    /// The `buf` should be then passed to subsequent renderer calls, without having its dimensions
    /// modified beforehand.
    pub fn clear_screen<T: QuantizePixel>(
        &mut self,
        bg: &Background,
        conv_params: &T::Params,
        dithering: &mut impl Dithering,
        buf: &mut Vec<T>,
        w: usize,
        h: usize,
    ) {
        self.fragment_state
            .clear_screen(bg, conv_params, dithering, buf, w, h)
    }

    /// Draws objects on screen.
    ///
    /// This function takes a list of objects, their materials, and draws them to given buffer.
    /// Note that `buf` must be first cleared using [`Renderer::clear_screen`] function.
    pub fn render<T: QuantizePixel, M: Material + ?Sized>(
        &mut self,
        camera: &Camera,
        conv_params: &T::Params,
        mats: &mut [impl AsMut<M>],
        objects: &[Object],
        dithering: &mut impl Dithering,
        buf: &mut Vec<T>,
    ) {
        let pos = camera.transform.transform_point(&Vector3::default().into());
        let dir = camera
            .transform
            .transform_vector(&na::vector![0.0, 1.0, 0.0]);
        let view = Matrix4::look_at_rh(&pos, &(pos + dir), &na::vector![0.0, 0.0, 1.0]);

        let proj = camera.proj.matrix() * view;

        // First, split into view space triangles and lines, Sort of equivalent of vertex shading
        self.vertex_state.reset();

        for mat in mats.iter_mut() {
            mat.as_mut().new_frame();
        }

        for (i, obj) in objects.iter().enumerate() {
            self.vertex_state
                .obj_clip_center
                .push(proj * obj.transform.matrix() * na::vector![0.0, 0.0, 0.0, 1.0]);
            obj.ty.gen(
                proj,
                *obj.transform.matrix(),
                &mut self.vertex_state,
                mats[obj.material].as_mut(),
                i,
                obj.material,
            );
        }

        // Then, render into the buffer
        self.fragment_state.rasterize(
            &self.vertex_state,
            mats,
            objects.len(),
            conv_params,
            dithering,
            buf,
        );
    }

    /// Draws text on top of rendered objects.
    ///
    /// This function takes a list of objects (the identical set, to previously passed to
    /// [`Renderer::render`]), and draws auxiliary text, if it was set.
    pub fn text_pass<T: PixelText + QuantizePixel>(
        &mut self,
        objects: &[Object],
        buf: &mut Vec<T>,
    ) {
        for (i, obj) in objects.iter().enumerate() {
            let Some((min_x, min_y, mut max_x, mut max_y)) =
                self.fragment_state.output.obj_bb.get(i).and_then(|v| *v)
            else {
                continue;
            };

            max_x += 1;
            max_y += 1;

            if max_y - min_y < 3 {
                continue;
            }

            let Some(text) = obj.text.as_deref() else {
                continue;
            };

            // TODO: create optimal line wrapping that minimizes area
            let w = text.len();

            let max_chars = max_x - min_x - 2;

            let (chars, width) = if max_chars < w {
                (text.chars().take(max_chars), max_chars)
            } else {
                (text.chars().take(w), w)
            };

            // Center point

            let mut mid_x = (max_x + min_x) / 2;
            let mut mid_y = (max_y + min_y) / 2;

            let left_chars = width / 2;
            let right_chars = (width - left_chars).saturating_sub(1);

            // Adjust midpoint with object world space center. Doing so allows us to have more
            // consistent text position.
            if let Some(coords) = self.vertex_state.obj_clip_center.get(i) {
                let ndc = clip_to_ndc(*coords);
                let screen = self.fragment_state.ndc_to_screen(ndc);
                let mx = libm::roundf(screen.x) as usize;
                let my = libm::roundf(screen.y) as usize;
                if max_y != min_y {
                    mid_y = core::cmp::min(core::cmp::max(my, min_y + 1), max_y - 2);
                }

                if max_x != min_x {
                    mid_x = core::cmp::min(
                        core::cmp::max(mx, min_x + left_chars + 1),
                        max_x - right_chars - 2,
                    );
                }
            }

            let mut darken = |x: usize, y: usize| {
                let bidx = y * self.fragment_state.w + x;
                // Do not darken other object pixels
                if self.fragment_state.objs[bidx] == i {
                    buf[bidx].darken();
                }
            };

            // First, darken all pixels around the text
            for y in ((mid_y - 1)..=(mid_y + 1)).step_by(2) {
                for x in (mid_x - left_chars - 1)..=(mid_x + right_chars + 1) {
                    darken(x, y);
                }
            }
            darken(mid_x - left_chars - 1, mid_y);
            darken(mid_x + right_chars + 1, mid_y);

            // Then, embed the character values
            for (o, c) in chars.enumerate() {
                let x = mid_x - left_chars + o;
                let bidx = mid_y * self.fragment_state.w + x;
                if self.fragment_state.objs[bidx] == i {
                    buf[bidx].embed(c);
                }
            }
        }
    }
}