mesh.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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::gfx::{
  19. DebugTag, GfxDrawMesh, ManagedBufferPtr, ManagedTexturePtr, Point, Rectangle, RenderApi, Vertex,
  20. };
  21. pub type Color = [f32; 4];
  22. #[allow(dead_code)]
  23. pub const COLOR_RED: Color = [1., 0., 0., 1.];
  24. #[allow(dead_code)]
  25. pub const COLOR_DARKGREY: Color = [0.2, 0.2, 0.2, 1.];
  26. #[allow(dead_code)]
  27. pub const COLOR_LIGHTGREY: Color = [0.7, 0.7, 0.7, 1.];
  28. #[allow(dead_code)]
  29. pub const COLOR_GREEN: Color = [0., 1., 0., 1.];
  30. pub const COLOR_BLUE: Color = [0., 0., 1., 1.];
  31. pub const COLOR_PINK: Color = [0.8, 0.3, 0.8, 1.];
  32. #[allow(dead_code)]
  33. pub const COLOR_PURPLE: Color = [1., 0., 1., 1.];
  34. pub const COLOR_WHITE: Color = [1., 1., 1., 1.];
  35. #[allow(dead_code)]
  36. pub const COLOR_BLACK: Color = [1., 1., 1., 1.];
  37. #[allow(dead_code)]
  38. pub const COLOR_GREY: Color = [0.5, 0.5, 0.5, 1.];
  39. #[derive(Clone)]
  40. pub struct MeshInfo {
  41. pub vertex_buffer: ManagedBufferPtr,
  42. pub index_buffer: ManagedBufferPtr,
  43. pub num_elements: i32,
  44. }
  45. impl MeshInfo {
  46. /// Convenience method
  47. pub fn draw_with_texture(self, texture: ManagedTexturePtr) -> GfxDrawMesh {
  48. GfxDrawMesh {
  49. vertex_buffer: self.vertex_buffer,
  50. index_buffer: self.index_buffer,
  51. texture: Some(texture),
  52. num_elements: self.num_elements,
  53. }
  54. }
  55. /// Convenience method
  56. pub fn draw_untextured(self) -> GfxDrawMesh {
  57. GfxDrawMesh {
  58. vertex_buffer: self.vertex_buffer,
  59. index_buffer: self.index_buffer,
  60. texture: None,
  61. num_elements: self.num_elements,
  62. }
  63. }
  64. }
  65. // TODO: remove clipper => simplify impl
  66. pub struct MeshBuilder {
  67. pub verts: Vec<Vertex>,
  68. pub indices: Vec<u16>,
  69. clipper: Option<Rectangle>,
  70. tag: DebugTag,
  71. }
  72. impl MeshBuilder {
  73. pub fn new(tag: DebugTag) -> Self {
  74. Self { verts: vec![], indices: vec![], clipper: None, tag }
  75. }
  76. pub fn append(&mut self, mut verts: Vec<Vertex>, indices: Vec<u16>) {
  77. let mut indices = indices.into_iter().map(|i| i + self.verts.len() as u16).collect();
  78. self.verts.append(&mut verts);
  79. self.indices.append(&mut indices);
  80. }
  81. pub fn draw_box(&mut self, obj: &Rectangle, color: Color, uv: &Rectangle) {
  82. let clipped = match &self.clipper {
  83. Some(clipper) => {
  84. let Some(clipped) = clipper.clip(&obj) else { return };
  85. clipped
  86. }
  87. None => obj.clone(),
  88. };
  89. let (x1, y1) = clipped.pos().unpack();
  90. let (x2, y2) = clipped.corner().unpack();
  91. let (u1, v1) = uv.pos().unpack();
  92. let (u2, v2) = uv.corner().unpack();
  93. // Interpolate UV coords
  94. let i = (clipped.x - obj.x) / obj.w;
  95. let clip_u1 = u1 + i * (u2 - u1);
  96. let i = (clipped.rhs() - obj.x) / obj.w;
  97. let clip_u2 = u1 + i * (u2 - u1);
  98. let i = (clipped.y - obj.y) / obj.h;
  99. let clip_v1 = v1 + i * (v2 - v1);
  100. let i = (clipped.bhs() - obj.y) / obj.h;
  101. let clip_v2 = v1 + i * (v2 - v1);
  102. let (u1, u2) = (clip_u1, clip_u2);
  103. let (v1, v2) = (clip_v1, clip_v2);
  104. let verts = vec![
  105. // top left
  106. Vertex { pos: [x1, y1], color, uv: [u1, v1] },
  107. // top right
  108. Vertex { pos: [x2, y1], color, uv: [u2, v1] },
  109. // bottom left
  110. Vertex { pos: [x1, y2], color, uv: [u1, v2] },
  111. // bottom right
  112. Vertex { pos: [x2, y2], color, uv: [u2, v2] },
  113. ];
  114. let indices = vec![0, 2, 1, 1, 2, 3];
  115. self.append(verts, indices);
  116. }
  117. pub fn draw_filled_box(&mut self, obj: &Rectangle, color: Color) {
  118. let uv = Rectangle::zero();
  119. self.draw_box(obj, color, &uv);
  120. }
  121. pub fn draw_outline(&mut self, obj: &Rectangle, color: Color, thickness: f32) {
  122. let (x1, y1) = obj.pos().unpack();
  123. let (dist_x, dist_y) = (obj.w, obj.h);
  124. let (x2, y2) = obj.corner().unpack();
  125. // top
  126. self.draw_filled_box(&Rectangle::new(x1, y1, dist_x, thickness), color);
  127. // left
  128. self.draw_filled_box(&Rectangle::new(x1, y1, thickness, dist_y), color);
  129. // right
  130. self.draw_filled_box(&Rectangle::new(x2 - thickness, y1, thickness, dist_y), color);
  131. // bottom
  132. self.draw_filled_box(&Rectangle::new(x1, y2 - thickness, dist_x, thickness), color);
  133. }
  134. pub fn draw_line(&mut self, start: Point, end: Point, color: Color, thickness: f32) {
  135. let mut dir = end - start;
  136. dir.normalize();
  137. let left = dir.perp_left() * (thickness / 2.);
  138. let right = dir.perp_right() * (thickness / 2.);
  139. let p1 = start + left;
  140. let p2 = end + left;
  141. let p3 = start + right;
  142. let p4 = end + right;
  143. let uv = [0., 0.];
  144. let verts = vec![
  145. // top left
  146. Vertex { pos: [p1.x, p1.y], color, uv },
  147. // top right
  148. Vertex { pos: [p2.x, p2.y], color, uv },
  149. // bottom left
  150. Vertex { pos: [p3.x, p3.y], color, uv },
  151. // bottom right
  152. Vertex { pos: [p4.x, p4.y], color, uv },
  153. ];
  154. let indices = vec![0, 2, 1, 1, 2, 3];
  155. self.append(verts, indices);
  156. }
  157. pub fn alloc(self, render_api: &RenderApi) -> MeshInfo {
  158. //debug!(target: "mesh", "allocating {} verts:", self.verts.len());
  159. //for vert in &self.verts {
  160. // debug!(target: "mesh", " {:?}", vert);
  161. //}
  162. let num_elements = self.indices.len() as i32;
  163. let vertex_buffer = render_api.new_vertex_buffer(self.verts, self.tag);
  164. let index_buffer = render_api.new_index_buffer(self.indices, self.tag);
  165. MeshInfo { vertex_buffer, index_buffer, num_elements }
  166. }
  167. }