Skip to content

Commit

Permalink
Merge branch 'master' into 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
asny committed Nov 1, 2021
2 parents e108c91 + d0f1a0e commit df661e9
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 20 deletions.
11 changes: 11 additions & 0 deletions src/context/wgl2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ impl GLContext {
pub fn create_shader(&self, type_: ShaderType) -> Option<Shader> {
self.inner.create_shader(type_.to_const())
}
pub fn get_shader_info_log(&self, shader: &Shader) -> Option<String> {
if let Some(log) = self.inner.get_shader_info_log(shader) {
if log.len() > 0 {
Some(log)
} else {
None
}
} else {
None
}
}

pub fn compile_shader(&self, source: &str, shader: &Shader) {
let header = "#version 300 es\nprecision highp float;\nprecision highp int;\nprecision highp sampler2DArray;\n";
Expand Down
2 changes: 1 addition & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Context {
///
/// Creates a new context from a [OpenGL/WebGL context](GLContext).
///
pub fn new_from_gl_context(context: GLContext) -> Self {
pub fn from_gl_context(context: GLContext) -> Self {
Self {
context,
programs: Rc::new(RefCell::new(HashMap::new())),
Expand Down
4 changes: 2 additions & 2 deletions src/core/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Color {
///
/// Creates a new color from three float elements where each element are in the range `0.0..=1.0`.
///
pub fn new_from_rgb_slice(rgba: &[f32; 3]) -> Self {
pub fn from_rgb_slice(rgba: &[f32; 3]) -> Self {
Self {
r: (rgba[0] * 255.0) as u8,
g: (rgba[1] * 255.0) as u8,
Expand All @@ -44,7 +44,7 @@ impl Color {
///
/// Creates a new color from four float elements where each element are in the range `0.0..=1.0`.
///
pub fn new_from_rgba_slice(rgba: &[f32; 4]) -> Self {
pub fn from_rgba_slice(rgba: &[f32; 4]) -> Self {
Self {
r: (rgba[0] * 255.0) as u8,
g: (rgba[1] * 255.0) as u8,
Expand Down
2 changes: 1 addition & 1 deletion src/io/parser/gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn parse_tree<'a>(
};
cpu_materials.push(CPUMaterial {
name: material_name.clone(),
albedo: Color::new_from_rgba_slice(&color),
albedo: Color::from_rgba_slice(&color),
albedo_texture,
metallic: pbr.metallic_factor(),
roughness: pbr.roughness_factor(),
Expand Down
2 changes: 1 addition & 1 deletion src/io/parser/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Loaded {

cpu_materials.push(CPUMaterial {
name: material.name,
albedo: Color::new_from_rgba_slice(&[
albedo: Color::from_rgba_slice(&[
color.r as f32,
color.g as f32,
color.b as f32,
Expand Down
2 changes: 1 addition & 1 deletion src/io/parser/threed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Loaded {
name: material.name,
albedo: material
.color
.map(|color| Color::new_from_rgba_slice(&[color.0, color.1, color.2, color.3]))
.map(|color| Color::from_rgba_slice(&[color.0, color.1, color.2, color.3]))
.unwrap_or(Color::WHITE),
albedo_texture: if let Some(filename) = material.texture_path {
let texture_path = path
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/light/directional_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl DirectionalLight {

pub fn color(&self) -> Color {
let c = self.light_buffer.get(0).unwrap();
Color::new_from_rgb_slice(&[c[0], c[1], c[2]])
Color::from_rgb_slice(&[c[0], c[1], c[2]])
}

pub fn set_intensity(&mut self, intensity: f32) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/light/point_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl PointLight {

pub fn color(&self) -> Color {
let c = self.light_buffer.get(0).unwrap();
Color::new_from_rgb_slice(&[c[0], c[1], c[2]])
Color::from_rgb_slice(&[c[0], c[1], c[2]])
}

pub fn set_intensity(&mut self, intensity: f32) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/light/spot_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl SpotLight {

pub fn color(&self) -> Color {
let c = self.light_buffer.get(0).unwrap();
Color::new_from_rgb_slice(&[c[0], c[1], c[2]])
Color::from_rgb_slice(&[c[0], c[1], c[2]])
}

pub fn set_intensity(&mut self, intensity: f32) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/material/color_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ForwardMaterial for ColorMaterial {
_camera: &Camera,
_lights: &Lights,
) -> ThreeDResult<()> {
program.use_uniform_vec4("color", &self.color.to_vec4())?;
program.use_uniform_vec4("surfaceColor", &self.color.to_vec4())?;
if let Some(ref tex) = self.texture {
program.use_texture("tex", &**tex)?
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/material/shaders/color_material.frag
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uniform vec4 color;
uniform vec4 surfaceColor;

#ifdef USE_TEXTURE
uniform sampler2D tex;
Expand All @@ -8,7 +8,7 @@ layout (location = 0) out vec4 outColor;

void main()
{
outColor = color;
outColor = surfaceColor;

#ifdef USE_VERTEX_COLORS
outColor *= col;
Expand Down
34 changes: 29 additions & 5 deletions src/renderer/object/instanced_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ pub struct InstancedModel<M: ForwardMaterial> {
aabb_local: AxisAlignedBoundingBox,
aabb: AxisAlignedBoundingBox,
transformation: Mat4,
transformations: Vec<Mat4>,
/// The material applied to the instanced model
pub material: M,
}

impl InstancedModel<ColorMaterial> {
///
/// Creates a new instanced 3D model with a triangle mesh as geometry and a default [ColorMaterial].
/// The transformations are applied to each model instance before they are rendered.
/// The model is rendered in as many instances as there are transformation matrices.
///
pub fn new(
context: &Context,
Expand All @@ -40,15 +43,38 @@ impl<M: ForwardMaterial> InstancedModel<M> {
) -> ThreeDResult<Self> {
let mesh = InstancedMesh::new(context, transformations, cpu_mesh)?;
let aabb = cpu_mesh.compute_aabb();
Ok(Self {
let mut model = Self {
context: context.clone(),
mesh,
cull: Cull::default(),
aabb,
aabb_local: aabb.clone(),
transformation: Mat4::identity(),
transformations: transformations.to_vec(),
material,
})
};
model.update_aabb();
Ok(model)
}

///
/// Updates the transformations applied to each model instance before they are rendered.
/// The model is rendered in as many instances as there are transformation matrices.
///
pub fn update_transformations(&mut self, transformations: &[Mat4]) {
self.transformations = transformations.to_vec();
self.mesh.update_transformations(transformations);
self.update_aabb();
}

fn update_aabb(&mut self) {
let mut aabb = AxisAlignedBoundingBox::EMPTY;
for transform in self.transformations.iter() {
let mut aabb2 = self.aabb_local.clone();
aabb2.transform(&(transform * self.transformation));
aabb.expand_with_aabb(&aabb2);
}
self.aabb = aabb;
}

///
Expand Down Expand Up @@ -239,9 +265,7 @@ impl<M: ForwardMaterial> Geometry for InstancedModel<M> {
impl<M: ForwardMaterial> GeometryMut for InstancedModel<M> {
fn set_transformation(&mut self, transformation: Mat4) {
self.transformation = transformation;
let mut aabb = self.aabb_local.clone();
aabb.transform(&self.transformation);
self.aabb = aabb;
self.update_aabb();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/object/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct ParticleData {
}

///
/// Particle effect with fixed vertex shader and customizable fragment shader (see also [ParticlesProgram](ParticlesProgram)).
/// Particle effect that can be rendered with any material.
///
/// Each particle is initialised with a position and velocity using the [update](Particles::update) function and a global acceleration.
/// Then when time passes, their position is updated based on
Expand Down
2 changes: 1 addition & 1 deletion src/window/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Window {
context
.get_extension("OES_texture_float")
.map_err(|e| CanvasError::OESTextureFloatNotSupported(format!(": {:?}", e)))?;
Ok(crate::core::Context::new_from_gl_context(
Ok(crate::core::Context::from_gl_context(
crate::context::GLContext::new(context),
))
}
Expand Down
2 changes: 1 addition & 1 deletion src/window/glutin_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Window {
Ok(Window {
windowed_context,
event_loop,
gl: crate::core::Context::new_from_gl_context(context),
gl: crate::core::Context::from_gl_context(context),
})
}

Expand Down

0 comments on commit df661e9

Please sign in to comment.