-
-
Notifications
You must be signed in to change notification settings - Fork 135
A simple wireframe object that takes a CpuMesh, for use in debugging. #552
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
Open
nowakf
wants to merge
1
commit into
asny:master
Choose a base branch
from
nowakf:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,158 @@ | ||
| use crate::core::*; | ||
| use crate::renderer::*; | ||
|
|
||
| pub struct Wireframe{ | ||
| width: f32, | ||
| positions: VertexBuffer<Vec3>, | ||
| barycentric: VertexBuffer<Vec3>, | ||
| context: Context, | ||
| aabb: AxisAlignedBoundingBox, | ||
| transformation: Mat4, | ||
| } | ||
| impl Wireframe { | ||
| pub fn new(context: &Context, mesh: &CpuMesh, width: f32) -> Self { | ||
| let positions = VertexBuffer::new_with_data( | ||
| context, | ||
| &mesh.positions.to_f32() | ||
| ); | ||
| let barycentric = VertexBuffer::new_with_data( | ||
| context, | ||
| &(0..positions.count()/3).flat_map(|_| [ | ||
| vec3(1., 0., 0.), | ||
| vec3(0., 1., 0.), | ||
| vec3(0., 0., 1.), | ||
| ]).collect::<Vec<_>>() | ||
| ); | ||
| Self { | ||
| width, | ||
| positions, | ||
| barycentric, | ||
| context: context.clone(), | ||
| aabb: mesh.compute_aabb(), | ||
| transformation: Mat4::identity(), | ||
| } | ||
| } | ||
| pub fn transform_mut(&mut self) -> &mut Mat4 { | ||
| &mut self.transformation | ||
| } | ||
| pub fn line_width_mut(&mut self) -> &mut f32 { | ||
| &mut self.width | ||
| } | ||
| } | ||
|
|
||
| struct WireframeMaterial(f32); | ||
|
|
||
| impl Object for Wireframe { | ||
| fn render(&self, viewer: &dyn Viewer, lights: &[&dyn Light]) { | ||
| render_with_material(&self.context, viewer, &self, WireframeMaterial(self.width), lights); | ||
| } | ||
|
|
||
| fn material_type(&self) -> MaterialType { | ||
| WireframeMaterial(0.).material_type() | ||
| } | ||
| } | ||
|
|
||
| impl Material for WireframeMaterial { | ||
| fn fragment_shader_source(&self, lights: &[&dyn Light]) -> String { | ||
| r#" | ||
| layout (location = 0) out vec4 outColor; | ||
|
|
||
| uniform float u_line_width = 0.5; | ||
|
|
||
| in vec3 bary; | ||
| in vec3 pos; | ||
|
|
||
| void main() { | ||
| vec3 d = fwidth(bary); | ||
| vec3 f = step(d * u_line_width, bary); | ||
| float b = min(min(f.x, f.y), f.z); | ||
| outColor = vec4(1.-b); | ||
| } | ||
| "#.into() | ||
| } | ||
|
|
||
| fn id(&self) -> EffectMaterialId { | ||
| //TODO! | ||
|
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. Unresolved TODO |
||
| EffectMaterialId(0) | ||
| } | ||
|
|
||
| fn use_uniforms(&self, program: &Program, viewer: &dyn Viewer, lights: &[&dyn Light]) { | ||
| program.use_uniform("u_line_width", self.0); | ||
| } | ||
|
|
||
| fn render_states(&self) -> RenderStates { | ||
| RenderStates { | ||
| depth_test: DepthTest::Always, | ||
| blend: Blend::TRANSPARENCY, | ||
| cull: Cull::None, | ||
| ..Default::default() | ||
| } | ||
| } | ||
|
|
||
| fn material_type(&self) -> MaterialType { | ||
| MaterialType::Transparent | ||
| } | ||
| } | ||
|
|
||
| impl Geometry for Wireframe { | ||
| fn draw(&self, viewer: &dyn Viewer, program: &Program, render_states: RenderStates) { | ||
| program.use_uniform("viewProjection", viewer.projection() * viewer.view()); | ||
| program.use_uniform("modelMatrix", self.transformation); | ||
| program.use_vertex_attribute("position", &self.positions); | ||
| program.use_vertex_attribute("barycentric", &self.barycentric); | ||
| program.draw_arrays( | ||
| render_states, | ||
| viewer.viewport(), | ||
| self.positions.count(), | ||
| ); | ||
| } | ||
|
|
||
| fn vertex_shader_source(&self) -> String { | ||
| r#" | ||
| uniform mat4 viewProjection; | ||
| uniform mat4 modelMatrix; | ||
|
|
||
| in vec3 position; | ||
| in vec3 barycentric; | ||
|
|
||
| out vec3 pos; | ||
| out vec3 bary; | ||
|
|
||
| void main() { | ||
| vec4 worldPos = modelMatrix * vec4(position, 1.); | ||
| pos = worldPos.xyz; | ||
| bary = barycentric; | ||
| gl_Position = viewProjection * worldPos; | ||
| } | ||
| "#.into() | ||
| } | ||
|
|
||
| fn id(&self) -> GeometryId { | ||
| //TODO! | ||
| GeometryId(0) | ||
| } | ||
|
|
||
| fn render_with_material( | ||
| &self, | ||
| _material: &dyn Material, | ||
| _viewer: &dyn Viewer, | ||
| _lights: &[&dyn Light], | ||
| ) { | ||
| panic!("Wireframes must be rendered with the built-in material."); | ||
| } | ||
|
|
||
| fn render_with_effect( | ||
| &self, | ||
| _material: &dyn Effect, | ||
| _viewer: &dyn Viewer, | ||
| _lights: &[&dyn Light], | ||
| _color_texture: Option<ColorTexture>, | ||
| _depth_texture: Option<DepthTexture>, | ||
| ) { | ||
| panic!("Wireframes do not support effects."); | ||
| } | ||
|
|
||
| fn aabb(&self) -> AxisAlignedBoundingBox { | ||
| self.aabb.transformed(self.transformation) | ||
| } | ||
| } | ||
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.
Missing space, you should use
cargo fmt