object.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use wgpu::vertex_attr_array;
  2. #[repr(C)]
  3. #[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
  4. pub struct Vertex {
  5. position: [f32; 3],
  6. color: [f32; 3],
  7. uv: [f32; 2],
  8. }
  9. impl Vertex{
  10. const ATTRIB : [wgpu::VertexAttribute; 3]= vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Float32x2];
  11. pub fn desc() -> wgpu::VertexBufferLayout<'static>{
  12. wgpu::VertexBufferLayout{
  13. array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
  14. step_mode: wgpu::VertexStepMode::Vertex,
  15. attributes: &Self::ATTRIB
  16. }
  17. }
  18. }
  19. #[derive(Default, Debug, Clone)]
  20. pub struct Mesh {
  21. vertices: Vec<Vertex>,
  22. }
  23. impl Mesh{
  24. pub fn len(&self) -> u32{
  25. return self.vertices.len().try_into().unwrap()
  26. }
  27. pub fn vertices(self)-> Vec<Vertex>{
  28. self.vertices
  29. }
  30. pub fn positions_list(&self) -> Vec<[f32; 3]> {
  31. self.vertices.iter().map(|vertex| vertex.position).collect()
  32. }
  33. }
  34. pub fn generate_plane(num_segx: u32, num_segy: u32, width: f32, height: f32) -> Mesh {
  35. let mut result = Mesh::default();
  36. let normal: [f32; 3] = [0.0, 0.0, 1.0];
  37. // Ensure num_segx and num_segy are not zero to avoid division by zero
  38. if num_segx == 0 || num_segy == 0 {
  39. panic!("Number of segments cannot be zero.");
  40. }
  41. let xstep = width / num_segx as f32;
  42. let ystep = height / num_segy as f32;
  43. let width_half = width / 2.0;
  44. let height_half = height / 2.0;
  45. let mut x = -width_half;
  46. let mut y = -height_half;
  47. let mut seg_y = 0;
  48. while seg_y < num_segy {
  49. let mut seg_x = 0;
  50. x= -width_half;
  51. while seg_x < num_segx {
  52. //println!("Boucle interne numéro {}, avec y = {}",seg_x,seg_y);
  53. let x0 = x;
  54. let y0 = y;
  55. let x1 = x + xstep;
  56. let y1 = y + ystep;
  57. // Add first triangle
  58. result.vertices.push(Vertex {
  59. position: [x0, y0, 0.0],
  60. color: [1.0, 0.0, 0.0], // Replace with desired color
  61. uv: [1.0 - (x0 + width_half) / width, (y0 + height_half) / height],
  62. });
  63. result.vertices.push(Vertex {
  64. position: [x1, y0, 0.0],
  65. color: [0.0, 1.0, 0.0], // Replace with desired color
  66. uv: [1.0 - (x1 + width_half) / width, (y0 + height_half) / height],
  67. });
  68. result.vertices.push(Vertex {
  69. position: [x0, y1, 0.0],
  70. color: [0.0, 0.0, 1.0], // Replace with desired color
  71. uv: [1.0 - (x0 + width_half) / width, (y1 + height_half) / height],
  72. });
  73. // Add second triangle (degenerates into a line if numSegX or numSegY is 1)
  74. result.vertices.push(Vertex {
  75. position: [x0, y1, 0.0],
  76. color: [0.0, 0.0, 1.0], // Replace with desired color (same as previous)
  77. uv: [1.0 - (x0 + width_half) / width, (y1 + height_half) / height],
  78. });
  79. result.vertices.push(Vertex {
  80. position: [x1, y0, 0.0],
  81. color: [0.0, 1.0, 0.0], // Replace with desired color (same as previous)
  82. uv: [1.0 - (x1 + width_half) / width, (y0 + height_half) / height],
  83. });
  84. result.vertices.push(Vertex {
  85. position: [x1, y1, 0.0],
  86. color: [1.0, 0.0, 0.0], // Replace with desired color (same as previous)
  87. uv: [1.0 - (x1 + width_half) / width, (y1 + height_half) / height],
  88. });
  89. x += xstep;
  90. seg_x += 1;
  91. }
  92. y += ystep;
  93. seg_y += 1;
  94. }
  95. result
  96. }
  97. pub fn change_plane(buff: wgpu::Buffer){
  98. println!("{:?}", buff.usage())
  99. }