Skip to content

Commit f07f458

Browse files
committed
box example working
1 parent 8e1ef74 commit f07f458

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

crates/processing_pyo3/examples/box.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
angle = 0.0
44

55
def setup():
6-
size(800, 600, 1.0)
6+
size(800, 600)
77
mode_3d()
88

99
def draw():
10+
global angle
1011
camera_position(100.0, 100.0, 300.0)
1112
camera_look_at(0.0, 0.0, 0.0)
1213
background(220)
1314

1415

1516
push_matrix()
1617
rotate(angle)
17-
geometry(box)
18+
draw_box(100.0, 100.0, 100.0)
1819
pop_matrix()
1920

2021
angle += 0.02

crates/processing_pyo3/src/graphics.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ impl Graphics {
173173
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
174174
}
175175

176+
pub fn draw_box(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
177+
let box_geo = geometry_box(x, y, z).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
178+
graphics_record_command(self.entity, DrawCommand::Geometry(box_geo))
179+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
180+
}
181+
176182
pub fn scale(&self, x: f32, y: f32) -> PyResult<()> {
177183
graphics_record_command(self.entity, DrawCommand::Scale { x, y })
178184
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))

crates/processing_pyo3/src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
2222
m.add_class::<Image>()?;
2323
m.add_function(wrap_pyfunction!(size, m)?)?;
2424
m.add_function(wrap_pyfunction!(run, m)?)?;
25+
m.add_function(wrap_pyfunction!(mode_3d, m)?)?;
26+
m.add_function(wrap_pyfunction!(camera_position, m)?)?;
27+
m.add_function(wrap_pyfunction!(camera_look_at, m)?)?;
28+
m.add_function(wrap_pyfunction!(push_matrix, m)?)?;
29+
m.add_function(wrap_pyfunction!(pop_matrix, m)?)?;
30+
m.add_function(wrap_pyfunction!(rotate, m)?)?;
31+
m.add_function(wrap_pyfunction!(draw_box, m)?)?;
2532
m.add_function(wrap_pyfunction!(background, m)?)?;
2633
m.add_function(wrap_pyfunction!(fill, m)?)?;
2734
m.add_function(wrap_pyfunction!(no_fill, m)?)?;
@@ -97,6 +104,53 @@ fn run(module: &Bound<'_, PyModule>) -> PyResult<()> {
97104
})
98105
}
99106

107+
#[pyfunction]
108+
#[pyo3(pass_module)]
109+
fn mode_3d(module: &Bound<'_, PyModule>) -> PyResult<()> {
110+
get_graphics(module)?.mode_3d()
111+
}
112+
113+
#[pyfunction]
114+
#[pyo3(pass_module)]
115+
fn camera_position(module: &Bound<'_, PyModule>, x: f32, y: f32, z: f32) -> PyResult<()> {
116+
get_graphics(module)?.camera_position(x, y, z)
117+
}
118+
119+
#[pyfunction]
120+
#[pyo3(pass_module)]
121+
fn camera_look_at(
122+
module: &Bound<'_, PyModule>,
123+
target_x: f32,
124+
target_y: f32,
125+
target_z: f32,
126+
) -> PyResult<()> {
127+
get_graphics(module)?.camera_look_at(target_x, target_y, target_z)
128+
}
129+
130+
#[pyfunction]
131+
#[pyo3(pass_module)]
132+
fn push_matrix(module: &Bound<'_, PyModule>) -> PyResult<()> {
133+
get_graphics(module)?.push_matrix()
134+
}
135+
136+
#[pyfunction]
137+
#[pyo3(pass_module)]
138+
fn pop_matrix(module: &Bound<'_, PyModule>) -> PyResult<()> {
139+
get_graphics(module)?.push_matrix()
140+
}
141+
142+
#[pyfunction]
143+
#[pyo3(pass_module)]
144+
fn rotate(module: &Bound<'_, PyModule>, angle: f32) -> PyResult<()> {
145+
get_graphics(module)?.rotate(angle)
146+
}
147+
148+
#[pyfunction]
149+
#[pyo3(pass_module)]
150+
fn draw_box(module: &Bound<'_, PyModule>, x: f32, y: f32, z: f32) -> PyResult<()> {
151+
get_graphics(module)?.draw_box(x, y, z)
152+
}
153+
100154
#[pyfunction]
101155
#[pyo3(pass_module, signature = (*args))]
102156
fn background(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {

0 commit comments

Comments
 (0)