Skip to content

Commit

Permalink
Added a way to rebind the internal state after an external gl call ha…
Browse files Browse the repository at this point in the history
…s messed with it
  • Loading branch information
VictorKoenders committed Sep 26, 2024
1 parent 0bca465 commit fc0995e
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::window::Window;
struct State {
gl: Arc<Context>,

vao: glow::NativeVertexArray,
current_vertex_buffer: Cell<Option<glow::Buffer>>,
current_index_buffer: Cell<Option<glow::Buffer>>,
current_shader: Cell<Option<glow::Program>>,
Expand All @@ -46,8 +47,9 @@ impl Graphics {
pub fn new(window: &mut Window) -> Graphics {
let gl = window.load_gl();

let vao: glow::NativeVertexArray;
unsafe {
let vao = gl.create_vertex_array().unwrap();
vao = gl.create_vertex_array().unwrap();
gl.bind_vertex_array(Some(vao));

gl.enable(glow::CULL_FACE);
Expand All @@ -66,6 +68,8 @@ impl Graphics {
Graphics {
state: Rc::new(State {
gl: Arc::new(gl),

vao,
current_vertex_buffer: Cell::new(None),
current_index_buffer: Cell::new(None),
current_shader: Cell::new(None),
Expand Down Expand Up @@ -94,6 +98,7 @@ impl Graphics {
unsafe {
pass.target.bind(self);

self.state.gl.bind_vertex_array(Some(self.state.vao));
self.bind_vertex_buffer(Some(pass.mesh.raw.vertex_buffer));
self.bind_index_buffer(Some(pass.mesh.raw.index_buffer));
self.bind_shader(Some(pass.shader.raw.id));
Expand Down Expand Up @@ -237,6 +242,20 @@ impl Graphics {
pub unsafe fn gl(&self) -> &Arc<Context> {
&self.state.gl
}

/// Rebind everything to the current state.
///
/// You should never need to call this, unless you're manipulating `.gl()` directly
pub fn rebind(&self) {
unsafe {
self.state.gl.bind_vertex_array(Some(self.state.vao));
}
self.bind_vertex_buffer(self.state.current_vertex_buffer.take());
self.bind_index_buffer(self.state.current_index_buffer.take());
self.bind_shader(self.state.current_shader.take());
self.bind_texture(self.state.current_texture.take());
self.bind_canvas(self.state.current_canvas.take());
}
}

pub trait Target {
Expand Down

0 comments on commit fc0995e

Please sign in to comment.