diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2020-07-07 06:40:50 +0200 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2020-07-07 06:40:50 +0200 |
| commit | aac94e911b0bab8db5cdb5efb8d4d8f2d4072610 (patch) | |
| tree | a52a7a493076bba6e3ec5a7f9442bb3e77a5e16e /resources | |
| parent | 29339928d7a05ca11786885643631206a978655e (diff) | |
Add code
Diffstat (limited to 'resources')
| -rw-r--r-- | resources/fonts/arial.ttf | bin | 0 -> 296712 bytes | |||
| -rw-r--r-- | resources/shaders/algorithms/ray_marching.glsl | 5 | ||||
| -rw-r--r-- | resources/shaders/main.glsl | 77 | ||||
| -rw-r--r-- | resources/shaders/objects/sphere.glsl | 4 |
4 files changed, 81 insertions, 5 deletions
diff --git a/resources/fonts/arial.ttf b/resources/fonts/arial.ttf Binary files differnew file mode 100644 index 0000000..12cc15c --- /dev/null +++ b/resources/fonts/arial.ttf diff --git a/resources/shaders/algorithms/ray_marching.glsl b/resources/shaders/algorithms/ray_marching.glsl new file mode 100644 index 0000000..d984c0e --- /dev/null +++ b/resources/shaders/algorithms/ray_marching.glsl @@ -0,0 +1,5 @@ + + +RayMarching(vec3 ro, vec3 rd){ + +}
\ No newline at end of file diff --git a/resources/shaders/main.glsl b/resources/shaders/main.glsl index 7d4067a..2888d56 100644 --- a/resources/shaders/main.glsl +++ b/resources/shaders/main.glsl @@ -18,9 +18,76 @@ uniform float time; out vec3 color; -void main(){ - vec2 coord=gl_FragCoord.xy/resolution; - coord-=0.5; - float d=length(coord); - color=vec3(d,1,1); + +#define MAX_STEPS 100 +#define MAX_DIST 100. +#define SURF_DIST .01 + +float GetDist(vec3 p) { + vec4 s = vec4(0, 1, 6, 1); + + float sphereDist = length(p-s.xyz)-s.w; + float planeDist = p.y; + + float d = min(sphereDist, planeDist); + return d; +} + +float RayMarch(vec3 ro, vec3 rd) { + float dO=0.; + + for(int i=0; i<MAX_STEPS; i++) { + vec3 p = ro + rd*dO; + float dS = GetDist(p); + dO += dS; + if(dO>MAX_DIST || dS<SURF_DIST) break; + } + + return dO; +} + +vec3 GetNormal(vec3 p) { + float d = GetDist(p); + vec2 e = vec2(.01, 0); + + vec3 n = d - vec3( + GetDist(p-e.xyy), + GetDist(p-e.yxy), + GetDist(p-e.yyx)); + + return normalize(n); +} + +float GetLight(vec3 p) { + vec3 lightPos = vec3(0, 5, 6); + lightPos.xz += vec2(sin(time), cos(time))*2.; + vec3 l = normalize(lightPos-p); + vec3 n = GetNormal(p); + + float dif = clamp(dot(n, l), 0., 1.); + float d = RayMarch(p+n*SURF_DIST*2., l); + if(d<length(lightPos-p)) dif *= .1; + + return dif; +} + +void main() +{ + vec2 uv = (gl_FragCoord.xy-.5*resolution.xy)/resolution.y; + + vec3 col = vec3(0); + + vec3 ro = vec3(0, 1, 0); + vec3 rd = normalize(vec3(uv.x, uv.y, 1)); + + float d = RayMarch(ro, rd); + + vec3 p = ro + rd * d; + + float dif = GetLight(p); + col = vec3(dif); + + col = pow(col, vec3(.4545)); // gamma correction + + color = col; } diff --git a/resources/shaders/objects/sphere.glsl b/resources/shaders/objects/sphere.glsl new file mode 100644 index 0000000..a3d25eb --- /dev/null +++ b/resources/shaders/objects/sphere.glsl @@ -0,0 +1,4 @@ + +float Sphere(vec3 ray_position, vec3 sphere_position){ + +}
\ No newline at end of file |
