summaryrefslogtreecommitdiff
path: root/engine/src/renderer/mod.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-11-05 12:32:42 +0100
committerHampusM <hampus@hampusmat.com>2023-11-05 12:32:42 +0100
commit7001b4955c8c1b337ce26bf75305417f6963a836 (patch)
tree3ba9e7e2293074ad96a8d333a8603722ddff196d /engine/src/renderer/mod.rs
parentd5b84d968b570fbaeb6a1832a93875116001c504 (diff)
refactor(engine): add shader program currently bound safety
Diffstat (limited to 'engine/src/renderer/mod.rs')
-rw-r--r--engine/src/renderer/mod.rs93
1 files changed, 53 insertions, 40 deletions
diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs
index cad4deb..7d5e104 100644
--- a/engine/src/renderer/mod.rs
+++ b/engine/src/renderer/mod.rs
@@ -12,6 +12,7 @@ use crate::opengl::buffer::{
ElementArrayKind as ElementArrayBufferKind,
Usage as BufferUsage,
};
+use crate::opengl::currently_bound::CurrentlyBound;
#[cfg(feature = "debug")]
use crate::opengl::debug::{MessageSeverity, MessageSource, MessageType};
use crate::opengl::shader::Program as ShaderProgram;
@@ -70,37 +71,24 @@ impl Renderer
clear_buffers(BufferClearMask::COLOR | BufferClearMask::DEPTH);
for obj in objects {
- obj.renderable().shader_program.activate();
-
- apply_transformation_matrices(obj, &self.camera, window_size);
-
- let draw = || {
- obj.renderable().vertex_arr.bind(|vert_arr_curr_bound| {
- if let Some(index_info) = &obj.renderable().index_info {
- VertexArray::draw_elements(
- &vert_arr_curr_bound,
- PrimitiveKind::Triangles,
- 0,
- index_info.cnt,
- );
+ obj.renderable()
+ .shader_program
+ .activate(|shader_program_curr_bound| {
+ apply_transformation_matrices(
+ obj,
+ &self.camera,
+ window_size,
+ &shader_program_curr_bound,
+ );
+
+ if let Some(texture) = obj.texture() {
+ texture.inner().bind(|_| {
+ Self::draw_object(obj);
+ });
} else {
- VertexArray::draw_arrays(
- &vert_arr_curr_bound,
- PrimitiveKind::Triangles,
- 0,
- 3,
- );
+ Self::draw_object(obj);
}
});
- };
-
- if let Some(texture) = obj.texture() {
- texture.inner().bind(|_| {
- draw();
- });
- } else {
- draw();
- }
}
}
@@ -136,6 +124,27 @@ impl Renderer
set_debug_message_control(None, None, None, &[], MessageIdsAction::Disable);
}
+
+ fn draw_object(obj: &Object)
+ {
+ obj.renderable().vertex_arr.bind(|vert_arr_curr_bound| {
+ if let Some(index_info) = &obj.renderable().index_info {
+ VertexArray::draw_elements(
+ &vert_arr_curr_bound,
+ PrimitiveKind::Triangles,
+ 0,
+ index_info.cnt,
+ );
+ } else {
+ VertexArray::draw_arrays(
+ &vert_arr_curr_bound,
+ PrimitiveKind::Triangles,
+ 0,
+ 3,
+ );
+ }
+ });
+ }
}
#[derive(Debug)]
@@ -204,19 +213,22 @@ fn apply_transformation_matrices(
object: &Object,
camera: &Camera,
window_size: &WindowSize,
+ shader_program_curr_bound: &CurrentlyBound<ShaderProgram>,
)
{
- object
- .renderable()
- .shader_program
- .set_uniform_matrix_4fv(cstr!("model"), &object.transform().as_matrix());
+ object.renderable().shader_program.set_uniform_matrix_4fv(
+ shader_program_curr_bound,
+ cstr!("model"),
+ &object.transform().as_matrix(),
+ );
let view = camera.as_matrix();
- object
- .renderable()
- .shader_program
- .set_uniform_matrix_4fv(cstr!("view"), &view);
+ object.renderable().shader_program.set_uniform_matrix_4fv(
+ shader_program_curr_bound,
+ cstr!("view"),
+ &view,
+ );
#[allow(clippy::cast_precision_loss)]
let projection = new_perspective(
@@ -226,10 +238,11 @@ fn apply_transformation_matrices(
0.1,
);
- object
- .renderable()
- .shader_program
- .set_uniform_matrix_4fv(cstr!("projection"), &projection);
+ object.renderable().shader_program.set_uniform_matrix_4fv(
+ shader_program_curr_bound,
+ cstr!("projection"),
+ &projection,
+ );
}
#[cfg(feature = "debug")]