Skip to main content

modelio/
physically_plausible_light.rs

1use std::ptr;
2
3use crate::error::Result;
4use crate::ffi;
5use crate::handle::ObjectHandle;
6use crate::light::Light;
7use crate::object::Object;
8use crate::types::PhysicallyPlausibleLightInfo;
9use crate::util::{parse_json, required_handle};
10
11#[derive(Debug, Clone)]
12pub struct PhysicallyPlausibleLight {
13    handle: ObjectHandle,
14}
15
16impl PhysicallyPlausibleLight {
17    pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
18        Self { handle }
19    }
20
21    pub fn new() -> Result<Self> {
22        let mut out_light = ptr::null_mut();
23        let mut out_error = ptr::null_mut();
24        let status =
25            unsafe { ffi::mdl_physically_plausible_light_new(&mut out_light, &mut out_error) };
26        crate::util::status_result(status, out_error)?;
27        Ok(Self::from_handle(required_handle(
28            out_light,
29            "MDLPhysicallyPlausibleLight",
30        )?))
31    }
32
33    pub fn info(&self) -> Result<PhysicallyPlausibleLightInfo> {
34        parse_json(
35            unsafe { ffi::mdl_physically_plausible_light_info_json(self.handle.as_ptr()) },
36            "MDLPhysicallyPlausibleLight",
37        )
38    }
39
40    pub fn set_color_temperature(&self, temperature: f32) {
41        unsafe {
42            ffi::mdl_physically_plausible_light_set_color_temperature(
43                self.handle.as_ptr(),
44                temperature,
45            );
46        };
47    }
48
49    pub fn set_color(&self, color: [f32; 4]) {
50        unsafe {
51            ffi::mdl_physically_plausible_light_set_color(
52                self.handle.as_ptr(),
53                color[0],
54                color[1],
55                color[2],
56                color[3],
57            );
58        };
59    }
60
61    pub fn set_lumens(&self, lumens: f32) {
62        unsafe { ffi::mdl_physically_plausible_light_set_lumens(self.handle.as_ptr(), lumens) };
63    }
64
65    pub fn set_inner_cone_angle(&self, angle: f32) {
66        unsafe {
67            ffi::mdl_physically_plausible_light_set_inner_cone_angle(self.handle.as_ptr(), angle);
68        };
69    }
70
71    pub fn set_outer_cone_angle(&self, angle: f32) {
72        unsafe {
73            ffi::mdl_physically_plausible_light_set_outer_cone_angle(self.handle.as_ptr(), angle);
74        };
75    }
76
77    pub fn set_attenuation_start_distance(&self, distance: f32) {
78        unsafe {
79            ffi::mdl_physically_plausible_light_set_attenuation_start_distance(
80                self.handle.as_ptr(),
81                distance,
82            );
83        };
84    }
85
86    pub fn set_attenuation_end_distance(&self, distance: f32) {
87        unsafe {
88            ffi::mdl_physically_plausible_light_set_attenuation_end_distance(
89                self.handle.as_ptr(),
90                distance,
91            );
92        };
93    }
94
95    #[must_use]
96    pub fn as_light(&self) -> Light {
97        Light::from_handle(self.handle.clone())
98    }
99
100    #[must_use]
101    pub fn as_object(&self) -> Object {
102        Object::from_handle(self.handle.clone())
103    }
104}