URI: 
       Fixed view port setting. - icy_draw - icy_draw is the successor to mystic draw. fork / mirror
  HTML git clone https://git.drkhsh.at/icy_draw.git
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 608e4f4e7b5b14e921c81e76b7a5c4a518d2889b
   DIR parent 40567bb2601d5059046a7bb5cb0d6d0dbc75488a
  HTML Author: Mike Krueger <mkrueger@posteo.de>
       Date:   Tue, 13 Dec 2022 18:01:07 +0100
       
       Fixed view port setting.
       
       Diffstat:
         M src/ui/ansi_editor/mod.rs           |      10 ++++++----
         M src/ui/ansi_editor/render.rs        |      38 +++++++++++++++++++------------
         M src/ui/ansi_editor/render.shader.f… |     206 +++----------------------------
       
       3 files changed, 43 insertions(+), 211 deletions(-)
       ---
   DIR diff --git a/src/ui/ansi_editor/mod.rs b/src/ui/ansi_editor/mod.rs
       @@ -108,7 +108,8 @@ impl Document for AnsiEditor {
                    .auto_shrink([false; 2])
                    .stick_to_bottom(true)
                    .show_viewport(ui, |ui, viewport| {
       -                let (draw_area, mut response) = ui.allocate_at_least(size, egui::Sense::click());
       +                let (id, draw_area) = ui.allocate_space(size);
       +                let mut response = ui.interact(draw_area, id, egui::Sense::click());
        
                        let rect = Rect::from_min_size(
                            draw_area.left_top()
       @@ -136,11 +137,12 @@ impl Document for AnsiEditor {
                        
                        let buffer_view  = self.buffer_view.clone();
                        let callback = egui::PaintCallback {
       -                    rect,
       +                    rect: draw_area,
                            callback: std::sync::Arc::new(egui_glow::CallbackFn::new(
       -                        move |_info, painter| {
       +                        move |info, painter| {
       +
                                    buffer_view.lock().unwrap().update_buffer(painter.gl());
       -                            buffer_view.lock().unwrap().paint(painter.gl(), rect);
       +                            buffer_view.lock().unwrap().paint(painter.gl(), info, draw_area, rect);
                                },
                            )),
                        };
   DIR diff --git a/src/ui/ansi_editor/render.rs b/src/ui/ansi_editor/render.rs
       @@ -1,4 +1,4 @@
       -use eframe::epaint::{Rect, Vec2};
       +use eframe::epaint::{Rect, Vec2, PaintCallbackInfo};
        use glow::NativeTexture;
        use icy_engine::Buffer;
        use std::{
       @@ -9,7 +9,7 @@ use std::{
        use super::BufferView;
        
        impl BufferView {
       -    pub fn paint(&self, gl: &glow::Context, rect: Rect) {
       +    pub fn paint(&self, gl: &glow::Context, info: PaintCallbackInfo, draw_rect: Rect, rect: Rect) {
                use glow::HasContext as _;
                unsafe {
                    gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.framebuffer));
       @@ -236,14 +236,18 @@ impl BufferView {
                        }
                    }
        
       +            println!("{:?} / {:?}", draw_rect, rect);
       +
                    // draw Framebuffer
                    gl.bind_framebuffer(glow::FRAMEBUFFER, None);
                    gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
       +
       +
                    gl.viewport(
       -                rect.left() as i32,
       -                rect.top() as i32,
       -                rect.width() as i32,
       -                rect.height() as i32,
       +                draw_rect.left() as i32,
       +                (info.screen_size_px[1] as f32 - draw_rect.max.y * info.pixels_per_point) as i32,
       +                draw_rect.width() as i32,
       +                draw_rect.height() as i32,
                    );
                    gl.use_program(Some(self.draw_program));
                    gl.active_texture(glow::TEXTURE0);
       @@ -263,18 +267,22 @@ impl BufferView {
                            crate::ui::main_window::PostProcessing::CRT1 => 1.0,
                        },*/
                    );
       -
       -            gl.uniform_2_f32(
       -                gl.get_uniform_location(self.draw_program, "u_resolution")
       +            gl.uniform_4_f32(
       +                gl.get_uniform_location(self.draw_program, "u_draw_rect")
                            .as_ref(),
       -                rect.width(),
       -                rect.height(),
       +                    draw_rect.left(),
       +                draw_rect.top(),
       +                draw_rect.width(),
       +                draw_rect.height(),
                    );
       -            gl.uniform_2_f32(
       -                gl.get_uniform_location(self.draw_program, "u_position")
       +
       +            gl.uniform_4_f32(
       +                gl.get_uniform_location(self.draw_program, "u_draw_area")
                            .as_ref(),
       -                rect.left(),
       -                rect.top(),
       +                    draw_rect.left() + rect.left(),
       +                    draw_rect.top() + rect.top(),
       +                    draw_rect.left() + rect.right(),
       +                    draw_rect.top() + rect.bottom()
                    );
        
                    gl.bind_vertex_array(Some(self.vertex_array));
   DIR diff --git a/src/ui/ansi_editor/render.shader.frag b/src/ui/ansi_editor/render.shader.frag
       @@ -2,203 +2,25 @@
        in vec2 UV;
        
        uniform sampler2D u_render_texture;
       -uniform vec2      u_resolution;
       -uniform vec2      u_position;
       +uniform vec4      u_draw_rect;
       +uniform vec4      u_draw_area;
        uniform float     u_effect;
        
        out vec3 color;
        
       -// Scanline CRT shader from
       -// From https://www.shadertoy.com/view/4scSR8
       -//
       -// PUBLIC DOMAIN CRT STYLED SCAN-LINE SHADER
       -//
       -//         by Timothy Lottes
       -//
       -// This is more along the style of a really good CGA arcade monitor.
       -// With RGB inputs instead of NTSC.
       -// The shadow mask example has the mask rotated 90 degrees for less chromatic aberration.
       -//
       -// Left it unoptimized to show the theory behind the algorithm.
       -//
       -// It is an example what I personally would want as a display option for pixel art games.
       -// Please take and use, change, or whatever.
       -//
       -
       -// Hardness of scanline.
       -//        -8.0 = soft
       -// -16.0 = medium
       -float sHardScan = -8.0;
       -
       -// Hardness of pixels in scanline.
       -// -2.0 = soft
       -// -4.0 = hard
       -float kHardPix = -3.0;
       -
       -// Display warp.
       -// 0.0 = none
       -// 1.0 / 8.0 = extreme
       -vec2 kWarp = vec2(1.0 / 32.0, 1.0 / 24.0);
       -//const vec2 kWarp = vec2(0);
       -
       -// Amount of shadow mask.
       -float kMaskDark = 0.5;
       -float kMaskLight = 1.5;
       -
       -//------------------------------------------------------------------------
       -
       -// sRGB to Linear.
       -// Assuing using sRGB typed textures this should not be needed.
       -float toLinear1(float c) {
       -        return (c <= 0.04045) ?
       -                (c / 12.92) :
       -                pow((c + 0.055) / 1.055, 2.4);
       -}
       -vec3 toLinear(vec3 c) {
       -        return vec3(toLinear1(c.r), toLinear1(c.g), toLinear1(c.b));
       -}
       -
       -// Linear to sRGB.
       -// Assuing using sRGB typed textures this should not be needed.
       -float toSrgb1(float c) {
       -        return(c < 0.0031308 ?
       -                (c * 12.92) :
       -                (1.055 * pow(c, 0.41666) - 0.055));
       -}
       -vec3 toSrgb(vec3 c) {
       -        return vec3(toSrgb1(c.r), toSrgb1(c.g), toSrgb1(c.b));
       -}
       -
       -// Nearest emulated sample given floating point position and texel offset.
       -// Also zero's off screen.
       -vec4 fetch(vec2 pos, vec2 off)
       -{
       -        pos = floor(pos * u_resolution + off) / u_resolution;
       -        if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5)
       -                return vec4(vec3(0.0), 0.0);
       -           
       -    vec4 sampledColor = texture(u_render_texture, pos.xy, -16.0);
       -    /*
       -    sampledColor = vec4(
       -        (sampledColor.rgb * sampledColor.a) +
       -                (kBackgroundColor * (1.0 - sampledColor.a)),
       -        1.0
       -    );*/
       -    
       -        return vec4(
       -        toLinear(sampledColor.rgb),
       -        sampledColor.a
       -    );
       -}
       -
       -// Distance in emulated pixels to nearest texel.
       -vec2 dist(vec2 pos) {
       -        pos = pos * u_resolution;
       -        return -((pos - floor(pos)) - vec2(0.5));
       -}
       -
       -// 1D Gaussian.
       -float gaus(float pos, float scale) {
       -        return exp2(scale * pos * pos);
       -}
       -
       -// 3-tap Gaussian filter along horz line.
       -vec3 horz3(vec2 pos, float off)
       -{
       -        vec3 b = fetch(pos, vec2(-1.0, off)).rgb;
       -        vec3 c = fetch(pos, vec2( 0.0, off)).rgb;
       -        vec3 d = fetch(pos, vec2(+1.0, off)).rgb;
       -        float dst = dist(pos).x;
       -        // Convert distance to weight.
       -        float scale = kHardPix;
       -        float wb = gaus(dst - 1.0, scale);
       -        float wc = gaus(dst + 0.0, scale);
       -        float wd = gaus(dst + 1.0, scale);
       -        // Return filtered sample.
       -        return (b * wb + c * wc + d * wd) / (wb + wc + wd);
       -}
       -
       -// 5-tap Gaussian filter along horz line.
       -vec3 horz5(vec2 pos, float off)
       -{
       -        vec3 a = fetch(pos, vec2(-2.0, off)).rgb;
       -        vec3 b = fetch(pos, vec2(-1.0, off)).rgb;
       -        vec3 c = fetch(pos, vec2( 0.0, off)).rgb;
       -        vec3 d = fetch(pos, vec2(+1.0, off)).rgb;
       -        vec3 e = fetch(pos, vec2(+2.0, off)).rgb;
       -        float dst = dist(pos).x;
       -        // Convert distance to weight.
       -        float scale = kHardPix;
       -        float wa = gaus(dst - 2.0, scale);
       -        float wb = gaus(dst - 1.0, scale);
       -        float wc = gaus(dst + 0.0, scale);
       -        float wd = gaus(dst + 1.0, scale);
       -        float we = gaus(dst + 2.0, scale);
       -        // Return filtered sample.
       -        return (a * wa + b * wb + c * wc + d * wd + e * we) / (wa + wb + wc + wd + we);
       -}
       -
       -// Return scanline weight.
       -float scan(vec2 pos, float off) {
       -        float dst = dist(pos).y;
       -        return gaus(dst + off, sHardScan);
       -}
       -
       -// Allow nearest three lines to effect pixel.
       -vec3 tri(vec2 pos)
       -{
       -        vec3 a = horz3(pos, -1.0);
       -        vec3 b = horz5(pos,  0.0);
       -        vec3 c = horz3(pos, +1.0);
       -        float wa = scan(pos, -1.0);
       -        float wb = scan(pos,  0.0);
       -        float wc = scan(pos, +1.0);
       -        return a * wa + b * wb + c * wc;}
       -
       -// Distortion of scanlines, and end of screen alpha.
       -vec2 warp(vec2 pos)
       -{
       -        pos = pos * 2.0 - 1.0;
       -        pos *= vec2(
       -                1.0 + (pos.y * pos.y) * kWarp.x,
       -                1.0 + (pos.x * pos.x) * kWarp.y
       -        );
       -        return pos * 0.5 + 0.5;
       -}
       -
       -// Shadow mask.
       -vec3 mask(vec2 pos)
       -{
       -        pos.x += pos.y * 3.0;
       -        vec3 mask = vec3(kMaskDark, kMaskDark, kMaskDark);
       -        pos.x = fract(pos.x / 6.0);
       -        if (pos.x < 0.333)
       -                mask.r = kMaskLight;
       -        else if (pos.x < 0.666)
       -                mask.g = kMaskLight;
       -        else
       -                mask.b = kMaskLight;
       -        return mask;
       -}
       +void main() {
       +        /*
       +        vec2 uv   = (gl_FragCoord.xy - u_draw_rect.xy) / u_draw_rect.zw;
        
       -float rand(vec2 co) {
       -        return fract(sin(dot(co.xy , vec2(12.9898, 78.233))) * 43758.5453);
       -}
       +        vec2 from = u_draw_area.xy / u_draw_rect.zw;
       +        vec2 to   = u_draw_area.zw / u_draw_rect.zw;
        
       -void scanlines1()
       -{
       -    vec2 fragCoord = (gl_FragCoord.xy - u_position);
       -    vec2 pos = fragCoord / u_resolution;
       -    vec4 unmodifiedColor = fetch(pos, vec2(0));
       -    color.rgb = tri(pos) * mask(fragCoord.xy);
       -        color = toSrgb(color.rgb);
       -}
       +        if (from.x <= uv.x && uv.x < to.x && 
       +            from.y <= uv.y && uv.y < to.y) {
       +                color = texture(u_render_texture, (uv - from) / (to - from) ).xyz;
       +        } else {
       +                color = vec3(1.0);
       +        }*/
        
       -void main() {
       -    if (u_effect < 1.0) { 
       -        vec2 uv = (gl_FragCoord.xy - u_position) / u_resolution;
       -        color = texture(u_render_texture, uv).xyz;
       -    } else {
       -        scanlines1();
       -    }
       +        color = vec3(1.0);
        }
        \ No newline at end of file