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
#![doc(html_favicon_url = "https://rin.rs/favicon.ico")]
/*!
# 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:
```sh
cargo install cargo-rin
```

To update to a new version later you can run:
```sh
cargo install --force cargo-rin
```

You can now create new rin projects by running from a
console:
```sh
cargo rin new project_path
```

Which will create a default empty project. For more options run
```sh
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:

```sh
cargo rin new --no-ecs basic_example
```

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

```rust
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:

- A `new` function where you can initialize anything that you'll
use across your program.
- An `update` function where you can update all the attributes
that need updating like for example animations. The update funciton
in rin receives a `delta` which is the time that the last frame took
in seconds. This is useful to do time based animation. It also receives
a renderer and a window. You usually won't use the renderer in the update
function but sometimes it can be useful. The window can be used to query it's
properties, like it's width, height...
- A `draw` function. This is where you will usually draw things to
the screen. In `draw` you usually use the renderer, called `gl`. In rin
to draw you usually call a function on the renderer. For example:

```rust
gl.draw(&self.geometry);
```
- Callbacks to attend events from mouse, keyboard and others, like `key_pressed`.

## 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:

```sh
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:

```rust
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:

```sh
cargo rin new --immediate basic_example
```

An immediate renderer app looks like this:

```rust
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/](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](https://rin.rs/book.thml)


# API reference


*/

#[doc(inline)]
pub use rin_core::*;

#[cfg(feature = "dynamic_link")]
#[allow(unused_imports)]
use rin_dynamic;