aabb/camera: thicker aabb lines and more natural camera movement + removed unused pointlight boxes

This commit is contained in:
2023-10-24 02:10:44 +02:00
parent 7f4d5f8bcc
commit 54cac634e8
7 changed files with 18 additions and 118 deletions
-7
View File
@@ -1,7 +0,0 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0);
}
+4
View File
@@ -80,15 +80,19 @@ impl AABB {
shader.use_shader(); shader.use_shader();
shader.set_mat4fv("model", &model_mat); shader.set_mat4fv("model", &model_mat);
shader.set_3fv("material.ambient", glm::vec3(1.0, 0.627, 0.157));
shader.set_3fv("material.diffuse", glm::vec3(1.0, 0.627, 0.157));
unsafe { unsafe {
// draw Mesh // draw Mesh
gl::BindVertexArray(self.vao); gl::BindVertexArray(self.vao);
gl::LineWidth(5.0);
gl::PolygonMode(gl::FRONT_AND_BACK, gl::LINE); gl::PolygonMode(gl::FRONT_AND_BACK, gl::LINE);
gl::DrawElements(gl::TRIANGLES, self.indices_len as i32, gl::UNSIGNED_INT, std::ptr::null()); gl::DrawElements(gl::TRIANGLES, self.indices_len as i32, gl::UNSIGNED_INT, std::ptr::null());
// reset stuff to default // reset stuff to default
gl::BindVertexArray(0); gl::BindVertexArray(0);
gl::LineWidth(1.0);
} }
} }
} }
+1 -1
View File
@@ -56,7 +56,7 @@ impl Camera {
self._speed = self.speed * delta_time; self._speed = self.speed * delta_time;
} }
pub fn update_position(&mut self, active_model: Option<u32>, objects: &Vec<model::Model>) { pub fn focus_on_selected_model(&mut self, active_model: Option<u32>, objects: &Vec<model::Model>) {
if let Some(id) = active_model { if let Some(id) = active_model {
for obj in objects { for obj in objects {
if obj.id == id { if obj.id == id {
+1 -104
View File
@@ -48,16 +48,6 @@ fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
source: include_str!("../shaders/frag.glsl").to_string(), source: include_str!("../shaders/frag.glsl").to_string(),
}, },
)?; )?;
let light_shader = shader::Shader::new(
&mut shader::ShaderSource{
name: "vertex.glsl".to_string(),
source: include_str!("../shaders/vertex.glsl").to_string(),
},
&mut shader::ShaderSource{
name: "light_f.glsl".to_string(),
source: include_str!("../shaders/light_f.glsl").to_string(),
},
)?;
let grid_shader = shader::Shader::new( let grid_shader = shader::Shader::new(
&mut shader::ShaderSource{ &mut shader::ShaderSource{
name: "grid_v.glsl".to_string(), name: "grid_v.glsl".to_string(),
@@ -69,46 +59,6 @@ fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
}, },
)?; )?;
let vertices: [f32; 288] = [
// positions // normals // texture coords
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0,
0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0,
0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
-0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0,
-0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 0.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0,
0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0,
0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 1.0, 1.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0
];
let points_lights: [glm::Vec3; 4] = [ let points_lights: [glm::Vec3; 4] = [
glm::vec3(0.7, 0.2, 2.0), glm::vec3(0.7, 0.2, 2.0),
glm::vec3(2.3, -3.3, -4.0), glm::vec3(2.3, -3.3, -4.0),
@@ -116,12 +66,6 @@ fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
glm::vec3(0.0, 0.0, -3.0), glm::vec3(0.0, 0.0, -3.0),
]; ];
let ident_mat = glm::mat4(
1., 0., 0., 0.,
0., 1., 0., 0.,
0., 0., 1., 0.,
0., 0., 0., 1.);
let mut delta_time: f32 = 0.0; let mut delta_time: f32 = 0.0;
let mut last_frame: f32 = 0.0; let mut last_frame: f32 = 0.0;
let mut last_cursor = None; let mut last_cursor = None;
@@ -132,38 +76,6 @@ fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
let mut first_mouse: bool = true; let mut first_mouse: bool = true;
unsafe { unsafe {
// Object 1: Cube/container
//
let mut vbo: u32 = 0;
gl::GenBuffers(1, &mut vbo);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::BufferData(gl::ARRAY_BUFFER, (vertices.len() * core::mem::size_of::<f32>()) as isize, vertices.as_ptr() as *const std::ffi::c_void, gl::STATIC_DRAW);
// position attribute, maps to 'aPos' in vertex shader with location 0
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, 8 * std::mem::size_of::<f32>() as i32, std::ptr::null());
gl::EnableVertexAttribArray(0);
// normal attribute, maps to 'aNormal' in vertex shader with location 1
gl::VertexAttribPointer(1, 3, gl::FLOAT, gl::FALSE, 8 * std::mem::size_of::<f32>() as i32, (3 * std::mem::size_of::<f32>()) as *const std::ffi::c_void);
gl::EnableVertexAttribArray(1);
// texture attribute, maps to 'aTexCoords' in vertex shader with location 2
gl::VertexAttribPointer(2, 2, gl::FLOAT, gl::FALSE, 8 * std::mem::size_of::<f32>() as i32, (6 * std::mem::size_of::<f32>()) as *const std::ffi::c_void);
gl::EnableVertexAttribArray(2);
// Object 2: Light source
let mut light_vao: u32 = 0;
gl::GenVertexArrays(1, &mut light_vao);
gl::BindVertexArray(light_vao);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
// position attribute, maps to 'aPos' in vertex shader with location 0
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, 8 * std::mem::size_of::<f32>() as i32, std::ptr::null());
gl::EnableVertexAttribArray(0);
grid_shader.use_shader(); grid_shader.use_shader();
grid_shader.set_float("near", 0.01); grid_shader.set_float("near", 0.01);
grid_shader.set_float("far", 200.0); grid_shader.set_float("far", 200.0);
@@ -255,7 +167,7 @@ fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
if state.can_capture_cursor && window.get_mouse_button(glfw::MouseButtonLeft) == Action::Press { if state.can_capture_cursor && window.get_mouse_button(glfw::MouseButtonLeft) == Action::Press {
if window.get_key(glfw::Key::LeftShift) == Action::Press { if window.get_key(glfw::Key::LeftShift) == Action::Press {
state.camera.move_camera(xoffset, yoffset); state.camera.move_camera(-xoffset, -yoffset);
} else { } else {
if let Some(active_model) = state.active_model { if let Some(active_model) = state.active_model {
let x_rotation = xoffset * state.camera.sensitivity * state.rotation_speed; let x_rotation = xoffset * state.camera.sensitivity * state.rotation_speed;
@@ -302,19 +214,6 @@ fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
} }
gl::PolygonMode(gl::FRONT_AND_BACK, gl::FILL); gl::PolygonMode(gl::FRONT_AND_BACK, gl::FILL);
light_shader.use_shader();
light_shader.set_mat4fv("view", &view_mat);
light_shader.set_mat4fv("projection", &projection_mat);
// for i in 0..points_lights.len() {
// let light_model = glm::ext::translate(&ident_mat, points_lights[i]);
// let light_model = glm::ext::scale(&light_model, glm::vec3(0.2, 0.2, 0.2));
// light_shader.set_mat4fv("model", &light_model);
// gl::BindVertexArray(light_vao);
// gl::DrawArrays(gl::TRIANGLES, 0, 36);
// }
// draw grid // draw grid
if state.draw_grid {draw_grid(&grid_shader, &view_mat, &projection_mat);} if state.draw_grid {draw_grid(&grid_shader, &view_mat, &projection_mat);}
@@ -335,8 +234,6 @@ fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
gl::DeleteRenderbuffers(1, &rbo); gl::DeleteRenderbuffers(1, &rbo);
} }
gl::DeleteVertexArrays(1, &light_vao);
gl::DeleteBuffers(1, &vbo);
gl::DeleteFramebuffers(1, &scene_fb); gl::DeleteFramebuffers(1, &scene_fb);
} }
+8 -2
View File
@@ -105,8 +105,14 @@ impl Mesh {
let model_mat = glm::ext::translate(&model_mat, glm::vec3(self.position.x, self.position.y, self.position.z)); let model_mat = glm::ext::translate(&model_mat, glm::vec3(self.position.x, self.position.y, self.position.z));
shader.set_mat4fv("model", &model_mat); shader.set_mat4fv("model", &model_mat);
shader.set_3fv("material.ambient", self.material.ambient_color); let mut polygon_mode = 0;
shader.set_3fv("material.diffuse", self.material.diffuse_color); unsafe {
gl::GetIntegerv(gl::POLYGON_MODE, &mut polygon_mode);
}
let is_wireframe = polygon_mode as u32 == gl::LINE;
shader.set_3fv("material.ambient", if is_wireframe {glm::vec3(0.0, 0.0, 0.0)} else {self.material.ambient_color});
shader.set_3fv("material.diffuse", if is_wireframe {glm::vec3(0.0, 0.0, 0.0)} else {self.material.diffuse_color});
shader.set_3fv("material.specular", self.material.specular_color); shader.set_3fv("material.specular", self.material.specular_color);
shader.set_float("material.shininess", self.material.specular_exponent); shader.set_float("material.shininess", self.material.specular_exponent);
+3 -3
View File
@@ -188,7 +188,7 @@ fn draw_object_hierarchy(ui: &imgui::Ui, state: &mut State, idx: usize) -> bool
if ui.checkbox("Selected", &mut (Some(state.objects[idx].id) == state.active_model)) { if ui.checkbox("Selected", &mut (Some(state.objects[idx].id) == state.active_model)) {
state.objects[idx].reset_rotation(); state.objects[idx].reset_rotation();
state.active_model = Some(state.objects[idx].id); state.active_model = Some(state.objects[idx].id);
state.camera.update_position(state.active_model, &state.objects); state.camera.focus_on_selected_model(state.active_model, &state.objects);
} }
} }
@@ -208,7 +208,7 @@ fn draw_objects_window(ui: &imgui::Ui, state: &mut State) {
if state.active_model == Some(selected_obj_id) { if state.active_model == Some(selected_obj_id) {
let model = state.objects.last_mut().map(|m| m.reset_rotation()); let model = state.objects.last_mut().map(|m| m.reset_rotation());
state.active_model = model.and_then(|o| Some(o.id)); state.active_model = model.and_then(|o| Some(o.id));
state.camera.update_position(state.active_model, &state.objects); state.camera.focus_on_selected_model(state.active_model, &state.objects);
} }
continue; continue;
} }
@@ -307,7 +307,7 @@ fn draw_viewport(ui: &imgui::Ui, state: &mut State, texture: u32) {
state.viewport_size = tex_size; state.viewport_size = tex_size;
if ui.button("Reset Camera") { if ui.button("Reset Camera") {
state.camera.update_position(state.active_model, &state.objects); state.camera.focus_on_selected_model(state.active_model, &state.objects);
} }
ui.same_line(); ui.same_line();
if ui.button("Capture Scene") { if ui.button("Capture Scene") {
+1 -1
View File
@@ -94,7 +94,7 @@ pub fn import_models_from_paths(paths: &Vec<PathBuf>, state: &mut ui::ui::State)
m.name = model_name.to_str().unwrap().to_string(); m.name = model_name.to_str().unwrap().to_string();
} }
state.objects.push(m); state.objects.push(m);
state.camera.update_position(state.active_model, &state.objects); state.camera.focus_on_selected_model(state.active_model, &state.objects);
}, },
Err(e) => { Err(e) => {
let error = format!("Error loading model \"{}\": {}", model_path.to_str().unwrap(), e); let error = format!("Error loading model \"{}\": {}", model_path.to_str().unwrap(), e);