From d76ccd41f73cfed9df9eb451fd9e8ac81aa0b40e Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Thu, 8 Apr 2021 15:32:23 +0000 Subject: [PATCH] engine: fix normals, rework lighting and scene Still not sure if this lighting is correct, for that we probably want a more complex scene (and also some indirect lighting, or IBL, to see more than just a handful of dynamic lights). --- engine/scene.lua | 3 ++- engine/shaders/forward.frag | 4 +--- engine/shaders/forward.vert | 3 +-- engine/src/main.rs | 43 ++++++++++++------------------------- engine/src/render/light.rs | 2 +- 5 files changed, 19 insertions(+), 36 deletions(-) diff --git a/engine/scene.lua b/engine/scene.lua index 4cfd604..9751b23 100644 --- a/engine/scene.lua +++ b/engine/scene.lua @@ -23,7 +23,8 @@ sent.register({ for x=-2,2 do for y=-2,2 do for z=-2,2 do - if z > -2 and z < 2 and x > -2 and x < 2 then + --if z > -2 and z < 2 and x > -2 and x < 2 and y > 0 then + if x <= 0 and y <= 0 and z <= 0 then else Cube.new(x, y, z) end diff --git a/engine/shaders/forward.frag b/engine/shaders/forward.frag index e2fc158..0ee1159 100644 --- a/engine/shaders/forward.frag +++ b/engine/shaders/forward.frag @@ -75,9 +75,7 @@ void main() { F0 = mix(F0, albedo, metallic); vec3 Lo = BRDFIlluminance(N, V, F0, albedo, dielectric, roughness); - // Add 500 nits ambient light. - vec3 ambient = vec3(500.00) * albedo; - vec3 color = XYZ_TO_SRGB * ((ambient + Lo) * CAMERA_EXPOSURE); + vec3 color = XYZ_TO_SRGB * (Lo * CAMERA_EXPOSURE); outColor = vec4(GammaCorrect(color.x), GammaCorrect(color.y), GammaCorrect(color.z), 1.0); } diff --git a/engine/shaders/forward.vert b/engine/shaders/forward.vert index bb38cc7..13bdb7e 100644 --- a/engine/shaders/forward.vert +++ b/engine/shaders/forward.vert @@ -39,10 +39,9 @@ out gl_PerVertex { void main() { vec4 world = model * vec4(pos, 1.0); - vec4 normal4 = model * vec4(normal, 1.0); fragTexCoord = tex; - fragNormal = normal4.xyz / normal4.w; + fragNormal = mat3(model) * normal; fragWorldPos = world.xyz / world.w; gl_Position = pco.view * world; diff --git a/engine/src/main.rs b/engine/src/main.rs index a9145f5..c6840e1 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -51,7 +51,7 @@ impl Time { struct Main { light1: ecs::EntityID, - light2: ecs::EntityID, + cube1: ecs::EntityID, cx: f32, cy: f32, @@ -112,17 +112,6 @@ impl Main { roughness: Texture::from_image(String::from("//assets/test-128px-roughness.png")), }.build(), Some("test-128px")); - for x in -20..20 { - for y in -20..20 { - for z in -20..-10 { - world.new_entity() - .with(Transform::at((x as f32)*4.0, (y as f32)*4.0, (z as f32)*4.0)) - .with(Renderable::Mesh(mesh, material)) - .build(); - } - } - } - let light = rm.add(Light::omni_test(), Some("omni")); // The Sun (Sol) is 1AU from the Earth. We ignore the diameter of the Sun and the Earth, as @@ -145,9 +134,9 @@ impl Main { .with(Transform::at(-10.0, -10.0, -5.0)) .with(Renderable::Light(light)) .build(); - let light2 = world.new_entity() + let cube1 = world.new_entity() .with(Transform::at(-10.0, -10.0, -5.0)) - .with(Renderable::Light(light)) + .with(Renderable::Mesh(mesh, material)) .build(); world.new_entity() .with(Transform::at(0.0, sun_angle.sin() * sun_distance, sun_angle.cos() * sun_distance)) @@ -157,7 +146,7 @@ impl Main { world.set_global(rm); Self { - light1, light2, + light1, cube1, cx: 0., cy: 0., } @@ -185,9 +174,9 @@ impl<'a> ecs::System <'a> for Main { self.cy += (dy); let camera = cgm::Point3::new( - self.cx.sin() * 10.0, - (self.cx.cos()*self.cy.cos()) * 10.0, - self.cy.sin() * 10.0, + self.cx.sin() * 20.0, + (self.cx.cos()*self.cy.cos()) * 20.0, + self.cy.sin() * 20.0, ); let view = cgm::Matrix4::look_at( @@ -200,18 +189,14 @@ impl<'a> ecs::System <'a> for Main { sd.scene_info.get().view = view; sd.scene_info.get().lock_cursor = true; - *sd.transforms.get_mut(self.light1).unwrap() = Transform::at( - -0.0 + (ts*3.0).sin() * 4.0, - -0.0 + (ts*4.0).cos() * 4.0, - -0.0 + (ts*2.0).sin() * 3.0, - ); - - *sd.transforms.get_mut(self.light2).unwrap() = Transform::at( - -0.0 + (ts*3.0).cos() * 4.0, - -0.0 + (ts*4.0).sin() * 4.0, - -0.0 + (ts*2.0).cos() * 3.0, - ); + let lx = 0.0; + let ly = 4.0; + let lz = -0.0 + (ts*2.0).sin() * 4.0; + *sd.transforms.get_mut(self.light1).unwrap() = Transform::at(lx, ly, lz); + let mut ctransform = Transform::at(lx, ly, lz); + ctransform.0 = ctransform.0 * cgmath::Matrix4::from_scale(0.1); + *sd.transforms.get_mut(self.cube1).unwrap() = ctransform; } } diff --git a/engine/src/render/light.rs b/engine/src/render/light.rs index 85c91d7..1e66a14 100644 --- a/engine/src/render/light.rs +++ b/engine/src/render/light.rs @@ -40,7 +40,7 @@ impl Omni { // M. Krystek. 1985. "An algorithm to calculate correlated color temperature" // Color Research & Application, 10 (1), 38–40. pub fn test() -> Self { - Self::with_color(color::XYZ::new(234.7*100.0, 214.1*100.0, 207.9*100.0)) + Self::with_color(color::XYZ::new(234.7*200.0, 214.1*200.0, 207.9*200.0)) } pub fn with_color(color: color::XYZ) -> Self{