1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::ptr;
use crate::error::Result;
use crate::ffi;
use crate::handle::ObjectHandle;
use crate::object::Object;
use crate::types::{LightInfo, LightType};
use crate::util::{c_string, parse_json, required_handle};
#[derive(Debug, Clone)]
/// Wraps the corresponding Model I/O light counterpart.
pub struct Light {
handle: ObjectHandle,
}
impl Light {
/// Builds this wrapper from the retained handle of the wrapped Model I/O light counterpart.
pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
Self { handle }
}
/// Wraps the corresponding Model I/O initializer for the wrapped Model I/O light counterpart.
pub fn new() -> Result<Self> {
let mut out_light = ptr::null_mut();
let mut out_error = ptr::null_mut();
// SAFETY: Output pointers are initialized and managed; FFI function is called safely.
let status = unsafe { ffi::mdl_light_new(&mut out_light, &mut out_error) };
crate::util::status_result(status, out_error)?;
Ok(Self::from_handle(required_handle(out_light, "MDLLight")?))
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O light counterpart.
pub fn info(&self) -> Result<LightInfo> {
parse_json(
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_light_info_json(self.handle.as_ptr()) },
"MDLLight",
)
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O light counterpart.
pub fn set_light_type(&self, light_type: LightType) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_light_set_light_type(self.handle.as_ptr(), light_type.as_raw()) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O light counterpart.
pub fn set_color_space(&self, color_space: &str) -> Result<()> {
let color_space = c_string(color_space)?;
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_light_set_color_space(self.handle.as_ptr(), color_space.as_ptr()) };
Ok(())
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O light counterpart.
pub fn irradiance_at_point(&self, point: [f32; 3]) -> [f32; 4] {
let mut components = [0.0_f32; 4];
// SAFETY: The unsafe operation is valid in this context.
unsafe {
ffi::mdl_light_irradiance_at_point(
self.handle.as_ptr(),
point[0],
point[1],
point[2],
&mut components[0],
&mut components[1],
&mut components[2],
&mut components[3],
);
}
components
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O light counterpart.
pub fn as_object(&self) -> Object {
Object::from_handle(self.handle.clone())
}
}