Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions crates/processing_pyo3/examples/animated_mesh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from processing import *
from math import sin, cos

mesh = None
grid_size = 20
spacing = 10.0
offset = (grid_size * spacing) / 2.0;
time = 0.0

def setup():
global mesh
size(800, 600)
mode_3d()
mesh = Mesh()
for z in range(grid_size):
for x in range(grid_size):
px = x * spacing - offset
pz = z * spacing - offset
mesh.color(x/grid_size, 0.5, z/grid_size, 1.0)
mesh.normal(0.0, 1.0, 0.0)
mesh.vertex(px, 0.0, pz)

for z in range(grid_size-1):
for x in range(grid_size-1):
tl = z * grid_size + x
tr = tl + 1
bl = (z + 1) * grid_size + x
br = bl + 1

mesh.index(tl)
mesh.index(bl)
mesh.index(tr)

mesh.index(tr)
mesh.index(bl)
mesh.index(br)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, i believe because of our awesome bevy system, this can all just go in draw() and it won't do more than it needs to. perhaps i'll move it back.



def draw():
global mesh
global grid_size
global offset
global spacing
global time
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't like that we have to keep doing this. i want to figure out a way to declare a variable in setup() and have it available in draw() automagically.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i agree it's not the best. i think it would be nice if more things could be declared at module scope.

you could look at nannous api for one example, where we require the user to return an explicit state / model object from setup.


camera_position(150.0, 150.0, 150.0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that we might want camera to be it's own entity similar to p5: https://p5js.org/reference/p5/createCamera/

camera_look_at( 0.0, 0.0, 0.0)
background(220, 200, 140)

for z in range(grid_size):
for x in range(grid_size):
idx = int(z * grid_size + x)
px = x * spacing - offset
pz = z * spacing - offset
wave = sin(px * 0.1 + time) * cos(pz * 0.1 + time) * 20.0
mesh.set_vertex(idx, px, wave, pz)

draw_mesh(mesh)

time += 0.05


# TODO: this should happen implicitly on module load somehow
run()
25 changes: 25 additions & 0 deletions crates/processing_pyo3/examples/box.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from processing import *

angle = 0.0

def setup():
size(800, 600)
mode_3d()

def draw():
global angle
camera_position(100.0, 100.0, 300.0)
camera_look_at(0.0, 0.0, 0.0)
background(220)

push_matrix()
rotate(angle)
draw_box(100.0, 100.0, 100.0)
pop_matrix()

angle += 0.02


# TODO: this should happen implicitly on module load somehow
run()

48 changes: 48 additions & 0 deletions crates/processing_pyo3/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@ impl Drop for Image {
}
}

#[pyclass(unsendable)]
pub struct Mesh {
entity: Entity,
}

#[pymethods]
impl Mesh {
#[new]
pub fn new() -> PyResult<Self> {
let geometry = geometry_create(geometry::Topology::TriangleList)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Self { entity: geometry })
}

