Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. uint obj_index = -1;
  2. float t = 50000.0;
  3.  
  4. // find the closest hit
  5. for(uint i=0; i<_objects.size(); i++) {
  6.     // check if ray hits the object
  7.     float dist = _objects.at(i)->intersection(*r);
  8.  
  9.     // check the closest collision
  10.     if(dist >= 0.0 && dist < t) {
  11.         t = dist;
  12.         obj_index = i;
  13.     }
  14. }
  15.  
  16. // if nothing was hit
  17. if(t == 50000.0)
  18.     return 0;
  19.  
  20. // get the point on the sphere that was hit, and initialize some variables for calculating lighting
  21. Vector hit = *r->getStart() + r->getDirection()->getUnitVector() * t;
  22. float lambert = 0, shadow_coeff = 1.0, pct = float(1.0/_lightsources.size());
  23.  
  24. // go through each lightsource
  25. for(uint li=0; li<_lightsources.size(); li++) {
  26.     // set lighting on the point
  27.     Ray lightray = Ray(hit, *_lightsources.at(li)-hit);
  28.  
  29.     float tmp = lightray.getDirection()->getUnitVector() *
  30.                 _objects.at(obj_index)->getNormal(hit);
  31.  
  32.     if(tmp > lambert && tmp > 0.0) lambert = tmp;
  33.  
  34.     // after the lighting is decided, check if the spot is shadowed
  35.     for(uint oi=0; oi<_objects.size(); oi++) {
  36.  
  37.         // checks if other objects is casting shadows on the spot (and of course dont check on itself)
  38.         if(_objects.at(oi)->intersection(lightray) > 0.0 && oi != obj_index) { 
  39.             shadow_coeff -= pct;
  40.             break;
  41.         }
  42.     }
  43.  
  44. }
  45.  
  46. // shadow_coeff is a value between 0 and 1 that tells how many lightsources the pixel is hit by
  47. lambert *= shadow_coeff;
  48.  
  49. // dont let the lambert coeff go < 0.1
  50. if(lambert < 0.1)
  51.     lambert = 0.1;
  52.  
  53.  
  54. // looks more complicated than it is;
  55. // just extract color channel, normalise and factor in the lambert coefficient
  56. float red = ((int((_objects.at(obj_index)->getColor() >> 16) & 0xFF) / 255.0) *lambert)*255;
  57. float green = ((int((_objects.at(obj_index)->getColor() >> 8) & 0xFF) / 255.0) *lambert)*255;
  58. float blue = ((int(_objects.at(obj_index)->getColor() & 0xFF) / 255.0) *lambert)*255;
  59.  
  60. return ((Uint8)red << 16) + ((Uint8)green << 8) + (Uint8)blue;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement