+ float ndotl, ndoth, len, falloff, spec;
+ csg_object *o, *lt = plights;
+ float dcol[3], scol[3] = {0};
+ float ldir[3], lcol[3], hdir[3];
+ struct ray sray;
+ struct hit tmphit;
+
+ o = hit->o;
+ dcol[0] = ambient[0];
+ dcol[1] = ambient[1];
+ dcol[2] = ambient[2];
+
+ while(lt) {
+ ldir[0] = NULLXPOS(lt) - hit->x;
+ ldir[1] = NULLYPOS(lt) - hit->y;
+ ldir[2] = NULLZPOS(lt) - hit->z;
+
+ sray.x = hit->x;
+ sray.y = hit->y;
+ sray.z = hit->z;
+ sray.dx = ldir[0];
+ sray.dy = ldir[1];
+ sray.dz = ldir[2];
+
+ if(!find_intersection(&sray, &tmphit) || tmphit.t > 1.0f) {
+ if((len = sqrt(ldir[0] * ldir[0] + ldir[1] * ldir[1] + ldir[2] * ldir[2])) != 0.0f) {
+ float s = 1.0f / len;
+ ldir[0] *= s;
+ ldir[1] *= s;
+ ldir[2] *= s;
+ }
+ falloff = 1.0f / (len * len);
+
+ lcol[0] = lt->ob.emr * falloff;
+ lcol[1] = lt->ob.emg * falloff;
+ lcol[2] = lt->ob.emb * falloff;
+
+ if((ndotl = hit->nx * ldir[0] + hit->ny * ldir[1] + hit->nz * ldir[2]) < 0.0f) {
+ ndotl = 0.0f;
+ }
+
+ dcol[0] += o->ob.r * lcol[0] * ndotl;
+ dcol[1] += o->ob.g * lcol[1] * ndotl;
+ dcol[2] += o->ob.b * lcol[2] * ndotl;
+
+ if(o->ob.roughness < 1.0f) {
+ float gloss = 1.0f - o->ob.roughness;
+
+ hdir[0] = ldir[0] - ray->dx;
+ hdir[1] = ldir[1] - ray->dy;
+ hdir[2] = ldir[2] - ray->dz;
+ if((len = sqrt(hdir[0] * hdir[0] + hdir[1] * hdir[1] + hdir[2] * hdir[2])) != 0.0f) {
+ float s = 1.0f / len;
+ hdir[0] *= s;
+ hdir[1] *= s;
+ hdir[2] *= s;
+ }
+
+ if((ndoth = hit->nx * hdir[0] + hit->ny * hdir[1] + hit->nz * hdir[2]) < 0.0f) {
+ ndoth = 0.0f;
+ }
+ spec = gloss * pow(ndoth, 100.0f * gloss);
+
+ if(o->ob.metallic) {
+ lcol[0] *= o->ob.r;
+ lcol[1] *= o->ob.g;
+ lcol[2] *= o->ob.b;
+ }
+ scol[0] += lcol[0] * spec;
+ scol[1] += lcol[1] * spec;
+ scol[2] += lcol[2] * spec;
+ }
+ }
+
+ lt = lt->ob.plt_next;
+ }
+
+ col[0] = dcol[0] + scol[0];
+ col[1] = dcol[1] + scol[1];
+ col[2] = dcol[2] + scol[2];