1use std::ptr;
2
3use crate::error::Result;
4use crate::ffi;
5use crate::handle::ObjectHandle;
6use crate::object::Object;
7use crate::types::{LightInfo, LightType};
8use crate::util::{c_string, parse_json, required_handle};
9
10#[derive(Debug, Clone)]
11pub struct Light {
12 handle: ObjectHandle,
13}
14
15impl Light {
16 pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
17 Self { handle }
18 }
19
20 pub fn new() -> Result<Self> {
21 let mut out_light = ptr::null_mut();
22 let mut out_error = ptr::null_mut();
23 let status = unsafe { ffi::mdl_light_new(&mut out_light, &mut out_error) };
24 crate::util::status_result(status, out_error)?;
25 Ok(Self::from_handle(required_handle(out_light, "MDLLight")?))
26 }
27
28 pub fn info(&self) -> Result<LightInfo> {
29 parse_json(
30 unsafe { ffi::mdl_light_info_json(self.handle.as_ptr()) },
31 "MDLLight",
32 )
33 }
34
35 pub fn set_light_type(&self, light_type: LightType) {
36 unsafe { ffi::mdl_light_set_light_type(self.handle.as_ptr(), light_type.as_raw()) };
37 }
38
39 pub fn set_color_space(&self, color_space: &str) -> Result<()> {
40 let color_space = c_string(color_space)?;
41 unsafe { ffi::mdl_light_set_color_space(self.handle.as_ptr(), color_space.as_ptr()) };
42 Ok(())
43 }
44
45 #[must_use]
46 pub fn irradiance_at_point(&self, point: [f32; 3]) -> [f32; 4] {
47 let mut components = [0.0_f32; 4];
48 unsafe {
49 ffi::mdl_light_irradiance_at_point(
50 self.handle.as_ptr(),
51 point[0],
52 point[1],
53 point[2],
54 &mut components[0],
55 &mut components[1],
56 &mut components[2],
57 &mut components[3],
58 );
59 }
60 components
61 }
62
63 #[must_use]
64 pub fn as_object(&self) -> Object {
65 Object::from_handle(self.handle.clone())
66 }
67}