Skip to content

Commit

Permalink
Finally fixed the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
AJMC2002 committed Sep 29, 2023
1 parent 4c053f1 commit 19f9a44
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 46 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/doom-engine.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added resources/fonts/droid_sans.ttf
Binary file not shown.
8 changes: 8 additions & 0 deletions resources/shaders/basic.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 460 core
out vec4 FragColor;

in vec4 _color;

void main() {
FragColor = _color;
}
14 changes: 14 additions & 0 deletions resources/shaders/basic.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 460 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec4 color;

out vec4 _color;

uniform mat4 proj;
uniform mat4 view;
uniform mat4 model;

void main() {
gl_Position = proj * view * model * vec4(pos, 1.0);
_color = color;
}
10 changes: 10 additions & 0 deletions resources/shaders/texture.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 460 core
out vec4 FragColor;

in vec2 _tex_coords;

uniform sampler2D tex;

void main() {
FragColor = texture(tex, _tex_coords);
}
14 changes: 14 additions & 0 deletions resources/shaders/texture.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 460 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec2 tex_coords;

out vec2 _tex_coords;

uniform mat4 proj;
uniform mat4 view;
uniform mat4 model;

void main() {
gl_Position = proj * view * model * vec4(pos, 1.0);
_tex_coords = tex_coords;
}
12 changes: 12 additions & 0 deletions src/graphics/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::maths::Matrix;

pub struct Context {
pub projection: Matrix,
pub view: Matrix,
}

impl Context {
pub fn new(projection: Matrix, view: Matrix) -> Self {
Context { projection, view }
}
}
3 changes: 3 additions & 0 deletions src/graphics/geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod triangle;

pub use triangle::*;
121 changes: 121 additions & 0 deletions src/graphics/geometry/triangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use gl::types::*;
use std::{mem, os::raw::c_void, ptr};

use crate::{
graphics::{wrapper::*, Context},
maths::*,
};

pub trait Vertex {
fn x(&self) -> f32;
fn y(&self) -> f32;
fn z(&self) -> f32;
}

impl Vertex for Vector {
fn x(&self) -> f32 {
assert!(!self.is_empty());
self[0]
}
fn y(&self) -> f32 {
assert!(self.len() >= 2);
self[1]
}
fn z(&self) -> f32 {
assert!(self.len() >= 3);
self[2]
}
}

pub struct Triangle<'a> {
model: Matrix,
texture: Option<&'a Texture2D>,
shader: &'a mut ShaderProgram,
}

impl<'a> Triangle<'a> {
pub fn new(
model: Matrix,
texture: Option<&'a Texture2D>,
shader: &'a mut ShaderProgram,
) -> Self {
assert_eq!(model.cols(), 4);
assert_eq!(model.rows(), 4);
Triangle {
model,
texture,
shader,
}
}
pub fn draw(&mut self, context: &Context) {
self.shader.bind();
self.shader.uniform_matrix_4fv("proj", &context.projection);
self.shader.uniform_matrix_4fv("view", &context.view);
self.shader.uniform_matrix_4fv("model", &self.model);
if let Some(texture) = self.texture {
texture.bind();
self.shader.uniform_2dtex("tex", texture)
}
unsafe {
gl::DrawElements(gl::TRIANGLES, 6, gl::UNSIGNED_INT, ptr::null());
}
if let Some(texture) = self.texture {
texture.unbind();
}
self.shader.unbind();
}
}

pub struct TrianglePool<'a> {
vao: VAO,
attribs: Vec<VertexAttrib>,
pool: Vec<Triangle<'a>>,
}

impl<'a> TrianglePool<'a> {
pub fn new(
base_data: Vec<f32>,
attribs_sizes: Vec<i32>,
shader: &'a mut ShaderProgram,
) -> Self {
let vao = VAO::new();
let vbo: VBO = BO::new(gl::STATIC_DRAW, base_data);
let stride = attribs_sizes.iter().sum::<i32>() * mem::size_of::<GLfloat>() as GLsizei;
let mut cur_data_index = 0;
let attribs = attribs_sizes
.iter()
.enumerate()
.map(|(idx, attrib_size)| {
let attrib = VertexAttrib::new(
idx as u32,
*attrib_size,
gl::FLOAT,
gl::FALSE,
stride,
match cur_data_index {
0 => ptr::null(),
_ => (cur_data_index * mem::size_of::<GLfloat>()) as *const c_void,
},
);
cur_data_index += *attrib_size as usize;
attrib
})
.collect::<Vec<VertexAttrib>>();
let pool = vec![Triangle::new(Matrix::model_default(), None, shader)];

vbo.unbind();
vao.unbind();
attribs.iter().for_each(|attrib| attrib.disable());

TrianglePool { vao, attribs, pool }
}
pub fn draw(&mut self, context: &Context) {
self.vao.bind();
self.attribs.iter().for_each(|attrib| attrib.enable());
self.pool
.iter_mut()
.for_each(|triangle| triangle.draw(context));
self.attribs.iter().for_each(|attrib| attrib.disable());
self.vao.unbind();
}
}
30 changes: 9 additions & 21 deletions src/graphics/window.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ffi::CStr;
use egui_glfw::EguiBackend;
use gl::types::*;
use glfw::{self, Action, Context, Key, WindowEvent};
use std::ffi::CStr;
use std::os::raw::c_void;
use std::ptr;
use std::sync::mpsc::Receiver;
Expand Down Expand Up @@ -147,24 +147,12 @@ extern "system" fn debug_callback(
message: *const GLchar,
_user_param: *mut c_void,
) {
unsafe {
println!(
"GL CALLBACK: {} type = {}, severity = {}, message = {}",
match gltype {
gl::DEBUG_TYPE_ERROR => "** GL ERROR **",
_ => ""
},
CStr::from_ptr(gl::GetString(gltype) as _).to_str().expect("Failed to convert debug GL type to String"),
CStr::from_ptr(gl::GetString(severity) as _).to_str().expect("Failed to convert debug severity to String"),
CStr::from_ptr(message as _).to_str().expect("Failed to convert debug message to String")
)
}
// println!(
// "OpenGL Debug Message: source={}, type={}, id={}, severity={}, message={}",
// source,
// gltype,
// id,
// severity,
// unsafe { std::ffi::CStr::from_ptr(message).to_str().unwrap() }
// );
println!(
"OpenGL Debug Message: source={}, type={}, id={}, severity={}, message={}",
source,
gltype,
id,
severity,
unsafe { CStr::from_ptr(message).to_str().unwrap() }
);
}
50 changes: 25 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn main() {
{
t = window.glfw_handle().get_time() as f32;

shader_program.bind();
// shader_program.bind();
// _pos_attrib.enable();
// _tex_attrib.enable();
_vao.bind();
Expand All @@ -108,17 +108,17 @@ fn main() {
"model",
&(Matrix::translation((0., 0.5 * (1.5 * t).sin(), 0.))
* Matrix::translation((
translate.0.parse::<f32>().unwrap_or(0.0),
translate.1.parse::<f32>().unwrap_or(0.0),
translate.2.parse::<f32>().unwrap_or(0.0),
))
translate.0.parse::<f32>().unwrap_or(0.0),
translate.1.parse::<f32>().unwrap_or(0.0),
translate.2.parse::<f32>().unwrap_or(0.0),
))
* Matrix::rotation((0.12, t, 0.))
* Matrix::rotation(rotate)
* Matrix::scaling((
scale.0.parse::<f32>().unwrap_or(0.0),
scale.1.parse::<f32>().unwrap_or(0.0),
scale.2.parse::<f32>().unwrap_or(0.0),
))),
scale.0.parse::<f32>().unwrap_or(0.0),
scale.1.parse::<f32>().unwrap_or(0.0),
scale.2.parse::<f32>().unwrap_or(0.0),
))),
);

texture_gato.bind();
Expand All @@ -130,17 +130,17 @@ fn main() {
"model",
&(Matrix::translation(((2. * t).sin(), 0., (2. * t).cos()))
* Matrix::translation((
translate.0.parse::<f32>().unwrap_or(0.0),
translate.1.parse::<f32>().unwrap_or(0.0),
translate.2.parse::<f32>().unwrap_or(0.0),
))
translate.0.parse::<f32>().unwrap_or(0.0),
translate.1.parse::<f32>().unwrap_or(0.0),
translate.2.parse::<f32>().unwrap_or(0.0),
))
* Matrix::rotation((0.12, 2. * PI * t, 0.))
* Matrix::rotation(rotate)
* Matrix::scaling((
scale.0.parse::<f32>().unwrap_or(0.0),
scale.1.parse::<f32>().unwrap_or(0.0),
scale.2.parse::<f32>().unwrap_or(0.0),
))),
scale.0.parse::<f32>().unwrap_or(0.0),
scale.1.parse::<f32>().unwrap_or(0.0),
scale.2.parse::<f32>().unwrap_or(0.0),
))),
);

texture_gatorrito.bind();
Expand All @@ -152,17 +152,17 @@ fn main() {
"model",
&(Matrix::translation((0.2 * (t).sin(), (2. * t).sin(), (2. * t).cos()))
* Matrix::translation((
translate.0.parse::<f32>().unwrap_or(0.0),
translate.1.parse::<f32>().unwrap_or(0.0),
translate.2.parse::<f32>().unwrap_or(0.0),
))
translate.0.parse::<f32>().unwrap_or(0.0),
translate.1.parse::<f32>().unwrap_or(0.0),
translate.2.parse::<f32>().unwrap_or(0.0),
))
* Matrix::rotation((0.12, 2. * PI * t, 0.))
* Matrix::rotation(rotate)
* Matrix::scaling((
scale.0.parse::<f32>().unwrap_or(0.0),
scale.1.parse::<f32>().unwrap_or(0.0),
scale.2.parse::<f32>().unwrap_or(0.0),
))),
scale.0.parse::<f32>().unwrap_or(0.0),
scale.1.parse::<f32>().unwrap_or(0.0),
scale.2.parse::<f32>().unwrap_or(0.0),
))),
);

texture_pog.bind();
Expand Down

0 comments on commit 19f9a44

Please sign in to comment.