From a6a419fdff1c3d1b1b2de32dfd86458eb715f966 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sun, 17 Sep 2023 15:50:54 +0200 Subject: [PATCH] hud: crosshair with fragment shader --- shaders/shader-quad.fs | 18 ++++++++++++++++++ src/renderer.cpp | 3 +++ 2 files changed, 21 insertions(+) diff --git a/shaders/shader-quad.fs b/shaders/shader-quad.fs index c4dc9a6..de35cc8 100644 --- a/shaders/shader-quad.fs +++ b/shaders/shader-quad.fs @@ -4,7 +4,25 @@ in vec2 TexCoord; out vec4 FragColor; uniform sampler2D renderTex; +uniform int screenWidth; +uniform int screenHeight; +uniform int crosshairType; void main(){ + float crosshair_alpha = 0.8; + + float dist = length(gl_FragCoord.xy-vec2(screenWidth/2, screenHeight/2)); + FragColor = texture(renderTex, TexCoord); + /*float crosshair_color = (FragColor.x + FragColor.y + FragColor.z) / 3; + /*if(crosshair_color <= 0.5) crosshair_color = 1.0; + /*else crosshair_color = 0.0;*/ + float crosshair_color = 1.0; + + if(dist <= 7){ + if( (crosshairType == 0 && dist >= 5) || + (crosshairType == 1 && ( int(gl_FragCoord.x) == int(screenWidth / 2) || + int(gl_FragCoord.y) == int(screenHeight / 2)) ) + ) FragColor = vec4(vec3(crosshair_color), crosshair_alpha); + } } diff --git a/src/renderer.cpp b/src/renderer.cpp index eeaae93..ddd8a28 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -217,6 +217,9 @@ namespace renderer{ glDisable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, renderTex); quadShader->use(); + quadShader->setInt("screenWidth", screenWidth); + quadShader->setInt("screenHeight", screenHeight); + quadShader->setInt("crosshairType", 1); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); }