runs, however i do not have the correct formula for calculating the

finalmodel
Objects
Milk.H 2023-04-01 18:41:26 +02:00
parent 7eb4ece49f
commit f5253f16e8
No known key found for this signature in database
GPG Key ID: 3D9DAE46AAC37BD8
3 changed files with 51 additions and 5 deletions

View File

@ -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<i32>,Transform, &Camera, cgmath::Matrix4<f32> )>) {
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); }
}
}
}
}

View File

@ -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

View File

@ -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();