mesh.rs 6.4 KB

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