X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=csgray;a=blobdiff_plain;f=src%2Fcsgray.c;h=35bb5a84bd6bf8ac640349458108ab5c6412ebc0;hp=fe0228a29eb85934953155bd55cf964ccefe198f;hb=07f86df1b104515376419c7a214253fb435fc396;hpb=7abe0c51fc87e90674427c5ac5848f394af4575a diff --git a/src/csgray.c b/src/csgray.c index fe0228a..35bb5a8 100644 --- a/src/csgray.c +++ b/src/csgray.c @@ -273,6 +273,11 @@ void csg_opacity(csg_object *o, float p) o->ob.opacity = p; } +void csg_metallic(csg_object *o, int m) +{ + o->ob.metallic = m; +} + void csg_render_pixel(int x, int y, int width, int height, float aspect, float *color) { @@ -326,10 +331,10 @@ static int ray_trace(struct ray *ray, float *col) static void shade(float *col, struct ray *ray, struct hit *hit) { - float ndotl, len, falloff; + float ndotl, ndoth, len, falloff, spec; csg_object *o, *lt = plights; float dcol[3], scol[3] = {0}; - float ldir[3]; + float ldir[3], lcol[3], hdir[3]; struct ray sray; struct hit tmphit; @@ -350,7 +355,7 @@ static void shade(float *col, struct ray *ray, struct hit *hit) sray.dy = ldir[1]; sray.dz = ldir[2]; - if(1) {//!find_intersection(&sray, &tmphit) || tmphit.t > 1.0f) { + 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; @@ -359,13 +364,45 @@ static void shade(float *col, struct ray *ray, struct hit *hit) } 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 * lt->ob.emr * ndotl * falloff; - dcol[1] += o->ob.g * lt->ob.emg * ndotl * falloff; - dcol[2] += o->ob.b * lt->ob.emb * ndotl * falloff; + 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; @@ -383,16 +420,32 @@ static void background(float *col, struct ray *ray) static int find_intersection(struct ray *ray, struct hit *best) { + int idx = 0; csg_object *o; - struct hit *hit; + struct hinterv *hit, *it; best->t = FLT_MAX; best->o = 0; o = oblist; while(o) { - if((hit = ray_intersect(ray, o)) && hit->t < best->t) { - *best = *hit; + if((hit = ray_intersect(ray, o))) { + it = hit; + while(it) { + if(it->end[0].t > 1e-6) { + idx = 0; + break; + } + if(it->end[1].t > 1e-6) { + idx = 1; + break; + } + it = it->next; + } + + if(it && it->end[idx].t < best->t) { + *best = it->end[idx]; + } } free_hit_list(hit); o = o->ob.next;