mesh.rs 7.7 KB

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