Skip to content

Commit

Permalink
[WIP] Fixed shadows, motion blur and crash
Browse files Browse the repository at this point in the history
  • Loading branch information
damacaa committed Oct 3, 2024
1 parent a41844c commit 882081d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/weird-renderer/RenderPlane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace WeirdRenderer
}


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
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
Expand Down
8 changes: 5 additions & 3 deletions src/weird-renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ namespace WeirdRenderer
m_sdfShaderProgram.setUniform("u_time", time);
m_sdfShaderProgram.setUniform("u_resolution", glm::vec2(m_renderWidth, m_renderHeight));

m_sdfShaderProgram.setUniform("u_blendIterations", g_renderedFramesCounter % 3 == 0 ? 1 : 0);
m_sdfShaderProgram.setUniform("u_blendIterations", g_renderedFramesCounter % 1 == 0 ? 1 : 0);
g_renderedFramesCounter++;


Expand Down Expand Up @@ -270,16 +270,18 @@ namespace WeirdRenderer

m_outputRenderPlane.Draw(m_outputShaderProgram);



// Screenshot
if (Input::GetKeyDown(Input::O)) {
glBindTexture(GL_TEXTURE_2D, m_postProcessRenderPlane.m_colorTexture);
int width, height;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);

unsigned char* data = new unsigned char[width * height * 4]; // Assuming 4 channels (RGBA)
float* data = new float[width * height * 4]; // Assuming 4 channels (RGBA)

glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, data);

for (size_t i = 0; i < width * height; i++)
{
Expand Down
23 changes: 0 additions & 23 deletions src/weird-renderer/shaders/output.frag
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,7 @@ float rand(vec2 co){
}


const int MAX_STEPS = 100;
const float EPSILON = 0.01;
const float NEAR = 0.1f;
const float FAR = 100.0f;
float rayMarch(vec2 ro, vec2 rd)
{
float d;

float traveled = NEAR;

for (int i = 0; i < MAX_STEPS; i++)
{
vec2 p = ro + (traveled * rd);

d = texture(u_colorTexture, p).w;

if (d < EPSILON || traveled > FAR)
break;

traveled += d;
}

return traveled;
}

void main()
{
Expand Down
37 changes: 21 additions & 16 deletions src/weird-renderer/shaders/postProcess2d.frag
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


// Constants
const int MAX_STEPS = 100;
const float EPSILON = 0.01;
const int MAX_STEPS = 1000;
const float EPSILON = 0.002;
const float NEAR = 0.1f;
const float FAR = 100.0f;

Expand All @@ -26,7 +26,7 @@ uniform vec3 u_staticColors[16];
uniform sampler2D u_colorTexture;
uniform sampler2D u_depthTexture;

uniform vec2 u_directionalLightDirection = vec2(1.0, 0.0);
uniform vec2 u_directionalLightDirection = vec2(0.7071, 0.7071);

#if (DITHERING == 1)

Expand Down Expand Up @@ -71,33 +71,32 @@ float GetBayer8(int x, int y)

#endif







float map(vec2 p)
{
return 2.0 * (texture(u_colorTexture, p).w - 0.5);
return texture(u_colorTexture, p).w;
}

float rayMarch(vec2 ro, vec2 rd)
{
float d;

float traveled = NEAR;
float traveled = 0.0;

for (int i = 0; i < MAX_STEPS; i++)
{
vec2 p = ro + (traveled * rd);

d = map(p);

if (d < EPSILON || traveled > FAR)
if (d <= EPSILON)
break;

if(p.x <= 0.0 || p.x >= 1.0 || p.y <= 0.0 || p.y >= 1.0)
return 1.0;

//traveled += 0.001;
traveled += d;

}

return traveled;
Expand All @@ -106,15 +105,20 @@ float rayMarch(vec2 ro, vec2 rd)
vec3 render(vec2 uv)
{

vec3 background = vec3(0.2 + 0.4 * mod(floor(uv.x) + floor(uv.y), 2.0));
vec3 background = vec3(0.4 + 0.4 * mod(floor(10.0 * uv.x) + floor(10.0* (u_resolution.y / u_resolution.x) * uv.y), 2.0));
//vec3 background = vec3(1.0);


#if SHADOWS_ENABLED

float d = rayMarch(uv, u_directionalLightDirection.xy);
return (d < FAR ? 0.3 : 1.0) * background;
return (d < 1.0 ? 0.1: 1.0) * background;
//return d * background;

#else

return background;

#endif

}
Expand All @@ -126,17 +130,18 @@ void main()
vec4 color = texture(u_colorTexture, screenUV);
float distance = color.w;

vec3 col = distance <= 0.5 ? color.xyz : render(screenUV);

vec3 col = distance <= 0.0 ? color.xyz : render(screenUV);

#if (DITHERING == 1)

int x = int(gl_FragCoord.x);
int y = int(gl_FragCoord.y);
col = col + _Spread * GetBayer4(x, y);

col.r = floor((_ColorCount - 1.0f) * col.r + 0.5) / (_ColorCount - 1.0f);
col.g = floor((_ColorCount - 1.0f) * col.g + 0.5) / (_ColorCount - 1.0f);
col.b = floor((_ColorCount - 1.0f) * col.b + 0.5) / (_ColorCount - 1.0f);

#endif

FragColor = vec4(col.xyz, 1.0);
Expand Down
47 changes: 25 additions & 22 deletions src/weird-renderer/shaders/raymarching2d.frag
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#version 330 core


#define BLEND_SHAPES 0
#define BLEND_SHAPES 1
#define MOTION_BLUR 1

uniform float k = 1.5;
uniform float k = 0.5;



Expand Down Expand Up @@ -107,11 +108,8 @@ vec3 getMaterial(vec2 p, int materialId)

vec4 getColor(vec2 p)
{
if ( p.y <= 0) //p.x < 0.0 || p.x > 30.0 ||
return vec4(1.0,1.0,1.0,0.0);
float d = 100000.0;

float d = FAR;
// d = p.y - 2.5 * sin(0.5 * p.x);
vec3 col = vec3(0.0);

for (int i = 0; i < u_loadedObjects; i++)
Expand All @@ -122,43 +120,48 @@ vec4 getColor(vec2 p)
float objectDist = shape_circle(p - positionAndMaterial.xy);

#if BLEND_SHAPES

d = fOpUnionSoft(objectDist, d, k);
float delta = 1 - (max(k - abs(objectDist - d), 0.0) / k); // After new d is calculated
col = mix(getMaterial(p, materialId), col, delta);

#else

d = min(d, objectDist);
col = d == objectDist ? getMaterial(positionAndMaterial.xy, materialId) : col;

#endif
}

float floorDist = 0.25 * (p.y - 0.0 * sin(0.5 * p.x));
d = min(d, floorDist);
col = d == floorDist ? vec3(0.0) : col;

return vec4(col, d);
}



float scale = 10.f;



void main()
{
vec2 uv = (2.0 * gl_FragCoord.xy - u_resolution.xy) / u_resolution.y;
vec2 pos = (-u_cameraMatrix[3].z * uv) - u_cameraMatrix[3].xy;


float zoom = -u_cameraMatrix[3].z;
vec2 pos = (zoom * uv) - u_cameraMatrix[3].xy;

vec4 color = getColor(pos);

// float distance = 0.5 * round(texture(u_colorTexture, gl_FragCoord.xy).r) + clamp(0.1 * map(pos),0.0,1.0);
float distance = color.w;
float distanceScaled = (distance / scale) + 0.5f;
distanceScaled = clamp(distanceScaled, 0.0, 1.0);
float finalDistance = 0.6667 * 0.5 * distance / zoom;

#if MOTION_BLUR

vec2 screenUV = (gl_FragCoord.xy / u_resolution.xy);
float previousDistance = texture(u_colorTexture, screenUV.xy).w + (u_blendIterations * 0.0025);
float finalDistance = min(previousDistance, distanceScaled);
//float finalDistance = mix(previousDistance, distanceScaled, 0.99);
vec4 previousColor = texture(u_colorTexture, screenUV.xy);
float previousDistance = previousColor.w + (u_blendIterations * 0.0001);

FragColor = previousDistance < finalDistance ? vec4(previousColor.xyz, previousDistance) : vec4(color.xyz, finalDistance);
//FragColor = mix(vec4(color.xyz, finalDistance), previousColor, 0.6);

#else

FragColor = vec4(color.xyz, finalDistance);

#endif
}

0 comments on commit 882081d

Please sign in to comment.