Skip to content

Commit eb6dc36

Browse files
committed
create_directional_light method
1 parent f411d2d commit eb6dc36

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from processing import *
2+
3+
angle = 0.0
4+
5+
def setup():
6+
size(800, 600)
7+
mode_3d()
8+
dir_light = create_directional_light(0.5, 0.24, 1.0, 1500.0)
9+
10+
def draw():
11+
global angle
12+
camera_position(100.0, 100.0, 300.0)
13+
camera_look_at(0.0, 0.0, 0.0)
14+
background(220)
15+
16+
push_matrix()
17+
rotate(angle)
18+
draw_box(100.0, 100.0, 100.0)
19+
pop_matrix()
20+
21+
angle += 0.02
22+
23+
24+
# TODO: this should happen implicitly on module load somehow
25+
run()
26+

crates/processing_pyo3/src/graphics.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ impl Drop for Surface {
2323
}
2424
}
2525

26+
#[pyclass]
27+
#[derive(Debug)]
28+
pub struct Light {
29+
entity: Entity,
30+
}
31+
32+
// TODO: implement `light_destroy`
33+
// impl Drop for Light {
34+
// fn drop(&mut self) {
35+
// let _ = light_destroy(self.entity);
36+
// }
37+
// }
38+
2639
#[pyclass]
2740
#[derive(Debug)]
2841
pub struct Image {
@@ -308,6 +321,14 @@ impl Graphics {
308321
graphics_ortho(self.entity, left, right, bottom, top, near, far)
309322
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
310323
}
324+
325+
pub fn light_directional(&self, r: f32, g: f32, b: f32, illuminance: f32) -> PyResult<Light> {
326+
let color = bevy::color::Color::srgb(r, g, b);
327+
match light_create_directional(self.entity, color, illuminance) {
328+
Ok(light) => Ok(Light { entity: light }),
329+
Err(e) => Err(PyRuntimeError::new_err(format!("{e}"))),
330+
}
331+
}
311332
}
312333

313334
// TODO: a real color type. or color parser? idk. color is confusing. let's think

crates/processing_pyo3/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
mod glfw;
1212
mod graphics;
1313

14-
use graphics::{Geometry, Graphics, Image, Topology, get_graphics, get_graphics_mut};
14+
use graphics::{Geometry, Graphics, Image, Light, Topology, get_graphics, get_graphics_mut};
1515
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyTuple};
1616

1717
use std::env;
@@ -20,7 +20,7 @@ use std::env;
2020
fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
2121
m.add_class::<Graphics>()?;
2222
m.add_class::<Image>()?;
23-
m.add_class::<Geometry>()?;
23+
m.add_class::<Light>()?;
2424
m.add_class::<Topology>()?;
2525
m.add_function(wrap_pyfunction!(size, m)?)?;
2626
m.add_function(wrap_pyfunction!(run, m)?)?;
@@ -40,6 +40,7 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
4040
m.add_function(wrap_pyfunction!(rect, m)?)?;
4141
m.add_function(wrap_pyfunction!(image, m)?)?;
4242
m.add_function(wrap_pyfunction!(draw_geometry, m)?)?;
43+
m.add_function(wrap_pyfunction!(create_directional_light, m)?)?;
4344

4445
Ok(())
4546
}
@@ -223,3 +224,15 @@ fn rect(
223224
fn image(module: &Bound<'_, PyModule>, image_file: &str) -> PyResult<Image> {
224225
get_graphics(module)?.image(image_file)
225226
}
227+
228+
#[pyfunction]
229+
#[pyo3(pass_module, signature = (r, g, b, illuminance))]
230+
fn create_directional_light(
231+
module: &Bound<'_, PyModule>,
232+
r: f32,
233+
g: f32,
234+
b: f32,
235+
illuminance: f32,
236+
) -> PyResult<Light> {
237+
get_graphics(module)?.light_directional(r, g, b, illuminance)
238+
}

0 commit comments

Comments
 (0)