-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaderProg.c
185 lines (157 loc) · 4.88 KB
/
shaderProg.c
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
const int MAX_MARCHING_STEPS = 255;
const float MIN_DIST = 0.0;
const float MAX_DIST = 100.0;
const float PRECISION = 0.001;
const float AMBIENT = 0.05;
const vec3 BG_COLOR = vec3(AMBIENT);
struct Light {
vec3 position;
vec3 color;
};
struct Material {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float highlight;
};
struct Camera {
vec3 position;
vec3 lookAt;
vec3 up;
float fov;
};
// Definition of all curves
struct Sphere {
vec3 center;
float radius;
vec3 col;
Material mat;
};
float sdSphere(vec3 p, const Sphere sphere) {
return length(p - sphere.center) - sphere.radius;
}
struct RoundBox {
vec3 center;
vec3 size;
float radius;
vec3 col;
Material mat;
};
float sdRoundBox(vec3 p, const RoundBox round)
{
vec3 q = abs(p - round.center) - round.size;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - round.radius;
}
struct Plane {
float y_offset;
Material mat;
};
float sdPlane(vec3 p, const Plane plane) {
return p.y - plane.y_offset;
}
// build the scene
const Material mat_round = Material(BG_COLOR, vec3(0.6), vec3(0.35), 64.);
RoundBox round1 = RoundBox(vec3(-2., 0., 0.5), vec3(0.5, 0.3, 0.3), 0.2, vec3(1., 0., 0.), mat_round);
RoundBox round2 = RoundBox(vec3(2., 0., -0.5), vec3(0.5, 0.3, 0.3), 0.2, vec3(0., 1., 0.), mat_round);
const Material mat_sph = Material(BG_COLOR, vec3(0.3), vec3(0.85), 16.);
Sphere sphere1 = Sphere(vec3(0, 0, 0), 0.55, vec3(1., 1., 1.), mat_sph);
const Material mat_plan = Material(vec3(0.0), vec3(0.5), vec3(0.5), 64.);
Plane plane1 = Plane(-1., mat_plan);
vec2 opU(vec2 d1, vec2 d2) {
return (d1.x < d2.x) ? d1 : d2;
}
vec2 map(vec3 p) {
vec2 res = vec2(MAX_DIST, 0);
res = opU(res, vec2(sdRoundBox(p, round1), 1));
res = opU(res, vec2(sdRoundBox(p, round2), 2));
res = opU(res, vec2(sdSphere(p, sphere1), 3));
res = opU(res, vec2(sdPlane(p, plane1), 4));
return res;
}
// Ray Marching Definition
vec2 rayMarch(vec3 ro, vec3 rd, float start, float end) {
float depth = start;
vec2 res = vec2(0.0);
float id = 0.;
for (int i = 0; i < MAX_MARCHING_STEPS; i++) {
vec3 p = ro + depth * rd;
res = map(p);
depth += res.x;
id = res.y;
if (res.x < PRECISION || depth > end) break;
}
return vec2(depth, id);
}
vec3 calcNormal(vec3 p) {
vec2 e = vec2(1.0, -1.0) * 0.0005;
return normalize(
e.xyy * map(p + e.xyy).x +
e.yyx * map(p + e.yyx).x +
e.yxy * map(p + e.yxy).x +
e.xxx * map(p + e.xxx).x);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - .5 * iResolution.xy) / iResolution.y;
vec3 col = vec3(0);
// define the position of the camera
vec3 ro = vec3(0, 0, 5);
vec3 rd = normalize(vec3(uv, -1));
// define the light
int light_num = 3;
Light[3] lightList = Light[] (
Light(vec3(3. * cos(iTime * 1.2), 3, 3. * sin(iTime * 1.2)), vec3(1.0, 0.75, 0.388)),
Light(vec3(-3. * cos(iTime * 1.2), -3. * sin(iTime * 1.2), 3), vec3(0.373, 0.831, 1.0)),
Light(vec3(5. * cos(iTime * 0.25), 0, 5. * sin(iTime * 0.25)), vec3(0.7))
);
vec2 res = rayMarch(ro, rd, MIN_DIST, MAX_DIST);
if (res.x > 100.0) {
// ray didn't hit anything
col = BG_COLOR;
} else {
// ray hit something
vec3 p = ro + rd * res.x;
vec3 normal = calcNormal(p);
int id = int(res.y);
// first compute illumination
Material mat;
if (id == 1)
mat = round1.mat;
else if (id == 2)
mat = round2.mat;
else if (id == 3)
mat = sphere1.mat;
else if (id == 4)
mat = plane1.mat;
else
mat = Material(BG_COLOR, vec3(0.0), vec3(0.0), 2.);
vec3 ill_sum = vec3(0.0);
for (int i = 0; i < light_num; ++i) {
Light l = lightList[i];
vec3 lightDirection = normalize(l.position - p);
// ambient
vec3 ill = mat.ambient;
// diffuse
vec3 dif = clamp(dot(normal, lightDirection), 0., 1.) * mat.diffuse;
ill += clamp(dif, mat.ambient, vec3(1.));
// specular
ill += mat.specular * pow(clamp(dot(normal, reflect(-lightDirection, normal)), 0., 1.), mat.highlight);
ill *= l.color;
ill_sum += ill;
}
ill_sum = clamp(ill_sum, 0., 1.);
// then compute the color
if (id == 1) {
col = ill_sum * round1.col;
} else if (id == 2) {
col = ill_sum * round2.col;
} else if (id == 3) {
col = ill_sum * sphere1.col;
} else if (id == 4) {
col = ill_sum * vec3(1. + 0.7 * mod(floor(p.x) + floor(p.z), 2.0));
} else {
col = ill_sum;
}
}
// Output to screen
fragColor = vec4(col, 1.0);
}