Skip to content

Commit

Permalink
Further sketching out of the interface between renderer and simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
aeplay committed Jun 19, 2016
1 parent 063a901 commit f2ebe5f
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 126 deletions.
69 changes: 69 additions & 0 deletions citybound/resources/car.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// a simple car
//
// . B-------------C
// 3-------------4 ` \ 1.65
// / 9-A \ . D-------E |
// 1-2 | 5-------6 ` | Z
// | . 8- - - - - - - - - - -|------F | Y . 0.9
// 0----------------------------7 ` 0 -0.9
//
// -2.25-----------X----------2.25

extern crate monet;

use monet::Vertex;

pub fn create() -> monet::Thing {
monet::Thing::new(
vec![
Vertex { position: [ -2.25, -0.9, 0.00 ] }, // 0
Vertex { position: [ -2.25, -0.9, 0.80 ] }, // 1
Vertex { position: [ -2.00, -0.9, 1.00 ] }, // 2
Vertex { position: [ -1.75, -0.9, 1.65 ] }, // 3
Vertex { position: [ 0.30, -0.9, 1.65 ] }, // 4
Vertex { position: [ 1.00, -0.9, 1.00 ] }, // 5
Vertex { position: [ 2.25, -0.9, 0.80 ] }, // 6
Vertex { position: [ 2.25, -0.9, 0.00 ] }, // 7

Vertex { position: [ -2.25, 0.9, 0.00 ] }, // 8
Vertex { position: [ -2.25, 0.9, 0.80 ] }, // 9
Vertex { position: [ -2.00, 0.9, 1.00 ] }, // A
Vertex { position: [ -1.75, 0.9, 1.65 ] }, // B
Vertex { position: [ 0.30, 0.9, 1.65 ] }, // C
Vertex { position: [ 1.00, 0.9, 1.00 ] }, // D
Vertex { position: [ 2.25, 0.9, 0.80 ] }, // E
Vertex { position: [ 2.25, 0.9, 0.00 ] }, // F
],
vec![
// right side
0, 1, 2,
0, 2, 5,
0, 5, 7,
5, 6, 7,
2, 3, 4,
2, 4, 5,
// left side
8, 9, 0xA,
8, 0xA, 0xD,
8, 0xD, 0xF,
0xD, 0xE, 0xF,
0xA, 0xB, 0xC,
0xA, 0xC, 0xD,
// connection between sides (front to back)
8, 9, 1,
8, 1, 0,
9, 0xA, 2,
9, 2, 1,
0xA, 0xB, 3,
0xA, 3, 2,
0xB, 0xC, 4,
0xB, 4, 3,
0xC, 0xD, 5,
0xC, 5, 4,
0xD, 0xE, 6,
0xD, 6, 5,
0xE, 0xF, 7,
0xE, 7, 6u16
]
)
}
11 changes: 7 additions & 4 deletions citybound/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ use std::sync::mpsc::channel;
mod models;
mod steps;
mod simulation;
#[path = "../resources/car.rs"]
mod car;

