Crate rin_core[][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

Further install

Rust provides autocomplete and static analisys through the Rust Language Server https://github.com/rust-lang-nursery/rls

To install rls:

rustup update
rustup component add rls-preview rust-analysis rust-src

Now you can install any IDE with support for rls (usually through extensions) I’ve mostly tested rin with visual code and atom.

Basic example

After creating a new project with the rin tool 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);

Alternative template

Sometimes types in rust can be quite long and when using the above template you’ll need to specify every type of variables that are created inside the App struct. For variables created inside functions you usually just use let as in:

let radius = 10.;
let resolution = 30;
let circle = graphics::circle(radius, resolution);

Also because rin tries not to have any global object or function call you’ll need a reference to a renderer in order to draw or to a window in order to check things like it’s size. Because of that some of the callbacks in the above template are sometimes not so useful.

Another way to use rin is to use the loop template by creating a new project with the following call:

rin new --loop project_path

Which will create an empty project that looks like:

fn main() {
    let events = window::EventsPoll::new().unwrap();
    let mut window = window::Builder::new(events.clone())
        .create()
        .unwrap();

    let gl = gl::Renderer::new(&mut window).unwrap();

    while !window.should_close(){
        let _delta = window.curr_frame_time_s();
        window.make_current();
        gl.clear(&BLACK);
        let _gl = gl.with(Mvp::ortho_top_left(window.viewport()));

        // Your code goes here

        window.swap_buffers();
        events.poll_events();
        window.update();
    }
}

In this case the window and gl variables are always available and you can listen to whatever events you need by using the facilities in the events module. You can access window events by using window.event_stream().

There’s several ways to use events in rin but the most common is to get an iterator that we can later query for new events inside the application loop. As in:

let mouse_pressed = window.event_stream()
    .mouse()
    .pressed()
    .try_iter();

while !window.should_close(){
...

    for (pos, button, mods) in mouse_pressed.by_ref(){
        println!("pos {:?}", pos);
    }
}

Which prints every mouse press position that happened during the current frame.

Or for example:

let mouse_moved = window.event_stream()
    .mouse()
    .moved()
    .try_iter();

while !window.should_close(){
...

    if let Some(pos) = mouse_moved.by_ref().last(){
        println!("pos {:?}", pos);
    }
}

Which only prints the last position of the mouse during the current frame

Instead of an iterator sometimes you just want to keep the current value that an event stream has sent. For that we can use properties:

let mouse_pos = window.event_stream()
    .mouse()
    .moved()
    .to_property(pnt2(0., 0.));

while !window.should_close(){
...

    println!("pos {:?}", *mouse_pos);
}

Note how in this case we dereference the property in order to get it’s internal value. We could also simply call .value() on it.

Rin’s event iterators are thread safe (Send not Sync) so they can be stored on an object an sent to a different thread. Properties, though, are not so trying to use them from a different thread that the one they were created from will fail to compile.

If we need similar functionality to a property but across threads we can use a Parameter instead

let mouse_pos = window.event_stream()
    .mouse()
    .moved()
    .to_property(pnt2(0., 0.))
    .to_parameter();

This way of using rin also allows more control over the default loop by not hiding it away from the user and might become the default template in the future.

Mutiny template

One of the main tools in the rin ecosytem is Mutiny. Mutiny provides an ECS (Entity Component System) or data driven programming framework which works in a very different way to the examples above. If working with rin directly, has an imperative and more or less object oriented approach. Working with Mutiny 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 in rin to draw a geometry we usually create a mesh, a material and a light and then in the draw function we explicitly call draw on it. We might probably even put that mesh on a vao to optimize drawing.

When using Mutiny 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. 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 really recomended to use Mutiny rather than plain rin not only for performance reasons but also because of the rust ownership model (but really in any language) it is easier to work with the ECS architecgture that Mutiny uses, than using object oriented programming when there’s many objects that would interact with each other in a traditional OO application.

This is an example of the setup functions of a Mutiny application that creates hundreds of spheres reusing the geometry and material:

fn create_entities(scene: &mut mutiny::Scene){
    let sphere = rin::graphics::sphere_texcoords(0.1, 20, 20);
    let sphere = scene.add_mesh(sphere);

    let material = mutiny::material::StandardMaterialBuilder::default()
        .color(RED)
        .roughness(0.9)
        .build();
    let material = scene.register_material("material", material);

    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();
}

You can start a new Mutiny application using:

cargo rin new --mutiny project_path

To learn more about Mutiny you can check the specific documentation for it’s crate: https://rin.rs/doc/mutiny

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://…

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