Advertisement
Guest User

intersect function

a guest
Jul 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1.     double intersect(Ray *r, struct point* current_color, int level)
  2.     {
  3.         //printf("%f %f %f ", r->dir.x, r->dir.y, r->dir.z);
  4.  
  5.         double t = getT(r);
  6.  
  7.         //printf("here0\n");
  8.         if(t <= 0) return -1;
  9.  
  10.         //printf("here1\n");
  11.         if(level == 0) return t;
  12.  
  13.         //printf("here2\n");
  14.         struct point intersection;
  15.         intersection.x = r->start.x + t * r->dir.x;
  16.         intersection.y = r->start.y + t * r->dir.y;
  17.         intersection.z = r->start.z + t * r->dir.z;
  18.  
  19.         current_color->x = color[0] * co_efficients[AMBIENT];
  20.         current_color->y = color[1] * co_efficients[AMBIENT];
  21.         current_color->z = color[2] * co_efficients[AMBIENT];
  22.  
  23.         struct point normal = getNormal(intersection);
  24.         struct point reflection = getReflection(r, normal);
  25.  
  26.         for(int i = 0; i < lights.size(); i++){
  27.             struct point ldir;
  28.             ldir.x = lights[i].x - intersection.x;
  29.             ldir.y = lights[i].y - intersection.y;
  30.             ldir.z = lights[i].z - intersection.z;
  31.  
  32.             ldir = normalize(ldir);
  33.  
  34.             struct point lstart;
  35.             lstart.x = intersection.x + ldir.x * 1.0;
  36.             lstart.y = intersection.y + ldir.y * 1.0;
  37.             lstart.z = intersection.z + ldir.z * 1.0;
  38.  
  39.             Ray L(lstart, ldir);
  40.  
  41.             bool obscured = false;
  42.  
  43.             for(int j = 0; j < objects.size(); j++){
  44.                 double possibleobscure = objects[j]->getT(&L);
  45.                 //printf("%f ", possibleobscure);
  46.                 if(possibleobscure > 0 && abs(possibleobscure) < sqrt(pow(ldir.x,2) + pow(ldir.y,2) + pow(ldir.z,2))){
  47.                     obscured = true;
  48.                     break;
  49.                 }
  50.             }
  51.             //printf("\n");
  52.  
  53.             if(!obscured){
  54.                 //printf("direct");
  55.                 L.dir.x = -L.dir.x;
  56.                 L.dir.y = -L.dir.y;
  57.                 L.dir.z = -L.dir.z;
  58.                 double lambert = dotProduct(L.dir, normal);
  59.                 double phong = dotProduct(reflection, r->dir);
  60.                 //if(lambert > 0 || phong > 0) printf("emon to hobar kotha na\n");
  61.                 lambert = lambert > 0?lambert:0;
  62.                 phong = phong > 0?phong:0;
  63.  
  64.                 current_color->x += source_factor * color[0] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
  65.                 current_color->y += source_factor * color[1] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
  66.                 current_color->z += source_factor * color[2] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
  67.             }
  68.  
  69.             if(level < recursion_level){
  70.                 struct point rs;
  71.                 rs.x = intersection.x + reflection.x * 1.0;
  72.                 rs.y = intersection.y + reflection.y * 1.0;
  73.                 rs.z = intersection.z + reflection.z * 1.0;
  74.  
  75.                 //reflection = normalize(reflection);
  76.  
  77.                 Ray reflectionRay(rs, reflection);
  78.                 int nearest = -1;
  79.  
  80.                 struct point reflectColor;
  81.                 double t_min = 9999;
  82.                 for(int k = 0; k < objects.size(); k++){
  83.                     double t = objects[k]->getT(&reflectionRay);
  84.  
  85.                     if(t <= 0) continue;
  86.                     else if(t < t_min) {
  87.                         t_min = t;
  88.                         nearest = k;
  89.                     }
  90.                 }
  91.  
  92.                 if(nearest != -1){
  93.                     double t = objects[nearest]->intersect(&reflectionRay, &reflectColor, level + 1);
  94.                     current_color->x += reflectColor.x*co_efficients[REFLECTION];
  95.                     current_color->y += reflectColor.y*co_efficients[REFLECTION];
  96.                     current_color->z += reflectColor.z*co_efficients[REFLECTION];
  97.                 }
  98.             }
  99.  
  100.         }
  101.  
  102.         return t;
  103.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement