-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGLSLRayTracing.frag
200 lines (166 loc) · 4.31 KB
/
GLSLRayTracing.frag
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//Made by Jordan Duty(AKA djdduty) May 14, 2014.
#define M_PI 3.141592653589793238462643383279
struct Ray
{
vec3 Dir;
vec3 Pos;
};
struct Sphere
{
vec3 Pos;
vec3 Color;
float Rad;
float Reflection;
};
const int numBounces = 5;
const int numSpheres = 7;
Sphere spheres[numSpheres];
vec3 LightPos = vec3(0,-3,10);
vec3 Intersects(Sphere s, Ray r)
{
float t0, t1;
vec3 l = s.Pos - r.Pos;
float tca = dot(l, r.Dir);
if(tca < 0.0) return vec3(0,0,-1.0);
float d2 = dot(l, l) - tca*tca;
if(d2 > s.Rad*s.Rad) return vec3(0,0,-1.0);
float thc = sqrt((s.Rad*s.Rad) - d2);
return vec3(tca-thc, tca+thc, 1);
}
float mix2(float a, float b, float c)
{
return b*c+a*(1.0-c);
}
vec3 Trace(Ray r)
{
vec3 Color = vec3(0,0,0);
float frac=1.0;
for(int bounce = 0; bounce < numBounces; bounce++)
{
if(frac <0.01) break;
Sphere s;
bool col = false;
float tnear = 1e8;
for(int i = 0; i < numSpheres; i++)
{
vec3 intTest = Intersects(spheres[i],r);
if(intTest.z != -1.0) {
if(intTest.x < tnear) {
tnear = intTest.x;
s = spheres[i];
col = true;
}
}
}
if(col==false) continue;
vec3 phit = r.Pos + r.Dir * tnear;
vec3 nhit = phit - s.Pos;
nhit = normalize(nhit);
float bias = 1e-4;
bool inside = false;
if(dot(r.Dir, nhit) > 0.0) nhit *= -1.0, inside = true;
vec3 refldir = r.Dir - nhit * 2.0 * dot(r.Dir, nhit);
refldir = normalize(refldir);
vec3 localColor;
vec3 lightDir = LightPos - phit;
bool blocked = false;
lightDir = normalize(lightDir);
float DiffuseFactor = dot(nhit, lightDir);
vec3 diffuseColor = vec3(0,0,0);
vec3 ambientColor = vec3(s.Color * 0.2);
for(int n = 0; n < numSpheres; n++)
{
Ray rl;
rl.Pos = phit;
rl.Dir = lightDir;
vec3 intTestL = Intersects(spheres[n],rl);
if(intTestL.z != -1.0)
{
if(intTestL.x < length(LightPos-phit))
blocked = true;
}
}
if(!blocked)
{
if(DiffuseFactor > 0.0)
{
diffuseColor = vec3(1,1,1)*DiffuseFactor;
localColor += s.Color * diffuseColor + ambientColor;
}
else
{
localColor += ambientColor;
}
}
else
{
localColor += ambientColor;
}
Color.x += localColor.x * (1.0-s.Reflection) * frac;
Color.y += localColor.y * (1.0-s.Reflection) * frac;
Color.z += localColor.z * (1.0-s.Reflection) * frac;
r.Pos = phit;
r.Dir = refldir;
frac *= s.Reflection;
}
return Color;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
spheres[0].Pos = vec3(5,0,-0);
spheres[0].Color = vec3(0.9,0.1,0.1);
spheres[0].Rad = 4.0;
spheres[0].Reflection = 0.4;
spheres[1].Pos = vec3(-5,0,-0);
spheres[1].Color = vec3(0.3,0.3,0.3);
spheres[1].Rad = 4.0;
spheres[1].Reflection = 0.3;
spheres[2].Pos = vec3(-5,1004,-0);
spheres[2].Color = vec3(0.1,0.1,0.1);
spheres[2].Rad = 1000.0;
//spheres[2].Reflection = 0.15;
spheres[2].Reflection = 0.28;
spheres[3].Pos = vec3(-5,0,-1040);
spheres[3].Color = vec3(0.5,0.5,0.5);
spheres[3].Rad = 1000.0;
spheres[3].Reflection = 0.15;
spheres[4].Pos = vec3(1020,0,-0);
spheres[4].Color = vec3(0.9,0.9,0.0);
spheres[4].Rad = 1000.0;
spheres[4].Reflection = 0.15;
spheres[5].Pos = vec3(-1020,0,-0);
spheres[5].Color = vec3(0.2,0.2,0.7);
spheres[5].Rad = 1000.0;
spheres[5].Reflection = 0.15;
spheres[6].Pos = vec3(-5,0,1040);
spheres[6].Color = vec3(0.0,0.8,0.4);
spheres[6].Rad = 1000.0;
spheres[6].Reflection = 0.15;
float invWidth = 1.0 / iResolution.x;
float invHeight = 1.0 / iResolution.y;
float fov = 60.0;
float aspectratio = iResolution.x/iResolution.y;
float angle = tan(M_PI * 0.5 * fov / 180.0);
vec3 sinTime = vec3(sin(iTime*2.0));
vec3 camTrans = vec3(20.0*cos(0.5*iTime), 0, 20.0*sin(0.5*iTime));
vec3 camDir = camTrans - vec3(0);
camTrans.y = -5.0;
LightPos.y = -10.0;//camTrans;
mat3 rot;
vec3 f = normalize(camTrans);
vec3 u = vec3(0,1,0);
vec3 s = normalize(cross(f,u));
u = cross(s,f);
rot[0][0] = s.x; rot[1][0] = s.y; rot[2][0] = s.z;
rot[0][1] = u.x; rot[1][1] = u.y; rot[2][1] = u.z;
rot[0][2] = f.x; rot[1][2] = f.y; rot[2][2] = f.z;
Ray R;
float xx = (2.0 *((fragCoord.x+0.5) * invWidth) -1.0)*angle*aspectratio;
float yy = (1.0-2.0*((fragCoord.y+0.5)*invHeight))*angle;
R.Pos = camTrans;
R.Dir = vec3(xx,yy,-1) * rot;
R.Dir = normalize(R.Dir);
vec3 col=Trace(R);
col=pow(col, vec3(0.45454));
fragColor = vec4(col, 1.0);
}