From f5253f16e8f7a6cb5ec2c38122ec039f5e085202 Mon Sep 17 00:00:00 2001 From: Melik Houij Date: Sat, 1 Apr 2023 18:41:26 +0200 Subject: [PATCH] runs, however i do not have the correct formula for calculating the finalmodel --- src/model.rs | 42 +++++++++++++++++++++++++++++++++++++++--- src/model/mesh.rs | 1 + src/scene/objects.rs | 13 +++++++++++-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/model.rs b/src/model.rs index c4c9a7a..9082faa 100644 --- a/src/model.rs +++ b/src/model.rs @@ -13,6 +13,8 @@ use tobj; mod mesh; use mesh::{ Mesh, Texture, Vertex }; use crate::shader::shader; +use crate::scene::objects::Transform; +use crate::camera::Camera; #[derive(Default)] pub struct Model { @@ -29,11 +31,45 @@ impl Model { model.loadModel(path, Arc::clone(&gl)); model } +/// NOTE problem with this implementation is that only one range and transform could be specified + pub fn Draw(&self, shader: &shader, opt: Option<(std::ops::Range,Transform, &Camera, cgmath::Matrix4 )>) { + if opt.is_some() + { + let (range, transform, camera, model) = opt.unwrap(); + for (i, mesh) in self.meshes.iter().enumerate() { - pub fn Draw(&self, shader: &shader) { - for mesh in &self.meshes { + // run all meshes as normal + if !range.contains(&(i as i32)) + { + unsafe {mesh.Draw(shader); } + } - unsafe { mesh.Draw(shader); } + } + // run the special Transforms for the meshes specified + + let translation = cgmath::Matrix4::from_translation(transform.Position); + let rotation = cgmath::Matrix4::from(transform.Rotation); + let scale = cgmath::Matrix4::from_scale(transform.Scale); + let finalmod = model * (translation * rotation * scale); + + let projection = cgmath::perspective(cgmath::Deg(camera.Zoom), 1600.0 /900.0, 0.1, 100.0); + let view = camera.GetViewMatrix(); + shader.Use(); + shader.setMat4("projection", &projection); + shader.setMat4("view", &view); + shader.setMat4("model", &finalmod); + + for i in range + { + + unsafe {self.meshes[i as usize].Draw(shader)} + } + } else { + { + for (i, mesh) in self.meshes.iter().enumerate() { + unsafe { mesh.Draw(shader); } + } + } } } diff --git a/src/model/mesh.rs b/src/model/mesh.rs index 37727da..b00f012 100644 --- a/src/model/mesh.rs +++ b/src/model/mesh.rs @@ -11,6 +11,7 @@ use cgmath::prelude::*; use glow::*; use field_offset::offset_of; +use crate::scene::objects; use crate::shader::shader; // NOTE: without repr(C) the compiler may reorder the fields or use different padding/alignment than C. // Depending on how you pass the data to OpenGL, this may be bad. In this case it's not strictly diff --git a/src/scene/objects.rs b/src/scene/objects.rs index 95ebcaa..47a7a37 100644 --- a/src/scene/objects.rs +++ b/src/scene/objects.rs @@ -15,6 +15,8 @@ pub struct Input { pub Right: bool } +// TODO make a struct to pass info + impl Default for Input { fn default() -> Input { Input { @@ -103,6 +105,7 @@ impl Player { { shader.Use(); + let translation = cgmath::Matrix4::from_translation(self.Transform.Position); let rotation = cgmath::Matrix4::from(self.Transform.Rotation); let scale = cgmath::Matrix4::from_scale(self.Transform.Scale); @@ -115,7 +118,14 @@ impl Player { shader.setMat4("model", &model); - self.Model.Draw(shader); + let turned = Transform { + Scale: 1.0, + ..Transform::default() + }; + + + + self.Model.Draw(shader, Some((1..3, turned, camera, model))); } } @@ -184,7 +194,6 @@ impl plane { unsafe { shader.Use(); - let model = cgmath::Matrix4::from_angle_x(cgmath::Deg(-90.0)); let projection = cgmath::perspective(cgmath::Deg(camera.Zoom), SCR_WIDTH as f32 /SCR_HEIGHT as f32, 0.1, 100.0); let view = camera.GetViewMatrix();