mesh.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use crate::{
  19. error::Result,
  20. gfx2::{DrawMesh, Point, Rectangle, RenderApi, Vertex},
  21. };
  22. use miniquad::{BufferId, TextureId};
  23. pub type Color = [f32; 4];
  24. pub const COLOR_RED: Color = [1., 0., 0., 1.];
  25. pub const COLOR_DARKGREY: Color = [0.2, 0.2, 0.2, 1.];
  26. pub const COLOR_LIGHTGREY: Color = [0.7, 0.7, 0.7, 1.];
  27. pub const COLOR_GREEN: Color = [0., 1., 0., 1.];
  28. pub const COLOR_BLUE: Color = [0., 0., 1., 1.];
  29. pub const COLOR_WHITE: Color = [1., 1., 1., 1.];
  30. pub const COLOR_BLACK: Color = [1., 1., 1., 1.];
  31. pub const COLOR_GREY: Color = [0.5, 0.5, 0.5, 1.];
  32. #[derive(Clone)]
  33. pub struct MeshInfo {
  34. pub vertex_buffer: BufferId,
  35. pub index_buffer: BufferId,
  36. pub num_elements: i32,
  37. }
  38. impl MeshInfo {
  39. /// Convenience method
  40. pub fn draw_with_texture(self, texture: TextureId) -> DrawMesh {
  41. DrawMesh {
  42. vertex_buffer: self.vertex_buffer,
  43. index_buffer: self.index_buffer,
  44. texture: Some(texture),
  45. num_elements: self.num_elements,
  46. }
  47. }
  48. /// Convenience method
  49. pub fn draw_untextured(self) -> DrawMesh {
  50. DrawMesh {
  51. vertex_buffer: self.vertex_buffer,
  52. index_buffer: self.index_buffer,
  53. texture: None,
  54. num_elements: self.num_elements,
  55. }
  56. }
  57. }
  58. pub struct MeshBuilder {
  59. verts: Vec<Vertex>,
  60. indices: Vec<u16>,
  61. clipper: Option<Rectangle>,
  62. }
  63. impl MeshBuilder {
  64. pub fn new() -> Self {
  65. Self { verts: vec![], indices: vec![], clipper: None }
  66. }
  67. pub fn with_clip(clipper: Rectangle) -> Self {
  68. Self { verts: vec![], indices: vec![], clipper: Some(clipper) }
  69. }
  70. pub fn append(&mut self, mut verts: Vec<Vertex>, indices: Vec<u16>) {
  71. let mut indices = indices.into_iter().map(|i| i + self.verts.len() as u16).collect();
  72. self.verts.append(&mut verts);
  73. self.indices.append(&mut indices);
  74. }
  75. pub fn draw_box(&mut self, obj: &Rectangle, color: Color, uv: &Rectangle) {
  76. let clipped = match &self.clipper {
  77. Some(clipper) => {
  78. let Some(clipped) = clipper.clip(&obj) else {
  79. return;
  80. };
  81. clipped
  82. }
  83. None => obj.clone(),
  84. };
  85. let (x1, y1) = clipped.top_left().unpack();
  86. let (x2, y2) = clipped.bottom_right().unpack();
  87. let (u1, v1) = uv.top_left().unpack();
  88. let (u2, v2) = uv.bottom_right().unpack();
  89. // Interpolate UV coords
  90. assert!(obj.w >= clipped.w);
  91. assert!(obj.h >= clipped.h);
  92. let i = (clipped.x - obj.x) / obj.w;
  93. let clip_u1 = u1 + i * (u2 - u1);
  94. let i = (clipped.rhs() - obj.x) / obj.w;
  95. let clip_u2 = u1 + i * (u2 - u1);
  96. let i = (clipped.y - obj.y) / obj.h;
  97. let clip_v1 = v1 + i * (v2 - v1);
  98. let i = (clipped.bhs() - obj.y) / obj.h;
  99. let clip_v2 = v1 + i * (v2 - v1);
  100. let (u1, u2) = (clip_u1, clip_u2);
  101. let (v1, v2) = (clip_v1, clip_v2);
  102. let verts = vec![
  103. // top left
  104. Vertex { pos: [x1, y1], color, uv: [u1, v1] },
  105. // top right
  106. Vertex { pos: [x2, y1], color, uv: [u2, v1] },
  107. // bottom left
  108. Vertex { pos: [x1, y2], color, uv: [u1, v2] },
  109. // bottom right
  110. Vertex { pos: [x2, y2], color, uv: [u2, v2] },
  111. ];
  112. let indices = vec![0, 2, 1, 1, 2, 3];
  113. self.append(verts, indices);
  114. }
  115. pub fn draw_filled_box(&mut self, obj: &Rectangle, color: Color) {
  116. let uv = Rectangle { x: 0., y: 0., w: 0., h: 0. };
  117. self.draw_box(obj, color, &uv);
  118. }
  119. pub fn draw_outline(&mut self, obj: &Rectangle, color: Color, thickness: f32) {
  120. let (x1, y1) = obj.top_left().unpack();
  121. let (dist_x, dist_y) = (obj.w, obj.h);
  122. let (x2, y2) = obj.bottom_right().unpack();
  123. // top
  124. self.draw_filled_box(&Rectangle::new(x1, y1, dist_x, thickness), color);
  125. // left
  126. self.draw_filled_box(&Rectangle::new(x1, y1, thickness, dist_y), color);
  127. // right
  128. self.draw_filled_box(&Rectangle::new(x2 - thickness, y1, thickness, dist_y), color);
  129. // bottom
  130. self.draw_filled_box(&Rectangle::new(x1, y2 - thickness, dist_x, thickness), color);
  131. }
  132. pub async fn alloc(self, render_api: &RenderApi) -> Result<MeshInfo> {
  133. //debug!(target: "mesh", "allocating {} verts:", self.verts.len());
  134. //for vert in &self.verts {
  135. // debug!(target: "mesh", " {:?}", vert);
  136. //}
  137. let num_elements = self.indices.len() as i32;
  138. let vertex_buffer = render_api.new_vertex_buffer(self.verts).await?;
  139. let index_buffer = render_api.new_index_buffer(self.indices).await?;
  140. Ok(MeshInfo { vertex_buffer, index_buffer, num_elements })
  141. }
  142. }