Skip to content

Commit 882081d

Browse files
committed
[WIP] Fixed shadows, motion blur and crash
1 parent a41844c commit 882081d

File tree

5 files changed

+52
-65
lines changed

5 files changed

+52
-65
lines changed

src/weird-renderer/RenderPlane.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace WeirdRenderer
9090
}
9191

9292

93-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
93+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
9494
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterMode);
9595
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterMode);
9696
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // Prevents edge bleeding

src/weird-renderer/Renderer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ namespace WeirdRenderer
226226
m_sdfShaderProgram.setUniform("u_time", time);
227227
m_sdfShaderProgram.setUniform("u_resolution", glm::vec2(m_renderWidth, m_renderHeight));
228228

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

232232

@@ -270,16 +270,18 @@ namespace WeirdRenderer
270270

271271
m_outputRenderPlane.Draw(m_outputShaderProgram);
272272

273+
274+
273275
// Screenshot
274276
if (Input::GetKeyDown(Input::O)) {
275277
glBindTexture(GL_TEXTURE_2D, m_postProcessRenderPlane.m_colorTexture);
276278
int width, height;
277279
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
278280
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
279281

280-
unsigned char* data = new unsigned char[width * height * 4]; // Assuming 4 channels (RGBA)
282+
float* data = new float[width * height * 4]; // Assuming 4 channels (RGBA)
281283

282-
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
284+
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, data);
283285

284286
for (size_t i = 0; i < width * height; i++)
285287
{

src/weird-renderer/shaders/output.frag

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,7 @@ float rand(vec2 co){
1616
}
1717

1818

19-
const int MAX_STEPS = 100;
20-
const float EPSILON = 0.01;
21-
const float NEAR = 0.1f;
22-
const float FAR = 100.0f;
23-
float rayMarch(vec2 ro, vec2 rd)
24-
{
25-
float d;
26-
27-
float traveled = NEAR;
28-
29-
for (int i = 0; i < MAX_STEPS; i++)
30-
{
31-
vec2 p = ro + (traveled * rd);
32-
33-
d = texture(u_colorTexture, p).w;
3419

35-
if (d < EPSILON || traveled > FAR)
36-
break;
37-
38-
traveled += d;
39-
}
40-
41-
return traveled;
42-
}
4320

4421
void main()
4522
{

src/weird-renderer/shaders/postProcess2d.frag

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66

77
// Constants
8-
const int MAX_STEPS = 100;
9-
const float EPSILON = 0.01;
8+
const int MAX_STEPS = 1000;
9+
const float EPSILON = 0.002;
1010
const float NEAR = 0.1f;
1111
const float FAR = 100.0f;
1212

@@ -26,7 +26,7 @@ uniform vec3 u_staticColors[16];
2626
uniform sampler2D u_colorTexture;
2727
uniform sampler2D u_depthTexture;
2828

29-
uniform vec2 u_directionalLightDirection = vec2(1.0, 0.0);
29+
uniform vec2 u_directionalLightDirection = vec2(0.7071, 0.7071);
3030

3131
#if (DITHERING == 1)
3232

@@ -71,33 +71,32 @@ float GetBayer8(int x, int y)
7171

7272
#endif
7373

74-
75-
76-
77-
78-
79-
8074
float map(vec2 p)
8175
{
82-
return 2.0 * (texture(u_colorTexture, p).w - 0.5);
76+
return texture(u_colorTexture, p).w;
8377
}
8478

8579
float rayMarch(vec2 ro, vec2 rd)
8680
{
8781
float d;
8882

89-
float traveled = NEAR;
83+
float traveled = 0.0;
9084

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

9589
d = map(p);
9690

97-
if (d < EPSILON || traveled > FAR)
91+
if (d <= EPSILON)
9892
break;
9993

94+
if(p.x <= 0.0 || p.x >= 1.0 || p.y <= 0.0 || p.y >= 1.0)
95+
return 1.0;
96+
97+
//traveled += 0.001;
10098
traveled += d;
99+
101100
}
102101

103102
return traveled;
@@ -106,15 +105,20 @@ float rayMarch(vec2 ro, vec2 rd)
106105
vec3 render(vec2 uv)
107106
{
108107

109-
vec3 background = vec3(0.2 + 0.4 * mod(floor(uv.x) + floor(uv.y), 2.0));
108+
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));
109+
//vec3 background = vec3(1.0);
110+
110111

111112
#if SHADOWS_ENABLED
112113

113114
float d = rayMarch(uv, u_directionalLightDirection.xy);
114-
return (d < FAR ? 0.3 : 1.0) * background;
115+
return (d < 1.0 ? 0.1: 1.0) * background;
116+
//return d * background;
115117

116118
#else
119+
117120
return background;
121+
118122
#endif
119123

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

129-
vec3 col = distance <= 0.5 ? color.xyz : render(screenUV);
130-
133+
vec3 col = distance <= 0.0 ? color.xyz : render(screenUV);
131134

132135
#if (DITHERING == 1)
136+
133137
int x = int(gl_FragCoord.x);
134138
int y = int(gl_FragCoord.y);
135139
col = col + _Spread * GetBayer4(x, y);
136140

137141
col.r = floor((_ColorCount - 1.0f) * col.r + 0.5) / (_ColorCount - 1.0f);
138142
col.g = floor((_ColorCount - 1.0f) * col.g + 0.5) / (_ColorCount - 1.0f);
139143
col.b = floor((_ColorCount - 1.0f) * col.b + 0.5) / (_ColorCount - 1.0f);
144+
140145
#endif
141146

142147
FragColor = vec4(col.xyz, 1.0);

src/weird-renderer/shaders/raymarching2d.frag

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#version 330 core
22

33

4-
#define BLEND_SHAPES 0
4+
#define BLEND_SHAPES 1
5+
#define MOTION_BLUR 1
56

6-
uniform float k = 1.5;
7+
uniform float k = 0.5;
78

89

910

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

108109
vec4 getColor(vec2 p)
109110
{
110-
if ( p.y <= 0) //p.x < 0.0 || p.x > 30.0 ||
111-
return vec4(1.0,1.0,1.0,0.0);
111+
float d = 100000.0;
112112

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

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

124122
#if BLEND_SHAPES
123+
125124
d = fOpUnionSoft(objectDist, d, k);
126125
float delta = 1 - (max(k - abs(objectDist - d), 0.0) / k); // After new d is calculated
127126
col = mix(getMaterial(p, materialId), col, delta);
127+
128128
#else
129+
129130
d = min(d, objectDist);
130131
col = d == objectDist ? getMaterial(positionAndMaterial.xy, materialId) : col;
132+
131133
#endif
132134
}
133135

136+
float floorDist = 0.25 * (p.y - 0.0 * sin(0.5 * p.x));
137+
d = min(d, floorDist);
138+
col = d == floorDist ? vec3(0.0) : col;
139+
134140
return vec4(col, d);
135141
}
136142

137-
138-
139-
float scale = 10.f;
140-
141-
142-
143143
void main()
144144
{
145145
vec2 uv = (2.0 * gl_FragCoord.xy - u_resolution.xy) / u_resolution.y;
146-
vec2 pos = (-u_cameraMatrix[3].z * uv) - u_cameraMatrix[3].xy;
147-
148-
146+
float zoom = -u_cameraMatrix[3].z;
147+
vec2 pos = (zoom * uv) - u_cameraMatrix[3].xy;
149148

150149
vec4 color = getColor(pos);
151-
152-
// float distance = 0.5 * round(texture(u_colorTexture, gl_FragCoord.xy).r) + clamp(0.1 * map(pos),0.0,1.0);
153150
float distance = color.w;
154-
float distanceScaled = (distance / scale) + 0.5f;
155-
distanceScaled = clamp(distanceScaled, 0.0, 1.0);
151+
float finalDistance = 0.6667 * 0.5 * distance / zoom;
156152

153+
#if MOTION_BLUR
157154

158155
vec2 screenUV = (gl_FragCoord.xy / u_resolution.xy);
159-
float previousDistance = texture(u_colorTexture, screenUV.xy).w + (u_blendIterations * 0.0025);
160-
float finalDistance = min(previousDistance, distanceScaled);
161-
//float finalDistance = mix(previousDistance, distanceScaled, 0.99);
156+
vec4 previousColor = texture(u_colorTexture, screenUV.xy);
157+
float previousDistance = previousColor.w + (u_blendIterations * 0.0001);
158+
159+
FragColor = previousDistance < finalDistance ? vec4(previousColor.xyz, previousDistance) : vec4(color.xyz, finalDistance);
160+
//FragColor = mix(vec4(color.xyz, finalDistance), previousColor, 0.6);
161+
162+
#else
162163

163164
FragColor = vec4(color.xyz, finalDistance);
165+
166+
#endif
164167
}

0 commit comments

Comments
 (0)