#![allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GbvChannel {
Albedo,
Normal,
Roughness,
Metallic,
Depth,
Emissive,
}
#[derive(Debug, Clone)]
pub struct GBufferViewConfig2 {
pub channel: GbvChannel,
pub exposure: f32,
pub enabled: bool,
}
impl Default for GBufferViewConfig2 {
fn default() -> Self {
GBufferViewConfig2 {
channel: GbvChannel::Albedo,
exposure: 1.0,
enabled: true,
}
}
}
pub fn new_gbuffer_view2() -> GBufferViewConfig2 {
GBufferViewConfig2::default()
}
pub fn gbv2_set_channel(cfg: &mut GBufferViewConfig2, channel: GbvChannel) {
cfg.channel = channel;
}
pub fn gbv2_set_exposure(cfg: &mut GBufferViewConfig2, exposure: f32) {
cfg.exposure = exposure.max(0.0);
}
pub fn gbv2_set_enabled(cfg: &mut GBufferViewConfig2, enabled: bool) {
cfg.enabled = enabled;
}
pub fn gbv2_channel_name(cfg: &GBufferViewConfig2) -> &'static str {
match cfg.channel {
GbvChannel::Albedo => "albedo",
GbvChannel::Normal => "normal",
GbvChannel::Roughness => "roughness",
GbvChannel::Metallic => "metallic",
GbvChannel::Depth => "depth",
GbvChannel::Emissive => "emissive",
}
}
pub fn gbv2_apply_exposure(value: f32, exposure: f32) -> f32 {
value * exposure
}
pub fn gbv2_to_json(cfg: &GBufferViewConfig2) -> String {
format!(
r#"{{"channel":"{}","exposure":{:.4},"enabled":{}}}"#,
gbv2_channel_name(cfg),
cfg.exposure,
cfg.enabled
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_channel_albedo() {
let c = new_gbuffer_view2();
assert_eq!(
c.channel,
GbvChannel::Albedo,
);
}
#[test]
fn test_set_channel_normal() {
let mut c = new_gbuffer_view2();
gbv2_set_channel(&mut c, GbvChannel::Normal);
assert_eq!(
c.channel,
GbvChannel::Normal,
);
}
#[test]
fn test_set_exposure() {
let mut c = new_gbuffer_view2();
gbv2_set_exposure(&mut c, 2.0);
assert!((c.exposure - 2.0).abs() < 1e-5, );
}
#[test]
fn test_set_exposure_negative_clamps() {
let mut c = new_gbuffer_view2();
gbv2_set_exposure(&mut c, -1.0);
assert!((c.exposure).abs() < 1e-6, );
}
#[test]
fn test_set_enabled_false() {
let mut c = new_gbuffer_view2();
gbv2_set_enabled(&mut c, false);
assert!(!c.enabled ,);
}
#[test]
fn test_channel_name_albedo() {
let c = new_gbuffer_view2();
assert_eq!(
gbv2_channel_name(&c),
"albedo",
);
}
#[test]
fn test_channel_name_depth() {
let mut c = new_gbuffer_view2();
gbv2_set_channel(&mut c, GbvChannel::Depth);
assert_eq!(gbv2_channel_name(&c), "depth" ,);
}
#[test]
fn test_apply_exposure() {
let v = gbv2_apply_exposure(0.5, 2.0);
assert!((v - 1.0).abs() < 1e-5 ,);
}
#[test]
fn test_to_json_contains_channel() {
let c = new_gbuffer_view2();
let j = gbv2_to_json(&c);
assert!(j.contains("channel") ,);
}
#[test]
fn test_default_enabled_true() {
let c = new_gbuffer_view2();
assert!(c.enabled ,);
}
}