From 8add900db43bdd7c38805925524a0aa5bd333df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= <49535803+damacaa@users.noreply.github.com> Date: Sat, 5 Oct 2024 16:13:55 +0200 Subject: [PATCH] Renderer refactor --- src/weird-renderer/Renderer.cpp | 4 +++ src/weird-renderer/Texture.cpp | 15 ++++------ src/weird-renderer/Texture.h | 5 ---- src/weird-renderer/shaders/output.frag | 41 +++++++------------------- 4 files changed, 21 insertions(+), 44 deletions(-) diff --git a/src/weird-renderer/Renderer.cpp b/src/weird-renderer/Renderer.cpp index e423680..432b7ae 100644 --- a/src/weird-renderer/Renderer.cpp +++ b/src/weird-renderer/Renderer.cpp @@ -196,7 +196,11 @@ namespace WeirdRenderer // Tell OpenGL which Shader Program we want to use glUseProgram(m_outputShaderProgram.ID); + GLuint u_colorTextureLocation = glGetUniformLocation(m_outputShaderProgram.ID, "u_colorTexture"); + glUniform1i(u_colorTextureLocation, 0); + m_outputTexture.bind(0); + m_outputRenderPlane.Draw(m_outputShaderProgram); diff --git a/src/weird-renderer/Texture.cpp b/src/weird-renderer/Texture.cpp index eb3e8a4..e2c6cc4 100644 --- a/src/weird-renderer/Texture.cpp +++ b/src/weird-renderer/Texture.cpp @@ -111,8 +111,6 @@ namespace WeirdRenderer glBindTexture(GL_TEXTURE_2D, ID); } - - void Texture::unbind() const { glBindTexture(GL_TEXTURE_2D, 0); @@ -211,21 +209,20 @@ namespace WeirdRenderer Texture::Texture(int width, int height, GLuint filterMode) { glGenTextures(1, &ID); - bind(); + glBindTexture(GL_TEXTURE_2D, ID); - unsigned char* textureData = new unsigned char[width * height * 4]; + float* textureData = new float[width * height * 4]; for (size_t i = 0; i < width * height * 4; i++) { - textureData[i] = 255; + textureData[i] = 1.0f; } - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, textureData); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterMode); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterMode); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // Prevents edge bleeding - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Prevents edge bleeding + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); delete[] textureData; diff --git a/src/weird-renderer/Texture.h b/src/weird-renderer/Texture.h index 849d5e6..e792791 100644 --- a/src/weird-renderer/Texture.h +++ b/src/weird-renderer/Texture.h @@ -40,11 +40,6 @@ namespace WeirdRenderer void dispose() const; void saveToDisk(const char* fileName); - - private: - - GLenum m_textureSlot; - }; } diff --git a/src/weird-renderer/shaders/output.frag b/src/weird-renderer/shaders/output.frag index 9d6ae43..c285e80 100644 --- a/src/weird-renderer/shaders/output.frag +++ b/src/weird-renderer/shaders/output.frag @@ -1,7 +1,7 @@ #version 330 core // Outputs colors in RGBA -out vec4 FragColor; +out vec3 FragColor; uniform vec2 u_resolution; uniform float u_renderScale; @@ -9,9 +9,9 @@ uniform float u_renderScale; uniform float u_time; uniform sampler2D u_colorTexture; -uniform sampler2D u_depthTexture; -float rand(vec2 co){ +float rand(vec2 co) +{ return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); } @@ -24,38 +24,19 @@ void main() vec4 color = texture(u_colorTexture, screenUV); float distance = color.w; - - vec2 texelOffset = vec2(0.005, 0.0025); - - // Sample the texture at the center and neighboring pixels - vec4 colorCenter = texture(u_colorTexture, screenUV); - vec4 colorRight = texture(u_colorTexture, screenUV + vec2(texelOffset.x, 0.0)); - vec4 colorLeft = texture(u_colorTexture, screenUV - vec2(texelOffset.x, 0.0)); - vec4 colorUp = texture(u_colorTexture, screenUV + vec2(0.0, texelOffset.y)); - vec4 colorDown = texture(u_colorTexture, screenUV - vec2(0.0, texelOffset.y)); - - // Simple average of the neighboring pixels for antialiasing - vec4 averagedColor = (colorCenter + colorRight + colorLeft + colorUp + colorDown) / 5.0; - - //distance = averagedColor.r; - - //distance = sin(100.0 * distance); - - //vec3 col = vec3(distance); vec3 col = distance <= 0.5 ? color.xyz : vec3(0.9); - //vec2 TexCoords = fract(gl_FragCoord.xy * u_renderScale); - // Dot effect - //vec2 dist = TexCoords - vec2(0.5f, 0.5f); - //float mask = ((1-length(dist)) - 0.25) * 2; - //col *= mask; + // vec2 TexCoords = fract(gl_FragCoord.xy * u_renderScale); + // vec2 dist = TexCoords - vec2(0.5f, 0.5f); + // float mask = ((1.0 -length(dist)) - 0.25) * 2; // CRT Line effect - //float mask = sin(3.14 * (gl_FragCoord.y * u_renderScale)); - //mask = mask > 0.25 ? 1.0 : 0.0; - //col *= mask; + // float mask = sin(3.14 * (gl_FragCoord.y * u_renderScale)); + // mask = mask > 0.25 ? 1.0 : 0.0; + //FragColor = color.xyz * mask; //FragColor = color.wwww; - FragColor = color; + + FragColor = color.xyz; }