fmod/core/geometry/polygons.rs
1// Copyright (c) 2024 Lily Lyons
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use fmod_sys::*;
8use std::{
9 ffi::{c_float, c_int},
10 mem::MaybeUninit,
11};
12
13use crate::{Geometry, Vector};
14
15impl Geometry {
16 /// Sets individual attributes for a polygon inside a geometry object.
17 pub fn set_polygon_attributes(
18 &self,
19 index: c_int,
20 direct_occlusion: c_float,
21 reverb_occlusion: c_float,
22 double_sided: bool,
23 ) -> Result<()> {
24 unsafe {
25 FMOD_Geometry_SetPolygonAttributes(
26 self.inner,
27 index,
28 direct_occlusion,
29 reverb_occlusion,
30 double_sided.into(),
31 )
32 .to_result()
33 }
34 }
35
36 /// Retrieves the attributes for a polygon.
37 pub fn get_polygon_attributes(&self, index: c_int) -> Result<(c_float, c_float, bool)> {
38 let mut direct = 0.0;
39 let mut reverb = 0.0;
40 let mut double_sided = FMOD_BOOL::FALSE;
41 unsafe {
42 FMOD_Geometry_GetPolygonAttributes(
43 self.inner,
44 index,
45 &mut direct,
46 &mut reverb,
47 &mut double_sided,
48 )
49 .to_result()?;
50 }
51 Ok((direct, reverb, double_sided.into()))
52 }
53
54 /// Gets the number of vertices in a polygon.
55 pub fn get_polygon_vertex_count(&self, index: c_int) -> Result<c_int> {
56 let mut count = 0;
57 unsafe {
58 FMOD_Geometry_GetPolygonNumVertices(self.inner, index, &mut count).to_result()?;
59 }
60 Ok(count)
61 }
62
63 /// Alters the position of a polygon's vertex inside a geometry object.
64 ///
65 /// Vertices are relative to the position of the object. See [`Geometry::set_position`].
66 ///
67 /// There may be some significant overhead with this function as it may cause some reconfiguration of internal data structures used to speed up sound-ray testing.
68 ///
69 /// You may get better results if you want to modify your object by using [`Geometry::set_position`], [`Geometry::set_scale`] and [`Geometry::set_rotation`].
70 pub fn set_polygon_vertex(
71 &self,
72 index: c_int,
73 vertex_index: c_int,
74 vertex: Vector,
75 ) -> Result<()> {
76 unsafe {
77 FMOD_Geometry_SetPolygonVertex(
78 self.inner,
79 index,
80 vertex_index,
81 std::ptr::from_ref(&vertex).cast(),
82 )
83 .to_result()
84 }
85 }
86
87 /// Retrieves the position of a vertex.
88 ///
89 /// Vertices are relative to the position of the object. See [`Geometry::set_osition`].
90 pub fn get_polygon_vertex(&self, index: c_int, vertex_index: c_int) -> Result<Vector> {
91 let mut vertex = MaybeUninit::uninit();
92 unsafe {
93 FMOD_Geometry_GetPolygonVertex(self.inner, index, vertex_index, vertex.as_mut_ptr())
94 .to_result()?;
95 let vertex = vertex.assume_init().into();
96 Ok(vertex)
97 }
98 }
99}