Skip to content

Commit

Permalink
Merge pull request #303 from tdaffin/fix_crop
Browse files Browse the repository at this point in the history
Fix for scissor problem
  • Loading branch information
bvssvni authored May 21, 2019
2 parents d33398b + 13d9e88 commit 0eb95de
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ image = "0.21.0"
gl = "0.11.0"
piston-shaders_graphics2d = "0.3.1"
piston-texture = "0.6.0"
piston-viewport = "0.5.0"
shader_version = "0.3.0"
fnv = "1.0.2"

Expand Down
11 changes: 8 additions & 3 deletions src/back_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ pub struct GlGraphics {
current_program: Option<GLuint>,
// Keeps track of the current draw state.
current_draw_state: Option<DrawState>,
// Keeps track of the current viewport
current_viewport: Option<Viewport>,
}

impl<'a> GlGraphics {
Expand All @@ -321,6 +323,7 @@ impl<'a> GlGraphics {
textured: Textured::new(glsl),
current_program: None,
current_draw_state: None,
current_viewport: None,
}
}

Expand All @@ -339,11 +342,12 @@ impl<'a> GlGraphics {
textured: textured,
current_program: None,
current_draw_state: None,
current_viewport: None,
}
}

/// Sets viewport with normalized coordinates and center as origin.
pub fn viewport(&mut self, x: i32, y: i32, w: i32, h: i32) {
fn viewport(&mut self, x: i32, y: i32, w: i32, h: i32) {
unsafe {
gl::Viewport(x as GLint, y as GLint, w as GLsizei, h as GLsizei);
}
Expand Down Expand Up @@ -382,12 +386,12 @@ impl<'a> GlGraphics {
pub fn use_draw_state(&mut self, draw_state: &DrawState) {
match self.current_draw_state {
None => {
draw_state::bind_scissor(draw_state.scissor);
draw_state::bind_scissor(draw_state.scissor, &self.current_viewport);
draw_state::bind_stencil(draw_state.stencil);
draw_state::bind_blend(draw_state.blend);
}
Some(ref old_state) => {
draw_state::bind_state(old_state, draw_state);
draw_state::bind_state(old_state, draw_state, &self.current_viewport);
}
}
self.current_draw_state = Some(*draw_state);
Expand All @@ -405,6 +409,7 @@ impl<'a> GlGraphics {
let rect = viewport.rect;
let (x, y, w, h) = (rect[0], rect[1], rect[2], rect[3]);
self.viewport(x, y, w, h);
self.current_viewport = Some(viewport);
self.clear_program();
unsafe {
gl::Enable(gl::FRAMEBUFFER_SRGB);
Expand Down
33 changes: 24 additions & 9 deletions src/draw_state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use gl;
use graphics::draw_state::*;
use viewport::Viewport;

pub fn bind_state(old_state: &DrawState, new_state: &DrawState) {
pub fn bind_state(old_state: &DrawState, new_state: &DrawState, viewport: &Option<Viewport>) {
if old_state.scissor != new_state.scissor {
bind_scissor(new_state.scissor);
bind_scissor(new_state.scissor, viewport);
}
if old_state.stencil != new_state.stencil {
bind_stencil(new_state.stencil);
Expand All @@ -13,14 +14,28 @@ pub fn bind_state(old_state: &DrawState, new_state: &DrawState) {
}
}

pub fn bind_scissor(rect: Option<[u32; 4]>) {
pub fn bind_scissor(rect: Option<[u32; 4]>, viewport: &Option<Viewport>) {
match rect {
Some(r) => unsafe {
gl::Enable(gl::SCISSOR_TEST);
gl::Scissor(r[0] as gl::types::GLint,
r[1] as gl::types::GLint,
r[2] as gl::types::GLint,
r[3] as gl::types::GLint);
Some(r) => {
// https://www.khronos.org/opengl/wiki/Scissor_Test indicates that
// gl::Scissor takes x,y defined as lower left,
// but piston passes rect with x,y defined as upper left.
// To fix this we need to know height of the viewport
// so that we can transform y as top measured from top (yt)
// into y as bottom measured from bottom (yb)
// using yb = viewport_height - (yt + rect_height)
let yb = if let Some(vp) = viewport {
vp.rect[3] - (r[1] + r[3]) as i32
} else {
r[1] as i32
};
unsafe {
gl::Enable(gl::SCISSOR_TEST);
gl::Scissor(r[0] as gl::types::GLint,
yb as gl::types::GLint,
r[2] as gl::types::GLint,
r[3] as gl::types::GLint);
}
},
None => unsafe { gl::Disable(gl::SCISSOR_TEST) },
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate image;
extern crate gl;
extern crate graphics;
extern crate texture as texture_lib;
extern crate viewport;

pub use shader_version::{OpenGL, Shaders};
pub use shader_version::glsl::{GLSL};
Expand Down

0 comments on commit 0eb95de

Please sign in to comment.