Skip to content

Commit 11b67ba

Browse files
committed
Docs and move of materials
1 parent 89d9c2b commit 11b67ba

File tree

5 files changed

+62
-49
lines changed

5 files changed

+62
-49
lines changed

citro3d/examples/fragment-light.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::f32::consts::PI;
33

44
use citro3d::{
55
attrib, buffer,
6-
light::{DistanceAttenuation, LightEnv, Lut, LutId, LutInput, Spotlight},
7-
material::{Color, Material},
6+
color::Color,
7+
light::{DistanceAttenuation, LightEnv, Lut, LutId, LutInput, Material, Spotlight},
88
math::{AspectRatio, ClipPlanes, FVec3, Matrix4, Projection, StereoDisplacement},
99
render::{self, ClearFlags},
1010
shader, texenv,
@@ -318,7 +318,7 @@ fn main() {
318318
// Create a new light instance.
319319
let light = light_env.as_mut().create_light().unwrap();
320320
let mut light = light_env.as_mut().light_mut(light).unwrap();
321-
light.as_mut().set_color(1.0, 1.0, 1.0); // White color
321+
light.as_mut().set_color(Color::new(1.0, 1.0, 1.0)); // White color
322322
light.as_mut().set_position(FVec3::new(0.0, 0.0, -1.0)); // Approximately emitting from the camera
323323
// Set how the light attenuates over distance.
324324
// This particular LUT is optimized to work between 0 and 10 units of distance from the light point.
@@ -331,7 +331,7 @@ fn main() {
331331
// Subtle spotlight pointed at the top of the cube.
332332
let light = light_env.as_mut().create_light().unwrap();
333333
let mut light = light_env.as_mut().light_mut(light).unwrap();
334-
light.as_mut().set_color(0.5, 0.5, 0.5);
334+
light.as_mut().set_color(Color::new(0.5, 0.5, 0.5));
335335
light
336336
.as_mut()
337337
.set_spotlight(Some(Spotlight::with_cutoff(PI / 8.0))); // Spotlight angle of PI/6

citro3d/src/color.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! Color manipulation module.
2+
3+
/// RGB color in linear space ([0, 1]).
4+
#[derive(Debug, Default, Clone, Copy)]
5+
pub struct Color {
6+
pub r: f32,
7+
pub g: f32,
8+
pub b: f32,
9+
}
10+
11+
impl Color {
12+
pub fn new(r: f32, g: f32, b: f32) -> Self {
13+
Self { r, g, b }
14+
}
15+
16+
/// Splits the color into RGB ordered parts.
17+
pub fn to_parts_rgb(self) -> [f32; 3] {
18+
[self.r, self.g, self.b]
19+
}
20+
21+
/// Splits the color into BGR ordered parts.
22+
pub fn to_parts_bgr(self) -> [f32; 3] {
23+
[self.b, self.g, self.r]
24+
}
25+
}

citro3d/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
pub mod attrib;
2121
pub mod buffer;
22+
pub mod color;
2223
pub mod error;
2324
pub mod light;
24-
pub mod material;
2525
pub mod math;
2626
pub mod render;
2727
pub mod shader;

citro3d/src/light.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::{marker::PhantomPinned, mem::MaybeUninit, ops::Range, pin::Pin};
4343
use pin_array::PinArray;
4444

4545
use crate::{
46-
material::Material,
46+
color::Color,
4747
math::{FVec3, FVec4},
4848
};
4949

@@ -99,6 +99,28 @@ pub struct Light {
9999
_pin: PhantomPinned,
100100
}
101101

102+
/// Lighting and surface material used by the fragment stage.
103+
#[derive(Debug, Default, Clone, Copy)]
104+
pub struct Material {
105+
pub ambient: Option<Color>,
106+
pub diffuse: Option<Color>,
107+
pub specular0: Option<Color>,
108+
pub specular1: Option<Color>,
109+
pub emission: Option<Color>,
110+
}
111+
112+
impl Material {
113+
pub fn to_raw(self) -> citro3d_sys::C3D_Material {
114+
citro3d_sys::C3D_Material {
115+
ambient: self.ambient.unwrap_or_default().to_parts_bgr(),
116+
diffuse: self.diffuse.unwrap_or_default().to_parts_bgr(),
117+
specular0: self.specular0.unwrap_or_default().to_parts_bgr(),
118+
specular1: self.specular1.unwrap_or_default().to_parts_bgr(),
119+
emission: self.emission.unwrap_or_default().to_parts_bgr(),
120+
}
121+
}
122+
}
123+
102124
impl LightEnv {
103125
/// Constructs a new lighting environment.
104126
///
@@ -322,8 +344,15 @@ impl Light {
322344

323345
/// Sets the color of the light source.
324346
#[doc(alias = "C3D_LightColor")]
325-
pub fn set_color(self: Pin<&mut Self>, r: f32, g: f32, b: f32) {
326-
unsafe { citro3d_sys::C3D_LightColor(self.get_unchecked_mut().as_raw_mut(), r, g, b) }
347+
pub fn set_color(self: Pin<&mut Self>, color: Color) {
348+
unsafe {
349+
citro3d_sys::C3D_LightColor(
350+
self.get_unchecked_mut().as_raw_mut(),
351+
color.r,
352+
color.g,
353+
color.b,
354+
)
355+
}
327356
}
328357

329358
/// Enables/disables the light source.

citro3d/src/material.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)