started on the loader
[csgray] / src / csgray.c
index 44c7eb9..7fc2b46 100644 (file)
@@ -3,6 +3,7 @@
 #include <string.h>
 #include <math.h>
 #include <float.h>
+#include <treestore.h>
 #include "csgimpl.h"
 #include "matrix.h"
 #include "geom.h"
@@ -12,6 +13,7 @@ static int ray_trace(struct ray *ray, float *col);
 static void shade(float *col, struct ray *ray, struct hit *hit);
 static void background(float *col, struct ray *ray);
 static int find_intersection(struct ray *ray, struct hit *best);
+static csg_object *load_object(struct ts_node *node);
 
 static float ambient[3];
 static struct camera cam;
@@ -58,12 +60,38 @@ void csg_fov(float fov)
 
 int csg_load(const char *fname)
 {
-       return 0;       /* TODO */
+       struct ts_node *root = 0, *c;
+
+       if(!(root = ts_load(fname))) {
+               fprintf(stderr, "failed to open %s\n", fname);
+               return -1;
+       }
+       if(strcmp(root->name, "scene") != 0) {
+               fprintf(stderr, "invalid scene file: %s\n", fname);
+               goto err;
+       }
+
+       c = root->child_list;
+       while(c) {
+               csg_object *o = load_object(c);
+               if(!o) goto err;
+               csg_add_object(o);
+               c = c->next;
+       }
+
+       ts_free_tree(root);
+       return 0;
+
+err:
+       if(root) {
+               ts_free_tree(root);
+       }
+       return -1;
 }
 
 int csg_save(const char *fname)
 {
-       return 0;       /* TODO */
+       return -1;      /* TODO */
 }
 
 void csg_add_object(csg_object *o)
@@ -273,6 +301,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 +359,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 +392,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;
@@ -416,3 +481,60 @@ static int find_intersection(struct ray *ray, struct hit *best)
 
        return best->o != 0;
 }
+
+static csg_object *load_object(struct ts_node *node)
+{
+       float *apos, *arot, *ascale, *acolor;
+       float aroughness;
+       struct ts_node *c;
+       csg_object *o, *olist = 0, *otail = 0;
+       int num_subobj = 0;
+
+       c = node->child_list;
+       while(c) {
+               if((o = load_object(c))) {
+                       if(olist) {
+                               otail->ob.next = o;
+                               otail = o;
+                       } else {
+                               olist = otail = o;
+                       }
+                       ++num_subobj;
+               }
+               c = c->next;
+       }
+
+       apos = ts_get_attr_vec(node, "position", 0);
+       arot = ts_get_attr_vec(node, "rotation", 0);
+       ascale = ts_get_attr_vec(node, "scaling", 0);
+       acolor = ts_get_attr_vec(node, "color", 0);
+       aroughness = ts_get_attr_num(node, "roughness", 0);
+
+       if(strcmp(node->name, "null") == 0) {
+               if(num_subobj) {
+                       fprintf(stderr, "null can't have sub-objects\n");
+                       goto err;
+               }
+               if(!(o = alloc_object(OB_NULL))) {
+                       goto err;
+               }
+               return o;
+
+       } else if(strcmp(node->name, "sphere") == 0) {
+               if(num_subobj) {
+                       fprintf(stderr, "sphere can't have sub-objects\n");
+                       goto err;
+               }
+               if(!(o = alloc_object(OB_SPHERE))) {
+                       goto err;
+               }
+       }
+
+err:
+       while(olist) {
+               o = olist;
+               olist = olist->ob.next;
+               csg_free_object(o);
+       }
+       return 0;
+}