-
Notifications
You must be signed in to change notification settings - Fork 2
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
Merged
+231
−2
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8e1ef74
box.py sketch
catilac f07f458
box example working
catilac 2241e99
wip animated_mesh.py
catilac 3d70536
rendering mesh
catilac 4748187
animated_mesh.py works!
catilac b75067d
kwargs for `mesh` method and default topology
catilac d4c2e5e
Mesh -> Geometry
catilac 75c18ef
fmt check
catilac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| from processing import * | ||
| from math import sin, cos | ||
|
|
||
| geometry = None | ||
| grid_size = 20 | ||
| spacing = 10.0 | ||
| offset = (grid_size * spacing) / 2.0; | ||
| time = 0.0 | ||
|
|
||
| def setup(): | ||
| global geometry | ||
| size(800, 600) | ||
| mode_3d() | ||
| geometry = Geometry() | ||
| for z in range(grid_size): | ||
| for x in range(grid_size): | ||
| px = x * spacing - offset | ||
| pz = z * spacing - offset | ||
| geometry.color(x/grid_size, 0.5, z/grid_size, 1.0) | ||
| geometry.normal(0.0, 1.0, 0.0) | ||
| geometry.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 | ||
|
|
||
| geometry.index(tl) | ||
| geometry.index(bl) | ||
| geometry.index(tr) | ||
|
|
||
| geometry.index(tr) | ||
| geometry.index(bl) | ||
| geometry.index(br) | ||
|
|
||
|
|
||
| def draw(): | ||
| global geometry | ||
| global grid_size | ||
| global offset | ||
| global spacing | ||
| global time | ||
|
|
||
| camera_position(150.0, 150.0, 150.0) | ||
| 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 | ||
| geometry.set_vertex(idx, px, wave, pz) | ||
|
|
||
| draw_geometry(geometry) | ||
|
|
||
| time += 0.05 | ||
|
|
||
|
|
||
| # TODO: this should happen implicitly on module load somehow | ||
| run() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| use bevy::prelude::Entity; | ||
| use processing::prelude::*; | ||
| use pyo3::{exceptions::PyRuntimeError, prelude::*}; | ||
| use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyDict}; | ||
|
|
||
| use crate::glfw::GlfwContext; | ||
|
|
||
|
|
@@ -35,6 +35,71 @@ impl Drop for Image { | |
| } | ||
| } | ||
|
|
||
| #[pyclass(unsendable)] | ||
| pub struct Geometry { | ||
| entity: Entity, | ||
| } | ||
|
|
||
| #[pyclass] | ||
| pub enum Topology { | ||
| PointList = 0, | ||
| LineList = 1, | ||
| LineStrip = 2, | ||
| TriangleList = 3, | ||
| TriangleStrip = 4, | ||
| } | ||
|
|
||
| impl Topology { | ||
| pub fn as_u8(&self) -> u8 { | ||
| match self { | ||
| Self::PointList => 0, | ||
| Self::LineList => 1, | ||
| Self::LineStrip => 2, | ||
| Self::TriangleList => 3, | ||
| Self::TriangleStrip => 4, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl Geometry { | ||
| #[new] | ||
| #[pyo3(signature = (**kwargs))] | ||
| pub fn new(kwargs: Option<&Bound<'_, PyDict>>) -> PyResult<Self> { | ||
| let topology = kwargs | ||
| .and_then(|k| k.get_item("topology").ok().flatten()) | ||
| .and_then(|t| t.cast_into::<Topology>().ok()) | ||
| .and_then(|t| geometry::Topology::from_u8(t.borrow().as_u8())) | ||
| .unwrap_or(geometry::Topology::TriangleList); | ||
|
|
||
| let geometry = | ||
| geometry_create(topology).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, | ||
|
|
@@ -173,6 +238,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_geometry(&self, geometry: &Geometry) -> PyResult<()> { | ||
| graphics_record_command(self.entity, DrawCommand::Geometry(geometry.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}"))) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Considering that we might want camera to be it's own entity similar to p5: https://p5js.org/reference/p5/createCamera/