mesh.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use crate::{
  2. error::Result,
  3. gfx2::{Point, Rectangle, RenderApi, Vertex},
  4. };
  5. use miniquad::BufferId;
  6. pub type Color = [f32; 4];
  7. pub const COLOR_RED: Color = [1., 0., 0., 1.];
  8. pub const COLOR_DARKGREY: Color = [0.2, 0.2, 0.2, 1.];
  9. pub const COLOR_GREEN: Color = [0., 1., 0., 1.];
  10. pub const COLOR_BLUE: Color = [0., 0., 1., 1.];
  11. pub const COLOR_WHITE: Color = [1., 1., 1., 1.];
  12. #[derive(Clone)]
  13. pub struct MeshInfo {
  14. pub vertex_buffer: BufferId,
  15. pub index_buffer: BufferId,
  16. pub num_elements: i32,
  17. }
  18. pub struct MeshBuilder {
  19. verts: Vec<Vertex>,
  20. indices: Vec<u16>,
  21. clipper: Option<Rectangle>,
  22. }
  23. impl MeshBuilder {
  24. pub fn new() -> Self {
  25. Self { verts: vec![], indices: vec![], clipper: None }
  26. }
  27. pub fn with_clip(clipper: Rectangle) -> Self {
  28. Self { verts: vec![], indices: vec![], clipper: Some(clipper) }
  29. }
  30. pub fn append(&mut self, mut verts: Vec<Vertex>, indices: Vec<u16>) {
  31. let mut indices = indices.into_iter().map(|i| i + self.verts.len() as u16).collect();
  32. self.verts.append(&mut verts);
  33. self.indices.append(&mut indices);
  34. }
  35. pub fn draw_box(&mut self, obj: &Rectangle, color: Color, uv: &Rectangle) {
  36. let clipped = match &self.clipper {
  37. Some(clipper) => {
  38. let Some(clipped) = clipper.clip(&obj) else {
  39. return;
  40. };
  41. clipped
  42. }
  43. None => obj.clone(),
  44. };
  45. let (x1, y1) = clipped.top_left().unpack();
  46. let (x2, y2) = clipped.bottom_right().unpack();
  47. let (u1, v1) = uv.top_left().unpack();
  48. let (u2, v2) = uv.bottom_right().unpack();
  49. // Interpolate UV coords
  50. assert!(obj.w >= clipped.w);
  51. assert!(obj.h >= clipped.h);
  52. let i = (clipped.x - obj.x) / obj.w;
  53. let clip_u1 = u1 + i * (u2 - u1);
  54. let i = (clipped.rhs() - obj.x) / obj.w;
  55. let clip_u2 = u1 + i * (u2 - u1);
  56. let i = (clipped.y - obj.y) / obj.h;
  57. let clip_v1 = v1 + i * (v2 - v1);
  58. let i = (clipped.bhs() - obj.y) / obj.h;
  59. let clip_v2 = v1 + i * (v2 - v1);
  60. let (u1, u2) = (clip_u1, clip_u2);
  61. let (v1, v2) = (clip_v1, clip_v2);
  62. let verts = vec![
  63. // top left
  64. Vertex { pos: [x1, y1], color, uv: [u1, v1] },
  65. // top right
  66. Vertex { pos: [x2, y1], color, uv: [u2, v1] },
  67. // bottom left
  68. Vertex { pos: [x1, y2], color, uv: [u1, v2] },
  69. // bottom right
  70. Vertex { pos: [x2, y2], color, uv: [u2, v2] },
  71. ];
  72. let indices = vec![0, 2, 1, 1, 2, 3];
  73. self.append(verts, indices);
  74. }
  75. pub fn draw_outline(&mut self, obj: &Rectangle, color: Color, thickness: f32) {
  76. let uv = Rectangle { x: 0., y: 0., w: 0., h: 0. };
  77. let (x1, y1) = obj.top_left().unpack();
  78. let (dist_x, dist_y) = (obj.w, obj.h);
  79. let (x2, y2) = obj.bottom_right().unpack();
  80. // top
  81. self.draw_box(&Rectangle::new(x1, y1, dist_x, thickness), color, &uv);
  82. // left
  83. self.draw_box(&Rectangle::new(x1, y1, thickness, dist_y), color, &uv);
  84. // right
  85. self.draw_box(&Rectangle::new(x2 - thickness, y1, thickness, dist_y), color, &uv);
  86. // bottom
  87. self.draw_box(&Rectangle::new(x1, y2 - thickness, dist_x, thickness), color, &uv);
  88. }
  89. pub async fn alloc(self, render_api: &RenderApi) -> Result<MeshInfo> {
  90. //debug!(target: "mesh", "allocating {} verts:", self.verts.len());
  91. //for vert in &self.verts {
  92. // debug!(target: "mesh", " {:?}", vert);
  93. //}
  94. let num_elements = self.indices.len() as i32;
  95. let vertex_buffer = render_api.new_vertex_buffer(self.verts).await?;
  96. let index_buffer = render_api.new_index_buffer(self.indices).await?;
  97. Ok(MeshInfo { vertex_buffer, index_buffer, num_elements })
  98. }
  99. }