fn main() {
let (to_simulation, from_renderer) = channel::<()>();
let (to_renderer, from_simulation) = channel::<String>();
let (to_renderer, from_simulation) = channel::<monet::Scene>();

let renderer_listener = move |past: &models::State, future: &models::State| {
match from_renderer.try_recv() {
Ok(_) => {
println!("creating renderer state...");
to_renderer.send(
format!("Simulation frame: {}", past.core.header.ticks)
).unwrap();
let mut scene = monet::Scene::new();
scene.things.insert("car", car::create());
scene.debug_text = format!("Simulation frame: {}", past.core.header.ticks);
to_renderer.send(scene).unwrap();
},
Err(_) => {}
};
Expand Down
168 changes: 64 additions & 104 deletions monet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,114 +4,70 @@ extern crate glium_text;
extern crate nalgebra;
use nalgebra::{Point3, Vector3, Isometry3, Perspective3, ToHomogeneous};

use glium::glutin;
use glium::index::PrimitiveType;
use glium::Surface;
use glium::{glutin, index, Surface};

use std::sync::mpsc::{Sender, Receiver};
use std::collections::HashMap;

#[derive(Copy, Clone)]
pub struct Vertex {
pub position: [f32; 3]
}

implement_vertex!(Vertex, position);

pub struct Eye {
pub position: Point3<f32>,
pub target: Point3<f32>,
pub up: Vector3<f32>,
pub field_of_view: f32
}

pub struct Thing {
vertices: Vec<Vertex>,
indices: Vec<u16>
}

impl Thing {
pub fn new(vertices: Vec<Vertex>, indices: Vec<u16>) -> Thing {
Thing{vertices: vertices, indices: indices}
}
}

pub struct Scene {
eye: Eye,
pub things: HashMap<&'static str, Thing>,
pub debug_text: String
}

impl Scene {
pub fn new() -> Scene {
Scene{
eye: Eye{
position: Point3::new(-5.0, -5.0, 5.0),
target: Point3::new(0.0, 0.0, 0.0),
up: Vector3::new(0.0, 0.0, 1.0),
field_of_view: 0.3 * std::f32::consts::PI
},
things: HashMap::with_capacity(3000),
debug_text: String::new()
}
}
}

pub fn main_loop (prepare_frame: Sender<()>, data_provider: Receiver<String>) {
pub fn main_loop (prepare_frame: Sender<()>, data_provider: Receiver<Scene>) {
use glium::DisplayBuild;

let window = glutin::WindowBuilder::new()
.with_title("Citybound".to_string())
.with_dimensions(512, 512)
.with_multitouch()
.with_vsync().build_glium().unwrap();

#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 3]
}

implement_vertex!(Vertex, position);

// a simple car
//
// . B-------------C
// 3-------------4 ` \ 1.65
// / 9-A \ . D-------E |
// 1-2 | 5-------6 ` | Z
// | . 8- - - - - - - - - - -|------F | Y . 0.9
// 0----------------------------7 ` 0 -0.9
//
// -2.25-----------X----------2.25

let vertex_buffer = glium::VertexBuffer::new(&window, &[
Vertex { position: [ -2.25, -0.9, 0.00 ] }, // 0
Vertex { position: [ -2.25, -0.9, 0.80 ] }, // 1
Vertex { position: [ -2.00, -0.9, 1.00 ] }, // 2
Vertex { position: [ -1.75, -0.9, 1.65 ] }, // 3
Vertex { position: [ 0.30, -0.9, 1.65 ] }, // 4
Vertex { position: [ 1.00, -0.9, 1.00 ] }, // 5
Vertex { position: [ 2.25, -0.9, 0.80 ] }, // 6
Vertex { position: [ 2.25, -0.9, 0.00 ] }, // 7

Vertex { position: [ -2.25, 0.9, 0.00 ] }, // 8
Vertex { position: [ -2.25, 0.9, 0.80 ] }, // 9
Vertex { position: [ -2.00, 0.9, 1.00 ] }, // A
Vertex { position: [ -1.75, 0.9, 1.65 ] }, // B
Vertex { position: [ 0.30, 0.9, 1.65 ] }, // C
Vertex { position: [ 1.00, 0.9, 1.00 ] }, // D
Vertex { position: [ 2.25, 0.9, 0.80 ] }, // E
Vertex { position: [ 2.25, 0.9, 0.00 ] }, // F
]).unwrap();

let index_buffer = glium::IndexBuffer::new(&window, PrimitiveType::TrianglesList, &[
// right side
0, 1, 2,
0, 2, 5,
0, 5, 7,
5, 6, 7,
2, 3, 4,
2, 4, 5,
// left side
8, 9, 0xA,
8, 0xA, 0xD,
8, 0xD, 0xF,
0xD, 0xE, 0xF,
0xA, 0xB, 0xC,
0xA, 0xC, 0xD,
// connection between sides (front to back)
8, 9, 1,
8, 1, 0,
9, 0xA, 2,
9, 2, 1,
0xA, 0xB, 3,
0xA, 3, 2,
0xB, 0xC, 4,
0xB, 4, 3,
0xC, 0xD, 5,
0xC, 5, 4,
0xD, 0xE, 6,
0xD, 6, 5,
0xE, 0xF, 7,
0xE, 7, 6u16
]).unwrap();

let program = program!(&window,
140 => {
vertex: "
#version 140
uniform mat4 model;
uniform mat4 view;
uniform mat4 perspective;
in vec3 position;
out vec3 p;
void main() {
mat4 modelview = view * model;
gl_Position = perspective * modelview * vec4(position, 1.0);
p = position;
}
",

fragment: "
#version 140
out vec4 f_color;
in vec3 p;
void main() {
f_color = vec4(1.0, p.x, p.y, 1.0);
}
"
vertex: include_str!("shader/solid_140.glslv"),
fragment: include_str!("shader/solid_140.glslf"),
},
).unwrap();

Expand All @@ -133,19 +89,19 @@ pub fn main_loop (prepare_frame: Sender<()>, data_provider: Receiver<String>) {
}

prepare_frame.send(()).unwrap();
let new_data = data_provider.recv().unwrap();
let scene = data_provider.recv().unwrap();
println!("rendering...");

let mut target = window.draw();

let view : [[f32; 4]; 4] = *Isometry3::look_at_rh(
&Point3::new(-5.0, -5.0, 5.0),
&Point3::new(0.0, 0.0, 0.0),
&Vector3::new(0.0, 0.0, 1.0)
&scene.eye.position,
&scene.eye.target,
&scene.eye.up
).to_homogeneous().as_ref();
let perspective : [[f32; 4]; 4] = *Perspective3::new(
target.get_dimensions().0 as f32 / target.get_dimensions().1 as f32,
0.3 * std::f32::consts::PI,
scene.eye.field_of_view,
0.1,
1000.0
).to_matrix().as_ref();
Expand All @@ -169,15 +125,19 @@ pub fn main_loop (prepare_frame: Sender<()>, data_provider: Receiver<String>) {
write: true,
.. Default::default()
},
backface_culling: glium::draw_parameters::BackfaceCullingMode::CullingDisabled,
.. Default::default()
};

// draw a frame
target.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
target.draw(&vertex_buffer, &index_buffer, &program, &uniforms, &params).unwrap();

let text = glium_text::TextDisplay::new(&text_system, &font, new_data.as_str());
for thing in scene.things.values() {
let vertices = glium::VertexBuffer::new(&window, thing.vertices.as_slice()).unwrap();
let indices = glium::IndexBuffer::new(&window, index::PrimitiveType::TrianglesList, thing.indices.as_slice()).unwrap();
target.draw(&vertices, &indices, &program, &uniforms, &params).unwrap();
}

let text = glium_text::TextDisplay::new(&text_system, &font, scene.debug_text.as_str());
let text_matrix = [
[0.05, 0.0, 0.0, 0.0],
[0.0, 0.05, 0.0, 0.0],
Expand Down
6 changes: 6 additions & 0 deletions monet/src/shader/solid_140.glslf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#version 140
out vec4 f_color;
in vec3 p;
void main() {
f_color = vec4(1.0, p.x, p.y, 1.0);
}
11 changes: 11 additions & 0 deletions monet/src/shader/solid_140.glslv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 140
uniform mat4 model;
uniform mat4 view;
uniform mat4 perspective;
in vec3 position;
out vec3 p;
void main() {
mat4 modelview = view * model;
gl_Position = perspective * modelview * vec4(position, 1.0);
p = position;
}
8 changes: 0 additions & 8 deletions monet/src/shader/triangle_150.glslf

This file was deleted.

10 changes: 0 additions & 10 deletions monet/src/shader/triangle_150.glslv

This file was deleted.

0 comments on commit f2ebe5f

Please sign in to comment.