Skip to content

Commit

Permalink
fix image flipping
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed Jan 23, 2024
1 parent 2b8ef72 commit 9c94202
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
10 changes: 5 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ impl App {
Output::FlipHorizontal => {
let view = self.image_view.as_mut().unwrap();
if view.rotation() % 2 != 0 {
view.flip_horizontal();
stack.push(UndoFrame::FlipHorizontal);
} else {
view.flip_vertical();
stack.push(UndoFrame::FlipVertical);
} else {
view.flip_horizontal();
stack.push(UndoFrame::FlipHorizontal);
}
}
Output::FlipVertical => {
let view = self.image_view.as_mut().unwrap();
if view.rotation() % 2 != 0 {
view.flip_horizontal();
stack.push(UndoFrame::FlipVertical);
stack.push(UndoFrame::FlipHorizontal);
} else {
view.flip_vertical();
stack.push(UndoFrame::FlipHorizontal);
stack.push(UndoFrame::FlipVertical);
}
}
Output::Rotate(dir) => {
Expand Down
21 changes: 18 additions & 3 deletions src/app/image_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ impl ImageView {
(pre_rotation * rotation) * post_rotation
}

pub fn get_flip_mat(&self) -> Matrix4<f32> {
let hori = if self.horizontal_flip { -1.0 } else { 1.0 };

let vert = if self.vertical_flip { -1.0 } else { 1.0 };

let flip = Matrix4::from_nonuniform_scale(hori, vert, 1.0);
let pre_rotation =
Matrix4::from_translation(Vector3::new(self.size.x() / 2.0, self.size.y() / 2.0, 0.0));
let post_rotation = Matrix4::from_translation(Vector3::new(
-self.size.x() / 2.0,
-self.size.y() / 2.0,
0.0,
));
(pre_rotation * flip) * post_rotation
}

pub fn get_uniform(&self, size: Vec2<f32>) -> image_renderer::Uniform {
let ortho: Matrix4<f32> = Ortho {
left: 0.0,
Expand All @@ -106,13 +122,12 @@ impl ImageView {
let scale = Matrix4::from_scale(self.scale);
let translation = Matrix4::from_translation(Vector3::new(position.x(), position.y(), 0.0));

let flip = self.get_flip_mat();
let rotation = self.get_rotation_mat();
let matrix = OPENGL_TO_WGPU_MATRIX * ortho * translation * scale * rotation;
let matrix = OPENGL_TO_WGPU_MATRIX * ortho * translation * scale * flip * rotation;

image_renderer::Uniform {
matrix,
flip_horizontal: self.horizontal_flip as u32,
flip_vertical: self.vertical_flip as u32,
size,
padding: Default::default(),
hue: self.hue,
Expand Down
6 changes: 1 addition & 5 deletions src/app/image_view/image_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ pub struct Uniform {
pub matrix: Matrix4<f32>,
pub size: Vec2<f32>,
pub padding: Vec2<f32>, // Padding because glsl is adding dumb padding
pub flip_horizontal: u32,
pub flip_vertical: u32,
pub hue: f32,
pub contrast: f32,
pub brightness: f32,
Expand All @@ -63,8 +61,6 @@ impl Default for Uniform {
fn default() -> Self {
Self {
matrix: Matrix4::identity(),
flip_horizontal: Default::default(),
flip_vertical: Default::default(),
size: Default::default(),
padding: Default::default(),
hue: Default::default(),
Expand Down Expand Up @@ -162,7 +158,7 @@ impl Renderer {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: Some(wgpu::Face::Back),
cull_mode: None,
polygon_mode: wgpu::PolygonMode::Fill,
unclipped_depth: false,
conservative: false,
Expand Down
8 changes: 4 additions & 4 deletions src/app/image_view/mosaic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ fn get_vertex_buffer(
Vec2::new(0.0, 0.0),
);
let shape = [
Vertex::new(start_x, start_y, texture_cords.0.x(), texture_cords.0.y()),
Vertex::new(start_x, end_y, texture_cords.1.x(), texture_cords.1.y()),
Vertex::new(end_x, start_y, texture_cords.2.x(), texture_cords.2.y()),
Vertex::new(end_x, end_y, texture_cords.3.x(), texture_cords.3.y()),
Vertex::new(start_x, start_y, texture_cords.3.x(), texture_cords.3.y()),
Vertex::new(start_x, end_y, texture_cords.2.x(), texture_cords.2.y()),
Vertex::new(end_x, start_y, texture_cords.1.x(), texture_cords.1.y()),
Vertex::new(end_x, end_y, texture_cords.0.x(), texture_cords.0.y()),
];

wgpu.device
Expand Down
2 changes: 0 additions & 2 deletions src/shader/image.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ layout(location = 0) out vec4 color;
struct InputUniform {
mat4 matrix;
vec2 size;
uint flip_horizontal;
uint flip_vertical;
float hue;
float contrast;
float brightness;
Expand Down
12 changes: 1 addition & 11 deletions src/shader/image.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ layout(location = 0) out vec2 v_tex_coords;

struct InputUniform {
mat4 matrix;
uint flip_horizontal;
uint flip_vertical;
vec2 size;
float hue;
float contrast;
float brightness;
Expand All @@ -16,18 +15,9 @@ struct InputUniform {
uint invert;
};

vec2 size = vec2(1920, 1080);


layout(set = 0, binding = 0) uniform InputUniform input;

void main() {
v_tex_coords = tex_coords;
if(bool(input.flip_horizontal)) {
v_tex_coords.x = 1 - v_tex_coords.x;
}
if(bool(input.flip_vertical)) {
v_tex_coords.y = 1 - v_tex_coords.y;
}
gl_Position = input.matrix * vec4(position, 0.0, 1.0);
}

0 comments on commit 9c94202

Please sign in to comment.