David Jones avatar David Jones committed 9d5312b

foo

Comments (0)

Files changed (8)

 }
 
 void 
-color_mult(Color * a, float b, Color *x)
+color_scalar_mult(Color * a, float b, Color *x)
 {
  assert( a != NULL);
  assert(x  != NULL);
 
 
 void 
+color_mult(Color * a, Color *b, Color *x)
+{
+  assert(a != NULL);
+  assert(b != NULL);
+  assert(x != NULL);
+  x->r = a->r * b->r;
+  x->g = a->g * b->g;
+  x->b = a->b * b->b;
+}
+
+void 
 in_range(float *f, float a, float b)
 {
   if (*f < a) {
 
 
 void 
-color_mult(Color * a, float b, Color *x);
+color_scalar_mult(Color * a, float b, Color *x);
+
+void 
+color_mult(Color * a, Color *b, Color *x);
 
 void 
 color_truncate(Color *a);
 #include "plane.h"
 
 static struct Primitive_Ops plane_ops  =
-  {*plane_intersects, *plane_normal,*plane_get_color};
+  {*plane_intersects, 
+   *plane_normal,
+   *plane_get_color,
+   *plane_is_light};
 
 
 void 
 
   assert(s != NULL);
   s->ops = &plane_ops;
+  s->light = 0;
 }
 
 
   assert(prim != NULL);
   return ((Plane *)prim)->color;
 }
+
+
+int
+plane_is_light(Primitive * p)
+{
+  assert(p != NULL);
+  return ((Plane*)p)->light;
+}
+
   Vec3 normal;
   Vec3 origin;
   Color color;
+  int light;
 } Plane;
 
 void
 Color
 plane_get_color(Primitive * prim);
 
+int 
+plane_is_light(Primitive * p);
+
 #endif
   int view_height;
   int view_width;
 
-
-  int num_lights;
-  Light ** lights;
-
-
   Primitive ** primitives;
   int num_primitives;
 
 char log_buffer[1024];
 
 
