Use a default white texture, srgb clear color

This commit is contained in:
Thomas Forgione 2018-04-24 11:57:25 +02:00
parent 9349c45431
commit 066ec023f2
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
3 changed files with 32 additions and 27 deletions

View File

@ -91,7 +91,7 @@ pub struct Model {
pub materials: HashMap<String, Material>, pub materials: HashMap<String, Material>,
/// Map associating the name of a texture and the real texture. /// Map associating the name of a texture and the real texture.
pub textures: HashMap<String, Option<SrgbTexture2d>>, pub textures: HashMap<String, SrgbTexture2d>,
/// The list of parts of the model. /// The list of parts of the model.
pub parts: Vec<Part>, pub parts: Vec<Part>,
@ -157,6 +157,7 @@ impl Model {
if let Some(id) = face.id { if let Some(id) = face.id {
face.id = Some(id + face_offset); face.id = Some(id + face_offset);
} }
face.material_name = new_part.material_name.clone();
new_part.faces.push(face.clone()); new_part.faces.push(face.clone());
self.faces.push(face.clone()); self.faces.push(face.clone());
} }
@ -167,6 +168,7 @@ impl Model {
for (name, material) in other.materials { for (name, material) in other.materials {
let entry = self.materials.entry(name); let entry = self.materials.entry(name);
if let Entry::Occupied(_) = entry { if let Entry::Occupied(_) = entry {
eprintln!("Warning: materials merged but sharing the same name");
// Return error // Return error
} else { } else {
entry.or_insert(material); entry.or_insert(material);
@ -177,6 +179,7 @@ impl Model {
for (name, texture) in other.textures { for (name, texture) in other.textures {
let entry = self.textures.entry(name); let entry = self.textures.entry(name);
if let Entry::Occupied(_) = entry { if let Entry::Occupied(_) = entry {
eprintln!("Warning: textures merged but sharing the same name");
// return Error; // return Error;
} else { } else {
entry.or_insert(texture); entry.or_insert(texture);
@ -435,17 +438,17 @@ impl Model {
if let Some(path) = material.textures.get("map_Kd") { if let Some(path) = material.textures.get("map_Kd") {
let texture = renderer.make_texture(path); let texture = renderer.make_texture(path);
// Don't need to insert multiple times the same texture // Don't need to insert multiple times the same texture
self.textures.entry(path.to_owned()).or_insert(Some(texture)); self.textures.entry(path.to_owned()).or_insert(texture);
}; };
} }
/// Returns the rendering texture. /// Returns the rendering texture.
pub fn get_texture_by_name(&self, name: &str) -> Option<&SrgbTexture2d> { pub fn get_texture_by_name(&self, name: &str) -> Option<&SrgbTexture2d> {
self.textures[name].as_ref() self.textures.get(name)
} }
/// Returns a ref mut to the table of textures. /// Returns a ref mut to the table of textures.
pub fn textures_mut(&mut self) -> &mut HashMap<String, Option<SrgbTexture2d>> { pub fn textures_mut(&mut self) -> &mut HashMap<String, SrgbTexture2d> {
&mut self.textures &mut self.textures
} }
} }

View File

@ -67,7 +67,9 @@ fn main() {
for input in matches.values_of("input").unwrap() { for input in matches.values_of("input").unwrap() {
match parse(&input) { match parse(&input) {
Ok(model) => { Ok(model) => {
bbox = bbox.union(&model.bounding_box()); if model.vertices.len() > 0 {
bbox = bbox.union(&model.bounding_box());
}
models.push((input.to_owned(), model)) models.push((input.to_owned(), model))
}, },
Err(e) => { Err(e) => {

View File

@ -50,6 +50,9 @@ pub struct Renderer {
/// The background color. /// The background color.
clear_color: (f32, f32, f32, f32), clear_color: (f32, f32, f32, f32),
/// The texture that will be applied to non textured models.
default_texture: SrgbTexture2d,
} }
impl Renderer { impl Renderer {
@ -65,10 +68,14 @@ impl Renderer {
None None
).unwrap(); ).unwrap();
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();
Renderer { Renderer {
display: display, display: display,
program: program, program: program,
clear_color: (0.0, 0.0, 0.0, 1.0), clear_color: (0.0, 0.0, 0.0, 1.0),
default_texture: texture,
} }
} }
@ -107,7 +114,7 @@ impl Renderer {
pub fn render<C: RenderCamera>(&self, scene: &Scene, camera: &C) { pub fn render<C: RenderCamera>(&self, scene: &Scene, camera: &C) {
let mut target = self.draw(); let mut target = self.draw();
target.clear_color_and_depth(self.clear_color, 1.0); target.clear_color_srgb_and_depth(self.clear_color, 1.0);
let perspective = camera.get_perspective_matrix(); let perspective = camera.get_perspective_matrix();
let view = camera.get_view_matrix(); let view = camera.get_view_matrix();
@ -130,30 +137,23 @@ impl Renderer {
let texture = self.get_texture_of_part(&model, part); let texture = self.get_texture_of_part(&model, part);
if let Some(texture) = texture { let texture = if let Some(texture) = texture {
target.draw( texture
buffer, } else {
NoIndices(PrimitiveType::TrianglesList), &self.default_texture
&self.program, };
&uniform!(
tex: texture, target.draw(
perspective: perspective, buffer,
view: view, NoIndices(PrimitiveType::TrianglesList),
&self.program,
&uniform!(
tex: texture,
perspective: perspective,
view: view,
), ),
&params, &params,
).unwrap(); ).unwrap();
} else {
target.draw(
buffer,
NoIndices(PrimitiveType::TrianglesList),
&self.program,
&uniform!(
perspective: perspective,
view: view,
),
&params,
).unwrap();
}
} }
} }
} }