Added color render

This commit is contained in:
Thomas Forgione 2018-10-30 17:58:07 +01:00
parent 4a4ae65cc1
commit 8bea93e865
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
7 changed files with 96 additions and 56 deletions

15
assets/shaders/color.frag Normal file
View File

@ -0,0 +1,15 @@
#version 140
uniform sampler2D tex;
uniform vec3 diffuse;
in vec3 v_color;
out vec4 color;
void main() {
color = vec4(v_color, 1.0);
}

17
assets/shaders/color.vert Normal file
View File

@ -0,0 +1,17 @@
#version 140
uniform mat4 perspective;
uniform mat4 view;
uniform vec3 texture_size;
in vec3 vertex;
in vec2 tex_coords;
in vec3 normal;
in vec3 face_color;
out vec3 v_color;
void main() {
v_color = face_color;
gl_Position = perspective * view * vec4(vertex, 1.0);
}

View File

@ -1,49 +0,0 @@
//! This module contains the FaceVertex struct.
use std::fmt;
#[derive(Copy, Clone, PartialEq)]
/// The indices needed for each vertex of a face.
pub struct FaceVertex {
/// The index of the vertex.
pub vertex: usize,
/// The index of the texture coordinate, None if there is no texture coordinate.
pub texture_coordinate: Option<usize>,
/// The index of the normal, None if there is no normal.
pub normal: Option<usize>,
}
impl FaceVertex {
/// Creates a new face vertex from its attributes.
pub fn new(vertex: usize, texture_coordinate: Option<usize>, normal: Option<usize>) -> FaceVertex {
FaceVertex {
vertex: vertex,
texture_coordinate: texture_coordinate,
normal: normal,
}
}
}
impl fmt::Debug for FaceVertex {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let texture_coordinate = if let Some(tc) = self.texture_coordinate {
tc.to_string()
} else {
"".to_owned()
};
let normal = if let Some(n) = self.normal {
n.to_string()
} else {
"".to_owned()
};
write!(formatter, "{}/{}/{}", self.vertex, texture_coordinate, normal)
}
}

View File

@ -26,9 +26,10 @@ pub struct Vertex {
vertex: [f64; 3],
tex_coords: [f64; 2],
normal: [f64; 3],
face_color: [f64; 3],
}
implement_vertex!(Vertex, vertex, tex_coords, normal);
implement_vertex!(Vertex, vertex, tex_coords, normal, face_color);
/// A part of a 3D model.
///
@ -523,10 +524,26 @@ impl Model {
[0.0, 0.0, 0.0]
};
let [r, g, b] = if let Some(id) = face.id {
let b = (id % 256) as u8;
let g = ((id - b as usize) / 256 % 256) as u8;
let r = ((id - g as usize * 256 - b as usize) / (256 * 256)) as u8;
[r, g, b]
} else {
[255, 255, 255]
};
let [r, g, b] = [
r as f64 / 255.0,
g as f64 / 255.0,
b as f64 / 255.0,
];
vertex_buffer.push(Vertex {
vertex: vertex,
tex_coords: tex_coord,
normal: normal,
face_color: [r, g, b],
});
}
}
@ -567,4 +584,9 @@ impl Model {
pub fn textures_mut(&mut self) -> &mut HashMap<String, Rc<SrgbTexture2d>> {
&mut self.textures
}
/// Clears the OpenGL textures.
pub fn clear_textures(&mut self) {
self.textures = HashMap::new();
}
}

View File

@ -56,17 +56,52 @@ pub struct Renderer {
}
impl Renderer {
/// Creates the program with the default shader.
pub fn default_shader(display: &Display) -> Program {
Program::from_source(
display,
include_str!("../assets/shaders/default.vert"),
include_str!("../assets/shaders/default.frag"),
None
).unwrap()
}
/// Creates the shader with one color per face.
pub fn color_shader(display: &Display) -> Program {
Program::from_source(
display,
include_str!("../assets/shaders/color.vert"),
include_str!("../assets/shaders/color.frag"),
None
).unwrap()
}
/// Creates a new renderer from a display.
///
/// Is uses the default shaders and creates an empty vec of models.
pub fn new(display: Display) -> Renderer {
let program = Program::from_source(
&display,
include_str!("../assets/shaders/shader.vert"),
include_str!("../assets/shaders/shader.frag"),
None
).unwrap();
let program = Renderer::default_shader(&display);
Renderer::from_display_and_program(display, program)
}
/// Creates a new colored renderer from a display.
///
/// Is uses the face colors shaders and creates an empty vec of models.
pub fn color(display: Display) -> Renderer {
let program = Renderer::color_shader(&display);
Renderer::from_display_and_program(display, program)
}
/// Creates a new renderer from a program.
///
/// It allows you to use a custom shader.
pub fn from_display_and_program(display: Display, program: Program) -> Renderer {
let image = RawImage2d::from_raw_rgba(vec![1.0, 1.0, 1.0, 1.0], (1, 1));
let texture = SrgbTexture2d::new(&display, image).ok().unwrap();