Crate rin[][src]

Introduction

Installation

In order to use rin you’ll need to install rust first: https://www.rust-lang.org

Once installed you can install rin’s extension to cargo running:

cargo install cargo-rin

To update to a new version later you can run:

cargo install --force cargo-rin

You can now create new rin projects by running from a console:

cargo rin new project_path

Which will create a default empty project. For more options run

cargo rin --help

Basic example

In order to create a basic example we need to pass the --no-ecs parameter to the cargo rin tool:

cargo rin new --no-ecs basic_example

After creating a new project you’ll end up with something that should look like:

struct App{

}

impl App{
    pub fn new(gl: &gl::Renderer, window: &mut window::Window) -> App{
        App{}
    }
}

impl ApplicationCallbacks for App{
    fn update(&mut self, delta: f64, gl: &gl::Renderer, window: &mut window::Window){

    }

    fn draw(&mut self, gl: &gl::Renderer, window: &window::Window){
        gl.clear_color(&BLACK);
        gl.clear_depth();
        let gl = gl.with(Mvp::ortho_top_left(window.viewport()));

    }

    fn key_pressed(&mut self, key: window::Key, mods: window::KeyModifiers, repeat: bool){

    }
}

If you’ve used other creative frameworks the above should result familiar with some differences mostly comming from rust’s own syntax.

The most important parts in this template are:

gl.draw(&self.geometry);

Scene template

The main way to use rin is through it’s scene module which provides an ECS (Entity Component System) or data driven programming framework which works in a very different way to the examples above. Working with the scene module implies a more declarative style in which the data we create is managed by the system for a lot of basic tasks like rendering.

For example using the non ECS approach, to draw a geometry we usually create a mesh, a material and then in the draw function we explicitly call draw on it. We might even put that mesh on a vao in GPU memory to optimize drawing.

When using the scene module we first create a scene object and through that scene create a model with a geometry and material. Also using the scene we would create a light and a camera. From there on the system will draw that object without any explicit draw call. The scene holds those components and we don’t usually care about the details of how they are drawn to the screen. Since the scene handles all the objects, that gives it a much better opportunity for optimization.

In general when working with bigger applications particularly with 3D worlds it’s highly recommended to use the scene module rather than trying than an imperative method and indeed the most advanced 3D features like PBR materials, lights or shadows are only supported when using a scene. Apart from performance reasons, Rust ownership model makes it easier to work with the ECS architecture that the scene module uses, than using object oriented programming when there’s many objects that would interact with each other in a traditional OO application.

Using the cargo rin tool we can create an application to use the ECS template:

cargo rin new scene_example

This is an example of the setup functions of a rin application that creates hundreds of spheres reusing the geometry and material through the scene module:

use rin::color::consts::*;
use rin::graphics::{sphere_coords, Node, arcball_camera};
use rin::scene::Scene;
use rin::material::StandardMaterialBuilder;

fn create_entities(scene: &mut Scene){
    // Create a sphere geometry and register it in the scene so we can use it later.
    // as a return value we get it's entity id
    let sphere = sphere_texcoords(0.1, 20, 20);
    let sphere = scene.register_mesh(sphere);

    // Create a new material of with a red color
    let material = StandardMaterialBuilder::default()
        .color(RED)
        .roughness(0.9)
        .build();
    let material = scene.register_material("material", material);

    // Create multiple models using the previous material and geometry
    for y in (-60 .. 60).step_by(2){
        let fy = y as f32 / 10.;
        for x in (-100 .. 100).step_by(2){
            let fx = x as f32 / 10.;
            scene.add_model(&format!("sphere{}_{}", x,y))
                .geometry(sphere)
                .material(material)
                .transformation(pnt3(fx, fy, 0.))
                .build();
        }
    }

    let _light = scene.add_directional_light("DirLight")
        .transformation(Node::new_look_at(pnt3(3., 3., 3.), Pnt3::origin(), Vec3::y()))
        .build();

    // Add a camera to the scene
    let events_stream = scene.event_stream();
    let window_size = scene.viewport().size();
    let camera = arcball_camera::Builder::new(events_stream, window_size)
        .position(pnt3(0., 3., 3.))
        .look_at(Pnt3::origin())
        .build()
        .unwrap();
    scene.set_camera(camera);
}

To learn more about the scene module check it’s own reference: scene

Immediate renderer + ECS

In some cases we might want to use ECS but not the full scene. In that case we can create a new application using the immediate parameter for cargo rin:

cargo rin new --immediate basic_example

An immediate renderer app looks like this:

pub fn setup(window: Window, gl: gl::Renderer<'static>, events: EventsPoll) -> Scene {
    let mut scene_builder = SceneBuilder::new(events);

    scene_builder.add_update_system(update);

    let renderer = ImmediateRenderer{ renderer: gl, window, system: render};
    let mut scene = scene_builder.with_renderer(renderer).build();

    scene
}

#[update_system(name = "update")]
fn update(clock: &Clock, entities: Entities, resources: Resources) {

}

#[render_system(name = "renderer")]
fn render(
    gl: &gl::Renderer,
    viewport: Rect<i32>,
    entities: EntitiesThreadLocal,
    resources: ResourcesThreadLocal)
{
    let gl = gl.with_mvp(Mvp::ortho_top_left(viewport));
    gl.clear(&BLACK);
}

Where we have one or more update systems and usually a render one.

To learn more about the immediate renderer check it’s own reference: scene::immediate_renderer

Learning rust

For more information on using rust, you can check the rust book https://doc.rust-lang.org/book/second-edition/

Examples

You can find examples on how to use rin at: https://…

Book

The rin book has more in-depth documentation on how to use rin by showing examples for the main functionalities: https://rin.rs/book.thml

API reference

Modules

app
blender

The blender module allows to import blender files into the scene, including geometry, materials, skeletons, animations, lights and more

color
ecs

Rinecs

events
gl
graphics
material

Materials in mutiny. This module is renderer independent and the Components in it only specify materials data and some utilities. Each specific renderer should read this components in the scene and implement their behaivour

math

This module mostly re-exports na (a graphics oriented wrapper for nalgebra) angle (a type safe wrapper for angle measseures) and adds a few simple functions useful for graphics math

postpo
prelude

Prelude mod to make it easier to use rin, on applications import preamble like:

scene

Mutiny is part of the rin ecosystem of rust crates for creative expression with code.

util
window

Structs

Error

Type Definitions

Result