Skip to content

Commit

Permalink
no blinn phong when in shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-george committed Mar 11, 2018
1 parent 26c7f6c commit e678736
Showing 1 changed file with 62 additions and 51 deletions.
113 changes: 62 additions & 51 deletions raw.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ void main(void)
Expand Down Expand Up @@ -123,6 +126,9 @@ void main() {
Expand Down Expand Up @@ -173,6 +179,9 @@ void main(void)
Expand Down Expand Up @@ -217,6 +226,9 @@ void main ()
Expand All @@ -228,7 +240,7 @@ void main ()
in vec3 dir;
out vec4 outcolour;
#define FLT_MAX 3.402823e38;
#define FLT_MAX 1.0/0.0;
#define PI 3.141592
uniform mat4 mMatrix;
Expand All @@ -239,20 +251,18 @@ uniform mat3 normalMatrix; //mv matrix without translation
const int raytraceDepth = 42;
const int numSpheres = 6;
const float decay = 0.4;
const float decay = 0.3;
const vec3 lightSource = vec3(6,4,3);
// Blinn-Phong
vec3 diffuse = vec3(0,0,0);
vec3 diffuse = vec3(0.1,0.1,0.1);
vec3 specular = vec3(0.1,0.1,0.1);
float ambientCoefficent = 0.8;
float shininess = 10.0;
float ambientCoefficent = 0.5;
float shininess = 32.0;
float diffuseCoefficent = 0.5;
float specularCoefficent = 0.5;
vec3 blinn_phong(vec3 ambient,vec3 pos, vec3 normal, bool diff_spec){
vec3 blinn_phong(vec3 ambient, vec3 pos, vec3 normal){
vec3 ambientFinal = ambientCoefficent*ambient;
if (!diff_spec) return ambientFinal;
vec3 l = normalize(lightSource-pos);
vec3 v = normalize(-pos);
vec3 r = normalize(2*dot(normal,l)*normal-l);
Expand Down Expand Up @@ -294,6 +304,7 @@ struct Intersection
float t; //closest hit
vec3 point; // hit point
vec3 normal; // normal
vec3 bp_colour;
vec3 colour;
int hit; //did it hit?
float r; // reflection constant
Expand Down Expand Up @@ -327,10 +338,11 @@ void sphere_intersect(Sphere sph, inout Ray ray, inout Intersection intersect)
intersect.hit = 1;
intersect.normal = normalize(intersect.point - sph.centre);
//if (u<intersect.t){
intersect.colour = sph.colour;//
if (u<intersect.t){
intersect.colour = sph.colour;
intersect.bp_colour = blinn_phong(intersect.colour,intersect.point,intersect.normal);
intersect.t=u;
//}
}
}
Expand All @@ -339,17 +351,18 @@ void sphere_intersect(Sphere sph, inout Ray ray, inout Intersection intersect)
void plane_intersect(Plane pl, inout Ray ray, inout Intersection intersect)
{
float denom = -dot(ray.dir,pl.normal);
if (denom >= 1e-10){
if (denom >0){
float u = dot(ray.origin-pl.point,pl.normal)/denom;
if (u<intersect.t && u >epsilon){
if (u >epsilon){ // u<intersect.t &&
intersect.hit = 1;
vec3 p = u*ray.dir;
intersect.point = p;
intersect.point = ray.origin + p;
vec3 col = pl.colour;
if (bool((int(p.x)+int(p.y)+int(p.z))%2)){
col = -pl.colour;
if (!bool((int(p.x)+int(p.z))%2)){
col = vec3(0,0,0);
}
intersect.colour += pow(ray.depth,-2)*col;
intersect.colour += col;
intersect.bp_colour += blinn_phong(intersect.colour,intersect.point,intersect.normal);
intersect.normal = normalize(pl.normal);
intersect.t = u;
}
Expand All @@ -359,9 +372,17 @@ void plane_intersect(Plane pl, inout Ray ray, inout Intersection intersect)
Sphere sphere[numSpheres];
Plane plane;
void Intersect(inout Ray r, inout Intersection i)
{ float closest = FLT_MAX;
{
float closest = i.t;
Intersection intersect;
plane_intersect(plane, r, intersect);
if (bool(intersect.hit) && intersect.t < closest){
i = intersect;
closest = intersect.t;
}
// For each object in the scene, do intersect function
for (int j=0;j<numSpheres;j++){
intersect.t = closest;
sphere_intersect(sphere[j],r,intersect);
Expand All @@ -370,13 +391,8 @@ void Intersect(inout Ray r, inout Intersection i)
closest = intersect.t;
}
}
// For each object in the scene, do intersect function
plane_intersect(plane, r, intersect);
if (bool(intersect.hit) && intersect.t < closest){
i = intersect;
closest = intersect.t;
}
}
int seed = 0;
Expand All @@ -386,21 +402,23 @@ float rnd()
return float(seed)/509.0;
}
bool computeShadow(in Intersection intersect)
vec3 computeShadow(in Intersection intersect)
{
// ray from intersect to the light source
Ray shadowRay;
shadowRay.dir = (intersect.point -lightSource)*seed;
shadowRay.dir = seed*(intersect.point -lightSource);
shadowRay.origin = intersect.point + shadowRay.dir;
shadowRay.depth = 1;
Intersection i2;
i2.hit = 0;
i2.t = FLT_MAX;
// see if there are any intersections
Intersect(shadowRay, i2);
return bool(i2.hit);
return false;
if (!bool(i2.hit)){
return intersect.bp_colour;
}
else {
return intersect.colour;
}
}
void main()
Expand Down Expand Up @@ -429,11 +447,11 @@ void main()
plane.colour = vec3(1, 1, 1);
seed = int(mod(dir.x * dir.y * 39786038.0, 65536.0));
//scene definition end
//mat4 transMat1;
//transMat1[0] = vec4(-1.0,0.0,0.0,0.0);
//transMat1[1] = vec4(0.0,-1.0,0.0,0.0);
//transMat1[2] = vec4(0.0,0.0,-1.0,0.0);
//transMat1[3] = vec4(0.0,0.0,0.0,1.0);
mat4 transMat1;
transMat1[0] = vec4(-1.0,0.0,0.0,0.0);
transMat1[1] = vec4(0.0,-1.0,0.0,0.0);
transMat1[2] = vec4(0.0,0.0,-1.0,0.0);
transMat1[3] = vec4(0.0,0.0,0.0,1.0);
for (int k; k<numSpheres;k++){
sphere[k].radius2 = pow(sphere[k].radius,2);
Expand All @@ -442,31 +460,21 @@ void main()
vec4 colour = vec4(0,0,0,1);
Ray ray;
ray.origin = vec3(0,0,0);//((mvMatrixScene)*vec4(0,0,0,1)).xyz;// vec3(0,0,0);//(mvMatrix*vec4(0,0,0,1)).xyz;
ray.origin = vec3(0,0,0); //((mvMatrixScene)*vec4(0,0,0,1)).xyz;// vec3(0,0,0);//(mvMatrix*vec4(0,0,0,1)).xyz;
ray.dir = normalize((mvMatrixScene)*vec4(dir,0)).xyz;
float closest = 1.0/0.0;
Intersection i;
i.hit = 0;
i.colour=vec3(0,0,0);
for(int d = 0; d < 6;d++){
for(int d = 0; d < 10;d++){
ray.depth = d;
i.t = closest;
Intersect(ray,i);
if (i.hit != 0) {
if (i.t<closest){
closest = i.t;
colour += pow(decay,d)*vec4(blinn_phong(i.colour,i.point,i.normal,true),1);
//colour += pow(decay,d)*vec4(i.colour,1);
if (computeShadow(i)) {
colour = vec4(colour.xyz/2,0);
// add blinn phong shading
}
colour += (pow(decay,pow(d,2)))*vec4(computeShadow(i),1);//pow(decay,pow(d,2))*vec4(blinn_phong(i.colour,i.point,i.normal,true),1);
}
ray.origin = i.point;
ray.origin += epsilon * i.normal;
ray.origin = i.point + epsilon * i.normal;
ray.dir = reflect(ray.dir,i.normal);
} else {
break;
Expand Down Expand Up @@ -494,6 +502,9 @@ void main()
Expand Down

0 comments on commit e678736

Please sign in to comment.