Skip to content

Commit

Permalink
Display car, use nalgebra #1
Browse files Browse the repository at this point in the history
  • Loading branch information
aeplay committed Jun 12, 2016
1 parent 8af777a commit 063a901
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 21 deletions.
3 changes: 2 additions & 1 deletion monet/Cargo.toml
Expand Up @@ -5,4 +5,5 @@ authors = ["Anselm Eickhoff <anselm.eickhoff@gmail.com>"]

[dependencies]
glium = "*"
glium_text = "*"
glium_text = "*"
nalgebra = "*"
124 changes: 104 additions & 20 deletions monet/src/lib.rs
@@ -1,6 +1,9 @@
#[macro_use]
extern crate glium;
extern crate glium_text;
extern crate nalgebra;
use nalgebra::{Point3, Vector3, Isometry3, Perspective3, ToHomogeneous};

use glium::glutin;
use glium::index::PrimitiveType;
use glium::Surface;
Expand All @@ -17,40 +20,96 @@ pub fn main_loop (prepare_frame: Sender<()>, data_provider: Receiver<String>) {

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

implement_vertex!(Vertex, position, color);
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: [-0.5, -0.5], color: [0.0, 1.0, 0.0] },
Vertex { position: [ 0.0, 0.5], color: [0.0, 0.0, 1.0] },
Vertex { position: [ 0.5, -0.5], color: [1.0, 0.0, 0.0] },
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, &[0u16, 1, 2]).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 matrix;
in vec2 position;
in vec3 color;
out vec3 vColor;
uniform mat4 model;
uniform mat4 view;
uniform mat4 perspective;
in vec3 position;
out vec3 p;
void main() {
gl_Position = vec4(position, 0.0, 1.0) * matrix;
vColor = color;
mat4 modelview = view * model;
gl_Position = perspective * modelview * vec4(position, 1.0);
p = position;
}
",

fragment: "
#version 140
in vec3 vColor;
out vec4 f_color;
in vec3 p;
void main() {
f_color = vec4(vColor, 1.0);
f_color = vec4(1.0, p.x, p.y, 1.0);
}
"
},
Expand All @@ -77,21 +136,46 @@ pub fn main_loop (prepare_frame: Sender<()>, data_provider: Receiver<String>) {
let new_data = data_provider.recv().unwrap();
println!("rendering...");

let matrix = [
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)
).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,
0.1,
1000.0
).to_matrix().as_ref();

let model = [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0f32]
];

let uniforms = uniform! {
matrix: matrix
model: model,
view: view,
perspective: perspective
};

let params = glium::DrawParameters {
depth: glium::Depth {
test: glium::draw_parameters::DepthTest::IfLess,
write: true,
.. Default::default()
},
backface_culling: glium::draw_parameters::BackfaceCullingMode::CullingDisabled,
.. Default::default()
};

// draw a frame
let mut target = window.draw();
target.clear_color(0.0, 0.0, 0.0, 0.0);
target.draw(&vertex_buffer, &index_buffer, &program, &uniforms, &Default::default()).unwrap();
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());
let text_matrix = [
Expand Down

0 comments on commit 063a901

Please sign in to comment.