pub fn color(&self, r: f32, g: f32, b: f32, a: f32) -> PyResult<()> {
geometry_color(self.entity, r, g, b, a).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn normal(&self, nx: f32, ny: f32, nz: f32) -> PyResult<()> {
geometry_normal(self.entity, nx, ny, nz)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn vertex(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
geometry_vertex(self.entity, x, y, z).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn index(&self, i: u32) -> PyResult<()> {
geometry_index(self.entity, i).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn set_vertex(&self, i: u32, x: f32, y: f32, z: f32) -> PyResult<()> {
geometry_set_vertex(self.entity, i, x, y, z)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
}

#[pyclass(unsendable)]
pub struct Graphics {
entity: Entity,
Expand Down Expand Up @@ -173,6 +210,17 @@ impl Graphics {
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn draw_box(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
let box_geo = geometry_box(x, y, z).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if we want the immediate mode 3d, we should probably just pre-cache the shapes in libprocessing render but this totally works for now.

graphics_record_command(self.entity, DrawCommand::Geometry(box_geo))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn draw_mesh(&self, mesh: &Mesh) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::Geometry(mesh.entity))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn scale(&self, x: f32, y: f32) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::Scale { x, y })
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
Expand Down
65 changes: 64 additions & 1 deletion crates/processing_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
mod glfw;
mod graphics;

use graphics::{Graphics, Image, get_graphics, get_graphics_mut};
use graphics::{Graphics, Image, Mesh, get_graphics, get_graphics_mut};
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyTuple};

use std::env;
Expand All @@ -20,8 +20,16 @@ use std::env;
fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Graphics>()?;
m.add_class::<Image>()?;
m.add_class::<Mesh>()?;
m.add_function(wrap_pyfunction!(size, m)?)?;
m.add_function(wrap_pyfunction!(run, m)?)?;
m.add_function(wrap_pyfunction!(mode_3d, m)?)?;
m.add_function(wrap_pyfunction!(camera_position, m)?)?;
m.add_function(wrap_pyfunction!(camera_look_at, m)?)?;
m.add_function(wrap_pyfunction!(push_matrix, m)?)?;
m.add_function(wrap_pyfunction!(pop_matrix, m)?)?;
m.add_function(wrap_pyfunction!(rotate, m)?)?;
m.add_function(wrap_pyfunction!(draw_box, m)?)?;
m.add_function(wrap_pyfunction!(background, m)?)?;
m.add_function(wrap_pyfunction!(fill, m)?)?;
m.add_function(wrap_pyfunction!(no_fill, m)?)?;
Expand All @@ -30,6 +38,8 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(stroke_weight, m)?)?;
m.add_function(wrap_pyfunction!(rect, m)?)?;
m.add_function(wrap_pyfunction!(image, m)?)?;
m.add_function(wrap_pyfunction!(draw_mesh, m)?)?;

Ok(())
}

Expand Down Expand Up @@ -97,6 +107,59 @@ fn run(module: &Bound<'_, PyModule>) -> PyResult<()> {
})
}

#[pyfunction]
#[pyo3(pass_module)]
fn mode_3d(module: &Bound<'_, PyModule>) -> PyResult<()> {
get_graphics(module)?.mode_3d()
}

#[pyfunction]
#[pyo3(pass_module)]
fn camera_position(module: &Bound<'_, PyModule>, x: f32, y: f32, z: f32) -> PyResult<()> {
get_graphics(module)?.camera_position(x, y, z)
}

#[pyfunction]
#[pyo3(pass_module)]
fn camera_look_at(
module: &Bound<'_, PyModule>,
target_x: f32,
target_y: f32,
target_z: f32,
) -> PyResult<()> {
get_graphics(module)?.camera_look_at(target_x, target_y, target_z)
}

#[pyfunction]
#[pyo3(pass_module)]
fn push_matrix(module: &Bound<'_, PyModule>) -> PyResult<()> {
get_graphics(module)?.push_matrix()
}

#[pyfunction]
#[pyo3(pass_module)]
fn pop_matrix(module: &Bound<'_, PyModule>) -> PyResult<()> {
get_graphics(module)?.push_matrix()
}

#[pyfunction]
#[pyo3(pass_module)]
fn rotate(module: &Bound<'_, PyModule>, angle: f32) -> PyResult<()> {
get_graphics(module)?.rotate(angle)
}

#[pyfunction]
#[pyo3(pass_module)]
fn draw_box(module: &Bound<'_, PyModule>, x: f32, y: f32, z: f32) -> PyResult<()> {
get_graphics(module)?.draw_box(x, y, z)
}

#[pyfunction]
#[pyo3(pass_module, signature = (mesh))]
fn draw_mesh(module: &Bound<'_, PyModule>, mesh: &Bound<'_, Mesh>) -> PyResult<()> {
get_graphics(module)?.draw_mesh(&*mesh.extract::<PyRef<Mesh>>()?)
}

#[pyfunction]
#[pyo3(pass_module, signature = (*args))]
fn background(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {
Expand Down