summaryrefslogtreecommitdiffstats
path: root/src/shader.frag
blob: b4cabf950baccc0eb5316ab72bdaa2840e5f5b90 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#version 410 core

uniform vec2 u_resolution; // viewport resolution (in pixels)
uniform float u_time; // in seconds
uniform float u_xrot;
uniform float u_yrot;
uniform float u_zrot;
uniform sampler1D texFFT;

layout(location = 0) out vec4 out_color; // out_color must be written in order to see anything

const int   MAX_STEP = 255;
const float MIN_DIST = 0.0;
const float MAX_DIST = 100.0;
const float EPSILON  = 0.0001;

float f(float x, float z)
{
  return 2.0;
}


// transformations
mat4 rotateX(float theta)
{
  float c = cos(theta);
  float s = sin(theta);

  return mat4(
    vec4(1, 0, 0, 0),
    vec4(0, c, s, 0),
    vec4(0, -s, c, 0),
    vec4(0, 0, 0, 1)
  );  
}

// transformations
mat4 rotateY(float theta)
{
  float c = cos(theta);
  float s = sin(theta);

  return mat4(
    vec4(c, 0, s, 0),
    vec4(0, 1, 0, 0),
    vec4(-s, 0, c, 0),
    vec4(0, 0, 0, 1)
  );  
}

// transformations
mat4 rotateZ(float theta)
{
  float c = cos(theta);
  float s = sin(theta);

  return mat4(
    vec4(c, s, 0, 0),
    vec4(-s, c, 0, 0),
    vec4(0, 0, 1, 0),
    vec4(0, 0, 0, 1)
  );  
}

float tHeight(vec2 p)
{
  return smoothstep(0.0, 200.0, p.y*20000)*5;
}

float torusSDF( vec3 p, vec2 t )
{
  vec2 q = vec2(length(p.xz)-t.x,p.y);
  float d1 = length(p)-0.4;
  float d2 = tHeight(p.xy)*0.45;
  return d1 + (d2/20);
}

vec3 transformScene(vec3 p)
{
    //p = (inverse(rotateX(u_time*3)) * vec4(p, 1.0)).xyz;
    p = (inverse(rotateX(u_xrot)) *
         inverse(rotateY(u_yrot)) *
         inverse(rotateZ(u_zrot)) * vec4(p, 1.0)).xyz;
    return p;
}

// Standard distance function for the whole scene!
float sceneSDF(vec3 p)
{
  p = transformScene(p);
  //p = p * vec3(1.0, sin(u_time),1.0);
  return torusSDF(p, vec2(0.3, 0.1));
}

vec3 estNormal(vec3 p)
{
  return normalize(vec3(
    sceneSDF(vec3(p.x + EPSILON, p.y, p.z)) - sceneSDF(vec3(p.x - EPSILON, p.y, p.z)),
    sceneSDF(vec3(p.x, p.y + EPSILON, p.z)) - sceneSDF(vec3(p.x, p.y - EPSILON, p.z)),
    sceneSDF(vec3(p.x, p.y, p.z + EPSILON)) - sceneSDF(vec3(p.x, p.y, p.z - EPSILON))
  ));
}

vec3 rayDirection(float fov, vec2 size, vec2 fragCoord)
{
  vec2 xy = fragCoord - size / 2.0;
  float z = size.y  / tan(radians(fov) / 2.0);
  return normalize(vec3(xy, -z));
}

// Raymarcher!
float march(vec3 eye, vec3 dir, float start, float end)
{
  float depth = start;
  for (int i=0; i<MAX_STEP; i++) {
    float dist = sceneSDF(eye + depth * dir);
    if (dist < 0.0001) {
      return depth; // we're inside the scene
    }
    depth+=dist;
    if (depth >= end) {
      return end; // gone too far! 
    }
  }
}

// Does something related to phong for each light that we want
vec3 phongLightContrib(vec3 kd, vec3 ks, float alpha, vec3 p, vec3 eye, vec3 lightPos, vec3 lightInt)
{
  vec3 N = estNormal(p);
  vec3 L = normalize(lightPos - p);
  vec3 V = normalize(eye - p);
  vec3 R = normalize(reflect(-L, N));

  float dotLN = dot(L, N);
  float dotRV = dot(R, V);

  if (dotLN < 0.0) {return vec3(0.0);}
  if (dotRV < 0.0) {return lightInt * (kd * dotLN);}
  return lightInt * (kd * dotLN + ks * pow(dotRV, alpha));
}

vec3 phong(vec3 ka, vec3 kd, vec3 ks, float alpha, vec3 p, vec3 eye)
{
  const vec3 ambientLux = vec3(0.3);
  vec3 colour = ambientLux * ka;
  
  vec3 light1Pos = vec3(-1.0, -1.2, 0.7);
  vec3 light1Int = vec3(0.2);
  
  vec3 light2Pos = vec3(0.8, 0.8, 1.2);
  vec3 light2Int = vec3(0.55);
  
  colour += phongLightContrib(kd, ks, alpha, p, eye, light1Pos, light1Int);
  colour += phongLightContrib(kd, ks, alpha, p, eye, light2Pos, light2Int);
  return colour;
}

void main(void)
{
  vec3 dir = rayDirection(45.0, u_resolution.xy, gl_FragCoord.xy);
  vec3 eye = vec3(0.0,0.0,5.0);
  float dist = march(eye, dir, MIN_DIST, MAX_DIST);
  
  if (dist > MAX_DIST - EPSILON) {out_color = vec4(0.1); return;}
  
  vec3 p = eye + dist * dir;
  vec3 colour;
  
  vec3 Ka = vec3(0.0, 0.729, 0.745)*texture(texFFT, 0.123).r*1000; // ambient  reflection constant
  vec3 Kd = vec3(0.0, 0.467, 0.745);                               // diffuse  reflection constant
  vec3 Ks = vec3(0.0, 0.0, 0.0);                                   // specular reflection constant
  float shininess = 1.0;
  colour = phong(Ka, Kd, Ks, shininess, p, eye);
  
  if (transformScene(p).y< 0) {
    vec3 Ka = vec3(0.2);            // ambient  reflection constant
    vec3 Kd = vec3(0.95, 0.2, 0.1); // diffuse  reflection constant
    vec3 Ks = vec3(0.95, 0.2, 0.1); // specular reflection constant
    float shininess = 300.0;
    colour = phong(Ka, Kd, Ks, shininess, p, eye);
  }
  
  out_color = vec4(colour, 1.0);
}