runs however does not work

master
Milk.H 2023-02-23 22:19:48 +01:00
parent a539c71481
commit 0b225454ce
No known key found for this signature in database
GPG Key ID: 3D9DAE46AAC37BD8
4 changed files with 169 additions and 18 deletions

136
src/camera.rs Normal file
View File

@ -0,0 +1,136 @@
use cgmath::{vec3, prelude::*};
type Point3 = cgmath::Point3<f32>;
type Vector3 = cgmath::Vector3<f32>;
type Matrix4 = cgmath::Matrix4<f32>;
#[derive(PartialEq, Clone, Copy)]
pub enum Camera_Movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT,
}
use self::Camera_Movement::*;
const YAW: f32 = -90.0;
const PITCH: f32 = 0.0;
const SPEED: f32 = 2.5;
const SENSITIVITY: f32 = 0.1;
const ZOOM: f32 = 45.0;
pub struct Camera {
// camera attributes
pub Position: Point3,
pub Front: Vector3,
pub Up: Vector3,
pub Right: Vector3,
pub WorldUp: Vector3,
pub Yaw: f32,
pub Pitch: f32,
pub MovementSpeed: f32,
pub MouseSensitivity: f32,
pub Zoom: f32,
}
impl Default for Camera {
fn default() -> Camera {
let mut camera = Camera {
Position: Point3::new(0.0, 0.0, 0.0),
Front: vec3(0.0, 0.0, -1.0),
Up: Vector3::zero(),
Right: Vector3::zero(),
WorldUp: Vector3::unit_y(),
Yaw: YAW,
Pitch: PITCH,
MovementSpeed: SPEED,
MouseSensitivity: SENSITIVITY,
Zoom: ZOOM
};
camera.updateCameraVectors();
camera
}
}
impl Camera {
pub fn GetViewMatrix(&self) -> Matrix4 {
Matrix4::look_at(self.Position, self.Position + self.Front, self.Up)
}
pub fn ProcessKeyboard(&mut self, direction: Camera_Movement, deltaTime: f32) {
let velocity = self.MovementSpeed * deltaTime;
if direction == FORWARD {
self.Position += self.Front * velocity;
}
if direction == BACKWARD {
self.Position += -(self.Front * velocity);
}
if direction == LEFT {
self.Position += -(self.Right * velocity);
}
if direction == RIGHT {
self.Position += self.Right * velocity;
}
}
pub fn ProcessMouseMovement(&mut self, mut xoffset: f32, mut yoffset: f32, constrainPitch: bool) {
xoffset *= self.MouseSensitivity;
yoffset *= self.MouseSensitivity;
self.Yaw += xoffset;
self.Pitch += yoffset;
if constrainPitch {
if self.Pitch > 89.0 {
self.Pitch = 89.0;
}
if self.Pitch < -89.0 {
self.Pitch = -89.0;
}
}
self.updateCameraVectors();
}
pub fn ProcessMouseScroll(&mut self, yoffset: f32) {
if self.Zoom >= 1.0 && self.Zoom <= 45.0 {
self.Zoom -= yoffset;
}
if self.Zoom <= 1.0 {
if self.Zoom >= 45.0 {
self.Zoom = 45.0;
}
}
}
fn updateCameraVectors(&mut self)
{
let front = Vector3 {
x: self.Yaw.to_radians().cos() * self.Pitch.to_radians().cos(),
y: self.Pitch.to_radians().sin(),
z: self.Yaw.to_radians().sin() * self.Pitch.to_radians().cos(),
};
self.Front = front.normalize();
self.Right = self.Front.cross(self.WorldUp).normalize();
self.Up = self.Right.cross(self.Front).normalize();
}
}

View File

