diff --git a/src/info/mod.rs b/src/info/mod.rs index 0b48882..fb7f805 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -64,6 +64,14 @@ fn model_info(db: &Database, model_id: usize) { } } } + println!(" Culling Mode: {}", + match (material.cull_backface, material.cull_frontface) { + (true, true) => "Cull All", + (true, false) => "Cull Backfacing", + (false, true) => "Cull Frontfacing", + (false, false) => "No Culling", + } + ); } println!(); } diff --git a/src/nitro/model.rs b/src/nitro/model.rs index 36596df..9e88ded 100644 --- a/src/nitro/model.rs +++ b/src/nitro/model.rs @@ -25,6 +25,8 @@ pub struct Material { pub params: TextureParameters, pub width: u16, pub height: u16, + pub cull_backface: bool, + pub cull_frontface: bool, pub texture_mat: Matrix4, } @@ -176,6 +178,9 @@ fn read_material(cur: Cur, name: Name) -> Result { let params = TextureParameters::from_u32(params); + let cull_backface = polygon_attr.bits(6,7) == 0; + let cull_frontface = polygon_attr.bits(7,8) == 0; + // For now, use the section size to determine whether there // is texture matrix data. let texture_mat = match section_size { @@ -196,6 +201,8 @@ fn read_material(cur: Cur, name: Name) -> Result { params, width, height, + cull_backface, + cull_frontface, texture_mat, }) } diff --git a/src/viewer/draw.rs b/src/viewer/draw.rs index 79c7f77..0f86f3c 100644 --- a/src/viewer/draw.rs +++ b/src/viewer/draw.rs @@ -1,9 +1,7 @@ use cgmath::Matrix4; use errors::Result; use primitives::{Primitives, Vertex}; -use glium::{self, VertexBuffer, IndexBuffer, - Display, Frame, DrawParameters, Surface, -}; +use glium::{self, VertexBuffer, IndexBuffer, Display, Frame, Surface}; use glium::texture::Texture2d; use viewer::gl_context::GlContext; use viewer::state::ViewState; @@ -85,7 +83,6 @@ impl DrawingData { db: &Database, ctx: &GlContext, target: &mut Frame, - draw_params: &DrawParameters ) { if let Ok(ref gl_prims) = self.gl_prims { let model = &db.models[self.view_state.model_id]; @@ -93,6 +90,8 @@ impl DrawingData { let mvp = self.view_state.eye.model_view_persp(); for call in &gl_prims.primitives.draw_calls { + let material = &model.materials[call.mat_id as usize]; + let texture = match self.textures[call.mat_id as usize] { Ok(Some(ref tex)) => tex, @@ -116,7 +115,7 @@ impl DrawingData { (true, true) => SamplerWrapFunction::Mirror, } }; - let params = &model.materials[call.mat_id as usize].params; + let params = &material.params; s.1.wrap_function.0 = wrap_fn(params.repeat_s, params.mirror_s); s.1.wrap_function.1 = wrap_fn(params.repeat_t, params.mirror_t); @@ -136,12 +135,30 @@ impl DrawingData { let indices = &gl_prims.index_buffer .slice(call.index_range.clone()).unwrap(); + let draw_params = glium::DrawParameters { + depth: glium::Depth { + test: glium::draw_parameters::DepthTest::IfLess, + write: true, + .. Default::default() + }, + backface_culling: { + use glium::draw_parameters::BackfaceCullingMode as Mode; + match (material.cull_backface, material.cull_frontface) { + (false, false) => Mode::CullingDisabled, + (true, false) => Mode::CullClockwise, + (false, true) => Mode::CullCounterClockwise, + (true, true) => continue, + } + }, + .. Default::default() + }; + target.draw( &gl_prims.vertex_buffer, indices, &ctx.program, &uniforms, - draw_params, + &draw_params, ).unwrap(); } } diff --git a/src/viewer/ui.rs b/src/viewer/ui.rs index db81f7e..4480eef 100644 --- a/src/viewer/ui.rs +++ b/src/viewer/ui.rs @@ -286,23 +286,7 @@ impl Ui { } else { let middle_grey = (0.4666, 0.4666, 0.4666, 1.0); target.clear_color_srgb_and_depth(middle_grey, 1.0); - - let draw_params = glium::DrawParameters { - depth: glium::Depth { - test: glium::draw_parameters::DepthTest::IfLess, - write: true, - .. Default::default() - }, - backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise, - .. Default::default() - }; - - self.drawing_data.draw( - &self.db, - &self.ctx, - &mut target, - &draw_params, - ); + self.drawing_data.draw(&self.db, &self.ctx, &mut target); } target.finish().unwrap();