Advertisement
Guest User

Untitled

a guest
May 4th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 9.26 KB | None | 0 0
  1. use crate::{collision::*, renderable::Renderable};
  2. use nalgebra::{Matrix4, Vector3};
  3. use std::{ffi::c_void, ptr};
  4.  
  5. #[derive(Debug, Copy, Clone)]
  6. pub struct Cube {
  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 Cube {
  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.     ) -> Cube {
  25.         // let vertices: [f32; 288] = [
  26.         //     // back face
  27.         //     -0.5, -0.5, -0.5,   0.0,  0.0, -1.0,   0.0, 0.0,
  28.         //     0.5, -0.5, -0.5,   0.0,  0.0, -1.0,   tile, 0.0,
  29.         //     0.5,  0.5, -0.5,   0.0,  0.0, -1.0,   tile, tile,
  30.         //     0.5,  0.5, -0.5,   0.0,  0.0, -1.0,   tile, tile,
  31.         //     -0.5,  0.5, -0.5,   0.0,  0.0, -1.0,   0.0, tile,
  32.         //     -0.5, -0.5, -0.5,   0.0,  0.0, -1.0,   0.0, 0.0,
  33.  
  34.         //     // front face
  35.         //     -0.5, -0.5,  0.5,   0.0,  0.0,  1.0,   0.0, 0.0,
  36.         //     0.5, -0.5,  0.5,   0.0,  0.0,  1.0,   tile, 0.0,
  37.         //     0.5,  0.5,  0.5,   0.0,  0.0,  1.0,   tile, tile,
  38.         //     0.5,  0.5,  0.5,   0.0,  0.0,  1.0,   tile, tile,
  39.         //     -0.5,  0.5,  0.5,   0.0,  0.0,  1.0,   0.0, tile,
  40.         //     -0.5, -0.5,  0.5,   0.0,  0.0,  1.0,   0.0, 0.0,
  41.  
  42.         //     // left face
  43.         //     -0.5,  0.5,  0.5,  -1.0,  0.0,  0.0,   tile, 0.0,
  44.         //     -0.5,  0.5, -0.5,  -1.0,  0.0,  0.0,   tile, tile,
  45.         //     -0.5, -0.5, -0.5,  -1.0,  0.0,  0.0,   0.0, tile,
  46.         //     -0.5, -0.5, -0.5,  -1.0,  0.0,  0.0,   0.0, tile,
  47.         //     -0.5, -0.5,  0.5,  -1.0,  0.0,  0.0,   0.0, 0.0,
  48.         //     -0.5,  0.5,  0.5,  -1.0,  0.0,  0.0,   tile, 0.0,
  49.  
  50.         //     // right face
  51.         //     0.5,  0.5,  0.5,   1.0,  0.0,  0.0,   tile, 0.0,
  52.         //     0.5,  0.5, -0.5,   1.0,  0.0,  0.0,   tile, tile,
  53.         //     0.5, -0.5, -0.5,   1.0,  0.0,  0.0,   0.0, tile,
  54.         //     0.5, -0.5, -0.5,   1.0,  0.0,  0.0,   0.0, tile,
  55.         //     0.5, -0.5,  0.5,   1.0,  0.0,  0.0,   0.0, 0.0,
  56.         //     0.5,  0.5,  0.5,   1.0,  0.0,  0.0,   tile, 0.0,
  57.  
  58.         //     // bottom face
  59.         //     -0.5, -0.5, -0.5,   0.0, -1.0,  0.0,   0.0, tile,
  60.         //     0.5, -0.5, -0.5,   0.0, -1.0,  0.0,   tile, tile,
  61.         //     0.5, -0.5,  0.5,   0.0, -1.0,  0.0,   tile, 0.0,
  62.         //     0.5, -0.5,  0.5,   0.0, -1.0,  0.0,   tile, 0.0,
  63.         //     -0.5, -0.5,  0.5,   0.0, -1.0,  0.0,   0.0, 0.0,
  64.         //     -0.5, -0.5, -0.5,   0.0, -1.0,  0.0,   0.0, tile,
  65.  
  66.         //     // top face
  67.         //     -0.5,  0.5, -0.5,   0.0,  1.0,  0.0,   0.0, tile,
  68.         //     0.5,  0.5, -0.5,   0.0,  1.0,  0.0,   tile, tile,
  69.         //     0.5,  0.5,  0.5,   0.0,  1.0,  0.0,   tile, 0.0,
  70.         //     0.5,  0.5,  0.5,   0.0,  1.0,  0.0,   tile, 0.0,
  71.         //     -0.5,  0.5,  0.5,   0.0,  1.0,  0.0,   0.0, 0.0,
  72.         //     -0.5,  0.5, -0.5,   0.0,  1.0,  0.0,   0.0, tile,
  73.         // ];
  74.  
  75.         // how many times to repeat the texture along each local axis:
  76.         let repeat_x = scale_x * tile;
  77.         let repeat_y = scale_y * tile;
  78.         let repeat_z = scale_z * tile;
  79.  
  80.         // build a Vec<f32> with 36 vertices × (pos3 + norm3 + uv2) = 288 floats
  81.         let mut v: Vec<f32> = Vec::with_capacity(36 * 8);
  82.         macro_rules! push {
  83.             ($px:expr, $py:expr, $pz:expr,
  84.              $nx:expr, $ny:expr, $nz:expr,
  85.              $u:expr,  $w:expr) => {
  86.                 v.extend_from_slice(&[$px, $py, $pz, $nx, $ny, $nz, $u, $w]);
  87.             };
  88.         }
  89.  
  90.         // BACK (–Z): spans X×Y
  91.         push!(-0.5, -0.5, -0.5,  0.0,  0.0, -1.0,   0.0,        0.0      );
  92.         push!( 0.5, -0.5, -0.5,  0.0,  0.0, -1.0,   repeat_x,   0.0      );
  93.         push!( 0.5,  0.5, -0.5,  0.0,  0.0, -1.0,   repeat_x,   repeat_y );
  94.         push!( 0.5,  0.5, -0.5,  0.0,  0.0, -1.0,   repeat_x,   repeat_y );
  95.         push!(-0.5,  0.5, -0.5,  0.0,  0.0, -1.0,   0.0,        repeat_y );
  96.         push!(-0.5, -0.5, -0.5,  0.0,  0.0, -1.0,   0.0,        0.0      );
  97.  
  98.         // FRONT (+Z): spans X×Y
  99.         push!(-0.5, -0.5,  0.5,  0.0,  0.0,  1.0,   0.0,        0.0      );
  100.         push!( 0.5, -0.5,  0.5,  0.0,  0.0,  1.0,   repeat_x,   0.0      );
  101.         push!( 0.5,  0.5,  0.5,  0.0,  0.0,  1.0,   repeat_x,   repeat_y );
  102.         push!( 0.5,  0.5,  0.5,  0.0,  0.0,  1.0,   repeat_x,   repeat_y );
  103.         push!(-0.5,  0.5,  0.5,  0.0,  0.0,  1.0,   0.0,        repeat_y );
  104.         push!(-0.5, -0.5,  0.5,  0.0,  0.0,  1.0,   0.0,        0.0      );
  105.  
  106.         // LEFT (–X): spans Z×Y
  107.         push!(-0.5, -0.5,  0.5, -1.0,  0.0,  0.0,   0.0,        0.0      );
  108.         push!(-0.5,  0.5,  0.5, -1.0,  0.0,  0.0,   0.0,        repeat_y );
  109.         push!(-0.5,  0.5, -0.5, -1.0,  0.0,  0.0,   repeat_z,   repeat_y );
  110.         push!(-0.5,  0.5, -0.5, -1.0,  0.0,  0.0,   repeat_z,   repeat_y );
  111.         push!(-0.5, -0.5, -0.5, -1.0,  0.0,  0.0,   repeat_z,   0.0      );
  112.         push!(-0.5, -0.5,  0.5, -1.0,  0.0,  0.0,   0.0,        0.0      );
  113.  
  114.         // RIGHT (+X): spans Z×Y
  115.         push!( 0.5, -0.5,  0.5,  1.0,  0.0,  0.0,   0.0,        0.0      );
  116.         push!( 0.5,  0.5,  0.5,  1.0,  0.0,  0.0,   0.0,        repeat_y );
  117.         push!( 0.5,  0.5, -0.5,  1.0,  0.0,  0.0,   repeat_z,   repeat_y );
  118.         push!( 0.5,  0.5, -0.5,  1.0,  0.0,  0.0,   repeat_z,   repeat_y );
  119.         push!( 0.5, -0.5, -0.5,  1.0,  0.0,  0.0,   repeat_z,   0.0      );
  120.         push!( 0.5, -0.5,  0.5,  1.0,  0.0,  0.0,   0.0,        0.0      );
  121.  
  122.         // BOTTOM (–Y): spans X×Z
  123.         push!(-0.5, -0.5, -0.5,  0.0, -1.0,  0.0,   0.0,        repeat_z );
  124.         push!( 0.5, -0.5, -0.5,  0.0, -1.0,  0.0,   repeat_x,   repeat_z );
  125.         push!( 0.5, -0.5,  0.5,  0.0, -1.0,  0.0,   repeat_x,   0.0      );
  126.         push!( 0.5, -0.5,  0.5,  0.0, -1.0,  0.0,   repeat_x,   0.0      );
  127.         push!(-0.5, -0.5,  0.5,  0.0, -1.0,  0.0,   0.0,        0.0      );
  128.         push!(-0.5, -0.5, -0.5,  0.0, -1.0,  0.0,   0.0,        repeat_z );
  129.  
  130.         // TOP (+Y): spans X×Z
  131.         push!(-0.5,  0.5, -0.5,  0.0,  1.0,  0.0,   0.0,        repeat_z );
  132.         push!( 0.5,  0.5, -0.5,  0.0,  1.0,  0.0,   repeat_x,   repeat_z );
  133.         push!( 0.5,  0.5,  0.5,  0.0,  1.0,  0.0,   repeat_x,   0.0      );
  134.         push!( 0.5,  0.5,  0.5,  0.0,  1.0,  0.0,   repeat_x,   0.0      );
  135.         push!(-0.5,  0.5,  0.5,  0.0,  1.0,  0.0,   0.0,        0.0      );
  136.         push!(-0.5,  0.5, -0.5,  0.0,  1.0,  0.0,   0.0,        repeat_z );
  137.  
  138.         // ── Upload to GPU ────────────────────────────────────────────────────────
  139.         let (mut vao, mut vbo) = (0, 0);
  140.         unsafe {
  141.             gl::GenVertexArrays(1, &mut vao);
  142.             gl::GenBuffers(1, &mut vbo);
  143.             gl::BindVertexArray(vao);
  144.             gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
  145.             gl::BufferData(
  146.                 gl::ARRAY_BUFFER,
  147.                 (v.len() * std::mem::size_of::<f32>()) as isize,
  148.                 v.as_ptr() as *const c_void,
  149.                 gl::STATIC_DRAW,
  150.             );
  151.             // pos (loc=0)
  152.             gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, 8 * 4, ptr::null());
  153.             gl::EnableVertexAttribArray(0);
  154.             // normal (loc=1)
  155.             gl::VertexAttribPointer(1, 3, gl::FLOAT, gl::FALSE, 8 * 4, (3 * 4) as *const _);
  156.             gl::EnableVertexAttribArray(1);
  157.             // texcoord (loc=2)
  158.             gl::VertexAttribPointer(2, 2, gl::FLOAT, gl::FALSE, 8 * 4, (6 * 4) as *const _);
  159.             gl::EnableVertexAttribArray(2);
  160.         }
  161.  
  162.         Cube {
  163.             vao,
  164.             position: Vector3::new(pos_x, pos_y, pos_z),
  165.             rotation: Vector3::new(rot_x, rot_y, rot_z),
  166.             scale: Vector3::new(scale_x, scale_y, scale_z),
  167.             color: Vector3::new(color_r, color_g, color_b),
  168.             texture_id: None,
  169.             normal_map: None,
  170.             roughness_map: None,
  171.         }
  172.     }
  173.  
  174.     pub fn normal_map(&self) -> Option<u32> {
  175.         self.normal_map
  176.     }
  177.  
  178.     pub fn roughness_map(&self) -> Option<u32> {
  179.         self.roughness_map
  180.     }
  181. }
  182.  
  183. impl Renderable for Cube {
  184.     fn vao(&self) -> u32               { self.vao }
  185.     fn model_matrix(&self) -> Matrix4<f32> {
  186.         let t = Matrix4::new_translation(&self.position);
  187.         let rx = Matrix4::from_euler_angles(self.rotation.x, 0.0, 0.0);
  188.         let ry = Matrix4::from_euler_angles(0.0, self.rotation.y, 0.0);
  189.         let rz = Matrix4::from_euler_angles(0.0, 0.0, self.rotation.z);
  190.         let s  = Matrix4::new_nonuniform_scaling(&self.scale);
  191.         t * rz * ry * rx * s
  192.     }
  193.     fn vertex_count(&self) -> i32      { 36 }
  194.     fn color(&self) -> Vector3<f32>   { self.color }
  195.     fn texture(&self) -> Option<u32>  { self.texture_id }
  196. }
  197.  
  198. impl Collidable for Cube {
  199.     fn collider(&self) -> Collider {
  200.         let half = self.scale / 2.0;
  201.         Collider { min: self.position - half, max: self.position + half }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement