Skip to content

Commit

Permalink
Fix color conversion for textures
Browse files Browse the repository at this point in the history
The textures were appearing washed out in all the renders, which turned
out to be because of the gamma correction we do. So we do the inverse
when converting texture values to Color. This is pretty rudimentary, but
works for now.

Also fixed the staircase scene to use the correct texture for the
stairs.
  • Loading branch information
banga committed Jan 31, 2024
1 parent 037b716 commit 327f38b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 32 deletions.
Binary file modified images/staircase.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions objs/staircase/staircase.mtl
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,18 @@ newmtl Wood_-_Chair
Ns 250.0
Ka 1.0 1.0 1.0
Kd 0.8 0.3 0.1
Ks 0.5 0.5 0.5
Ks 1.0 1.0 1.0
Ke 0.0 0.0 0.0
Ni 1.0
d 1.0
map_Kd textures/WoodChair.jpg
illum 2

newmtl Wood_-_Floor
Ns 250.0
Ns 1000.0
Ka 1.0 1.0 1.0
Kd 0.8 0.3 0.1
Ks 0.5 0.5 0.5
Ks 1.0 1.0 1.0
Ke 0.0 0.0 0.0
Ni 1.0
d 1.0
Expand All @@ -243,22 +243,22 @@ newmtl Wood_-_Lamp
Ns 250.0
Ka 1.0 1.0 1.0
Kd 0.6 0.4 0.1
Ks 0.5 0.5 0.5
Ks 1.0 1.0 1.0
Ke 0.0 0.0 0.0
Ni 1.0
d 1.0
map_Kd textures/WoodPanel.jpg
map_Kd textures/Wood.jpg
illum 2

newmtl Wood_-_Stairs
Ns 250.0
Ns 1000.0
Ka 1.0 1.0 1.0
Kd 0.8 0.3 0.1
Ks 0.5 0.5 0.5
Ks 1.0 1.0 1.0
Ke 0.0 0.0 0.0
Ni 1.0
d 1.0
map_Kd textures/Wood.jpg
map_Kd textures/WoodPanel.jpg
illum 2

newmtl copper.001
Expand Down
10 changes: 6 additions & 4 deletions scenes/museum.cry
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
num_samples: 16,
camera: Perspective {
origin: Point(2, 10, -2),
target: Point(0.5, 3, -3),
origin: Point(2.5, 8, -9),
target: Point(1, 0, -8),
up: Vector(0, 0, -1),
lens_radius: 0.05,
focal_distance: 10,
fov: 60,
film: {
width: 800,
height: 600,
width: 850,
height: 1000,
}
},
lights: [
Expand Down
29 changes: 29 additions & 0 deletions scenes/mustang.cry
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
num_samples: 16,
camera: Perspective {
origin: Point(1, 0.75, -2),
target: Point(-0.25, -0.25, 0),
up: Vector(0, 1, 0),
fov: 45,
film: {
width: 800,
height: 600,
}
},
lights: [
Infinite { intensity: Color(0.5, 0.55, 0.6) }
],
materials: {
fallback: Matte { reflectance: Color(1, 0.9, 0.8), sigma: 0 },
ground: Matte { reflectance: Color(0.9, 0.95, 1.0), sigma: 1000 },
},
shapes: {
light: Disk { origin: Point(0, 2, 0), radius: 3, rotate_x: 90 },
ground: Disk { origin: Point(0, 0, 0), radius: 2, rotate_x: 90 },
},
primitives: [
Mesh { file_name: 'objs/local/mustang/mustang.obj', fallback_material: 'fallback' },
Shape { shape: 'light', emittance: Color(2, 2, 2) },
Shape { shape: 'ground', material: 'ground' }
]
}
14 changes: 7 additions & 7 deletions scenes/staircase.cry
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
origin: Point(0, 2, -4.92),
target: Point(0, 2.5, 0),
up: Vector(0, 1, 0),
lens_radius: 0.01,
focal_distance: 7,
lens_radius: 0.001,
focal_distance: 3,
fov: 35,
film: {
width: 360,
height: 640,
width: 720,
height: 1280,
}
},
lights: [
Point { origin: Point(0, 2.25, -4.5), intensity: Color(0.2, 0.2, 0.2) }
Point { origin: Point(0, 2.25, -4.5), intensity: Color(0.3, 0.3, 0.3) }
],
materials: {
default: Matte { reflectance: Color(1, 1, 1), sigma: 0 }
},
shapes: {
light1: Disk { origin: Point(0, 5.5, 2.5), rotate_x: 60, rotate_y: 0, radius: 2 },
light1: Disk { origin: Point(1, 5.5, 2.5), rotate_x: 60, rotate_y: 0, radius: 2 },
},
primitives: [
Shape { shape: 'light1', emittance: Color(4, 3, 2) },
Shape { shape: 'light1', emittance: Color(5, 5, 5) },

// Model from https://blendswap.com/blend/14449. Exported from Blender
// and tweaked the material file to make the "Emission" material actually
Expand Down
4 changes: 1 addition & 3 deletions src/bin/craytracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ fn show_preview<C, F>(
g: pixels[3 * offset + 1] as f64,
b: pixels[3 * offset + 2] as f64,
}
// Gamma correction
.powf(1.0 / 2.2)
.to_rgb();
preview_buffer[offset] = (r as u32) << 16 | (g as u32) << 8 | b as u32;
}
Expand Down Expand Up @@ -277,7 +275,7 @@ where
|x, y| {
info!("Rendering pixel at ({x},{y})");
let color = render_pixel(&mut sampler, x, y, scene);
info!("Color = {} {:?}", color, color.powf(1.0 / 2.2).to_rgb());
info!("Color = {} {:?}", color, color.to_rgb());
},
on_finish,
);
Expand Down
29 changes: 19 additions & 10 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub struct Color {
pub b: f64,
}

const GAMMA: f64 = 2.2;

impl Color {
pub const BLACK: Color = Color {
r: 0.0,
Expand All @@ -22,25 +24,32 @@ impl Color {
b: 1.0,
};

pub fn from_rgb(r: u8, g: u8, b: u8) -> Color {
Color {
r: r as f64 / 255.0,
g: g as f64 / 255.0,
b: b as f64 / 255.0,
}
}
pub fn powf(self, pow: f64) -> Color {
Color {
r: self.r.powf(pow),
g: self.g.powf(pow),
b: self.b.powf(pow),
}
}

// Very naive conversion to/from rgb color space that just applies some
// gamma correction. This should be replaced with a proper spectrum
// representation that is then converted to rgb values depending on the film
// properties.
pub fn from_rgb(r: u8, g: u8, b: u8) -> Color {
Color {
r: r as f64 / 255.0,
g: g as f64 / 255.0,
b: b as f64 / 255.0,
}
.powf(GAMMA)
}
pub fn to_rgb(self) -> (u8, u8, u8) {
let Color { r, g, b } = self.powf(1.0 / GAMMA);
(
(self.r.clamp(0.0, 1.0) * 255.0) as u8,
(self.g.clamp(0.0, 1.0) * 255.0) as u8,
(self.b.clamp(0.0, 1.0) * 255.0) as u8,
(r.clamp(0.0, 1.0) * 255.0) as u8,
(g.clamp(0.0, 1.0) * 255.0) as u8,
(b.clamp(0.0, 1.0) * 255.0) as u8,
)
}
pub fn is_black(self) -> bool {
Expand Down

0 comments on commit 327f38b

Please sign in to comment.