Advertisement
Guest User

Floor Class

a guest
Jul 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. class Floor: public Object
  2. {
  3. public:
  4.     Floor(double floor_width, double tile_width){
  5.         reference_point.x = -floor_width/2.0;
  6.         reference_point.y = -floor_width/2.0;
  7.         reference_point.z = 0;
  8.         length = tile_width;
  9.         height = 0;
  10.     }
  11.  
  12.     struct point getNormal(struct point pointed){
  13.         struct point normal;
  14.         normal.x = 0;
  15.         normal.y = 0;
  16.         normal.z = 1;
  17.         return normal;
  18.     }
  19.  
  20.     double getT(Ray *r){
  21.         struct point normal = getNormal(reference_point);
  22.         return ((-1.0) * dotProduct(normal, r->start) / dotProduct(normal, r->dir));
  23.     }
  24.  
  25.     double intersect(Ray *r, struct point* current_color, int level){
  26.         double t = getT(r);
  27.  
  28.         //if(t <= 0) return -1;
  29.  
  30.         struct point intersection;
  31.         intersection.x = r->start.x + r->dir.x * t;
  32.         intersection.y = r->start.y + r->dir.y * t;
  33.         intersection.z = r->start.z + r->dir.z * t;
  34.  
  35.         if(intersection.x < reference_point.x || intersection.x > -reference_point.x || intersection.y < reference_point.y || intersection.y > -reference_point.y){
  36.             return -1;
  37.         }
  38.         if(level == 0) return t;
  39.  
  40.         int x = (intersection.x - reference_point.x) / length;
  41.         int y = (intersection.y - reference_point.y) / length;
  42.  
  43.         if((x + y) % 2 == 0){
  44.             color[0] = color[1] = color[2] = 0;
  45.         }
  46.         else {
  47.             color[0] = color[1] = color[2] = 1;
  48.         }
  49.         current_color->x = color[0] * co_efficients[AMBIENT];
  50.         current_color->y = color[1] * co_efficients[AMBIENT];
  51.         current_color->z = color[2] * co_efficients[AMBIENT];
  52.  
  53.         struct point normal = getNormal(intersection);
  54.         struct point reflection = getReflection(r, normal);
  55.  
  56.         for(int i = 0; i < lights.size(); i++){
  57.             struct point ldir;
  58.             ldir.x = lights[i].x - intersection.x;
  59.             ldir.y = lights[i].y - intersection.y;
  60.             ldir.z = lights[i].z - intersection.z;
  61.  
  62.             double len = sqrt(pow(ldir.x,2) + pow(ldir.y,2) + pow(ldir.z,2));
  63.  
  64.             ldir = normalize(ldir);
  65.  
  66.             struct point lstart;
  67.             lstart.x = intersection.x + ldir.x * 1.0;
  68.             lstart.y = intersection.y + ldir.y * 1.0;
  69.             lstart.z = intersection.z + ldir.z * 1.0;
  70.  
  71.             Ray L(lstart, ldir);
  72.  
  73.             bool obscured = false;
  74.  
  75.             for(int j = 0; j < objects.size(); j++){
  76.                 double possibleobscure = objects[j]->getT(&L);
  77.                 //printf("%f ", possibleobscure);
  78.                 if(possibleobscure > 0 && abs(possibleobscure) > len){
  79.                     obscured = true;
  80.                     break;
  81.                 }
  82.             }
  83.             //printf("\n");
  84.  
  85.             if(!obscured){
  86.                 //printf("direct");
  87.                 //L.dir.x = -L.dir.x;
  88.                 //L.dir.y = -L.dir.y;
  89.                 //L.dir.z = -L.dir.z;
  90.                 double lambert = dotProduct(L.dir, normal);
  91.                 double phong = dotProduct(reflection, r->dir);
  92.                 //if(lambert > 0 || phong > 0) printf("emon to hobar kotha na\n");
  93.                 lambert = lambert > 0?lambert:0;
  94.                 phong = phong > 0?phong:0;
  95.  
  96.                 current_color->x += source_factor * color[0] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
  97.                 current_color->y += source_factor * color[1] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
  98.                 current_color->z += source_factor * color[2] * (lambert * co_efficients[DIFFUSE] + pow(phong, Shine) * co_efficients[SPECULAR]);
  99.             }
  100.  
  101.             if(level < recursion_level){
  102.                 struct point rs;
  103.                 rs.x = intersection.x + reflection.x * 1.0;
  104.                 rs.y = intersection.y + reflection.y * 1.0;
  105.                 rs.z = intersection.z + reflection.z * 1.0;
  106.  
  107.                 //reflection = normalize(reflection);
  108.  
  109.                 Ray reflectionRay(rs, reflection);
  110.                 int nearest = -1;
  111.  
  112.                 struct point reflectColor;
  113.                 double t_min = 9999;
  114.                 for(int k = 0; k < objects.size(); k++){
  115.                     double t = objects[k]->getT(&reflectionRay);
  116.  
  117.                     if(t <= 0) continue;
  118.                     else if(t < t_min) {
  119.                         t_min = t;
  120.                         nearest = k;
  121.                     }
  122.                 }
  123.  
  124.                 if(nearest != -1){
  125.                     double t = objects[nearest]->intersect(&reflectionRay, &reflectColor, level + 1);
  126.                     if(t!=-1){
  127.                         current_color->x += reflectColor.x*co_efficients[REFLECTION];
  128.                         current_color->y += reflectColor.y*co_efficients[REFLECTION];
  129.                         current_color->z += reflectColor.z*co_efficients[REFLECTION];
  130.                     }
  131.  
  132.                     if(current_color->x > 1)    current_color->x = 1;
  133.                     if(current_color->x < 0)    current_color->x = 0;
  134.                     if(current_color->y > 1)    current_color->y = 1;
  135.                     if(current_color->y < 0)    current_color->y = 0;
  136.                     if(current_color->z > 1)    current_color->z = 1;
  137.                     if(current_color->z < 0)    current_color->z = 0;
  138.                 }
  139.             }
  140.  
  141.             //Check whether all current_color pixel value is within 1 or 0 if not set it
  142.         }
  143.  
  144.         return t;
  145.     }
  146.  
  147.  
  148.     void draw(){
  149.         struct point temp = reference_point;
  150.  
  151.         int black = 1;
  152.         while(temp.y < -reference_point.y){
  153.             while(temp.x < -reference_point.x){
  154.                 if(black == 1){
  155.                     glColor3f(0, 0, 0);
  156.                 }
  157.                 else{
  158.                     glColor3f(1, 1, 1);
  159.                 }
  160.  
  161.                 black = 1 - black;
  162.                 glBegin(GL_QUADS);
  163.                     glVertex3f(temp.x, temp.y, temp.z);
  164.                     glVertex3f(temp.x, temp.y + length, temp.z);
  165.                     glVertex3f(temp.x + length, temp.y + length, temp.z);
  166.                     glVertex3f(temp.x + length, temp.y, temp.z);
  167.                 glEnd();
  168.  
  169.                 temp.x += length;
  170.             }
  171.             temp.x = reference_point.x;
  172.             temp.y += length;
  173.             black = 1 - black;
  174.         }
  175.     }
  176. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement