Rewritten mesh.h from learnopengl.com

master
Milk.H 2023-02-23 09:28:48 +01:00
parent d4c82ded65
commit 5fa37d5f06
No known key found for this signature in database
GPG Key ID: 3D9DAE46AAC37BD8
3 changed files with 109 additions and 41 deletions

View File

@ -7,6 +7,9 @@ edition = "2021"
[dependencies]
gl = "0.14.0"
glfw = "0.51.0"
glm = "0.2.3"
stb = { version = "0.3.2", default-features = false, features = ["stb_image"] }
field-offset = "0.3.4"
[dependencies.glfw]
version = "*"

View File

@ -1,15 +1,15 @@
use std::convert::TryInto;
use glfw;
use glfw::Context;
use glfw::{Action, Context, Key};
use gl;
const ScreenWidth: u32 = 480;
const ScreenHeight: u32 = 320;
const TITLE: &str = "GLFWtest";
mod shader;
mod mesh;
fn main() {
@ -18,10 +18,10 @@ fn main() {
// set window hints
glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3));
glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core));
glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true));
glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(false));
glfw.window_hint(glfw::WindowHint::Resizable(false));
glfw.window_hint(glfw::WindowHint::TransparentFramebuffer(true));
glfw.window_hint(glfw::WindowHint::Decorated(true));
glfw.window_hint(glfw::WindowHint::Decorated(false));
//create window
@ -36,38 +36,12 @@ fn main() {
unsafe {
gl::Viewport(0, 0, screen_width, screen_height);
gl::ClearColor(0.4, 0.4, 0.4, 1.0);
gl::ClearColor(1.0, 1.0, 1.0, 1.0);
}
// put biggie thingies here
let vertices = [
-0.5f32, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0,
];
let mut vao = 0;
unsafe { gl::GenVertexArrays(1, &mut vao)};
let mut vbo = 0;
unsafe{gl::GenBuffers(1, &mut vbo)};
unsafe {
gl::BindVertexArray(vao);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::BufferData(gl::ARRAY_BUFFER, std::mem::size_of_val(&vertices) as isize, vertices.as_ptr().cast(), gl::STATIC_DRAW);
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, 3 * std::mem::size_of::<f32>() as i32, 0 as *const _);
gl::EnableVertexAttribArray(0);
gl::BindBuffer(gl::ARRAY_BUFFER, 0);
gl::BindVertexArray(0);
}
let mut test_Shader = shader::shader::new("basic");
let test_Shader = shader::shader::new("basic");
// NOTE window loop
while !window.should_close() {
@ -76,7 +50,7 @@ fn main() {
glfw_handle_event(&mut window, event);
}
clear_color(0.1, 0.1, 0.1, 0.0);
clear_color(0.1, 0.1, 0.1, -0.0);
unsafe {
gl::Clear(gl::COLOR_BUFFER_BIT);
@ -84,13 +58,6 @@ fn main() {
test_Shader.Use();
unsafe {
gl::BindVertexArray(vao);
gl::DrawArrays(gl::TRIANGLES, 0, 3);
gl::BindVertexArray(0);
}
window.swap_buffers();
}

98
src/mesh.rs Normal file
View File

@ -0,0 +1,98 @@
use field_offset::offset_of;
pub struct Vertex {
Position: glm::Vec3,
Normal: glm::Vec2,
TexCoords: glm::Vec2,
Tangent: glm::Vec3,
Bitangent: glm::Vec3,
m_BoneIDs: [i32; 4],
m_Weights: [f32; 4],
}
pub struct Texture {
id: u32,
Type: String,
path: String,
}
struct Mesh {
pub vertices: Vec<Vertex>,
pub indices: Vec<u32>,
pub textures: Vec<Texture>,
pub VAO: Option<u32>,
VBO: Option<u32>,
EBO: Option<u32>,
}
impl Mesh{
pub fn new(vertices: Vec<Vertex>, indices: Vec<u32>, textures: Vec<Texture>)
{
let mut temp = Mesh {
vertices,
indices,
textures,
VAO: None,
VBO: None,
EBO: None,
};
}
fn setupMesh(mut temp: Mesh)
{
// create buffers/arrays
unsafe {
gl::GenVertexArrays(1, temp.VAO.as_mut().unwrap());
gl::GenBuffers(1, temp.VBO.as_mut().unwrap());
gl::GenBuffers(1, temp.EBO.as_mut().unwrap());
gl::BindVertexArray(temp.VAO.unwrap());
// NOTE, i am not sure if this one will work
gl::BindBuffer(gl::ARRAY_BUFFER, temp.VBO.unwrap());
gl::BufferData(gl::ARRAY_BUFFER, std::mem::size_of_val(&temp.vertices) as isize, temp.vertices.as_ptr().cast(), gl::STATIC_DRAW);
// the vertex attribPointers
// vert pox
gl::EnableVertexAttribArray(0);
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, std::mem::size_of::<Vertex>() as i32, 0 as *const _);
// vertex normals
gl::EnableVertexAttribArray(1);
gl::VertexAttribPointer(1, 3, gl::FLOAT, gl::FALSE, std::mem::size_of::<Vertex>() as i32, offset_of!(Vertex => Normal).get_byte_offset() as *const _);
// vertex texture coords
gl::EnableVertexAttribArray(2);
gl::VertexAttribPointer(2, 2, gl::FLOAT, gl::FALSE, std::mem::size_of::<Vertex>() as i32, offset_of!(Vertex => TexCoords).get_byte_offset() as *const _);
//vertex tangent
gl::EnableVertexAttribArray(3);
gl::VertexAttribPointer(3, 3, gl::FLOAT, gl::FALSE, std::mem::size_of::<Vertex>() as i32, offset_of!(Vertex => Tangent).get_byte_offset() as *const _);
// vertex Bitangent
gl::EnableVertexAttribArray(4);
gl::VertexAttribPointer(4, 3, gl::FLOAT, gl::FALSE, std::mem::size_of::<Vertex>() as i32, offset_of!(Vertex => Bitangent).get_byte_offset() as *const _);
// ids
gl::EnableVertexAttribArray(5);
gl::VertexAttribIPointer(5, 4, gl::INT, std::mem::size_of::<Vertex>() as i32, offset_of!(Vertex => m_BoneIDs).get_byte_offset() as *const _);
// weights
gl::EnableVertexAttribArray(6);
gl::VertexAttribPointer(6, 4, gl::FLOAT, gl::FALSE, std::mem::size_of::<Vertex>() as i32, offset_of!(Vertex => m_Weights).get_byte_offset() as *const _);
}
}
}