added specular
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 27 Mar 2018 20:05:51 +0000 (23:05 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 27 Mar 2018 20:05:51 +0000 (23:05 +0300)
src/csgimpl.h
src/csgray.c
src/csgray.h
src/geom.c
src/main.c

index ab92f1e..19cd43a 100644 (file)
@@ -21,6 +21,7 @@ struct object {
        float emr, emg, emb;
        float roughness;
        float opacity;
+       int metallic;
 
        float xform[16], inv_xform[16];
 
index 44c7eb9..35bb5a8 100644 (file)
@@ -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;
 
@@ -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;
index 154ca5d..5e6fc31 100644 (file)
@@ -32,6 +32,7 @@ void csg_emission(csg_object *o, float r, float g, float b);
 void csg_color(csg_object *o, float r, float g, float b);
 void csg_roughness(csg_object *o, float r);
 void csg_opacity(csg_object *o, float p);
+void csg_metallic(csg_object *o, int m);
 
 void csg_render_pixel(int x, int y, int width, int height, float aspect, float *color);
 void csg_render_image(float *pixels, int width, int height);
index 341e7c3..bd340bf 100644 (file)
@@ -300,12 +300,12 @@ static struct hinterv *interval_isect(struct hinterv *a, struct hinterv *b)
        }
 
        /* partial overlap */
-       if(a->end[0].t < b->end[1].t) {
-               res->end[0] = b->end[1];
-               res->end[1] = a->end[0];
+       if(a->end[0].t < b->end[0].t) {
+               res->end[0] = b->end[0];
+               res->end[1] = a->end[1];
        } else {
-               res->end[0] = a->end[1];
-               res->end[1] = a->end[0];
+               res->end[0] = a->end[0];
+               res->end[1] = b->end[1];
        }
        return res;
 }
index 301966f..93c7442 100644 (file)
@@ -39,9 +39,20 @@ int main(int argc, char **argv)
 
        oa = csg_sphere(0, 0, 0, 1);
        csg_color(oa, 1, 0.1, 0.05);
+       csg_roughness(oa, 0.3);
        ob = csg_sphere(0.3, 0.7, 0.7, 0.7);
        csg_color(ob, 0.2, 0.3, 1);
+       csg_roughness(ob, 0.3);
        oc = csg_subtraction(oa, ob);
+
+       oa = oc;
+
+       ob = csg_sphere(-0.9, -0.1, 0.7, 0.5);
+       csg_color(ob, 1, 0.9, 0.2);
+       csg_roughness(ob, 0.3);
+
+       oc = csg_subtraction(oa, ob);
+
        csg_add_object(oc);
 
        obj = csg_plane(0, -1, 0, 0, 1, 0);