summaryrefslogtreecommitdiff
path: root/gl-vfog/fs.glsl
blob: 00ec9b1709efb1fb6e945eb90b276ba78264c50d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#version 330 core

in vec3 f_p;

out vec4 f_color;

uniform vec3  u_cam;
uniform float u_time;
uniform int   u_mode;
uniform float u_sigma;

// Ray-AABB intersection
// ref: https://tavianator.com/2022/ray_box_boundary.html
bool intersect(vec3 ro, vec3 rd, vec3 bbmin, vec3 bbmax, out float t0, out float t1)
{
  vec3 inv = 1.0f / rd;
  vec3 ta = (bbmin - ro) * inv;
  vec3 tb = (bbmax - ro) * inv;
  vec3 tmin = min(ta, tb);
  vec3 tmax = max(ta, tb);
  t0 = max(max(tmin.x, tmin.y), tmin.z);
  t1 = min(min(tmax.x, tmax.y), tmax.z);
  return t1 >= max(t0, 0.0f);
}

// Mode 0: Basic
void main_0()
{
  vec3 bbmin = vec3(0.0f, 0.0f, 0.0f);
  vec3 bbmax = vec3(1.0f, 1.0f, 1.0f);

  vec3 ro = u_cam;
  vec3 rd = normalize(f_p - ro);

  float t0 = 0.0f;
  float t1 = 0.0f;
  if (!intersect(ro, rd, bbmin, bbmax, t0, t1)) {
    f_color = vec4(0.0f, 0.0f, 0.0f, 0.0f);
    return;
  }

  // Clamp entry point in front of camera
  float t_enter = max(t0, 0.0f);
  float t_exit  = t1;
  float t_len   = max(0.0f, t_exit - t_enter);

  // Beer-Lambert attenuation
  // ref: https://en.wikipedia.org/wiki/Beer%E2%80%93Lambert_law
  float a = 1.0f - exp(-u_sigma * t_len);

  f_color = vec4(vec3(1.0f), a);
}

void main()
{
  switch (u_mode) {
  case 0: { main_0(); } break;
  default: { f_color = vec4(0.0f, 1.0f, 0.0f, 1.0f); } break;
  }
}