+int 
+intersects(Primitive * p, Ray * r, Vec3 * po) 
+{
+  assert(p != NULL);
+  assert(p->ops != NULL);
+  assert(p->ops->intersects != NULL);
+  return (p->ops->intersects)(p,r,po);
+
+}
+
+void 
+get_normal(Primitive * p, Vec3 * r, Vec3 * po) 
+{
+  assert(p != NULL);
+  assert(p->ops != NULL);
+  assert(p->ops->get_normal != NULL);
+  (p->ops->get_normal)(p,r,po);
+
+}
+
+
+int
+is_light(Primitive * p)
+{
+  assert(p != NULL);
+  assert(p->ops != NULL);
+  assert(p->ops->is_light != NULL);
+  return (p->ops->is_light)(p);
+}
+
 Primitive *
 ray_trace(Scene * s, Ray * ray, Color * color, int depth, Vec3 * point)
 {
 
   for(ii = 0; ii < s->num_primitives; ++ii) {
     // for each primitive
+    // printf("\na");
     c = s->primitives[ii];
-    int tmp = (c->ops->intersects)(c, ray, point);
+    int tmp = intersects(c, ray, point);
     // find distance
     if(tmp) {
       Vec3 dist;
 
     int ii = 0;
     
-    for(ii = 0; ii < s->num_lights; ++ii) {
-      Light * li = s->lights[ii];
+    for(ii = 0; ii < s->num_primitives; ++ii) {
+      Primitive * li_tmp = s->primitives[ii];
+      //printf("\nh");
+      if ((li_tmp->ops->is_light)(li_tmp) == 0) continue;
+      // printf("k\n");
+      Sphere * li = (Sphere *) li_tmp;
       Vec3 normal;
       Vec3 to_light;
       float shade = 1.0;
-      vec_subtract(&(li->position), &best_point, &to_light);
+      vec_subtract(&(li->center), &best_point, &to_light);
       float dist_to_light = vec_dot(&to_light, &to_light);
       float divisor = 1.0/vec_dot(&to_light, &to_light);
       divisor = sqrt(divisor);
           if(jj == best_prim) continue;
           Primitive * cp = s->primitives[jj];
           Vec3 np;
-          int r = (cp->ops->intersects)(cp, &tmp, &np);
+          int r = intersects(cp, &tmp, &np);
 
-          if(r) {
+          if(r && (is_light(cp) == 0)) {
             Vec3 dist;
             vec_subtract(&np, &(tmp.origin),  &dist);
             float d = vec_dot(&dist, &dist);
       
       if (dot > 0) {
         Color prim_color = (c->ops->get_color)(c);
-        color_mult(&prim_color, shade * dot, &prim_color);
+        color_scalar_mult(&prim_color, shade * dot, &prim_color);
+        color_mult(&prim_color, &li->color, &prim_color);
         color_add(color, &prim_color, color);
       } else {
       }
   s.view_height = 400;
   s.view_width = 400;
 
-  s.num_lights = 2;
-  s.lights = dmalloc(s.num_lights * sizeof(Light*));
+  s.num_primitives = 6;
+  s.primitives = dmalloc(s.num_primitives*sizeof(Primitive*));
 
-  s.lights[0] = dmalloc(sizeof(Light));
-  s.lights[0]->position.y = 2000;
-  s.lights[0]->position.x =  -20000;
-  s.lights[0]->position.z = -5000;
+  s.primitives[4] = dmalloc(sizeof(Sphere));
+  sphere_init((Sphere*)s.primitives[4]);
+  Sphere * tmps = (Sphere *) s.primitives[4];
+  tmps->light = 1;
 
-  s.lights[1] = dmalloc(sizeof(Light));
-  s.lights[1]->position.x = 10000;
-  s.lights[1]->position.y = 10000;
-  s.lights[1]->position.z = 5000;
+  tmps->center.y = 2000;
+  tmps->center.x =  -20000;
+  tmps->center.z = -5000;
+  tmps->radius = 100;
+  tmps->color.r = .1;
+  tmps->color.g = .1;
+  tmps->color.b = .9;
 
-  s.num_primitives = 4;
-  s.primitives = dmalloc(s.num_primitives*sizeof(Primitive*));
+  s.primitives[5] = dmalloc(sizeof(Sphere));
+  sphere_init((Sphere*)s.primitives[5]);
+  tmps = (Sphere *) s.primitives[5];
+  tmps->light = 1;
+  tmps->center.x = 10000;
+  tmps->center.y = 10000;
+  tmps->center.z = 5000;
+  tmps->radius = 100;
+  tmps->color.r = .9;
+  tmps->color.g = .4;
+  tmps->color.b = .4;
 
   s.primitives[0] = dmalloc(sizeof(Sphere));
   sphere_init((Sphere*)s.primitives[0]);
-  Sphere * tmps = (Sphere *) s.primitives[0];
-
+  tmps = (Sphere *) s.primitives[0];
   tmps->center.x = 5000;
   tmps->center.y = 2000;
   tmps->center.z = 15000;
   tmps->radius = 3000.0;
-  tmps->color.r = .2;
-  tmps->color.g = .1;
-  tmps->color.b = .001;
+  tmps->color.r = .9;
+  tmps->color.g = .9;
+  tmps->color.b = .9;
 
 
   s.primitives[1] = dmalloc(sizeof(Sphere));
   tmps->center.z = 15000;
   tmps->radius =   3000;
   tmps->color.r = .1;
-  tmps->color.g = .2;
-  tmps->color.b = .1;
+  tmps->color.g = .9;
+  tmps->color.b = .9;
 
 
 
   tmps->center.x = -4000;
   tmps->center.z = 17000;
   tmps->radius =   1000;
-  tmps->color.r = .1;
-  tmps->color.g = .2;
-  tmps->color.b = .3;
+  tmps->color.r = .9;
+  tmps->color.g = .9;
+  tmps->color.b = .9;
 
 
 
   tmpp->normal.x = 0;
   tmpp->normal.z = -.2;
 
-  tmpp->color.r = .1;
-  tmpp->color.g = .7;
-  tmpp->color.b = .7;
+  tmpp->color.r = .9;
+  tmpp->color.g = .9;
+  tmpp->color.b = .9;
 
 
-
+  printf("------------\n");
 
   render(&s, ",output.ppm");
   /* 
                      Vec3* point,
                      Vec3 * normal);
   Color (*get_color)(Primitive * prim);
+  int (*is_light)(Primitive * prim);
+
 };
 
 
 #include "sphere.h"
 
 static struct Primitive_Ops sphere_ops  =
-  {*sphere_intersects, *sphere_normal,
-   *sphere_get_color};
+  {*sphere_intersects,
+   *sphere_normal,
+   *sphere_get_color,
+   *sphere_is_light};
 
 
 void 
 
   assert(s != NULL);
   s->ops = &sphere_ops;
+  s->light = 0;
 }
 
 
   assert(prim != NULL);
   return ((Sphere *)prim)->color;
 }
+
+int
+sphere_is_light(Primitive * p)
+{
+  assert(p != NULL);
+  return ((Sphere*)p)->light;
+}
   Vec3 center;
   float radius;
   Color color;
+  int light;
 } Sphere;
 
 void
 void 
 sphere_init(Sphere * s);
 
-
 Color
 sphere_get_color(Primitive * prim);
 
+int
+sphere_is_light(Primitive * p);
+
+
 #endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.