Skip to content

Commit 3d70536

Browse files
committed
rendering mesh
1 parent 2241e99 commit 3d70536

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

crates/processing_pyo3/examples/animated_mesh.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,49 @@
33
mesh = None
44
grid_size = 20
55
spacing = 10.0
6+
offset = (grid_size * spacing) / 2.0;
67

78
def setup():
9+
global mesh
810
size(800, 600)
911
mode_3d()
10-
global mesh = geometry_create(TriangleList)
11-
12-
def draw():
13-
# TODO: maybe all of this should be in `setup()`
14-
global grid_size
15-
12+
mesh = Mesh()
1613
for z in range(grid_size):
1714
for x in range(grid_size):
1815
px = x * spacing - offset
1916
pz = z * spacing - offset
20-
geometry_color(mesh, x/grid_size, 0.5, z/grid_size, 1.0)
21-
geometry_normal(mesh, 0.0,1.0,0.0)
22-
geometry_vertex(mesh, px, 0.0, pz)
17+
mesh.color(x/grid_size, 0.5, z/grid_size, 1.0)
18+
mesh.normal(0.0, 1.0, 0.0)
19+
mesh.vertex(px, 0.0, pz)
20+
2321
for z in range(grid_size-1):
2422
for x in range(grid_size-1):
2523
tl = z * grid_size + x
2624
tr = tl + 1
2725
bl = (z + 1) * grid_size + x
2826
br = bl + 1
2927

30-
geometry_index(mesh, tl)
31-
geometry_index(mesh, bl)
32-
geometry_index(mesh, tr)
28+
mesh.index(tl)
29+
mesh.index(bl)
30+
mesh.index(tr)
31+
32+
mesh.index(tr)
33+
mesh.index(bl)
34+
mesh.index(br)
35+
36+
37+
def draw():
38+
global mesh
39+
global grid_size
40+
global offset
41+
global spacing
42+
43+
camera_position(150.0, 150.0, 150.0)
44+
camera_look_at( 0.0, 0.0, 0.0)
45+
background(220, 200, 140)
3346

34-
geometry_index(mesh, tr)
35-
geometry_index(mesh, bl)
36-
geometry_index(mesh, br)
47+
draw_mesh(mesh)
3748

38-
3949

4050

4151
# TODO: this should happen implicitly on module load somehow

crates/processing_pyo3/examples/box.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def draw():
1212
camera_look_at(0.0, 0.0, 0.0)
1313
background(220)
1414

15-
1615
push_matrix()
1716
rotate(angle)
1817
draw_box(100.0, 100.0, 100.0)

crates/processing_pyo3/src/graphics.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@ impl Drop for Image {
3535
}
3636
}
3737

38+
#[pyclass(unsendable)]
39+
pub struct Mesh {
40+
entity: Entity,
41+
}
42+
43+
#[pymethods]
44+
impl Mesh {
45+
#[new]
46+
pub fn new() -> PyResult<Self> {
47+
let geometry = geometry_create(geometry::Topology::TriangleList)
48+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
49+
Ok(Self { entity: geometry })
50+
}
51+
52+
pub fn color(&self, r: f32, g: f32, b: f32, a: f32) -> PyResult<()> {
53+
geometry_color(self.entity, r, g, b, a).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
54+
}
55+
56+
pub fn normal(&self, nx: f32, ny: f32, nz: f32) -> PyResult<()> {
57+
geometry_normal(self.entity, nx, ny, nz)
58+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
59+
}
60+
61+
pub fn vertex(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
62+
geometry_vertex(self.entity, x, y, z).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
63+
}
64+
65+
pub fn index(&self, i: u32) -> PyResult<()> {
66+
geometry_index(self.entity, i).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
67+
}
68+
}
69+
3870
#[pyclass(unsendable)]
3971
pub struct Graphics {
4072
entity: Entity,
@@ -179,6 +211,11 @@ impl Graphics {
179211
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
180212
}
181213

214+
pub fn draw_mesh(&self, mesh: &Mesh) -> PyResult<()> {
215+
graphics_record_command(self.entity, DrawCommand::Geometry(mesh.entity))
216+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
217+
}
218+
182219
pub fn scale(&self, x: f32, y: f32) -> PyResult<()> {
183220
graphics_record_command(self.entity, DrawCommand::Scale { x, y })
184221
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))

crates/processing_pyo3/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
mod glfw;
1212
mod graphics;
1313

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

1717
use std::env;
@@ -20,6 +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::<Mesh>()?;
2324
m.add_function(wrap_pyfunction!(size, m)?)?;
2425
m.add_function(wrap_pyfunction!(run, m)?)?;
2526
m.add_function(wrap_pyfunction!(mode_3d, m)?)?;
@@ -37,6 +38,8 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
3738
m.add_function(wrap_pyfunction!(stroke_weight, m)?)?;
3839
m.add_function(wrap_pyfunction!(rect, m)?)?;
3940
m.add_function(wrap_pyfunction!(image, m)?)?;
41+
m.add_function(wrap_pyfunction!(draw_mesh, m)?)?;
42+
4043
Ok(())
4144
}
4245

@@ -151,6 +154,12 @@ fn draw_box(module: &Bound<'_, PyModule>, x: f32, y: f32, z: f32) -> PyResult<()
151154
get_graphics(module)?.draw_box(x, y, z)
152155
}
153156

157+
#[pyfunction]
158+
#[pyo3(pass_module, signature = (mesh))]
159+
fn draw_mesh(module: &Bound<'_, PyModule>, mesh: &Bound<'_, Mesh>) -> PyResult<()> {
160+
get_graphics(module)?.draw_mesh(&*mesh.extract::<PyRef<Mesh>>()?)
161+
}
162+
154163
#[pyfunction]
155164
#[pyo3(pass_module, signature = (*args))]
156165
fn background(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {

0 commit comments

Comments
 (0)