@ -4,6 +4,7 @@ use glfw;
use glfw::{Action, Context, Key};
use gl;
use cgmath::{Matrix4, vec3, Point3, Deg, perspective};
use std::ffi::CStr;
const ScreenWidth: u32 = 480;
const ScreenHeight: u32 = 320;
@ -12,6 +13,8 @@ const TITLE: &str = "GLFWtest";
mod shader;
mod model;
use model::Model;
mod camera;
fn main() {
// initialize GLFW
@ -45,6 +48,10 @@ fn main() {
// put biggie thingies here
let model_Shader = shader::shader::new("model");
let ourModel = Model::new("resources/models/backpack");
let mut camera = camera::Camera {
Position: cgmath::Point3::new(0.0, 0.0, 3.0),
..camera::Camera::default()
};
println!("entering main loop, time since init: {} seconds", glfw.get_time());
// NOTE window loop
@ -58,9 +65,17 @@ fn main() {
// view/projection transformations
//;
let projection: Matrix4<f32> = perspective(Deg(camera.Zoom), ScreenWidth as f32 / ScreenHeight as f32, 0.1, 100.0);
let view = camera.GetViewMatrix();
model_Shader.setMat4(String::from("projection").as_str(), &projection);
model_Shader.setMat4(String::from("view").as_str(), &view);
let mut model = Matrix4::<f32>::from_translation(vec3(0.0, -1.75, 0.0));
model = model * Matrix4::from_scale(0.2);
model_Shader.setMat4(String::from("projection").as_str(), &model);
ourModel.Draw(&model_Shader);
unsafe {
gl::Clear(gl::COLOR_BUFFER_BIT);
};

View File

@ -22,7 +22,7 @@ impl Model {
}
pub fn Draw(&self, shader: shader) {
pub fn Draw(&self, shader: &shader) {
for mesh in &self.meshes {
unsafe { mesh.Draw(&shader); }
}

View File

@ -2,6 +2,7 @@
use gl;
use std::fs;
use glm;
use cgmath::prelude::*;
/// Shader Struct for creating and using shaders in the Context of engine
///
pub struct shader {
@ -93,7 +94,6 @@ use glm;
unsafe {gl::UseProgram(self.ID);}
}
// BOILERPLATE JUMPSCARE
pub fn setBool(&self, name: &str, value: bool )
{unsafe {gl::Uniform1i(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), value as i32);}}
@ -101,24 +101,24 @@ use glm;
{unsafe {gl::Uniform1i(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), value as i32);}}
pub fn setFloat(&self, name: &str, value: f32 )
{unsafe {gl::Uniform1f(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), value as f32);}}
pub fn setVec2(&self, name: &str, value: glm::Vec2 )
{unsafe {gl::Uniform2fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, &value[0]);}}
pub fn setVec2d(&self, name: &str, x: f32, y: f32)
pub fn setVec2(&self, name: &str, value: &cgmath::Vector2<f32> )
{unsafe {gl::Uniform2fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, value.as_ptr());}}
pub fn setVector2d(&self, name: &str, x: f32, y: f32)
{unsafe {gl::Uniform2f(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), x, y);}}
pub fn setVec3(&self, name: &str, value: glm::Vec3 )
{unsafe {gl::Uniform3fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, &value[0]);}}
pub fn setVec3d(&self, name: &str, x: f32, y: f32, z: f32 )
pub fn setVector3(&self, name: &str, value: cgmath::Vector3<f32> )
{unsafe {gl::Uniform3fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, value.as_ptr());}}
pub fn setVector3d(&self, name: &str, x: f32, y: f32, z: f32 )
{unsafe {gl::Uniform3f(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), x, y, z);}}
pub fn setVec4(&self, name: &str, value: glm::Vec4 )
{unsafe {gl::Uniform4fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, &value[0]);}}
pub fn setVec4d(&self, name: &str, x: f32, y: f32, z: f32, w: f32)
pub fn setVector4(&self, name: &str, value: &cgmath::Vector4<f32> )
{unsafe {gl::Uniform4fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, value.as_ptr());}}
pub fn setVector4d(&self, name: &str, x: f32, y: f32, z: f32, w: f32)
{unsafe {gl::Uniform4f(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), x, y, z, w);}}
pub fn setMat2(&self, name: &str, value: glm::Mat2)
{unsafe {gl::UniformMatrix2fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, gl::FALSE, &value[0][0]);}}
pub fn setMat3(&self, name: &str, value: glm::Mat3)
{unsafe {gl::UniformMatrix3fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, gl::FALSE, &value[0][0]);}}
pub fn setMat4(&self, name: &str, value: glm::Mat4)
{unsafe {gl::UniformMatrix4fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, gl::FALSE, &value[0][0]);}}
pub fn setMat2(&self, name: &str, value: &cgmath::Matrix2<f32>)
{unsafe {gl::UniformMatrix2fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, gl::FALSE, value.as_ptr());}}
pub fn setMat3(&self, name: &str, value: &cgmath::Matrix3<f32>)
{unsafe {gl::UniformMatrix3fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, gl::FALSE, value.as_ptr());}}
pub fn setMat4(&self, name: &str, value: &cgmath::Matrix4<f32>)
{unsafe {gl::UniformMatrix4fv(gl::GetUniformLocation(self.ID, name.as_ptr() as *const gl::types::GLchar), 1, gl::FALSE, value.as_ptr());}}
}