Code cleanup

This commit is contained in:
Piotr Siuszko 2020-09-24 17:42:39 +02:00
parent 6462867440
commit bfeee3dce4
12 changed files with 212 additions and 216 deletions

View File

@ -1,12 +1,11 @@
use crate::gaia::camera::*; use crate::gaia::camera::*;
use crate::gaia::{consts};
use crate::gaia::*;
use imgui_glfw_rs::glfw;
use crate::gaia::components::{Transform,ModelComponent};
use crate::gaia::engine::Engine;
use crate::gaia::client::Client; use crate::gaia::client::Client;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3}; use crate::gaia::components::{ModelComponent, Transform};
use crate::gaia::consts;
use crate::gaia::engine::Engine;
use crate::gaia::*;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3};
use imgui_glfw_rs::glfw;
pub struct ExampleClient { pub struct ExampleClient {
model: ModelComponent, model: ModelComponent,
@ -18,7 +17,7 @@ impl ExampleClient {
pub fn create() -> ExampleClient { pub fn create() -> ExampleClient {
let mut t = Transform::default(); let mut t = Transform::default();
t.position = vec3(3.0, -1.75, -1.25); t.position = vec3(3.0, -1.75, -1.25);
t.scale = vec3(0.2,0.1,0.2); t.scale = vec3(0.2, 0.1, 0.2);
let model = ModelComponent { let model = ModelComponent {
transform: t, transform: t,
model: model::Model::new("resources/objects/nanosuit/nanosuit.obj"), model: model::Model::new("resources/objects/nanosuit/nanosuit.obj"),
@ -39,51 +38,44 @@ impl ExampleClient {
impl Client for ExampleClient { impl Client for ExampleClient {
unsafe fn draw(&mut self) { unsafe fn draw(&mut self) {
self.shader.useProgram(); self.shader.use_program();
// view/projection transformations // view/projection transformations
let projection: Matrix4<f32> = perspective( let projection: Matrix4<f32> = perspective(
Deg(self.camera.Zoom), Deg(self.camera.zoom),
consts::SCR_WIDTH as f32 / consts::SCR_HEIGHT as f32, consts::SCR_WIDTH as f32 / consts::SCR_HEIGHT as f32,
0.1, 0.1,
100.0, 100.0,
); );
let view = self.camera.get_view_matrix(); let view = self.camera.get_view_matrix();
self.shader.setMat4(c_str!("projection"), &projection); self.shader.set_mat4(c_str!("projection"), &projection);
self.shader.setMat4(c_str!("view"), &view); self.shader.set_mat4(c_str!("view"), &view);
self.model.draw(&self.shader); self.model.draw(&self.shader);
} }
fn update(&mut self, engine: &mut Engine) { fn update(&mut self, _engine: &mut Engine) {}
}
fn process_input(&mut self, window: &glfw::Window, delta: f32) { fn process_input(&mut self, window: &glfw::Window, delta: f32) {
use imgui_glfw_rs::glfw::{Action, Key}; use imgui_glfw_rs::glfw::{Action, Key};
if window.get_key(Key::W) == Action::Press { if window.get_key(Key::W) == Action::Press {
self.camera self.camera.process_keyboard(CameraMovement::FORWARD, delta);
.process_keyboard(Camera_Movement::FORWARD, delta);
} }
if window.get_key(Key::S) == Action::Press { if window.get_key(Key::S) == Action::Press {
self.camera self.camera
.process_keyboard(Camera_Movement::BACKWARD, delta); .process_keyboard(CameraMovement::BACKWARD, delta);
} }
if window.get_key(Key::A) == Action::Press { if window.get_key(Key::A) == Action::Press {
self.camera self.camera.process_keyboard(CameraMovement::LEFT, delta);
.process_keyboard(Camera_Movement::LEFT, delta);
} }
if window.get_key(Key::D) == Action::Press { if window.get_key(Key::D) == Action::Press {
self.camera self.camera.process_keyboard(CameraMovement::RIGHT, delta);
.process_keyboard(Camera_Movement::RIGHT, delta);
} }
self.camera self.camera
.enable_mouse_movement(window.get_key(Key::LeftControl) != Action::Press); .enable_mouse_movement(window.get_key(Key::LeftControl) != Action::Press);
} }
fn debug_draw(&mut self, ui: &imgui_glfw_rs::imgui::Ui) { fn debug_draw(&mut self, ui: &imgui_glfw_rs::imgui::Ui) {
use imgui_glfw_rs::imgui; use imgui_glfw_rs::imgui::*;
use imgui::*;
use imgui_inspect::InspectArgsStruct; use imgui_inspect::InspectArgsStruct;
Window::new(im_str!("Object info")) Window::new(im_str!("Object info"))
.size([250.0, 250.0], Condition::FirstUseEver) .size([250.0, 250.0], Condition::FirstUseEver)
@ -98,10 +90,10 @@ impl Client for ExampleClient {
}); });
} }
fn on_mouse_scroll(&mut self, yoffset: f32){ fn on_mouse_scroll(&mut self, yoffset: f32) {
self.camera.process_mouse_scroll(yoffset as f32); self.camera.process_mouse_scroll(yoffset as f32);
} }
fn on_mouse_move(&mut self, x: f32, y: f32){ fn on_mouse_move(&mut self, x: f32, y: f32) {
self.camera.process_mouse_movement(x, y, true); self.camera.process_mouse_movement(x, y, true);
} }
} }

View File

@ -1,5 +1,3 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)] #![allow(dead_code)]
use cgmath; use cgmath;
@ -12,13 +10,13 @@ type Matrix4 = cgmath::Matrix4<f32>;
// Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods // Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods
#[derive(PartialEq, Clone, Copy)] #[derive(PartialEq, Clone, Copy)]
pub enum Camera_Movement { pub enum CameraMovement {
FORWARD, FORWARD,
BACKWARD, BACKWARD,
LEFT, LEFT,
RIGHT, RIGHT,
} }
use self::Camera_Movement::*; use self::CameraMovement::*;
// Default camera values // Default camera values
const YAW: f32 = -90.0; const YAW: f32 = -90.0;
@ -30,32 +28,32 @@ const ZOOM: f32 = 45.0;
pub struct Camera { pub struct Camera {
// Camera Attributes // Camera Attributes
pub position: Point3, pub position: Point3,
pub Front: Vector3, pub front: Vector3,
pub Up: Vector3, pub up: Vector3,
pub Right: Vector3, pub right: Vector3,
pub WorldUp: Vector3, pub worldup: Vector3,
// Euler Angles // Euler Angles
pub Yaw: f32, pub yaw: f32,
pub Pitch: f32, pub pitch: f32,
// Camera options // Camera options
pub MovementSpeed: f32, pub movement_speed: f32,
pub MouseSensitivity: f32, pub mouse_sensivity: f32,
pub Zoom: f32, pub zoom: f32,
} }
impl Default for Camera { impl Default for Camera {
fn default() -> Camera { fn default() -> Camera {
let mut camera = Camera { let mut camera = Camera {
position: Point3::new(0.0, 0.0, 0.0), position: Point3::new(0.0, 0.0, 0.0),
Front: vec3(0.0, 0.0, -1.0), front: vec3(0.0, 0.0, -1.0),
Up: Vector3::zero(), // initialized later up: Vector3::zero(), // initialized later
Right: Vector3::zero(), // initialized later right: Vector3::zero(), // initialized later
WorldUp: Vector3::unit_y(), worldup: Vector3::unit_y(),
Yaw: YAW, yaw: YAW,
Pitch: PITCH, pitch: PITCH,
MovementSpeed: SPEED, movement_speed: SPEED,
MouseSensitivity: SENSITIVTY, mouse_sensivity: SENSITIVTY,
Zoom: ZOOM, zoom: ZOOM,
}; };
camera.update_camera_vectors(); camera.update_camera_vectors();
camera camera
@ -65,23 +63,23 @@ impl Default for Camera {
impl Camera { impl Camera {
/// Returns the view matrix calculated using Eular Angles and the LookAt Matrix /// Returns the view matrix calculated using Eular Angles and the LookAt Matrix
pub fn get_view_matrix(&self) -> Matrix4 { pub fn get_view_matrix(&self) -> Matrix4 {
Matrix4::look_at(self.position, self.position + self.Front, self.Up) Matrix4::look_at(self.position, self.position + self.front, self.up)
} }
/// Processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems) /// Processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
pub fn process_keyboard(&mut self, direction: Camera_Movement, deltaTime: f32) { pub fn process_keyboard(&mut self, direction: CameraMovement, delta_time: f32) {
let velocity = self.MovementSpeed * deltaTime; let velocity = self.movement_speed * delta_time;
if direction == FORWARD { if direction == FORWARD {
self.position += self.Front * velocity; self.position += self.front * velocity;
} }
if direction == BACKWARD { if direction == BACKWARD {
self.position += -(self.Front * velocity); self.position += -(self.front * velocity);
} }
if direction == LEFT { if direction == LEFT {
self.position += -(self.Right * velocity); self.position += -(self.right * velocity);
} }
if direction == RIGHT { if direction == RIGHT {
self.position += self.Right * velocity; self.position += self.right * velocity;
} }
} }
@ -90,56 +88,56 @@ impl Camera {
&mut self, &mut self,
mut xoffset: f32, mut xoffset: f32,
mut yoffset: f32, mut yoffset: f32,
constrainPitch: bool, constrainpitch: bool,
) { ) {
xoffset *= self.MouseSensitivity; xoffset *= self.mouse_sensivity;
yoffset *= self.MouseSensitivity; yoffset *= self.mouse_sensivity;
self.Yaw += xoffset; self.yaw += xoffset;
self.Pitch += yoffset; self.pitch += yoffset;
// Make sure that when pitch is out of bounds, screen doesn't get flipped // Make sure that when pitch is out of bounds, screen doesn't get flipped
if constrainPitch { if constrainpitch {
if self.Pitch > 89.0 { if self.pitch > 89.0 {
self.Pitch = 89.0; self.pitch = 89.0;
} }
if self.Pitch < -89.0 { if self.pitch < -89.0 {
self.Pitch = -89.0; self.pitch = -89.0;
} }
} }
// Update Front, Right and Up Vectors using the updated Eular angles // update front, right and up Vectors using the updated Eular angles
self.update_camera_vectors(); self.update_camera_vectors();
} }
// Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis // Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
pub fn process_mouse_scroll(&mut self, yoffset: f32) { pub fn process_mouse_scroll(&mut self, yoffset: f32) {
if self.Zoom >= 1.0 && self.Zoom <= 45.0 { if self.zoom >= 1.0 && self.zoom <= 45.0 {
self.Zoom -= yoffset; self.zoom -= yoffset;
} }
if self.Zoom <= 1.0 { if self.zoom <= 1.0 {
self.Zoom = 1.0; self.zoom = 1.0;
} }
if self.Zoom >= 45.0 { if self.zoom >= 45.0 {
self.Zoom = 45.0; self.zoom = 45.0;
} }
} }
/// Calculates the front vector from the Camera's (updated) Eular Angles /// Calculates the front vector from the Camera's (updated) Eular Angles
fn update_camera_vectors(&mut self) { fn update_camera_vectors(&mut self) {
// Calculate the new Front vector // Calculate the new front vector
let front = Vector3 { let front = Vector3 {
x: self.Yaw.to_radians().cos() * self.Pitch.to_radians().cos(), x: self.yaw.to_radians().cos() * self.pitch.to_radians().cos(),
y: self.Pitch.to_radians().sin(), y: self.pitch.to_radians().sin(),
z: self.Yaw.to_radians().sin() * self.Pitch.to_radians().cos(), z: self.yaw.to_radians().sin() * self.pitch.to_radians().cos(),
}; };
self.Front = front.normalize(); self.front = front.normalize();
// Also re-calculate the Right and Up vector // Also re-calculate the right and up vector
self.Right = self.Front.cross(self.WorldUp).normalize(); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement. self.right = self.front.cross(self.worldup).normalize(); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
self.Up = self.Right.cross(self.Front).normalize(); self.up = self.right.cross(self.front).normalize();
} }
pub fn enable_mouse_movement(&mut self, enable: bool) { pub fn enable_mouse_movement(&mut self, enable: bool) {
self.MouseSensitivity = if enable { SENSITIVTY } else { 0.0 }; self.mouse_sensivity = if enable { SENSITIVTY } else { 0.0 };
} }
} }

View File

@ -1,37 +1,48 @@
use imgui_inspect::InspectArgsDefault;
use imgui_inspect::InspectRenderDefault;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3};
use crate::gaia::model::Model; use crate::gaia::model::Model;
use crate::gaia::shader::Shader; use crate::gaia::shader::Shader;
use cgmath::{vec3, Matrix4, Vector3};
use imgui_glfw_rs::imgui; use imgui_glfw_rs::imgui;
use imgui_inspect::*; use imgui_inspect::InspectArgsDefault;
use imgui_inspect::InspectRenderDefault;
use imgui_inspect_derive::Inspect; use imgui_inspect_derive::Inspect;
use imgui_inspect::InspectArgsStruct;
struct cgmathVec3f32; struct CgmathVec3f32;
impl InspectRenderDefault<Vector3<f32>> for cgmathVec3f32 { impl InspectRenderDefault<Vector3<f32>> for CgmathVec3f32 {
fn render(data: &[&Vector3<f32>], label: &'static str, ui: &imgui::Ui, args: &InspectArgsDefault) { fn render(
data: &[&Vector3<f32>],
label: &'static str,
ui: &imgui::Ui,
_args: &InspectArgsDefault,
) {
ui.text(label); ui.text(label);
ui.text(format!("{:?}", data)); ui.text(format!("{:?}", data));
} }
fn render_mut(data: &mut [&mut Vector3<f32>], label: &'static str, ui: &imgui::Ui, args: &InspectArgsDefault) -> bool { fn render_mut(
data: &mut [&mut Vector3<f32>],
label: &'static str,
ui: &imgui::Ui,
_args: &InspectArgsDefault,
) -> bool {
use imgui::*; use imgui::*;
ui.text(label); ui.text(label);
let mut change = false; let mut change = false;
for el in data.iter_mut() { for el in data.iter_mut() {
change |= ui.input_float(im_str!("x"), &mut el.x) change |= ui
.input_float(im_str!("x"), &mut el.x)
.step(0.01)
.step_fast(1.0)
.build();
change |= ui
.input_float(im_str!("y"), &mut el.y)
.step(0.01)
.step_fast(1.0)
.build();
change |= ui
.input_float(im_str!("z"), &mut el.z)
.step(0.01) .step(0.01)
.step_fast(1.0) .step_fast(1.0)
.build(); .build();
change |= ui.input_float(im_str!("y"), &mut el.y)
.step(0.01)
.step_fast(1.0)
.build();
change |= ui.input_float(im_str!("z"), &mut el.z)
.step(0.01)
.step_fast(1.0)
.build();
} }
change change
} }
@ -39,20 +50,20 @@ impl InspectRenderDefault<Vector3<f32>> for cgmathVec3f32 {
#[derive(Inspect, Clone)] #[derive(Inspect, Clone)]
pub struct Transform { pub struct Transform {
#[inspect(proxy_type = "cgmathVec3f32")] #[inspect(proxy_type = "CgmathVec3f32")]
pub position: Vector3<f32>, pub position: Vector3<f32>,
#[inspect(proxy_type = "cgmathVec3f32")] #[inspect(proxy_type = "CgmathVec3f32")]
pub scale: Vector3<f32>, pub scale: Vector3<f32>,
#[inspect(proxy_type = "cgmathVec3f32")] #[inspect(proxy_type = "CgmathVec3f32")]
pub rotation: Vector3<f32>, pub rotation: Vector3<f32>,
} }
impl Default for Transform { impl Default for Transform {
fn default() -> Transform { fn default() -> Transform {
Transform { Transform {
position: vec3(0.0,0.0,0.0), position: vec3(0.0, 0.0, 0.0),
scale: vec3(1.0,1.0,1.0), scale: vec3(1.0, 1.0, 1.0),
rotation: vec3(0.0,0.0,0.0), rotation: vec3(0.0, 0.0, 0.0),
} }
} }
} }
@ -60,7 +71,7 @@ impl Default for Transform {
impl Transform { impl Transform {
pub fn get_matrix(&self) -> Matrix4<f32> { pub fn get_matrix(&self) -> Matrix4<f32> {
let mut m = Matrix4::<f32>::from_translation(self.position); 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 = m * Matrix4::from_nonuniform_scale(self.scale.x, self.scale.y, self.scale.z);
m m
} }
@ -72,10 +83,10 @@ pub struct ModelComponent {
} }
impl ModelComponent { impl ModelComponent {
pub unsafe fn draw(&mut self, shader: &Shader ) { pub unsafe fn draw(&mut self, shader: &Shader) {
let matrix = self.transform.get_matrix(); let matrix = self.transform.get_matrix();
shader.setMat4(c_str!("model"), &matrix); shader.set_mat4(c_str!("model"), &matrix);
self.model.Draw(&shader); self.model.Draw(&shader);
} }
} }

View File

@ -1,3 +1,5 @@
#![allow(dead_code)]
// settings // settings
pub const SCR_WIDTH: u32 = 1280; pub const SCR_WIDTH: u32 = 1280;
pub const SCR_HEIGHT: u32 = 720; pub const SCR_HEIGHT: u32 = 720;

View File

@ -1,10 +1,9 @@
use crate::example_client::ExampleClient;
use crate::gaia::bg_info::BgInfo; use crate::gaia::bg_info::BgInfo;
use crate::gaia::camera::*; use crate::gaia::camera::*;
use crate::gaia::consts;
use crate::gaia::*;
use crate::example_client::ExampleClient;
use crate::gaia::client::Client; use crate::gaia::client::Client;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3}; use crate::gaia::consts;
use cgmath::Point3;
use imgui_glfw_rs::glfw; use imgui_glfw_rs::glfw;
use imgui_glfw_rs::glfw::{Action, Context, Key}; use imgui_glfw_rs::glfw::{Action, Context, Key};
use imgui_glfw_rs::imgui; use imgui_glfw_rs::imgui;
@ -66,7 +65,7 @@ impl Engine {
{ {
self.client.debug_draw(&ui); self.client.debug_draw(&ui);
use imgui::*; use imgui::*;
let fps = 1.0 / delta_time; let fps = 1.0 / delta_time;
let mut info = self.bg_info; let mut info = self.bg_info;
Window::new(im_str!("Hello world")) Window::new(im_str!("Hello world"))
.size([300.0, 110.0], Condition::FirstUseEver) .size([300.0, 110.0], Condition::FirstUseEver)
@ -107,7 +106,7 @@ impl Engine {
if self.window.get_key(Key::Escape) == Action::Press { if self.window.get_key(Key::Escape) == Action::Press {
self.window.set_should_close(true) self.window.set_should_close(true)
} }
self.client.process_input(&self.window,delta_time); self.client.process_input(&self.window, delta_time);
} }
pub fn process_events( pub fn process_events(
@ -194,60 +193,60 @@ impl Default for Engine {
let mut imgui = imgui::Context::create(); let mut imgui = imgui::Context::create();
{ {
use imgui_glfw_rs::imgui::StyleColor;
use imgui_glfw_rs::imgui::FontSource; use imgui_glfw_rs::imgui::FontSource;
use imgui_glfw_rs::imgui::StyleColor;
let mut style = imgui.style_mut(); let mut style = imgui.style_mut();
style.scale_all_sizes(1.5); style.scale_all_sizes(1.5);
style[StyleColor::Text] = [1.0, 1.0, 1.0, 1.0]; style[StyleColor::Text] = [1.0, 1.0, 1.0, 1.0];
style[StyleColor::TextDisabled] = [0.5,0.5,0.5, 1.0]; style[StyleColor::TextDisabled] = [0.5, 0.5, 0.5, 1.0];
style[StyleColor::WindowBg] = [0.13, 0.14, 0.15, 1.0]; style[StyleColor::WindowBg] = [0.13, 0.14, 0.15, 1.0];
style[StyleColor::ChildBg] = [0.13, 0.14, 0.15, 1.0]; style[StyleColor::ChildBg] = [0.13, 0.14, 0.15, 1.0];
style[StyleColor::PopupBg] = [0.13, 0.14, 0.15, 1.0]; style[StyleColor::PopupBg] = [0.13, 0.14, 0.15, 1.0];
style[StyleColor::Border] = [0.43, 0.43, 0.50, 0.50]; style[StyleColor::Border] = [0.43, 0.43, 0.50, 0.50];
style[StyleColor::BorderShadow] = [0.00, 0.00, 0.00, 0.00]; style[StyleColor::BorderShadow] = [0.00, 0.00, 0.00, 0.00];
style[StyleColor::FrameBg] = [0.25, 0.25, 0.25, 1.00]; style[StyleColor::FrameBg] = [0.25, 0.25, 0.25, 1.00];
style[StyleColor::FrameBgHovered] = [0.38, 0.38, 0.38, 1.00]; style[StyleColor::FrameBgHovered] = [0.38, 0.38, 0.38, 1.00];
style[StyleColor::FrameBgActive] = [0.67, 0.67, 0.67, 0.39]; style[StyleColor::FrameBgActive] = [0.67, 0.67, 0.67, 0.39];
style[StyleColor::TitleBg] = [0.08, 0.08, 0.09, 1.00]; style[StyleColor::TitleBg] = [0.08, 0.08, 0.09, 1.00];
style[StyleColor::TitleBgActive] = [0.08, 0.08, 0.09, 1.00]; style[StyleColor::TitleBgActive] = [0.08, 0.08, 0.09, 1.00];
style[StyleColor::TitleBgCollapsed] = [0.00, 0.00, 0.00, 0.51]; style[StyleColor::TitleBgCollapsed] = [0.00, 0.00, 0.00, 0.51];
style[StyleColor::MenuBarBg] = [0.14, 0.14, 0.14, 1.00]; style[StyleColor::MenuBarBg] = [0.14, 0.14, 0.14, 1.00];
style[StyleColor::ScrollbarBg] = [0.02, 0.02, 0.02, 0.53]; style[StyleColor::ScrollbarBg] = [0.02, 0.02, 0.02, 0.53];
style[StyleColor::ScrollbarGrab] = [0.31, 0.31, 0.31, 1.00]; style[StyleColor::ScrollbarGrab] = [0.31, 0.31, 0.31, 1.00];
style[StyleColor::ScrollbarGrabHovered] = [0.41, 0.41, 0.41, 1.00]; style[StyleColor::ScrollbarGrabHovered] = [0.41, 0.41, 0.41, 1.00];
style[StyleColor::ScrollbarGrabActive] = [0.51, 0.51, 0.51, 1.00]; style[StyleColor::ScrollbarGrabActive] = [0.51, 0.51, 0.51, 1.00];
style[StyleColor::CheckMark] = [0.11, 0.64, 0.92, 1.00]; style[StyleColor::CheckMark] = [0.11, 0.64, 0.92, 1.00];
style[StyleColor::SliderGrab] = [0.11, 0.64, 0.92, 1.00]; style[StyleColor::SliderGrab] = [0.11, 0.64, 0.92, 1.00];
style[StyleColor::SliderGrabActive] = [0.08, 0.50, 0.72, 1.00]; style[StyleColor::SliderGrabActive] = [0.08, 0.50, 0.72, 1.00];
style[StyleColor::Button] = [0.25, 0.25, 0.25, 1.00]; style[StyleColor::Button] = [0.25, 0.25, 0.25, 1.00];
style[StyleColor::ButtonHovered] = [0.38, 0.38, 0.38, 1.00]; style[StyleColor::ButtonHovered] = [0.38, 0.38, 0.38, 1.00];
style[StyleColor::ButtonActive] = [0.67, 0.67, 0.67, 0.39]; style[StyleColor::ButtonActive] = [0.67, 0.67, 0.67, 0.39];
style[StyleColor::Header] = [0.22, 0.22, 0.22, 1.00]; style[StyleColor::Header] = [0.22, 0.22, 0.22, 1.00];
style[StyleColor::HeaderHovered] = [0.25, 0.25, 0.25, 1.00]; style[StyleColor::HeaderHovered] = [0.25, 0.25, 0.25, 1.00];
style[StyleColor::HeaderActive] = [0.67, 0.67, 0.67, 0.39]; style[StyleColor::HeaderActive] = [0.67, 0.67, 0.67, 0.39];
style[StyleColor::Separator] = style[StyleColor::Border]; style[StyleColor::Separator] = style[StyleColor::Border];
style[StyleColor::SeparatorHovered] = [0.41, 0.42, 0.44, 1.00]; style[StyleColor::SeparatorHovered] = [0.41, 0.42, 0.44, 1.00];
style[StyleColor::SeparatorActive] = [0.26, 0.59, 0.98, 0.95]; style[StyleColor::SeparatorActive] = [0.26, 0.59, 0.98, 0.95];
style[StyleColor::ResizeGrip] = [0.00, 0.00, 0.00, 0.00]; style[StyleColor::ResizeGrip] = [0.00, 0.00, 0.00, 0.00];
style[StyleColor::ResizeGripHovered] = [0.29, 0.30, 0.31, 0.67]; style[StyleColor::ResizeGripHovered] = [0.29, 0.30, 0.31, 0.67];
style[StyleColor::ResizeGripActive] = [0.26, 0.59, 0.98, 0.95]; style[StyleColor::ResizeGripActive] = [0.26, 0.59, 0.98, 0.95];
style[StyleColor::Tab] = [0.08, 0.08, 0.09, 0.83]; style[StyleColor::Tab] = [0.08, 0.08, 0.09, 0.83];
style[StyleColor::TabHovered] = [0.33, 0.34, 0.36, 0.83]; style[StyleColor::TabHovered] = [0.33, 0.34, 0.36, 0.83];
style[StyleColor::TabActive] = [0.23, 0.23, 0.24, 1.00]; style[StyleColor::TabActive] = [0.23, 0.23, 0.24, 1.00];
style[StyleColor::TabUnfocused] = [0.08, 0.08, 0.09, 1.00]; style[StyleColor::TabUnfocused] = [0.08, 0.08, 0.09, 1.00];
style[StyleColor::TabUnfocusedActive] = [0.13, 0.14, 0.15, 1.00]; style[StyleColor::TabUnfocusedActive] = [0.13, 0.14, 0.15, 1.00];
// style[StyleColor::DockingPreview] = [0.26, 0.59, 0.98, 0.70]; // style[StyleColor::DockingPreview] = [0.26, 0.59, 0.98, 0.70];
// style[StyleColor::DockingEmptyBg] = [0.20, 0.20, 0.20, 1.00]; // style[StyleColor::DockingEmptyBg] = [0.20, 0.20, 0.20, 1.00];
style[StyleColor::PlotLines] = [0.61, 0.61, 0.61, 1.00]; style[StyleColor::PlotLines] = [0.61, 0.61, 0.61, 1.00];
style[StyleColor::PlotLinesHovered] = [1.00, 0.43, 0.35, 1.00]; style[StyleColor::PlotLinesHovered] = [1.00, 0.43, 0.35, 1.00];
style[StyleColor::PlotHistogram] = [0.90, 0.70, 0.00, 1.00]; style[StyleColor::PlotHistogram] = [0.90, 0.70, 0.00, 1.00];
style[StyleColor::PlotHistogramHovered] = [1.00, 0.60, 0.00, 1.00]; style[StyleColor::PlotHistogramHovered] = [1.00, 0.60, 0.00, 1.00];
style[StyleColor::TextSelectedBg] = [0.26, 0.59, 0.98, 0.35]; style[StyleColor::TextSelectedBg] = [0.26, 0.59, 0.98, 0.35];
style[StyleColor::DragDropTarget] = [0.11, 0.64, 0.92, 1.00]; style[StyleColor::DragDropTarget] = [0.11, 0.64, 0.92, 1.00];
style[StyleColor::NavHighlight] = [0.26, 0.59, 0.98, 1.00]; style[StyleColor::NavHighlight] = [0.26, 0.59, 0.98, 1.00];
style[StyleColor::NavWindowingHighlight] = [1.00, 1.00, 1.00, 0.70]; style[StyleColor::NavWindowingHighlight] = [1.00, 1.00, 1.00, 0.70];
style[StyleColor::NavWindowingDimBg] = [0.80, 0.80, 0.80, 0.20]; style[StyleColor::NavWindowingDimBg] = [0.80, 0.80, 0.80, 0.20];
style[StyleColor::ModalWindowDimBg] = [0.80, 0.80, 0.80, 0.35]; style[StyleColor::ModalWindowDimBg] = [0.80, 0.80, 0.80, 0.35];
style.grab_rounding = 2.3; style.grab_rounding = 2.3;
style.frame_rounding = style.grab_rounding; style.frame_rounding = style.grab_rounding;
imgui.fonts().clear(); imgui.fonts().clear();
@ -257,10 +256,12 @@ impl Default for Engine {
config: None, config: None,
}]); }]);
} }
let mut imgui_glfw = ImguiGLFW::new(&mut imgui, &mut window); let imgui_glfw = ImguiGLFW::new(&mut imgui, &mut window);
// configure global opengl state // configure global opengl state
// ----------------------------- // -----------------------------
unsafe{gl::Enable(gl::DEPTH_TEST);} unsafe {
gl::Enable(gl::DEPTH_TEST);
}
Engine { Engine {
bg_info: BgInfo::default(), bg_info: BgInfo::default(),

View File

@ -1,5 +1,5 @@
#![allow(non_snake_case)]
#![allow(dead_code)] #![allow(dead_code)]
#![allow(non_snake_case)]
use std::ffi::CString; use std::ffi::CString;
use std::mem::size_of; use std::mem::size_of;
@ -20,23 +20,23 @@ pub struct Vertex {
// position // position
pub position: Vector3<f32>, pub position: Vector3<f32>,
// normal // normal
pub Normal: Vector3<f32>, pub normal: Vector3<f32>,
// texCoords // texCoords
pub TexCoords: Vector2<f32>, pub text_coords: Vector2<f32>,
// tangent // tangent
pub Tangent: Vector3<f32>, pub tangent: Vector3<f32>,
// bitangent // bitangent
pub Bitangent: Vector3<f32>, pub bitangent: Vector3<f32>,
} }
impl Default for Vertex { impl Default for Vertex {
fn default() -> Self { fn default() -> Self {
Vertex { Vertex {
position: Vector3::zero(), position: Vector3::zero(),
Normal: Vector3::zero(), normal: Vector3::zero(),
TexCoords: Vector2::zero(), text_coords: Vector2::zero(),
Tangent: Vector3::zero(), tangent: Vector3::zero(),
Bitangent: Vector3::zero(), bitangent: Vector3::zero(),
} }
} }
} }
@ -171,7 +171,7 @@ impl Mesh {
gl::FLOAT, gl::FLOAT,
gl::FALSE, gl::FALSE,
size, size,
offset_of!(Vertex, Normal) as *const c_void, offset_of!(Vertex, normal) as *const c_void,
); );
// vertex texture coords // vertex texture coords
gl::EnableVertexAttribArray(2); gl::EnableVertexAttribArray(2);
@ -181,7 +181,7 @@ impl Mesh {
gl::FLOAT, gl::FLOAT,
gl::FALSE, gl::FALSE,
size, size,
offset_of!(Vertex, TexCoords) as *const c_void, offset_of!(Vertex, text_coords) as *const c_void,
); );
// vertex tangent // vertex tangent
gl::EnableVertexAttribArray(3); gl::EnableVertexAttribArray(3);
@ -191,7 +191,7 @@ impl Mesh {
gl::FLOAT, gl::FLOAT,
gl::FALSE, gl::FALSE,
size, size,
offset_of!(Vertex, Tangent) as *const c_void, offset_of!(Vertex, tangent) as *const c_void,
); );
// vertex bitangent // vertex bitangent
gl::EnableVertexAttribArray(4); gl::EnableVertexAttribArray(4);
@ -201,7 +201,7 @@ impl Mesh {
gl::FLOAT, gl::FLOAT,
gl::FALSE, gl::FALSE,
size, size,
offset_of!(Vertex, Bitangent) as *const c_void, offset_of!(Vertex, bitangent) as *const c_void,
); );
gl::BindVertexArray(0); gl::BindVertexArray(0);

View File

@ -1,11 +1,12 @@
pub mod macros; pub mod macros;
pub mod camera;
pub mod components;
pub mod client;
pub mod consts;
pub mod utils;
pub mod bg_info; pub mod bg_info;
pub mod camera;
pub mod client;
pub mod components;
pub mod consts;
pub mod engine; pub mod engine;
pub mod mesh; pub mod mesh;
pub mod model; pub mod model;
pub mod shader; pub mod shader;
pub mod utils;

View File

@ -1,19 +1,12 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(dead_code)] #![allow(dead_code)]
use std::os::raw::c_void;
use std::path::Path;
use cgmath::{vec2, vec3};
use gl;
use image;
use image::DynamicImage::*;
use image::GenericImage;
use tobj;
use crate::gaia::mesh::{Mesh, Texture, Vertex}; use crate::gaia::mesh::{Mesh, Texture, Vertex};
use crate::gaia::shader::Shader; use crate::gaia::shader::Shader;
use crate::gaia::utils::*; use crate::gaia::utils::*;
use cgmath::{vec2, vec3};
use std::path::Path;
use tobj;
// #[derive(Default)] // #[derive(Default)]
pub struct Model { pub struct Model {
@ -37,7 +30,7 @@ impl Model {
.unwrap() .unwrap()
.into(), .into(),
}; };
model.loadModel(path); model.load_model(path);
model model
} }
@ -50,7 +43,7 @@ impl Model {
} }
// 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) { fn load_model(&mut self, path: &str) {
let path = Path::new(path); let path = Path::new(path);
println!("Started loading model from path: {}", path.display()); println!("Started loading model from path: {}", path.display());
@ -76,8 +69,8 @@ impl Model {
for i in 0..num_vertices { for i in 0..num_vertices {
vertices.push(Vertex { vertices.push(Vertex {
position: vec3(p[i * 3], p[i * 3 + 1], p[i * 3 + 2]), position: vec3(p[i * 3], p[i * 3 + 1], p[i * 3 + 2]),
Normal: vec3(n[i * 3], n[i * 3 + 1], n[i * 3 + 2]), normal: vec3(n[i * 3], n[i * 3 + 1], n[i * 3 + 2]),
TexCoords: vec2(t[i * 2], t[i * 2 + 1]), text_coords: vec2(t[i * 2], t[i * 2 + 1]),
..Vertex::default() ..Vertex::default()
}) })
} }
@ -90,19 +83,19 @@ impl Model {
// 1. diffuse map // 1. diffuse map
if !material.diffuse_texture.is_empty() { if !material.diffuse_texture.is_empty() {
let texture = let texture =
self.loadMaterialTexture(&material.diffuse_texture, "texture_diffuse"); self.load_material_texture(&material.diffuse_texture, "texture_diffuse");
textures.push(texture); textures.push(texture);
} }
// 2. specular map // 2. specular map
if !material.specular_texture.is_empty() { if !material.specular_texture.is_empty() {
let texture = let texture =
self.loadMaterialTexture(&material.specular_texture, "texture_specular"); self.load_material_texture(&material.specular_texture, "texture_specular");
textures.push(texture); textures.push(texture);
} }
// 3. normal map // 3. normal map
if !material.normal_texture.is_empty() { if !material.normal_texture.is_empty() {
let texture = let texture =
self.loadMaterialTexture(&material.normal_texture, "texture_normal"); self.load_material_texture(&material.normal_texture, "texture_normal");
textures.push(texture); textures.push(texture);
} }
// NOTE: no height maps // NOTE: no height maps
@ -113,7 +106,7 @@ impl Model {
println!("Finished loading model from path: {}", path.display()); println!("Finished loading model from path: {}", path.display());
} }
fn loadMaterialTexture(&mut self, path: &str, typeName: &str) -> Texture { fn load_material_texture(&mut self, path: &str, typeName: &str) -> Texture {
{ {
let texture = self.textures_loaded.iter().find(|t| t.path == path); let texture = self.textures_loaded.iter().find(|t| t.path == path);
if let Some(texture) = texture { if let Some(texture) = texture {

View File

@ -38,18 +38,18 @@ impl Shader {
let vertex = gl::CreateShader(gl::VERTEX_SHADER); let vertex = gl::CreateShader(gl::VERTEX_SHADER);
gl::ShaderSource(vertex, 1, &vShaderCode.as_ptr(), ptr::null()); gl::ShaderSource(vertex, 1, &vShaderCode.as_ptr(), ptr::null());
gl::CompileShader(vertex); gl::CompileShader(vertex);
shader.checkCompileErrors(vertex, "VERTEX"); shader.check_compile_errors(vertex, "VERTEX");
// fragment Shader // fragment Shader
let fragment = gl::CreateShader(gl::FRAGMENT_SHADER); let fragment = gl::CreateShader(gl::FRAGMENT_SHADER);
gl::ShaderSource(fragment, 1, &fShaderCode.as_ptr(), ptr::null()); gl::ShaderSource(fragment, 1, &fShaderCode.as_ptr(), ptr::null());
gl::CompileShader(fragment); gl::CompileShader(fragment);
shader.checkCompileErrors(fragment, "FRAGMENT"); shader.check_compile_errors(fragment, "FRAGMENT");
// shader Program // shader Program
let ID = gl::CreateProgram(); let ID = gl::CreateProgram();
gl::AttachShader(ID, vertex); gl::AttachShader(ID, vertex);
gl::AttachShader(ID, fragment); gl::AttachShader(ID, fragment);
gl::LinkProgram(ID); gl::LinkProgram(ID);
shader.checkCompileErrors(ID, "PROGRAM"); shader.check_compile_errors(ID, "PROGRAM");
// delete the shaders as they're linked into our program now and no longer necessary // delete the shaders as they're linked into our program now and no longer necessary
gl::DeleteShader(vertex); gl::DeleteShader(vertex);
gl::DeleteShader(fragment); gl::DeleteShader(fragment);
@ -82,7 +82,7 @@ impl Shader {
/// activate the shader /// activate the shader
/// ------------------------------------------------------------------------ /// ------------------------------------------------------------------------
pub unsafe fn useProgram(&self) { pub unsafe fn use_program(&self) {
gl::UseProgram(self.ID) gl::UseProgram(self.ID)
} }
@ -100,7 +100,7 @@ impl Shader {
gl::Uniform1f(gl::GetUniformLocation(self.ID, name.as_ptr()), value); gl::Uniform1f(gl::GetUniformLocation(self.ID, name.as_ptr()), value);
} }
/// ------------------------------------------------------------------------ /// ------------------------------------------------------------------------
pub unsafe fn setVector3(&self, name: &CStr, value: &Vector3<f32>) { pub unsafe fn set_vector3(&self, name: &CStr, value: &Vector3<f32>) {
gl::Uniform3fv( gl::Uniform3fv(
gl::GetUniformLocation(self.ID, name.as_ptr()), gl::GetUniformLocation(self.ID, name.as_ptr()),
1, 1,
@ -108,11 +108,11 @@ impl Shader {
); );
} }
/// ------------------------------------------------------------------------ /// ------------------------------------------------------------------------
pub unsafe fn setVec3(&self, name: &CStr, x: f32, y: f32, z: f32) { pub unsafe fn set_vec3(&self, name: &CStr, x: f32, y: f32, z: f32) {
gl::Uniform3f(gl::GetUniformLocation(self.ID, name.as_ptr()), x, y, z); gl::Uniform3f(gl::GetUniformLocation(self.ID, name.as_ptr()), x, y, z);
} }
/// ------------------------------------------------------------------------ /// ------------------------------------------------------------------------
pub unsafe fn setMat4(&self, name: &CStr, mat: &Matrix4<f32>) { pub unsafe fn set_mat4(&self, name: &CStr, mat: &Matrix4<f32>) {
gl::UniformMatrix4fv( gl::UniformMatrix4fv(
gl::GetUniformLocation(self.ID, name.as_ptr()), gl::GetUniformLocation(self.ID, name.as_ptr()),
1, 1,
@ -123,7 +123,7 @@ impl Shader {
/// utility function for checking shader compilation/linking errors. /// utility function for checking shader compilation/linking errors.
/// ------------------------------------------------------------------------ /// ------------------------------------------------------------------------
unsafe fn checkCompileErrors(&self, shader: u32, type_: &str) { unsafe fn check_compile_errors(&self, shader: u32, type_: &str) {
let mut is_success = gl::FALSE as GLint; let mut is_success = gl::FALSE as GLint;
let mut infoLog = Vec::with_capacity(1024); let mut infoLog = Vec::with_capacity(1024);
infoLog.set_len(1024 - 1); // subtract 1 to skip the trailing null character infoLog.set_len(1024 - 1); // subtract 1 to skip the trailing null character
@ -195,17 +195,17 @@ impl Shader {
let vertex = gl::CreateShader(gl::VERTEX_SHADER); let vertex = gl::CreateShader(gl::VERTEX_SHADER);
gl::ShaderSource(vertex, 1, &vShaderCode.as_ptr(), ptr::null()); gl::ShaderSource(vertex, 1, &vShaderCode.as_ptr(), ptr::null());
gl::CompileShader(vertex); gl::CompileShader(vertex);
shader.checkCompileErrors(vertex, "VERTEX"); shader.check_compile_errors(vertex, "VERTEX");
// fragment Shader // fragment Shader
let fragment = gl::CreateShader(gl::FRAGMENT_SHADER); let fragment = gl::CreateShader(gl::FRAGMENT_SHADER);
gl::ShaderSource(fragment, 1, &fShaderCode.as_ptr(), ptr::null()); gl::ShaderSource(fragment, 1, &fShaderCode.as_ptr(), ptr::null());
gl::CompileShader(fragment); gl::CompileShader(fragment);
shader.checkCompileErrors(fragment, "FRAGMENT"); shader.check_compile_errors(fragment, "FRAGMENT");
// geometry shader // geometry shader
let geometry = gl::CreateShader(gl::GEOMETRY_SHADER); let geometry = gl::CreateShader(gl::GEOMETRY_SHADER);
gl::ShaderSource(geometry, 1, &gShaderCode.as_ptr(), ptr::null()); gl::ShaderSource(geometry, 1, &gShaderCode.as_ptr(), ptr::null());
gl::CompileShader(geometry); gl::CompileShader(geometry);
shader.checkCompileErrors(geometry, "GEOMETRY"); shader.check_compile_errors(geometry, "GEOMETRY");
// shader Program // shader Program
let ID = gl::CreateProgram(); let ID = gl::CreateProgram();
@ -213,7 +213,7 @@ impl Shader {
gl::AttachShader(ID, fragment); gl::AttachShader(ID, fragment);
gl::AttachShader(ID, geometry); gl::AttachShader(ID, geometry);
gl::LinkProgram(ID); gl::LinkProgram(ID);
shader.checkCompileErrors(ID, "PROGRAM"); shader.check_compile_errors(ID, "PROGRAM");
// delete the shaders as they're linked into our program now and no longer necessary // delete the shaders as they're linked into our program now and no longer necessary
gl::DeleteShader(vertex); gl::DeleteShader(vertex);
gl::DeleteShader(fragment); gl::DeleteShader(fragment);

View File

@ -1,10 +1,9 @@
use std::os::raw::c_void;
use std::path::Path;
use gl; use gl;
use image; use image;
use image::DynamicImage::*; use image::DynamicImage::*;
use image::GenericImage;
use image::*; use image::*;
use std::os::raw::c_void;
use std::path::Path;
pub unsafe fn load_texture(path: &str) -> u32 { pub unsafe fn load_texture(path: &str) -> u32 {
println!("Loading texture from path: {}", path); println!("Loading texture from path: {}", path);

View File

@ -8,7 +8,6 @@ use human_panic::setup_panic;
mod gaia; mod gaia;
mod example_client; mod example_client;
use crate::gaia::engine::Engine; use crate::gaia::engine::Engine;
use crate::example_client::ExampleClient;
pub fn main() { pub fn main() {
setup_panic!(); setup_panic!();