-
Notifications
You must be signed in to change notification settings - Fork 6
Geometry Functions #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
8e1ef74
f07f458
2241e99
3d70536
4748187
b75067d
d4c2e5e
75c18ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
|
|
||
|
|
||
| def draw(): | ||
| global mesh | ||
| global grid_size | ||
| global offset | ||
| global spacing | ||
| global time | ||
|
||
|
|
||
| camera_position(150.0, 150.0, 150.0) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| 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() | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,43 @@ impl Drop for Image { | |
| } | ||
| } | ||
|
|
||
| #[pyclass(unsendable)] | ||
| pub struct Mesh { | ||
catilac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 }) | ||
| } | ||
|
|
||
catilac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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, | ||
|
|
@@ -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}")))?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<()> { | ||
catilac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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}"))) | ||
|
|
||
There was a problem hiding this comment.
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.