Transform

This commit is contained in:
Piotr Siuszko 2020-09-24 16:19:53 +02:00
parent 23aae0fa52
commit d778df90c3
6 changed files with 64 additions and 18 deletions

View File

@ -1,29 +1,30 @@
use crate::gaia::camera::*;
use crate::gaia::consts;
use crate::gaia::{consts,macros};
use crate::gaia::*;
#[macro_use]
use imgui_glfw_rs::glfw;
use crate::gaia::macros;
use crate::gaia::components::{Transform,ModelComponent};
use crate::gaia::engine::Engine;
use crate::gaia::client::Client;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3};
macro_rules! c_str {
($literal:expr) => {
std::ffi::CStr::from_bytes_with_nul_unchecked(concat!($literal, "\0").as_bytes())
};
}
pub struct ExampleClient {
model: model::Model,
model: ModelComponent,
camera: Camera,
shader: shader::Shader,
}
impl ExampleClient {
pub fn create() -> ExampleClient {
ExampleClient {
let mut t = Transform::default();
t.position = vec3(3.0, -1.75, -1.25);
t.scale = vec3(0.2,0.1,0.2);
let model = ModelComponent {
transform: t,
model: model::Model::new("resources/objects/nanosuit/nanosuit.obj"),
};
ExampleClient {
model: model,
camera: Camera {
position: Point3::new(0.0, 0.0, 3.0),
..Camera::default()
@ -51,10 +52,7 @@ impl Client for ExampleClient {
self.shader.setMat4(c_str!("projection"), &projection);
self.shader.setMat4(c_str!("view"), &view);
let mut m = Matrix4::<f32>::from_translation(vec3(3.0, -1.75, -1.25)); // translate it down so it's at the center of the scene
m = m * Matrix4::from_scale(0.2); // it's a bit too big for our scene, so scale it down
self.shader.setMat4(c_str!("model"), &m);
self.model.Draw(&self.shader);
self.model.draw(&self.shader);
}
fn update(&mut self, engine: &mut Engine) {

44
src/gaia/components.rs Normal file
View File

@ -0,0 +1,44 @@
use crate::gaia::*;
use crate::gaia::macros;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3};
use crate::gaia::model::Model;
use crate::gaia::shader::Shader;
pub struct Transform {
pub position: Vector3<f32>,
pub scale: Vector3<f32>,
pub rotation: Vector3<f32>,
}
impl Default for Transform {
fn default() -> Transform {
Transform {
position: vec3(0.0,0.0,0.0),
scale: vec3(1.0,1.0,1.0),
rotation: vec3(0.0,0.0,0.0),
}
}
}
impl Transform {
pub fn get_matrix(&self) -> Matrix4<f32> {
let mut m = Matrix4::<f32>::from_translation(self.position);
m = m * Matrix4::from_nonuniform_scale(self.scale.x,self.scale.y,self.scale.z);
m
}
}
pub struct ModelComponent {
pub model: Model,
pub transform: Transform,
}
impl ModelComponent {
pub unsafe fn draw(&mut self, shader: &Shader ) {
let matrix = self.transform.get_matrix();
shader.setMat4(c_str!("model"), &matrix);
self.model.Draw(&shader);
}
}

View File

@ -65,12 +65,13 @@ impl Engine {
{
use imgui::*;
let fps = 1.0 / delta_time;
let mut info = self.bg_info;
Window::new(im_str!("Hello world"))
.size([300.0, 110.0], Condition::FirstUseEver)
.build(&ui, || {
ui.text(im_str!("Hello world!"));
ui.text(im_str!("こんにちは世界!"));
ui.text(format!("{:.0}", fps));
ui.text(im_str!("This...is...imgui-rs!"));
ui.separator();
ui.text(format!("Mouse position: ({:.1},{:.1})", last_x, last_y));

View File

@ -1,3 +1,5 @@
#![macro_use]
/// Macro to get c strings from literals without runtime overhead
/// Literal must not contain any interior nul bytes!
macro_rules! c_str {

View File

@ -1,9 +1,9 @@
pub mod macros;
pub mod camera;
pub mod components;
pub mod client;
pub mod consts;
pub mod utils;
#[macro_use]
pub mod macros;
pub mod bg_info;
pub mod engine;
pub mod mesh;

View File

@ -4,6 +4,7 @@ extern crate gl;
extern crate image;
extern crate imgui_glfw_rs;
use human_panic::setup_panic;
#[macro_use]
mod gaia;
mod example_client;
use crate::gaia::engine::Engine;