Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use crate::{collision::{Collidable, Collider}, renderable::Renderable};
- use nalgebra::{Matrix4, Vector3};
- use std::ffi::c_void;
- #[derive(Debug, Copy, Clone)]
- pub struct Plane {
- pub vao: u32,
- pub position: Vector3<f32>,
- pub rotation: Vector3<f32>,
- pub scale: Vector3<f32>,
- pub color: Vector3<f32>,
- pub texture_id: Option<u32>,
- pub normal_map: Option<u32>,
- pub roughness_map: Option<u32>,
- }
- impl Plane {
- pub fn new(
- pos_x: f32, pos_y: f32, pos_z: f32,
- rot_x: f32, rot_y: f32, rot_z: f32,
- scale_x: f32, scale_y: f32, scale_z: f32,
- color_r: f32, color_g: f32, color_b: f32,
- tile: f32,
- ) -> Plane {
- // position (3) + normal (3) + texcoord (2) = 8 floats per vertex
- let vertices: [f32; 48] = [
- // x y z nx ny nz u v
- -10.0, 0.0, -10.0, 0.0, 1.0, 0.0, 0.0, 0.0,
- 10.0, 0.0, -10.0, 0.0, 1.0, 0.0, tile, 0.0,
- -10.0, 0.0, 10.0, 0.0, 1.0, 0.0, 0.0, tile,
- 10.0, 0.0, -10.0, 0.0, 1.0, 0.0, tile, 0.0,
- 10.0, 0.0, 10.0, 0.0, 1.0, 0.0, tile, tile,
- -10.0, 0.0, 10.0, 0.0, 1.0, 0.0, 0.0, tile,
- ];
- let (mut vao, mut vbo) = (0, 0);
- unsafe {
- gl::GenVertexArrays(1, &mut vao);
- gl::GenBuffers(1, &mut vbo);
- gl::BindVertexArray(vao);
- gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
- gl::BufferData(
- gl::ARRAY_BUFFER,
- (vertices.len() * std::mem::size_of::<f32>()) as isize,
- vertices.as_ptr() as *const _,
- gl::STATIC_DRAW,
- );
- let stride = (8 * std::mem::size_of::<f32>()) as i32;
- // position layout(location = 0)
- gl::VertexAttribPointer(
- 0,
- 3,
- gl::FLOAT,
- gl::FALSE,
- stride,
- std::ptr::null(),
- );
- gl::EnableVertexAttribArray(0);
- // normal layout(location = 1)
- gl::VertexAttribPointer(
- 1,
- 3,
- gl::FLOAT,
- gl::FALSE,
- stride,
- (3 * std::mem::size_of::<f32>()) as *const _,
- );
- gl::EnableVertexAttribArray(1);
- // texcoord layout(location = 2)
- gl::VertexAttribPointer(
- 2,
- 2,
- gl::FLOAT,
- gl::FALSE,
- stride,
- (6 * std::mem::size_of::<f32>()) as *const _,
- );
- gl::EnableVertexAttribArray(2);
- }
- Plane {
- vao,
- position: Vector3::new(pos_x, pos_y, pos_z),
- rotation: Vector3::new(rot_x, rot_y, rot_z),
- scale: Vector3::new(scale_x, scale_y, scale_z),
- color: Vector3::new(color_r, color_g, color_b),
- texture_id: None,
- normal_map: None,
- roughness_map: None,
- }
- }
- pub fn normal_map(&self) -> Option<u32> {
- self.normal_map
- }
- pub fn roughness_map(&self) -> Option<u32> {
- self.roughness_map
- }
- }
- impl Renderable for Plane {
- fn vao(&self) -> u32 {
- self.vao
- }
- fn model_matrix(&self) -> Matrix4<f32> {
- let translation = Matrix4::new_translation(&self.position);
- let rot_x = Matrix4::from_euler_angles(self.rotation.x, 0.0, 0.0);
- let rot_y = Matrix4::from_euler_angles(0.0, self.rotation.y, 0.0);
- let rot_z = Matrix4::from_euler_angles(0.0, 0.0, self.rotation.z);
- let rotation = rot_z * rot_y * rot_x;
- let scale = Matrix4::new_nonuniform_scaling(&self.scale);
- translation * rotation * scale
- }
- fn vertex_count(&self) -> i32 {
- 6
- }
- fn color(&self) -> Vector3<f32> {
- self.color
- }
- fn texture(&self) -> Option<u32> {
- self.texture_id
- }
- }
- impl Collidable for Plane {
- fn collider(&self) -> Collider {
- // X/Z half-extents as before:
- let half_x = 10.0 * self.scale.x;
- let half_z = 10.0 * self.scale.z;
- // Give the ground a small Y thickness (e.g. 0.1 units):
- let half_y = 0.1 * self.scale.y;
- let half = Vector3::new(half_x, half_y, half_z);
- Collider {
- min: self.position - half,
- max: self.position + half,
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement