Skip to content

Commit 6820ff6

Browse files
committed
REVIEWED: example: shaders_hybrid_rendering, shaders issues
1 parent 90af210 commit 6820ff6

File tree

5 files changed

+64
-52
lines changed

5 files changed

+64
-52
lines changed

examples/shaders/resources/shaders/glsl100/hybrid_raster.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#version 100
22

3-
#extension GL_EXT_frag_depth : enable // Extension required for writing depth
3+
#extension GL_EXT_frag_depth : enable // Extension required for writing depth
4+
45
precision mediump float; // Precision required for OpenGL ES2 (WebGL)
56

67
varying vec2 fragTexCoord;

examples/shaders/resources/shaders/glsl100/hybrid_raymarch.fs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#extension GL_EXT_frag_depth : enable //Extension required for writing depth
44
#extension GL_OES_standard_derivatives : enable //Extension used for fwidth()
5+
6+
#define ZERO 0
7+
58
precision mediump float; // Precision required for OpenGL ES2 (WebGL)
69

710
// Input vertex attributes (from vertex shader)
@@ -17,8 +20,6 @@ uniform vec3 camPos;
1720
uniform vec3 camDir;
1821
uniform vec2 screenCenter;
1922

20-
#define ZERO 0
21-
2223
// SRC: https://learnopengl.com/Advanced-OpenGL/Depth-testing
2324
float CalcDepth(in vec3 rd, in float Idist)
2425
{
@@ -128,7 +129,7 @@ float calcSoftshadow(in vec3 ro, in vec3 rd, in float mint, in float tmax)
128129

129130
float res = 1.0;
130131
float t = mint;
131-
for (int i=ZERO; i<24; i++)
132+
for (int i = ZERO; i < 24; i++)
132133
{
133134
float h = map(ro + rd*t).x;
134135
float s = clamp(8.0*h/t,0.0,1.0);
@@ -156,7 +157,7 @@ float calcAO(in vec3 pos, in vec3 nor)
156157
{
157158
float occ = 0.0;
158159
float sca = 1.0;
159-
for (int i=ZERO; i<5; i++)
160+
for (int i = ZERO; i < 5; i++)
160161
{
161162
float h = 0.01 + 0.12*float(i)/4.0;
162163
float d = map(pos + h*nor).x;
@@ -257,7 +258,8 @@ vec4 render(in vec3 ro, in vec3 rd)
257258
return vec4(vec3(clamp(col,0.0,1.0)),t);
258259
}
259260

260-
vec3 CalcRayDir(vec2 nCoord){
261+
vec3 CalcRayDir(vec2 nCoord)
262+
{
261263
vec3 horizontal = normalize(cross(camDir,vec3(.0 , 1.0, .0)));
262264
vec3 vertical = normalize(cross(horizontal,camDir));
263265
return normalize(camDir + horizontal*nCoord.x + vertical*nCoord.y);
@@ -287,6 +289,7 @@ void main()
287289
color = res.xyz;
288290
depth = CalcDepth(rd,res.w);
289291
}
292+
290293
gl_FragColor = vec4(color , 1.0);
291294
gl_FragDepthEXT = depth;
292295
}

examples/shaders/resources/shaders/glsl330/hybrid_raster.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ uniform sampler2D texture0;
99
uniform vec4 colDiffuse;
1010

1111
// Output fragment color
12-
//out vec4 finalColor;
12+
out vec4 finalColor;
1313

1414
// NOTE: Add your custom variables here
1515

1616
void main()
1717
{
1818
vec4 texelColor = texture(texture0, fragTexCoord);
1919

20-
gl_FragColor = texelColor*colDiffuse*fragColor;
21-
gl_FragDepth = gl_FragCoord.z;
20+
finalColor = texelColor*colDiffuse*fragColor;
21+
gl_FragDepth = finalColor.z;
2222
}

examples/shaders/resources/shaders/glsl330/hybrid_raymarch.fs

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# version 330
22

3+
#define ZERO 0
4+
35
// Input vertex attributes (from vertex shader)
46
in vec2 fragTexCoord;
57
in vec4 fragColor;
@@ -13,10 +15,12 @@ uniform vec3 camPos;
1315
uniform vec3 camDir;
1416
uniform vec2 screenCenter;
1517

16-
#define ZERO 0
18+
// Output fragment color
19+
out vec4 finalColor;
1720

1821
// https://learnopengl.com/Advanced-OpenGL/Depth-testing
19-
float CalcDepth(in vec3 rd, in float Idist){
22+
float CalcDepth(in vec3 rd, in float Idist)
23+
{
2024
float local_z = dot(normalize(camDir),rd)*Idist;
2125
return (1.0/(local_z) - 1.0/0.01)/(1.0/1000.0 -1.0/0.01);
2226
}
@@ -26,15 +30,13 @@ float sdHorseshoe(in vec3 p, in vec2 c, in float r, in float le, vec2 w)
2630
{
2731
p.x = abs(p.x);
2832
float l = length(p.xy);
29-
p.xy = mat2(-c.x, c.y,
30-
c.y, c.x)*p.xy;
31-
p.xy = vec2((p.y>0.0 || p.x>0.0)?p.x:l*sign(-c.x),
32-
(p.x>0.0)?p.y:l);
33-
p.xy = vec2(p.x,abs(p.y-r))-vec2(le,0.0);
33+
p.xy = mat2(-c.x, c.y, c.y, c.x)*p.xy;
34+
p.xy = vec2(((p.y > 0.0) || (p.x > 0.0))? p.x : l*sign(-c.x), (p.x>0.0)? p.y : l);
35+
p.xy = vec2(p.x, abs(p.y - r)) - vec2(le, 0.0);
3436

35-
vec2 q = vec2(length(max(p.xy,0.0)) + min(0.0,max(p.x,p.y)),p.z);
37+
vec2 q = vec2(length(max(p.xy, 0.0)) + min(0.0, max(p.x, p.y)), p.z);
3638
vec2 d = abs(q) - w;
37-
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
39+
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
3840
}
3941

4042
// r = sphere's radius
@@ -44,17 +46,16 @@ float sdSixWayCutHollowSphere(vec3 p, float r, float h, float t)
4446
{
4547
// Six way symetry Transformation
4648
vec3 ap = abs(p);
47-
if (ap.x < max(ap.y, ap.z)){
49+
if (ap.x < max(ap.y, ap.z))
50+
{
4851
if (ap.y < ap.z) ap.xz = ap.zx;
4952
else ap.xy = ap.yx;
5053
}
5154

5255
vec2 q = vec2(length(ap.yz), ap.x);
53-
5456
float w = sqrt(r*r-h*h);
5557

56-
return ((h*q.x<w*q.y) ? length(q-vec2(w,h)) :
57-
abs(length(q)-r)) - t;
58+
return ((h*q.x < w*q.y)? length(q - vec2(w, h)) : abs(length(q) - r)) - t;
5859
}
5960

6061
// https://iquilezles.org/articles/boxfunctions
@@ -65,44 +66,47 @@ vec2 iBox(in vec3 ro, in vec3 rd, in vec3 rad)
6566
vec3 k = abs(m)*rad;
6667
vec3 t1 = -n - k;
6768
vec3 t2 = -n + k;
68-
return vec2(max(max(t1.x, t1.y), t1.z),
69-
min(min(t2.x, t2.y), t2.z));
69+
70+
return vec2(max(max(t1.x, t1.y), t1.z), min(min(t2.x, t2.y), t2.z));
7071
}
7172

7273
vec2 opU(vec2 d1, vec2 d2)
7374
{
74-
return (d1.x<d2.x) ? d1 : d2;
75+
return (d1.x < d2.x)? d1 : d2;
7576
}
7677

77-
vec2 map(in vec3 pos){
78-
vec2 res = vec2(sdHorseshoe( pos-vec3(-1.0,0.08, 1.0), vec2(cos(1.3),sin(1.3)), 0.2, 0.3, vec2(0.03,0.5)), 11.5) ;
79-
res = opU(res, vec2(sdSixWayCutHollowSphere( pos-vec3(0.0, 1.0, 0.0), 4.0, 3.5, 0.5), 4.5)) ;
78+
vec2 map(in vec3 pos)
79+
{
80+
vec2 res = vec2(sdHorseshoe(pos - vec3(-1.0, 0.08, 1.0), vec2(cos(1.3), sin(1.3)), 0.2, 0.3, vec2(0.03,0.5)), 11.5);
81+
res = opU(res, vec2(sdSixWayCutHollowSphere(pos-vec3(0.0, 1.0, 0.0), 4.0, 3.5, 0.5), 4.5));
82+
8083
return res;
8184
}
8285

8386
// https://www.shadertoy.com/view/Xds3zN
84-
vec2 raycast(in vec3 ro, in vec3 rd){
85-
vec2 res = vec2(-1.0,-1.0);
87+
vec2 raycast(in vec3 ro, in vec3 rd)
88+
{
89+
vec2 res = vec2(-1.0, -1.0);
8690

8791
float tmin = 1.0;
8892
float tmax = 20.0;
8993

9094
// raytrace floor plane
9195
float tp1 = (-ro.y)/rd.y;
92-
if (tp1>0.0)
96+
if (tp1 > 0.0)
9397
{
9498
tmax = min(tmax, tp1);
9599
res = vec2(tp1, 1.0);
96100
}
97101

98102
float t = tmin;
99-
for (int i=0; i<70 ; i++)
103+
for (int i = 0; i < 70 ; i++)
100104
{
101-
if (t>tmax) break;
102-
vec2 h = map(ro+rd*t);
103-
if (abs(h.x)<(0.0001*t))
105+
if (t > tmax) break;
106+
vec2 h = map(ro + rd*t);
107+
if (abs(h.x )< (0.0001*t))
104108
{
105-
res = vec2(t,h.y);
109+
res = vec2(t, h.y);
106110
break;
107111
}
108112
t += h.x;
@@ -111,28 +115,28 @@ vec2 raycast(in vec3 ro, in vec3 rd){
111115
return res;
112116
}
113117

114-
115118
// https://iquilezles.org/articles/rmshadows
116119
float calcSoftshadow(in vec3 ro, in vec3 rd, in float mint, in float tmax)
117120
{
118121
// bounding volume
119-
float tp = (0.8-ro.y)/rd.y; if (tp>0.0) tmax = min(tmax, tp);
122+
float tp = (0.8 - ro.y)/rd.y; if (tp > 0.0) tmax = min(tmax, tp);
120123

121124
float res = 1.0;
122125
float t = mint;
123-
for (int i=ZERO; i<24; i++)
126+
for (int i = ZERO; i < 24; i++)
124127
{
125128
float h = map(ro + rd*t).x;
126-
float s = clamp(8.0*h/t,0.0,1.0);
129+
float s = clamp(8.0*h/t, 0.0, 1.0);
127130
res = min(res, s);
128131
t += clamp(h, 0.01, 0.2);
129-
if (res<0.004 || t>tmax) break;
132+
if ((res < 0.004) || (t > tmax)) break;
130133
}
134+
131135
res = clamp(res, 0.0, 1.0);
136+
132137
return res*res*(3.0-2.0*res);
133138
}
134139

135-
136140
// https://iquilezles.org/articles/normalsSDF
137141
vec3 calcNormal(in vec3 pos)
138142
{
@@ -148,14 +152,15 @@ float calcAO(in vec3 pos, in vec3 nor)
148152
{
149153
float occ = 0.0;
150154
float sca = 1.0;
151-
for (int i=ZERO; i<5; i++)
155+
for (int i = ZERO; i < 5; i++)
152156
{
153157
float h = 0.01 + 0.12*float(i)/4.0;
154158
float d = map(pos + h*nor).x;
155159
occ += (h-d)*sca;
156160
sca *= 0.95;
157161
if (occ>0.35) break;
158162
}
163+
159164
return clamp(1.0 - 3.0*occ, 0.0, 1.0)*(0.5+0.5*nor.y);
160165
}
161166

@@ -165,9 +170,9 @@ float checkersGradBox(in vec2 p)
165170
// filter kernel
166171
vec2 w = fwidth(p) + 0.001;
167172
// analytical integral (box filter)
168-
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
173+
vec2 i = 2.0*(abs(fract((p - 0.5*w)*0.5)-0.5) - abs(fract((p + 0.5*w)*0.5) - 0.5))/w;
169174
// xor pattern
170-
return 0.5 - 0.5*i.x*i.y;
175+
return (0.5 - 0.5*i.x*i.y);
171176
}
172177

173178
// https://www.shadertoy.com/view/tdS3DG
@@ -180,7 +185,7 @@ vec4 render(in vec3 ro, in vec3 rd)
180185
vec2 res = raycast(ro,rd);
181186
float t = res.x;
182187
float m = res.y;
183-
if (m>-0.5)
188+
if (m > -0.5)
184189
{
185190
vec3 pos = ro + t*rd;
186191
vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal(pos);
@@ -190,7 +195,7 @@ vec4 render(in vec3 ro, in vec3 rd)
190195
col = 0.2 + 0.2*sin(m*2.0 + vec3(0.0,1.0,2.0));
191196
float ks = 1.0;
192197

193-
if (m<1.5)
198+
if (m < 1.5)
194199
{
195200
float f = checkersGradBox(3.0*pos.xz);
196201
col = 0.15 + f*vec3(0.05);
@@ -207,14 +212,14 @@ vec4 render(in vec3 ro, in vec3 rd)
207212
vec3 lig = normalize(vec3(-0.5, 0.4, -0.6));
208213
vec3 hal = normalize(lig-rd);
209214
float dif = clamp(dot(nor, lig), 0.0, 1.0);
210-
//if (dif>0.0001)
215+
//if (dif>0.0001)
211216
dif *= calcSoftshadow(pos, lig, 0.02, 2.5);
212217
float spe = pow(clamp(dot(nor, hal), 0.0, 1.0),16.0);
213218
spe *= dif;
214219
spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,lig),0.0,1.0),5.0);
215220
//spe *= 0.04+0.96*pow(clamp(1.0-sqrt(0.5*(1.0-dot(rd,lig))),0.0,1.0),5.0);
216221
lin += col*2.20*dif*vec3(1.30,1.00,0.70);
217-
lin += 5.00*spe*vec3(1.30,1.00,0.70)*ks;
222+
lin += 5.00*spe*vec3(1.30,1.00,0.70)*ks;
218223
}
219224
// sky
220225
{
@@ -249,7 +254,8 @@ vec4 render(in vec3 ro, in vec3 rd)
249254
return vec4(vec3(clamp(col,0.0,1.0)),t);
250255
}
251256

252-
vec3 CalcRayDir(vec2 nCoord){
257+
vec3 CalcRayDir(vec2 nCoord)
258+
{
253259
vec3 horizontal = normalize(cross(camDir,vec3(.0 , 1.0, .0)));
254260
vec3 vertical = normalize(cross(horizontal,camDir));
255261
return normalize(camDir + horizontal*nCoord.x + vertical*nCoord.y);
@@ -279,6 +285,7 @@ void main()
279285
color = res.xyz;
280286
depth = CalcDepth(rd,res.w);
281287
}
282-
gl_FragColor = vec4(color , 1.0);
288+
289+
finalColor = vec4(color , 1.0);
283290
gl_FragDepth = depth;
284291
}

examples/shaders/shaders_hybrid_rendering.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ int main(void)
118118
// Raymarch Scene
119119
rlEnableDepthTest(); // Manually enable Depth Test to handle multiple rendering methods
120120
BeginShaderMode(shdrRaymarch);
121-
DrawRectangleRec((Rectangle){0,0, (float)screenWidth, (float)screenHeight},WHITE);
121+
DrawRectangleRec((Rectangle){ 0,0, (float)screenWidth, (float)screenHeight },WHITE);
122122
EndShaderMode();
123123

124124
// Rasterize Scene
@@ -138,6 +138,7 @@ int main(void)
138138
ClearBackground(RAYWHITE);
139139

140140
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);
141+
141142
DrawFPS(10, 10);
142143
EndDrawing();
143144
//----------------------------------------------------------------------------------

0 commit comments

Comments
 (0)