Cleaning, fixed textures bug
This commit is contained in:
parent
b25dddaa9c
commit
b9bdac0dae
|
@ -1,11 +1,22 @@
|
|||
#version 140
|
||||
|
||||
in vec2 v_tex_coords;
|
||||
out vec4 color;
|
||||
|
||||
uniform sampler2D tex;
|
||||
|
||||
in vec3 v_normal;
|
||||
in vec2 v_tex_coords;
|
||||
|
||||
out vec4 color;
|
||||
|
||||
vec3 ambientLight = vec3(0.3,0.3,0.3);
|
||||
vec3 directionnalLight = normalize(vec3(10,5,7));
|
||||
vec3 directionnalLightFactor = vec3(0.6,0.6,0.6);
|
||||
|
||||
void main() {
|
||||
color = texture(tex, v_tex_coords);
|
||||
vec3 lambertComponent = dot(directionnalLight, v_normal) * directionnalLightFactor;
|
||||
lambertComponent = max(vec3(0.0, 0.0, 0.0), lambertComponent);
|
||||
|
||||
vec4 factor = vec4(ambientLight + lambertComponent, 1.0);
|
||||
|
||||
color = factor * texture(tex, v_tex_coords);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
#version 140
|
||||
|
||||
in vec3 vertex;
|
||||
in vec2 tex_coords;
|
||||
|
||||
uniform mat4 perspective;
|
||||
uniform mat4 view;
|
||||
|
||||
in vec3 vertex;
|
||||
in vec2 tex_coords;
|
||||
in vec3 normal;
|
||||
|
||||
out vec2 v_tex_coords;
|
||||
out vec3 v_normal;
|
||||
|
||||
void main() {
|
||||
v_normal = transpose(inverse(mat3(view))) * normal;
|
||||
v_tex_coords = tex_coords;
|
||||
|
||||
gl_Position = perspective * view * vec4(vertex, 1.0);
|
||||
}
|
||||
|
|
|
@ -12,9 +12,9 @@ use glium::glutin::Event;
|
|||
use glium::glutin::WindowEvent;
|
||||
use glium::glutin::VirtualKeyCode;
|
||||
|
||||
use model_converter::math::vector::Vector3;
|
||||
use model_converter::parser::{parse, parse_into_model};
|
||||
use model_converter::renderer::{Camera, Renderer};
|
||||
use model_converter::renderer::Renderer;
|
||||
use model_converter::renderer::camera::RotatingCamera;
|
||||
|
||||
fn main() {
|
||||
|
||||
|
@ -31,14 +31,12 @@ fn main() {
|
|||
let mut renderer = Renderer::new(display);
|
||||
renderer.add_model(&model);
|
||||
|
||||
let camera = Camera::new(
|
||||
Vector3::new(50.0, 0.0, 25.0),
|
||||
Vector3::new(0.0, 0.0, 0.0),
|
||||
Vector3::new(0.0, 1.0, 0.0),
|
||||
);
|
||||
let mut camera = RotatingCamera::new(50.0);
|
||||
|
||||
while !closed {
|
||||
|
||||
camera.increase_theta(0.025);
|
||||
|
||||
let mut target = renderer.draw();
|
||||
renderer.render(&camera, &mut target);
|
||||
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
use math::vector::Vector3;
|
||||
|
||||
pub trait Camera {
|
||||
fn get_view_matrix(&self) -> [[f32; 4]; 4];
|
||||
fn get_perspective_matrix(&self, dimensions: (u32, u32)) -> [[f32; 4]; 4] {
|
||||
let (width, height) = dimensions;
|
||||
let aspect_ratio = height as f32 / width as f32;
|
||||
|
||||
let fov = 3.141592 / 3.0;
|
||||
let zfar = 1024.0;
|
||||
let znear = 0.1;
|
||||
|
||||
use num::Float;
|
||||
let f = 1.0 / (fov / 2.0).tan();
|
||||
|
||||
[
|
||||
[f * aspect_ratio , 0.0, 0.0 , 0.0],
|
||||
[ 0.0 , f , 0.0 , 0.0],
|
||||
[ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0],
|
||||
[ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0],
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn look_at_matrix(position: [f32; 3], target: [f32; 3], up: [f32; 3]) -> [[f32; 4]; 4] {
|
||||
let f = {
|
||||
let f = [
|
||||
target[0] - position[0],
|
||||
target[1] - position[1],
|
||||
target[2] - position[2],
|
||||
];
|
||||
let len = f[0] * f[0] + f[1] * f[1] + f[2] * f[2];
|
||||
let len = len.sqrt();
|
||||
[f[0] / len, f[1] / len, f[2] / len]
|
||||
};
|
||||
|
||||
let s = [up[1] * f[2] - up[2] * f[1],
|
||||
up[2] * f[0] - up[0] * f[2],
|
||||
up[0] * f[1] - up[1] * f[0]];
|
||||
|
||||
let s_norm = {
|
||||
let len = s[0] * s[0] + s[1] * s[1] + s[2] * s[2];
|
||||
let len = len.sqrt();
|
||||
[s[0] / len, s[1] / len, s[2] / len]
|
||||
};
|
||||
|
||||
let u = [f[1] * s_norm[2] - f[2] * s_norm[1],
|
||||
f[2] * s_norm[0] - f[0] * s_norm[2],
|
||||
f[0] * s_norm[1] - f[1] * s_norm[0]];
|
||||
|
||||
let p = [-position[0] * s_norm[0] - position[1] * s_norm[1] - position[2] * s_norm[2],
|
||||
-position[0] * u[0] - position[1] * u[1] - position[2] * u[2],
|
||||
-position[0] * f[0] - position[1] * f[1] - position[2] * f[2]];
|
||||
|
||||
[
|
||||
[-s_norm[0], u[0], f[0], 0.0],
|
||||
[-s_norm[1], u[1], f[1], 0.0],
|
||||
[-s_norm[2], u[2], f[2], 0.0],
|
||||
[-p[0], p[1], p[2], 1.0],
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
pub struct FixedCamera {
|
||||
position: Vector3<f32>,
|
||||
target: Vector3<f32>,
|
||||
up: Vector3<f32>,
|
||||
}
|
||||
|
||||
impl FixedCamera {
|
||||
pub fn new(position: Vector3<f32>, target: Vector3<f32>, up: Vector3<f32>) -> FixedCamera {
|
||||
FixedCamera {
|
||||
position: position,
|
||||
target: target,
|
||||
up: up,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Camera for FixedCamera {
|
||||
fn get_view_matrix(&self) -> [[f32; 4]; 4] {
|
||||
look_at_matrix(self.position.into(), self.target.into(), self.up.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RotatingCamera {
|
||||
distance: f32,
|
||||
theta: f32,
|
||||
}
|
||||
|
||||
impl RotatingCamera {
|
||||
pub fn new(distance: f32) -> RotatingCamera {
|
||||
RotatingCamera {
|
||||
distance: distance,
|
||||
theta: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn increase_theta(&mut self, dt: f32) {
|
||||
self.theta += dt;
|
||||
}
|
||||
}
|
||||
|
||||
impl Camera for RotatingCamera {
|
||||
fn get_view_matrix(&self) -> [[f32; 4]; 4] {
|
||||
let position = Vector3::new(
|
||||
self.distance * self.theta.cos(),
|
||||
0.0,
|
||||
self.distance * self.theta.sin(),
|
||||
);
|
||||
|
||||
let target = Vector3::new(0.0, 0.0, 0.0);
|
||||
let up = Vector3::new(0.0, 1.0, 0.0);
|
||||
|
||||
look_at_matrix(position.into(), target.into(), up.into())
|
||||
}
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
pub mod camera;
|
||||
|
||||
use glium::draw_parameters::DepthTest;
|
||||
use glium::texture::{
|
||||
RawImage2d,
|
||||
Texture2d,
|
||||
SrgbTexture2d,
|
||||
};
|
||||
use glium::index::{
|
||||
NoIndices,
|
||||
|
@ -18,10 +20,10 @@ use glium::{
|
|||
use image;
|
||||
|
||||
use model::{Model, Vertex};
|
||||
use math::vector::Vector3;
|
||||
use renderer::camera::Camera;
|
||||
|
||||
pub struct RenderMaterial {
|
||||
texture: Option<Texture2d>,
|
||||
texture: Option<SrgbTexture2d>,
|
||||
}
|
||||
|
||||
impl RenderMaterial {
|
||||
|
@ -40,7 +42,7 @@ impl RenderMaterial {
|
|||
let dim = image.dimensions();
|
||||
let image = RawImage2d::from_raw_rgba_reversed(&image.into_raw(), dim);
|
||||
RenderMaterial {
|
||||
texture: Texture2d::new(display, image).ok()
|
||||
texture: SrgbTexture2d::new(display, image).ok()
|
||||
}
|
||||
} else {
|
||||
RenderMaterial {
|
||||
|
@ -50,56 +52,6 @@ impl RenderMaterial {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Camera {
|
||||
position: Vector3<f32>,
|
||||
target: Vector3<f32>,
|
||||
up: Vector3<f32>,
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
pub fn new(position: Vector3<f32>, target: Vector3<f32>, up: Vector3<f32>) -> Camera {
|
||||
Camera {
|
||||
position: position,
|
||||
target: target,
|
||||
up: up,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_view_matrix(&self) -> [[f32; 4]; 4] {
|
||||
let f = {
|
||||
let f = self.target - self.position;
|
||||
let len = f[0] * f[0] + f[1] * f[1] + f[2] * f[2];
|
||||
let len = len.sqrt();
|
||||
[f[0] / len, f[1] / len, f[2] / len]
|
||||
};
|
||||
|
||||
let s = [self.up[1] * f[2] - self.up[2] * f[1],
|
||||
self.up[2] * f[0] - self.up[0] * f[2],
|
||||
self.up[0] * f[1] - self.up[1] * f[0]];
|
||||
|
||||
let s_norm = {
|
||||
let len = s[0] * s[0] + s[1] * s[1] + s[2] * s[2];
|
||||
let len = len.sqrt();
|
||||
[s[0] / len, s[1] / len, s[2] / len]
|
||||
};
|
||||
|
||||
let u = [f[1] * s_norm[2] - f[2] * s_norm[1],
|
||||
f[2] * s_norm[0] - f[0] * s_norm[2],
|
||||
f[0] * s_norm[1] - f[1] * s_norm[0]];
|
||||
|
||||
let p = [-self.position[0] * s_norm[0] - self.position[1] * s_norm[1] - self.position[2] * s_norm[2],
|
||||
-self.position[0] * u[0] - self.position[1] * u[1] - self.position[2] * u[2],
|
||||
-self.position[0] * f[0] - self.position[1] * f[1] - self.position[2] * f[2]];
|
||||
|
||||
[
|
||||
[s_norm[0], u[0], f[0], 0.0],
|
||||
[s_norm[1], u[1], f[1], 0.0],
|
||||
[s_norm[2], u[2], f[2], 0.0],
|
||||
[p[0], p[1], p[2], 1.0],
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Renderer<'a> {
|
||||
display: Display,
|
||||
program: Program,
|
||||
|
@ -111,8 +63,8 @@ impl<'a> Renderer<'a> {
|
|||
|
||||
let program = Program::from_source(
|
||||
&display,
|
||||
include_str!("../assets/shaders/shader.vert"),
|
||||
include_str!("../assets/shaders/shader.frag"),
|
||||
include_str!("../../assets/shaders/shader.vert"),
|
||||
include_str!("../../assets/shaders/shader.frag"),
|
||||
None
|
||||
).unwrap();
|
||||
|
||||
|
@ -123,26 +75,6 @@ impl<'a> Renderer<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_perspective_matrix(&self, dimensions: (u32, u32)) -> [[f32; 4]; 4] {
|
||||
let (width, height) = dimensions;
|
||||
let aspect_ratio = height as f32 / width as f32;
|
||||
|
||||
let fov = 3.141592 / 3.0;
|
||||
let zfar = 1024.0;
|
||||
let znear = 0.1;
|
||||
|
||||
use num::Float;
|
||||
let f = 1.0 / (fov / 2.0).tan();
|
||||
|
||||
[
|
||||
[f * aspect_ratio , 0.0, 0.0 , 0.0],
|
||||
[ 0.0 , f , 0.0 , 0.0],
|
||||
[ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0],
|
||||
[ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0],
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
pub fn add_model(&mut self, model: &'a Model) {
|
||||
let mut buffers = vec![];
|
||||
|
||||
|
@ -173,9 +105,9 @@ impl<'a> Renderer<'a> {
|
|||
self.display.draw()
|
||||
}
|
||||
|
||||
pub fn render(&self, camera: &Camera, target: &mut Frame) {
|
||||
pub fn render<C: Camera>(&self, camera: &C, target: &mut Frame) {
|
||||
use glium::Surface;
|
||||
target.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0);
|
||||
target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
|
||||
|
||||
let params = DrawParameters {
|
||||
depth: Depth {
|
||||
|
@ -189,7 +121,7 @@ impl<'a> Renderer<'a> {
|
|||
for &(_, ref buffers) in &self.models {
|
||||
for &(ref material, ref buffer) in buffers {
|
||||
|
||||
let perspective = self.get_perspective_matrix(target.get_dimensions());
|
||||
let perspective = camera.get_perspective_matrix(target.get_dimensions());
|
||||
let view = camera.get_view_matrix();
|
||||
|
||||
if let &Some(ref texture) = &material.texture {
|
Loading…
Reference in New Issue