Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uint obj_index = -1;
- float t = 50000.0;
- // find the closest hit
- for(uint i=0; i<_objects.size(); i++) {
- // check if ray hits the object
- float dist = _objects.at(i)->intersection(*r);
- // check the closest collision
- if(dist >= 0.0 && dist < t) {
- t = dist;
- obj_index = i;
- }
- }
- // if nothing was hit
- if(t == 50000.0)
- return 0;
- // get the point on the sphere that was hit, and initialize some variables for calculating lighting
- Vector hit = *r->getStart() + r->getDirection()->getUnitVector() * t;
- float lambert = 0, shadow_coeff = 1.0, pct = float(1.0/_lightsources.size());
- // go through each lightsource
- for(uint li=0; li<_lightsources.size(); li++) {
- // set lighting on the point
- Ray lightray = Ray(hit, *_lightsources.at(li)-hit);
- float tmp = lightray.getDirection()->getUnitVector() *
- _objects.at(obj_index)->getNormal(hit);
- if(tmp > lambert && tmp > 0.0) lambert = tmp;
- // after the lighting is decided, check if the spot is shadowed
- for(uint oi=0; oi<_objects.size(); oi++) {
- // checks if other objects is casting shadows on the spot (and of course dont check on itself)
- if(_objects.at(oi)->intersection(lightray) > 0.0 && oi != obj_index) {
- shadow_coeff -= pct;
- break;
- }
- }
- }
- // shadow_coeff is a value between 0 and 1 that tells how many lightsources the pixel is hit by
- lambert *= shadow_coeff;
- // dont let the lambert coeff go < 0.1
- if(lambert < 0.1)
- lambert = 0.1;
- // looks more complicated than it is;
- // just extract color channel, normalise and factor in the lambert coefficient
- float red = ((int((_objects.at(obj_index)->getColor() >> 16) & 0xFF) / 255.0) *lambert)*255;
- float green = ((int((_objects.at(obj_index)->getColor() >> 8) & 0xFF) / 255.0) *lambert)*255;
- float blue = ((int(_objects.at(obj_index)->getColor() & 0xFF) / 255.0) *lambert)*255;
- return ((Uint8)red << 16) + ((Uint8)green << 8) + (Uint8)blue;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement