diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/fshader_post.glsl | 8 | ||||
-rw-r--r-- | src/main.rs | 22 | ||||
-rw-r--r-- | src/win.rs | 113 |
4 files changed, 123 insertions, 22 deletions
@@ -22,7 +22,7 @@ you can also reduce `max-iterations` and increase `distance-threshold` if it's s - F to go fullscreen - F10 to take a screenshot (saved to a folder called `screenshots`). The SDF string is saved in the PNG file's metadata. If you open it with notepad, you'll see it towards the beginning of the file. -- Press Escape twice to quit +- Press Ctrl+Q to quit (or press escape and click the "quit" button) - You can use Shift to move faster and Ctrl to move slower. ## saving SDFs diff --git a/src/fshader_post.glsl b/src/fshader_post.glsl index 6e8a66f..c44371a 100644 --- a/src/fshader_post.glsl +++ b/src/fshader_post.glsl @@ -1,10 +1,16 @@ uniform sampler2D u_main_texture; uniform sampler2D u_menu_texture; uniform float u_paused; +uniform float u_aspect_ratio; +uniform float u_menu_scale; IN vec2 uv; void main() { vec4 color = texture(u_main_texture, uv) * (1.0 - 0.5 * u_paused); - color += texture(u_menu_texture, uv).xxxx * u_paused; + vec2 menu_uv = (uv * 2.0 - 1.0) * vec2(1.0, -1.0); + menu_uv *= 1.0 / u_menu_scale; + menu_uv = menu_uv * vec2(u_aspect_ratio, 1.0); + menu_uv = 0.5 * menu_uv + 0.5; + color = mix(color, vec4(1.0), u_paused * texture(u_menu_texture, menu_uv).x); gl_FragColor = clamp(color, 0.0, 1.0); } diff --git a/src/main.rs b/src/main.rs index 310206c..eaeaf9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,8 @@ type Mat3 = Matrix3<f32>; type Mat4 = Matrix4<f32>; type Rot3 = Rotation3<f32>; +const MENU_SCALE: f32 = 0.5; + #[repr(i32)] #[derive(Clone, Copy)] enum Icon { @@ -281,7 +283,7 @@ impl State { ); let main_texconfig = win::TextureParams { - mag_filter: win::TextureFilter::Nearest, + mag_filter: win::TextureMagFilter::Nearest, ..Default::default() }; let main_framebuffer_texture = window.create_texture(&main_texconfig); @@ -313,7 +315,9 @@ impl State { let menu_texture = { let params = win::TextureParams { - min_filter: win::TextureFilter::Linear, + min_filter: win::TextureMinFilter::LinearMipmapLinear, + wrap_mode: win::TextureWrap::ClampToEdge.both(), + mipmap: true, ..Default::default() }; let mut tex = window.create_texture(¶ms); @@ -507,12 +511,11 @@ impl State { use win::Key::*; match event { Quit => return false, + KeyDown { + key: Q, modifier, .. + } if modifier.ctrl() => return false, KeyDown { key: Escape, .. } => { - if self.esc_menu { - return false; - } else { - self.esc_menu = true; - } + self.esc_menu = !self.esc_menu; } KeyDown { key: F1, .. } => self.show_debug_info = !self.show_debug_info, KeyDown { key: R, .. } => { @@ -741,6 +744,11 @@ impl State { window.uniform1f("u_paused", if self.esc_menu { 1.0 } else { 0.0 }); window.uniform_texture("u_main_texture", 0); window.uniform_texture("u_menu_texture", 1); + window.uniform1f( + "u_aspect_ratio", + render_resolution.0 as f32 / render_resolution.1 as f32, + ); + window.uniform1f("u_menu_scale", MENU_SCALE); self.post_array.draw(); window.swap(); @@ -953,7 +953,17 @@ extern "system" fn gl_message_callback( if severity == gl::DEBUG_SEVERITY_NOTIFICATION { return; } - println!("Message from opengl: {message}"); + static COUNT: Mutex<u32> = Mutex::new(0); + if let Ok(mut count) = COUNT.lock() { + let max_messages = 10; + if *count < max_messages { + println!("Message from opengl: {message}"); + *count += 1; + if *count == max_messages { + println!("....Further messages from opengl will not be displayed."); + } + } + } } pub struct Texture { @@ -1044,6 +1054,25 @@ impl Texture { gl::TEXTURE_MAG_FILTER, params.mag_filter.to_gl(), ); + gl::TexParameteri( + gl::TEXTURE_2D, + gl::TEXTURE_WRAP_S, + params.wrap_mode.0.to_gl(), + ); + gl::TexParameteri( + gl::TEXTURE_2D, + gl::TEXTURE_WRAP_T, + params.wrap_mode.1.to_gl(), + ); + gl::TexParameterfv( + gl::TEXTURE_2D, + gl::TEXTURE_BORDER_COLOR, + ¶ms.border_color.r as *const f32, + ); + + if params.mipmap { + gl::GenerateMipmap(gl::TEXTURE_2D); + } } Ok(()) } @@ -1084,33 +1113,91 @@ impl Drop for Texture { } } -#[derive(Copy, Clone)] -pub enum TextureFilter { +#[derive(Copy, Clone, Debug)] +pub enum TextureMinFilter { Nearest, Linear, + NearestMipmapNearest, + LinearMipmapNearest, + NearestMipmapLinear, + LinearMipmapLinear, } -impl TextureFilter { +#[derive(Copy, Clone, Debug)] +pub enum TextureMagFilter { + Nearest, + Linear, +} + +impl TextureMinFilter { fn to_gl(self) -> GLint { - use TextureFilter::*; - match self { - Nearest => gl::NEAREST as _, - Linear => gl::LINEAR as _, - } + use TextureMinFilter::*; + (match self { + Nearest => gl::NEAREST, + Linear => gl::LINEAR, + NearestMipmapNearest => gl::NEAREST_MIPMAP_NEAREST, + LinearMipmapNearest => gl::LINEAR_MIPMAP_NEAREST, + NearestMipmapLinear => gl::NEAREST_MIPMAP_LINEAR, + LinearMipmapLinear => gl::LINEAR_MIPMAP_LINEAR, + }) as _ + } +} + +impl TextureMagFilter { + fn to_gl(self) -> GLint { + use TextureMagFilter::*; + (match self { + Nearest => gl::NEAREST, + Linear => gl::LINEAR, + }) as _ + } +} + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum TextureWrap { + ClampToEdge, + ClampToBorder, + MirroredRepeat, + Repeat, + MirrorClampToEdge, +} + +impl TextureWrap { + fn to_gl(self) -> GLint { + use TextureWrap::*; + (match self { + ClampToEdge => gl::CLAMP_TO_EDGE, + ClampToBorder => gl::CLAMP_TO_BORDER, + MirroredRepeat => gl::MIRRORED_REPEAT, + Repeat => gl::REPEAT, + MirrorClampToEdge => gl::MIRROR_CLAMP_TO_EDGE, + }) as _ + } + + /// use this wrap mode for both dimensions + pub fn both(self) -> (Self, Self) { + (self, self) } } #[derive(Clone)] pub struct TextureParams { - pub min_filter: TextureFilter, - pub mag_filter: TextureFilter, + pub min_filter: TextureMinFilter, + pub mag_filter: TextureMagFilter, + /// (wrap mode for s, wrap mode for t) + pub wrap_mode: (TextureWrap, TextureWrap), + pub border_color: ColorF32, + pub mipmap: bool, } impl Default for TextureParams { fn default() -> Self { Self { - min_filter: TextureFilter::Nearest, - mag_filter: TextureFilter::Linear, + min_filter: TextureMinFilter::Nearest, + mag_filter: TextureMagFilter::Linear, + wrap_mode: TextureWrap::Repeat.both(), + border_color: ColorF32::rgba(0.0, 0.0, 0.0, 0.0), + mipmap: false, } } } |