Crate retrofire_core

Crate retrofire_core 

Source
Expand description
                                                     ______
                        ___                       /´  ____/\
      __ ______ _____ /   /\_ _ ______ _____ __  /   /_/___/\ __ _____ ______
   ==/  ´ ____/ __   \   ____/ ´ ____/  __  ` __    ___,  /==/  ´  ___/ __   \
  ==/   /´=/   ______/  /==/   /´=/   /==/   /=/   /=/   /==/   /´=/   ______/\
 ==/   /==/   /____/   /__/   /==/   /__/   /=/   /=/   /__/   /==/   /______\/
==/___/ ==\_______/\______/__/ ==\________,´_/   /==\______/__/ ==\________/\
==\___\/ ==\______\/\_____\__\/ ==\______/_____,´ /==\_____\___\/==\_______\/
                                         \_____\,´

Core functionality of the retrofire project.

Includes a math library with vectors, matrices, colors, and angles; basic geometry primitives; a software 3D renderer with customizable shaders; with more to come.

§Crate features

  • std: Makes available items requiring I/O, timekeeping, or any floating-point functions not included in core. In particular this means trigonometric and transcendental functions.

    If this feature is disabled, the crate only depends on alloc.

  • libm: Provides software implementations of floating-point functions via the libm crate.

  • mm: Provides fast approximate implementations of floating-point functions via the micromath crate.

All features are disabled by default.

§Example

use retrofire_core::geom::tri;
use retrofire_core::{prelude::*, util::*};

fn main() {
    let verts = [
        vertex(pt3(-1.0, 1.0, 0.0), rgb(1.0, 0.0, 0.0)),
        vertex(pt3(1.0, 1.0, 0.0), rgb(0.0, 0.8, 0.0)),
        vertex(pt3(0.0, -1.0, 0.0), rgb(0.4, 0.4, 1.0)),
    ];

    #[cfg(feature = "fp")]
    let shader = shader::new(
        |v: Vertex3<Color3f>, mvp: &Mat4x4<ModelToProj>| {
            // Transform vertex position from model to projection space
            // Interpolate vertex colors in linear color space
            vertex(mvp.apply(&v.pos), v.attrib.to_linear())
        },
        |frag: Frag<Color3f<_>>| frag.var.to_srgb().to_color4(),
    );
    #[cfg(not(feature = "fp"))]
    let shader = shader::new(
        |v: Vertex3<Color3f>, mvp: &Mat4x4<ModelToProj>| {
            // Transform vertex position from model to projection space
            // Interpolate vertex colors in normal sRGB color space
            vertex(mvp.apply(&v.pos), v.attrib)
        },
        |frag: Frag<Color3f<_>>| frag.var.to_color4(),
    );

    let dims @ (w, h) = (640, 480);
    let modelview = translate3(0.0, 0.0, 2.0).to();
    let project = perspective(1.0, w as f32 / h as f32, 0.1..1000.0);
    let viewport = viewport(pt2(0, h)..pt2(w, 0));

    let mut framebuf = Buf2::<Color4>::new(dims);

    render(
        [tri(0, 1, 2)],
        verts,
        &shader,
        &modelview.then(&project),
        viewport,
        &mut framebuf,
        &Context::default(),
    );

    let center_pixel = framebuf[[w / 2, h / 2]];

    if cfg!(feature = "fp") {
        assert_eq!(center_pixel, rgba(150, 128, 186, 255));
    } else {
        assert_eq!(center_pixel, rgba(114, 102, 127, 255));
    }
    #[cfg(feature = "std")]
    {
        pnm::save_ppm("triangle.ppm", framebuf).unwrap();
    }
}

Modules§

geom
Basic geometric primitives.
math
Linear algebra and other useful mathematics.
prelude
Prelude module exporting many frequently used items.
render
Turning 3D geometry into raster images.
util
Various utility types and functions.

Macros§

assert_approx_eq
Asserts that two values are approximately equal. Requires that the left operand has an applicable ApproxEq impl and that both operands impl Debug unless a custom message is given.
mat
Slight syntactic sugar for creating Matrix instances.