Skip to content
This repository has been archived by the owner on Jun 19, 2021. It is now read-only.

Commit

Permalink
Add more structure for rendering
Browse files Browse the repository at this point in the history
Perspective rendering with hardcoded values works already
  • Loading branch information
LukasKalbertodt committed Jul 20, 2016
1 parent ab1d351 commit 3c78e3e
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 92 deletions.
19 changes: 19 additions & 0 deletions client/src/camera.rs
@@ -0,0 +1,19 @@
use base::math::*;

pub struct Camera {
// TODO
}

impl Camera {
/// Returns the projection matrix
pub fn proj_matrix(&self) -> Matrix4<f32> {
perspective(deg(60.0), 16.0 / 9.0, 0.1, 100.0)
}

/// Returns view matrix
pub fn view_matrix(&self) -> Matrix4<f32> {
Matrix4::look_at(Point3::new(0.0, 0.0, 60.0),
Point3::new(10.0, 10.0, 30.0),
Vector3::unit_z())
}
}
4 changes: 3 additions & 1 deletion client/src/game.rs
Expand Up @@ -6,6 +6,7 @@ use glium::{self, DisplayBuild, glutin};
use render::Renderer;
use super::Config;
use world::WorldView;
use Camera;

/// Main game function: contains the mai render loop and owns all important
/// components. This function should remain rather small, all heavy lifting
Expand All @@ -17,9 +18,10 @@ pub fn run(config: &Config, _: &WorldProvider) -> Result<(), ()> {
let event_manager = EventManager::new(context.clone());
let world = World::dummy();
let world_view = WorldView::from_world(&world, &context);
let camera = Camera {};

loop {
try!(renderer.render(&world_view));
try!(renderer.render(&world_view, &camera));

let event_resp = event_manager.poll_events();
if event_resp == EventResponse::Quit {
Expand Down
2 changes: 2 additions & 0 deletions client/src/lib.rs
Expand Up @@ -13,8 +13,10 @@ pub mod event_manager;
mod config;
mod game;
mod world;
mod camera;

pub use config::Config;
pub use camera::Camera;


pub fn start_game(config: Config, world_provider: &base::world::Provider) -> Result<(), ()> {
Expand Down
65 changes: 9 additions & 56 deletions client/src/render/mod.rs
@@ -1,7 +1,12 @@
use world::WorldView;
use glium::{self, Surface};
use glium::Surface;
use glium::backend::glutin_backend::GlutinFacade;
// use super::Config;
use Camera;

mod to_arr;

pub use self::to_arr::ToArr;

pub struct Renderer {
context: GlutinFacade,
Expand All @@ -12,67 +17,15 @@ impl Renderer {
Renderer { context: context }
}

pub fn render(&self, world_view: &WorldView) -> Result<(), ()> {
// #[derive(Copy, Clone)]
// struct Vertex {
// position: [f32; 2],
// }

// implement_vertex!(Vertex, position);

// let vertex1 = Vertex { position: [-0.5, -0.5] };
// let vertex2 = Vertex { position: [0.0, 0.5] };
// let vertex3 = Vertex { position: [0.5, -0.25] };
// let shape = vec![vertex1, vertex2, vertex3];

// let vertex_buffer = glium::VertexBuffer::new(&self.context, &shape).unwrap();
// let indices =
// glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);

// let vertex_shader_src = r#"
// #version 140
// in vec2 position;
// out vec2 my_attr; // our new attribute
// void main() {
// my_attr = position; // we need to set the value of each `out`
// variable.
// gl_Position = vec4(position, 0.0, 1.0);
// }
// "#;

// let fragment_shader_src = r#"
// #version 140
// in vec2 my_attr;
// out vec4 color;
// void main() {
// color = vec4(my_attr, 0.0, 1.0); // we build a vec4 from a vec2
// and two floats
// }
// "#;

// let program = glium::Program::from_source(&self.context,
// vertex_shader_src,
// fragment_shader_src,
// None)
// .unwrap();


/// Is called once every main loop iteration
pub fn render(&self, world_view: &WorldView, camera: &Camera) -> Result<(), ()> {
let mut target = self.context.draw();
target.clear_color(0.0, 0.0, 1.0, 1.0);

world_view.draw(&mut target);

// let uniforms = uniform!{};
world_view.draw(&mut target, camera);

// target.draw(&vertex_buffer,
// &indices,
// &program,
// &uniforms,
// &Default::default())
// .unwrap();
target.finish().unwrap();


Ok(())
}
}
19 changes: 19 additions & 0 deletions client/src/render/to_arr.rs
@@ -0,0 +1,19 @@
use base::math::*;

/// Helper trait to easily convert various `cgmath` types into array form to
/// use them in glium
pub trait ToArr {
type Output;

fn to_arr(&self) -> Self::Output;
}

impl<T: BaseNum> ToArr for Matrix4<T> {
type Output = [[T; 4]; 4];

fn to_arr(&self) -> Self::Output {
(*self).into()
}
}

// TODO: Add more impls
13 changes: 10 additions & 3 deletions client/src/world/chunk.rs
@@ -1,6 +1,8 @@
use base::world::{self, Chunk};
use base::math::*;
use glium;
use Camera;
use render::ToArr;

/// Graphical representation of the `base::Chunk`.
pub struct ChunkView {
Expand Down Expand Up @@ -28,7 +30,6 @@ impl ChunkView {
color: [0.0, 0.0, 1.0],
}];
let vbuf = glium::VertexBuffer::new(facade, &buffer).unwrap();
let ibuf = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
let prog = glium::Program::from_source(facade,
include_str!("chunk_std.vert"),
include_str!("chunk_std.frag"),
Expand All @@ -50,17 +51,23 @@ impl ChunkView {
}
}

pub fn draw<S>(&self, surface: &mut S)
pub fn draw<S>(&self, surface: &mut S, camera: &Camera)
where S: glium::Surface
{

let ibuf = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);

for pillar_pos in &self.pillars_positions {
let uniforms = uniform!{
offset: [pillar_pos.x, pillar_pos.y],
proj_matrix: camera.proj_matrix().to_arr(),
view_matrix: camera.view_matrix().to_arr(),
};

surface.draw(&self.vertices,
&ibuf,
&self.program,
&uniform!{},
&uniforms,
&Default::default())
.unwrap();

Expand Down
2 changes: 2 additions & 0 deletions client/src/world/chunk_std.frag
Expand Up @@ -3,6 +3,8 @@
in vec3 x_color;
out vec4 color;

uniform vec2 offset;

void main() {
color = vec4(x_color, 1.0);
color = vec4(1.0, 1.0, 1.0, 1.0);
Expand Down
6 changes: 5 additions & 1 deletion client/src/world/chunk_std.vert
Expand Up @@ -4,7 +4,11 @@ in vec3 position;
in vec3 color;
out vec3 x_color;

uniform vec2 offset;
uniform mat4 proj_matrix;
uniform mat4 view_matrix;

void main() {
gl_Position = vec4(position, 1);
gl_Position = proj_matrix * view_matrix * vec4(position.xy + offset, position.z, 1);
x_color = color;
}
5 changes: 3 additions & 2 deletions client/src/world/mod.rs
@@ -1,6 +1,7 @@
use base::world::{ChunkIndex, World};
use base::math::*;
use glium;
use Camera;

mod chunk;

Expand All @@ -23,9 +24,9 @@ impl WorldView {
}


pub fn draw<S>(&self, surface: &mut S)
pub fn draw<S>(&self, surface: &mut S, camera: &Camera)
where S: glium::Surface
{
self.chunk.draw(surface);
self.chunk.draw(surface, camera);
}
}
67 changes: 38 additions & 29 deletions plantex.sublime-project
@@ -1,31 +1,40 @@
{
"folders":
[
{
"file_exclude_patterns":
[
"*.lock",
"LICENSE-*",
"*.rs.bk",
"*.sublime-*"
],
"folder_exclude_patterns":
[
"target",
"ci"
],
"path": "."
}
],
"settings":
{
"rulers":
[
80,
100
],
"tab_size": 4,
"translate_tabs_to_spaces": true,
"use_tab_stops": true
}
"build_systems":
[
{
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"name": "Anaconda Python Builder",
"selector": "source.python",
"shell_cmd": "\"python\" -u \"$file\""
}
],
"folders":
[
{
"file_exclude_patterns":
[
"*.lock",
"LICENSE-*",
"*.rs.bk",
"*.sublime-*"
],
"folder_exclude_patterns":
[
"target",
"ci"
],
"path": "."
}
],
"settings":
{
"rulers":
[
80,
100
],
"tab_size": 4,
"translate_tabs_to_spaces": true,
"use_tab_stops": true
}
}

0 comments on commit 3c78e3e

Please sign in to comment.