Advertisement
Guest User

Untitled

a guest
May 4th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.53 KB | None | 0 0
  1. use crate::{collision::{Collidable, Collider}, renderable::Renderable};
  2. use nalgebra::{Matrix4, Vector3};
  3. use std::ffi::c_void;
  4.  
  5. #[derive(Debug, Copy, Clone)]
  6. pub struct Plane {
  7.     pub vao: u32,
  8.     pub position: Vector3<f32>,
  9.     pub rotation: Vector3<f32>,
  10.     pub scale: Vector3<f32>,
  11.     pub color: Vector3<f32>,
  12.     pub texture_id: Option<u32>,
  13.     pub normal_map: Option<u32>,
  14.     pub roughness_map: Option<u32>,
  15. }
  16.  
  17. impl Plane {
  18.     pub fn new(
  19.         pos_x: f32, pos_y: f32, pos_z: f32,
  20.         rot_x: f32, rot_y: f32, rot_z: f32,
  21.         scale_x: f32, scale_y: f32, scale_z: f32,
  22.         color_r: f32, color_g: f32, color_b: f32,
  23.         tile: f32,
  24.     ) -> Plane {
  25.         // position (3) + normal (3) + texcoord (2) = 8 floats per vertex
  26.         let vertices: [f32; 48] = [
  27.             //   x      y    z     nx  ny  nz    u       v
  28.             -10.0, 0.0, -10.0,  0.0, 1.0, 0.0,   0.0,     0.0,
  29.              10.0, 0.0, -10.0,  0.0, 1.0, 0.0,   tile,    0.0,
  30.             -10.0, 0.0,  10.0,  0.0, 1.0, 0.0,   0.0,     tile,
  31.              10.0, 0.0, -10.0,  0.0, 1.0, 0.0,   tile,    0.0,
  32.              10.0, 0.0,  10.0,  0.0, 1.0, 0.0,   tile,    tile,
  33.             -10.0, 0.0,  10.0,  0.0, 1.0, 0.0,   0.0,     tile,
  34.         ];
  35.  
  36.         let (mut vao, mut vbo) = (0, 0);
  37.  
  38.         unsafe {
  39.             gl::GenVertexArrays(1, &mut vao);
  40.             gl::GenBuffers(1, &mut vbo);
  41.  
  42.             gl::BindVertexArray(vao);
  43.             gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
  44.             gl::BufferData(
  45.                 gl::ARRAY_BUFFER,
  46.                 (vertices.len() * std::mem::size_of::<f32>()) as isize,
  47.                 vertices.as_ptr() as *const _,
  48.                 gl::STATIC_DRAW,
  49.             );
  50.  
  51.             let stride = (8 * std::mem::size_of::<f32>()) as i32;
  52.  
  53.             // position layout(location = 0)
  54.             gl::VertexAttribPointer(
  55.                 0,
  56.                 3,
  57.                 gl::FLOAT,
  58.                 gl::FALSE,
  59.                 stride,
  60.                 std::ptr::null(),
  61.             );
  62.             gl::EnableVertexAttribArray(0);
  63.  
  64.             // normal   layout(location = 1)
  65.             gl::VertexAttribPointer(
  66.                 1,
  67.                 3,
  68.                 gl::FLOAT,
  69.                 gl::FALSE,
  70.                 stride,
  71.                 (3 * std::mem::size_of::<f32>()) as *const _,
  72.             );
  73.             gl::EnableVertexAttribArray(1);
  74.  
  75.             // texcoord layout(location = 2)
  76.             gl::VertexAttribPointer(
  77.                 2,
  78.                 2,
  79.                 gl::FLOAT,
  80.                 gl::FALSE,
  81.                 stride,
  82.                 (6 * std::mem::size_of::<f32>()) as *const _,
  83.             );
  84.             gl::EnableVertexAttribArray(2);
  85.         }
  86.  
  87.         Plane {
  88.             vao,
  89.             position: Vector3::new(pos_x, pos_y, pos_z),
  90.             rotation: Vector3::new(rot_x, rot_y, rot_z),
  91.             scale: Vector3::new(scale_x, scale_y, scale_z),
  92.             color: Vector3::new(color_r, color_g, color_b),
  93.             texture_id: None,
  94.             normal_map: None,
  95.             roughness_map: None,
  96.         }
  97.     }
  98.  
  99.     pub fn normal_map(&self) -> Option<u32> {
  100.         self.normal_map
  101.     }
  102.  
  103.     pub fn roughness_map(&self) -> Option<u32> {
  104.         self.roughness_map
  105.     }
  106. }
  107.  
  108. impl Renderable for Plane {
  109.     fn vao(&self) -> u32 {
  110.         self.vao
  111.     }
  112.  
  113.     fn model_matrix(&self) -> Matrix4<f32> {
  114.         let translation = Matrix4::new_translation(&self.position);
  115.  
  116.         let rot_x = Matrix4::from_euler_angles(self.rotation.x, 0.0, 0.0);
  117.         let rot_y = Matrix4::from_euler_angles(0.0, self.rotation.y, 0.0);
  118.         let rot_z = Matrix4::from_euler_angles(0.0, 0.0, self.rotation.z);
  119.  
  120.         let rotation = rot_z * rot_y * rot_x;
  121.  
  122.         let scale = Matrix4::new_nonuniform_scaling(&self.scale);
  123.  
  124.         translation * rotation * scale
  125.     }
  126.  
  127.     fn vertex_count(&self) -> i32 {
  128.         6
  129.     }
  130.  
  131.     fn color(&self) -> Vector3<f32> {
  132.         self.color
  133.     }
  134.  
  135.     fn texture(&self) -> Option<u32> {
  136.         self.texture_id
  137.     }
  138. }
  139.  
  140. impl Collidable for Plane {
  141.     fn collider(&self) -> Collider {
  142.         // X/Z half-extents as before:
  143.         let half_x = 10.0 * self.scale.x;
  144.         let half_z = 10.0 * self.scale.z;
  145.         // Give the ground a small Y thickness (e.g. 0.1 units):
  146.         let half_y = 0.1 * self.scale.y;
  147.  
  148.         let half = Vector3::new(half_x, half_y, half_z);
  149.         Collider {
  150.             min: self.position - half,
  151.             max: self.position + half,
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement