mesh.rs 6.5 KB

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