mesh.rs 6.5 KB

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