|
|
@ -1,4 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vec3 foregroundColor = vec3(.0841, .5329, .9604);
|
|
|
|
|
|
|
|
vec3 groundColor = vec3(.2, .3, .5);
|
|
|
|
|
|
|
|
vec4 groundSpecular = vec4(.71, .71, .71, 10.);
|
|
|
|
uniform float uTime;// TIME, IN SECONDS
|
|
|
|
uniform float uTime;// TIME, IN SECONDS
|
|
|
|
uniform int flags;
|
|
|
|
uniform int flags;
|
|
|
|
//FLAGS 0-RT, 1-TEX, 2-
|
|
|
|
//FLAGS 0-RT, 1-TEX, 2-
|
|
|
@ -8,13 +11,19 @@ varying vec3 vPos;// -1 < vPos.x < +1
|
|
|
|
|
|
|
|
|
|
|
|
float fl=3.;
|
|
|
|
float fl=3.;
|
|
|
|
const float pi=3.14159265359;
|
|
|
|
const float pi=3.14159265359;
|
|
|
|
const int n_ref=10;
|
|
|
|
const int n_ref=1;
|
|
|
|
const int ns=2;
|
|
|
|
const int ns=2;
|
|
|
|
vec4 Sph[ns];
|
|
|
|
vec4 Sph[ns];
|
|
|
|
uniform sampler2D uSampler[ns];
|
|
|
|
uniform sampler2D uSampler[ns];
|
|
|
|
vec3 Ambient[ns];
|
|
|
|
vec3 Ambient[ns];
|
|
|
|
vec3 Diffuse[ns];
|
|
|
|
vec3 Diffuse[ns];
|
|
|
|
vec4 Specular[ns];
|
|
|
|
vec4 Specular[ns];
|
|
|
|
|
|
|
|
struct RT{
|
|
|
|
|
|
|
|
vec3 color;
|
|
|
|
|
|
|
|
float ks;
|
|
|
|
|
|
|
|
// vec3 ptr;
|
|
|
|
|
|
|
|
// vec3 normal;
|
|
|
|
|
|
|
|
} stack[n_ref];
|
|
|
|
|
|
|
|
|
|
|
|
bool getflag(int flag,int bit){
|
|
|
|
bool getflag(int flag,int bit){
|
|
|
|
float shifted = float(int(float(flag)/ pow(2.,float(bit))));
|
|
|
|
float shifted = float(int(float(flag)/ pow(2.,float(bit))));
|
|
|
@ -61,7 +70,8 @@ void main(){
|
|
|
|
vec3 V=vec3(0.,0.,fl);
|
|
|
|
vec3 V=vec3(0.,0.,fl);
|
|
|
|
vec3 W=normalize(vec3(x,y,-fl));
|
|
|
|
vec3 W=normalize(vec3(x,y,-fl));
|
|
|
|
// RAY TRACE TO ALL OBJECTS IN THE SCENE
|
|
|
|
// RAY TRACE TO ALL OBJECTS IN THE SCENE
|
|
|
|
|
|
|
|
bool rtxoff = getflag(flags, 1);
|
|
|
|
|
|
|
|
int cnt_ref = n_ref;
|
|
|
|
for(int j=0;j<n_ref;j++)
|
|
|
|
for(int j=0;j<n_ref;j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
float tMin=10000.;
|
|
|
|
float tMin=10000.;
|
|
|
@ -82,42 +92,98 @@ void main(){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// IF RAY HITS SPHERE
|
|
|
|
// IF RAY HITS SPHERE
|
|
|
|
float t = tMin;
|
|
|
|
if(iMin >= 0){
|
|
|
|
vec3 S=V+t*W;
|
|
|
|
float t = tMin;
|
|
|
|
for(int i = 0; i < ns; ++ i)
|
|
|
|
vec3 S=V+t*W;
|
|
|
|
if(i == iMin)
|
|
|
|
for(int i = 0; i < ns; ++ i)
|
|
|
|
|
|
|
|
if(i == iMin)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//*TEXTURE MAPPING
|
|
|
|
|
|
|
|
vec3 tex_sph=S-Sph[i].xyz;
|
|
|
|
|
|
|
|
float R=Sph[i].w;
|
|
|
|
|
|
|
|
float tex_x=acos(abs(tex_sph.x)/sqrt(R*R-tex_sph.y*tex_sph.y));
|
|
|
|
|
|
|
|
if(tex_sph.x>0.)
|
|
|
|
|
|
|
|
tex_x=pi-tex_x;
|
|
|
|
|
|
|
|
tex_x=R*tex_x;
|
|
|
|
|
|
|
|
tex_x*=1.5708;//*Correct aspect ratio of texture 2:1 -> 2pir:2r
|
|
|
|
|
|
|
|
tex_x=tex_x+float(uTime)*R;
|
|
|
|
|
|
|
|
float _2pir=2.*pi*R;
|
|
|
|
|
|
|
|
float quo=float(int(tex_x/_2pir));
|
|
|
|
|
|
|
|
tex_x=clampv((tex_x-quo*_2pir),0.,_2pir)/_2pir;
|
|
|
|
|
|
|
|
vec3 texture_color;
|
|
|
|
|
|
|
|
if(!getflag(flags,0))
|
|
|
|
|
|
|
|
texture_color=texture2D(uSampler[i],vec2(tex_x,((R-tex_sph.y)/(2.*R)))).xyz;
|
|
|
|
|
|
|
|
else texture_color = foregroundColor;
|
|
|
|
|
|
|
|
vec3 N=normalize(S-Sph[i].xyz);
|
|
|
|
|
|
|
|
//*DIRECTIONS ARE NORMALIZED TO GET THE CORRECT PHONG LIGHTING
|
|
|
|
|
|
|
|
vec3 realLDir=normalize(LDir-S);
|
|
|
|
|
|
|
|
color=(
|
|
|
|
|
|
|
|
Ambient[i]
|
|
|
|
|
|
|
|
+Diffuse[i]*max(0.,dot(N,realLDir))*LCol
|
|
|
|
|
|
|
|
)*texture_color
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
// + SPECULAR COMPONENT GOES HERE
|
|
|
|
|
|
|
|
if(rtxoff || j == n_ref - 1)
|
|
|
|
|
|
|
|
color += Specular[i].xyz*pow(max(0.,dot(2.*dot(N,realLDir)*N-realLDir,-W)),Specular[i].w);
|
|
|
|
|
|
|
|
stack[j] = RT(color, 0.05);
|
|
|
|
|
|
|
|
V = S;
|
|
|
|
|
|
|
|
W = -normalize(2. * dot(N, W) * N - W);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
// TO SIMIPIFY THINGS UP, I'LL ASSUME THAT EVERYTHING
|
|
|
|
|
|
|
|
// IS INSIDE THE BOUNDING BOX [(-1,-1,-1), (1,1,1)]
|
|
|
|
|
|
|
|
// AND THERE'S A INFINITE FLOOR [y = -1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float t = -(1.+V.y)/W.y;
|
|
|
|
|
|
|
|
if(t >= 0.)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//*TEXTURE MAPPING
|
|
|
|
vec3 S = vec3(V.x + t*W.x, -1, V.z + t*W.z);
|
|
|
|
vec3 tex_sph=S-Sph[i].xyz;
|
|
|
|
vec3 realLDir=normalize(LDir - S);
|
|
|
|
float R=Sph[i].w;
|
|
|
|
|
|
|
|
float tex_x=acos(abs(tex_sph.x)/sqrt(R*R-tex_sph.y*tex_sph.y));
|
|
|
|
|
|
|
|
if(tex_sph.x>0.)
|
|
|
|
|
|
|
|
tex_x=pi-tex_x;
|
|
|
|
|
|
|
|
tex_x=R*tex_x;
|
|
|
|
|
|
|
|
tex_x*=1.5708;//*Correct aspect ratio of texture 2:1 -> 2pir:2r
|
|
|
|
|
|
|
|
tex_x=tex_x+float(uTime)*R;
|
|
|
|
|
|
|
|
float _2pir=2.*pi*R;
|
|
|
|
|
|
|
|
float quo=float(int(tex_x/_2pir));
|
|
|
|
|
|
|
|
tex_x=clampv((tex_x-quo*_2pir),0.,_2pir)/_2pir;
|
|
|
|
|
|
|
|
vec3 texture_color;
|
|
|
|
|
|
|
|
if(!getflag(flags,0))
|
|
|
|
|
|
|
|
texture_color=texture2D(uSampler[i],vec2(tex_x,((R-tex_sph.y)/(2.*R)))).xyz;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vec3 N=normalize(S-Sph[i].xyz);
|
|
|
|
|
|
|
|
vec3 VDir=normalize(V-Sph[i].xyz);
|
|
|
|
|
|
|
|
//*DIRECTIONS ARE NORMALIZED TO GET THE CORRECT PHONG LIGHTING
|
|
|
|
|
|
|
|
vec3 realLDir=normalize(LDir-S);
|
|
|
|
|
|
|
|
color=(
|
|
|
|
color=(
|
|
|
|
Ambient[i]
|
|
|
|
0.5
|
|
|
|
+Diffuse[i]*max(0.,dot(N,realLDir))*LCol
|
|
|
|
+0.5*max(0.,realLDir.y)*LCol
|
|
|
|
)*texture_color
|
|
|
|
)*groundColor
|
|
|
|
// + SPECULAR COMPONENT GOES HERE
|
|
|
|
|
|
|
|
+Specular[i].xyz*pow(max(0.,dot(2.*dot(N,realLDir)*N-realLDir,VDir)),Specular[i].w)
|
|
|
|
|
|
|
|
;
|
|
|
|
;
|
|
|
|
|
|
|
|
// + SPECULAR COMPONENT GOES HERE
|
|
|
|
|
|
|
|
if(rtxoff || j == n_ref - 1)
|
|
|
|
|
|
|
|
color += groundSpecular.xyz*
|
|
|
|
|
|
|
|
pow(max(0., dot(vec3(-realLDir.x, realLDir.y,-realLDir.z),-W)),groundSpecular.w);
|
|
|
|
|
|
|
|
stack[j] = RT(color, 0.1);
|
|
|
|
|
|
|
|
V = S;
|
|
|
|
|
|
|
|
W = vec3(-W.x, W.y, -W.z);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
if(j > 0)
|
|
|
|
|
|
|
|
stack[j] = RT(vec3(1.,1.,1.)*pow(max(0.,dot(-W, normalize(LDir - V))), 10.), 1.);
|
|
|
|
|
|
|
|
cnt_ref = j;// + 1;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(getflag(flags, 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RTX off
|
|
|
|
|
|
|
|
if(rtxoff)
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rtxoff)
|
|
|
|
|
|
|
|
color = stack[0].color;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
color = vec3(0,0,0);
|
|
|
|
|
|
|
|
float currks = 1.;
|
|
|
|
|
|
|
|
for(int i = 0; i < n_ref; ++i)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(i >= cnt_ref)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
color += currks * stack[i - 1].color;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
color += currks *(1.-stack[i].ks) * stack[i].color;
|
|
|
|
|
|
|
|
currks *= stack[i].ks;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(n_ref == cnt_ref)
|
|
|
|
|
|
|
|
color += currks * stack[n_ref - 1].color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// APPLY GAMMA CORRECTION AND SET THE PIXEL COLOR.
|
|
|
|
// APPLY GAMMA CORRECTION AND SET THE PIXEL COLOR.
|
|
|
|
gl_FragColor=vec4(sqrt(color),1.);
|
|
|
|
gl_FragColor=vec4(sqrt(color),1.);
|
|
|
|