runs, however i do not have the correct formula for calculating the
finalmodelObjects
parent
7eb4ece49f
commit
f5253f16e8
40
src/model.rs
40
src/model.rs
|
@ -13,6 +13,8 @@ use tobj;
|
||||||
mod mesh;
|
mod mesh;
|
||||||
use mesh::{ Mesh, Texture, Vertex };
|
use mesh::{ Mesh, Texture, Vertex };
|
||||||
use crate::shader::shader;
|
use crate::shader::shader;
|
||||||
|
use crate::scene::objects::Transform;
|
||||||
|
use crate::camera::Camera;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
|
@ -29,13 +31,47 @@ impl Model {
|
||||||
model.loadModel(path, Arc::clone(&gl));
|
model.loadModel(path, Arc::clone(&gl));
|
||||||
model
|
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) {
|
// run all meshes as normal
|
||||||
for mesh in &self.meshes {
|
if !range.contains(&(i as i32))
|
||||||
|
{
|
||||||
|
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); }
|
unsafe { mesh.Draw(shader); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// loads a model from file and stores the resulting meshes in the meshes vector.
|
// loads a model from file and stores the resulting meshes in the meshes vector.
|
||||||
fn loadModel(&mut self, path: &str, gl: Arc<glow::Context>) {
|
fn loadModel(&mut self, path: &str, gl: Arc<glow::Context>) {
|
||||||
|
|
|
@ -11,6 +11,7 @@ use cgmath::prelude::*;
|
||||||
use glow::*;
|
use glow::*;
|
||||||
use field_offset::offset_of;
|
use field_offset::offset_of;
|
||||||
|
|
||||||
|
use crate::scene::objects;
|
||||||
use crate::shader::shader;
|
use crate::shader::shader;
|
||||||
// NOTE: without repr(C) the compiler may reorder the fields or use different padding/alignment than C.
|
// 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
|
// Depending on how you pass the data to OpenGL, this may be bad. In this case it's not strictly
|
||||||
|
|
|
@ -15,6 +15,8 @@ pub struct Input {
|
||||||
pub Right: bool
|
pub Right: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO make a struct to pass info
|
||||||
|
|
||||||
impl Default for Input {
|
impl Default for Input {
|
||||||
fn default() -> Input {
|
fn default() -> Input {
|
||||||
Input {
|
Input {
|
||||||
|
@ -103,6 +105,7 @@ impl Player {
|
||||||
{
|
{
|
||||||
shader.Use();
|
shader.Use();
|
||||||
|
|
||||||
|
|
||||||
let translation = cgmath::Matrix4::from_translation(self.Transform.Position);
|
let translation = cgmath::Matrix4::from_translation(self.Transform.Position);
|
||||||
let rotation = cgmath::Matrix4::from(self.Transform.Rotation);
|
let rotation = cgmath::Matrix4::from(self.Transform.Rotation);
|
||||||
let scale = cgmath::Matrix4::from_scale(self.Transform.Scale);
|
let scale = cgmath::Matrix4::from_scale(self.Transform.Scale);
|
||||||
|
@ -115,7 +118,14 @@ impl Player {
|
||||||
|
|
||||||
shader.setMat4("model", &model);
|
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 {
|
unsafe {
|
||||||
shader.Use();
|
shader.Use();
|
||||||
|
|
||||||
|
|
||||||
let model = cgmath::Matrix4::from_angle_x(cgmath::Deg(-90.0));
|
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 projection = cgmath::perspective(cgmath::Deg(camera.Zoom), SCR_WIDTH as f32 /SCR_HEIGHT as f32, 0.1, 100.0);
|
||||||
let view = camera.GetViewMatrix();
|
let view = camera.GetViewMatrix();
|
||||||
|
|
Loading…
Reference in New Issue