mesh.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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, DrawMesh, 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. #[allow(dead_code)]
  31. pub const COLOR_BLUE: Color = [0., 0., 1., 1.];
  32. #[allow(dead_code)]
  33. pub const COLOR_PINK: Color = [0.8, 0.3, 0.8, 1.];
  34. pub const COLOR_CYAN: Color = [0., 1., 1., 1.];
  35. pub const COLOR_TEAL: Color = [0.0784, 0.6824, 0.7216, 1.];
  36. #[allow(dead_code)]
  37. pub const COLOR_PURPLE: Color = [1., 0., 1., 1.];
  38. pub const COLOR_WHITE: Color = [1., 1., 1., 1.];
  39. #[allow(dead_code)]
  40. pub const COLOR_BLACK: Color = [1., 1., 1., 1.];
  41. #[allow(dead_code)]
  42. pub const COLOR_GREY: Color = [0.5, 0.5, 0.5, 1.];
  43. #[derive(Clone)]
  44. pub struct MeshInfo {
  45. pub vertex_buffer: ManagedBufferPtr,
  46. pub index_buffer: ManagedBufferPtr,
  47. pub num_elements: i32,
  48. }
  49. impl MeshInfo {
  50. /// Convenience method for textured mesh
  51. pub fn draw_with_textures(self, textures: Vec<ManagedTexturePtr>) -> DrawMesh {
  52. DrawMesh {
  53. vertex_buffer: self.vertex_buffer,
  54. index_buffer: self.index_buffer,
  55. textures: Some(textures),
  56. num_elements: self.num_elements,
  57. }
  58. }
  59. /// Convenience method
  60. pub fn draw_untextured(self) -> DrawMesh {
  61. DrawMesh {
  62. vertex_buffer: self.vertex_buffer,
  63. index_buffer: self.index_buffer,
  64. textures: None,
  65. num_elements: self.num_elements,
  66. }
  67. }
  68. }
  69. pub struct MeshBuilder {
  70. pub verts: Vec<Vertex>,
  71. pub indices: Vec<u16>,
  72. tag: DebugTag,
  73. }
  74. impl MeshBuilder {
  75. pub fn new(tag: DebugTag) -> Self {
  76. Self { verts: vec![], indices: vec![], tag }
  77. }
  78. pub fn append(&mut self, mut verts: Vec<Vertex>, indices: Vec<u16>) {
  79. let mut indices = indices.into_iter().map(|i| i + self.verts.len() as u16).collect();
  80. self.verts.append(&mut verts);
  81. self.indices.append(&mut indices);
  82. }
  83. pub fn draw_box(&mut self, obj: &Rectangle, color: Color, uv: &Rectangle) {
  84. let (x1, y1) = obj.pos().unpack();
  85. let (x2, y2) = obj.corner().unpack();
  86. let (u1, v1) = uv.pos().unpack();
  87. let (u2, v2) = uv.corner().unpack();
  88. let verts = vec![
  89. // top left
  90. Vertex { pos: [x1, y1], color, uv: [u1, v1] },
  91. // top right
  92. Vertex { pos: [x2, y1], color, uv: [u2, v1] },
  93. // bottom left
  94. Vertex { pos: [x1, y2], color, uv: [u1, v2] },
  95. // bottom right
  96. Vertex { pos: [x2, y2], color, uv: [u2, v2] },
  97. ];
  98. let indices = vec![0, 2, 1, 1, 2, 3];
  99. self.append(verts, indices);
  100. }
  101. pub fn draw_filled_box(&mut self, obj: &Rectangle, color: Color) {
  102. let uv = Rectangle::zero();
  103. self.draw_box(obj, color, &uv);
  104. }
  105. pub fn draw_box_shadow(&mut self, obj: &Rectangle, color: Color, spread: f32) {
  106. let (x1, y1) = obj.pos().unpack();
  107. let (x2, y2) = obj.corner().unpack();
  108. let uv = Rectangle::zero();
  109. let (u1, v1) = uv.pos().unpack();
  110. let (u2, v2) = uv.corner().unpack();
  111. let color2 = [color[0], color[1], color[2], 0.];
  112. // left
  113. self.append(
  114. vec![
  115. Vertex { pos: [x1, y1], color, uv: [u1, v1] },
  116. Vertex { pos: [x1 - spread, y1 - spread], color: color2, uv: [u2, v1] },
  117. Vertex { pos: [x1, y2], color, uv: [u1, v2] },
  118. Vertex { pos: [x1 - spread, y2 + spread], color: color2, uv: [u2, v2] },
  119. ],
  120. vec![0, 2, 1, 1, 2, 3],
  121. );
  122. // top
  123. self.append(
  124. vec![
  125. Vertex { pos: [x1, y1], color, uv: [u1, v1] },
  126. Vertex { pos: [x1 - spread, y1 - spread], color: color2, uv: [u2, v1] },
  127. Vertex { pos: [x2, y1], color, uv: [u1, v2] },
  128. Vertex { pos: [x2 + spread, y1 - spread], color: color2, uv: [u2, v2] },
  129. ],
  130. vec![0, 2, 1, 1, 2, 3],
  131. );
  132. // right
  133. self.append(
  134. vec![
  135. Vertex { pos: [x2, y1], color, uv: [u1, v1] },
  136. Vertex { pos: [x2 + spread, y1 - spread], color: color2, uv: [u2, v1] },
  137. Vertex { pos: [x2, y2], color, uv: [u1, v2] },
  138. Vertex { pos: [x2 + spread, y2 + spread], color: color2, uv: [u2, v2] },
  139. ],
  140. vec![0, 2, 1, 1, 2, 3],
  141. );
  142. // bottom
  143. self.append(
  144. vec![
  145. Vertex { pos: [x1, y2], color, uv: [u1, v1] },
  146. Vertex { pos: [x1 - spread, y2 + spread], color: color2, uv: [u2, v1] },
  147. Vertex { pos: [x2, y2], color, uv: [u1, v2] },
  148. Vertex { pos: [x2 + spread, y2 + spread], color: color2, uv: [u2, v2] },
  149. ],
  150. vec![0, 2, 1, 1, 2, 3],
  151. );
  152. }
  153. pub fn draw_outline(&mut self, obj: &Rectangle, color: Color, thickness: f32) {
  154. let (x1, y1) = obj.pos().unpack();
  155. let (dist_x, dist_y) = (obj.w, obj.h);
  156. let (x2, y2) = obj.corner().unpack();
  157. // top
  158. self.draw_filled_box(&Rectangle::new(x1, y1, dist_x, thickness), color);
  159. // left
  160. self.draw_filled_box(&Rectangle::new(x1, y1, thickness, dist_y), color);
  161. // right
  162. self.draw_filled_box(&Rectangle::new(x2 - thickness, y1, thickness, dist_y), color);
  163. // bottom
  164. self.draw_filled_box(&Rectangle::new(x1, y2 - thickness, dist_x, thickness), color);
  165. }
  166. pub fn draw_line(&mut self, start: Point, end: Point, color: Color, thickness: f32) {
  167. let mut dir = end - start;
  168. dir.normalize();
  169. let left = dir.perp_left() * (thickness / 2.);
  170. let right = dir.perp_right() * (thickness / 2.);
  171. let p1 = start + left;
  172. let p2 = end + left;
  173. let p3 = start + right;
  174. let p4 = end + right;
  175. let uv = [0., 0.];
  176. let verts = vec![
  177. // top left
  178. Vertex { pos: [p1.x, p1.y], color, uv },
  179. // top right
  180. Vertex { pos: [p2.x, p2.y], color, uv },
  181. // bottom left
  182. Vertex { pos: [p3.x, p3.y], color, uv },
  183. // bottom right
  184. Vertex { pos: [p4.x, p4.y], color, uv },
  185. ];
  186. let indices = vec![0, 2, 1, 1, 2, 3];
  187. self.append(verts, indices);
  188. }
  189. pub fn alloc<R: RenderApi>(self, renderer: &R) -> MeshInfo {
  190. //debug!(target: "mesh", "allocating {} verts:", self.verts.len());
  191. //for vert in &self.verts {
  192. // debug!(target: "mesh", " {:?}", vert);
  193. //}
  194. let num_elements = self.indices.len() as i32;
  195. let vertex_buffer = renderer.new_vertex_buffer(self.verts, self.tag);
  196. let index_buffer = renderer.new_index_buffer(self.indices, self.tag);
  197. MeshInfo { vertex_buffer, index_buffer, num_elements }
  198. }
  199. }