Commits

David Jones  committed c63b827

foo.

  • Participants
  • Parent commits 5f1334f

Comments (0)

Files changed (15)

 #include <stddef.h>
 
 #include "color.h"
+
 void
 color_add(Color * a, Color *b, 
           Color *x)
 #include <string.h>
+#include <stdlib.h>
 #include "dsocket.h"
 
 dsocket * my_socket;
   /* printf("connect returned: %d\n.", n); */
   socket_send(&msock, "host information \n.\n");
   socket_send(&msock, "This is a test----------\n.\n\n");
-  // sleep(2);
-  // socket_close(&msock);
+  /* sleep(2); */
+  /* socket_close(&msock); */
   return 0;
 
 
 
 LDFLAGS := -lm
 
-CFLAGS := -O2 -fpic
+CFLAGS := -O2 -fpic -ansi
 
 tester: all
 #	./test_file
 
 
 local_lib := ray_trace.a
-local_objs:= vec3.o color.o sphere.o plane.o ray_trace.o
+local_objs:= vec3.o color.o sphere.o plane.o ray_trace.o primitive.o
 include module.mk
 
 
 
 #include "dmem.h"
 #include "ray_types.h"
+#include "primitive.h"
 #include "plane.h"
 
 static struct Primitive_Ops plane_ops  =
 void 
 plane_init(Plane * s)
 {
-
   assert(s != NULL);
   s->ops = &plane_ops;
   s->light = 0;
 }
 
-
-
 void
 plane_normal(Primitive * p, 
              Vec3* point,
              Vec3 * normal)
 {
-  Plane * s ;
-  // assumes point is on plane....
+
+  /* assumes point is on plane.... */
   assert(p != NULL);
   assert(point != NULL);
   assert(normal != NULL);
-  s = (Plane *) p;
-  //vec_subtract(point, &s->origin, normal);
-  //vec_norm(normal,normal);
+  Plane * s = (Plane *) p;
   *normal = s->normal;
 }
 
   assert(ray != NULL);
   assert(point != NULL);
   Plane * plane = (Plane *) p;
-  //vec_print(stdout, &ray->direction);
-  //printf(" ");
-  //vec_print(stdout, &plane->normal);
-  //printf("\n");
+  /*vec_print(stdout, &ray->direction); */
+  /*printf(" "); */
+  /*vec_print(stdout, &plane->normal); */
+  /*printf("\n"); */
   if (fabs(vec_dot(&ray->direction, &plane->normal)) < 1e-6) 
     return 0;
   
   Vec3 diff;
 
   vec_subtract(&plane->origin, &ray->origin, &diff);
-  //printf("difference: ");
-  //vec_print(stdout, &diff);
-  //printf("\n");
+  /*printf("difference: "); */
+  /*vec_print(stdout, &diff); */
+  /*printf("\n"); */
   float a = vec_dot(&plane->normal, &diff);
   float b =  vec_dot(&plane->normal, &ray->direction);
   float n = a / b;
 
-  //printf("a: %f, b: %f, n: %f\n", a, b, n);
+  /*printf("a: %f, b: %f, n: %f\n", a, b, n); */
   if (n < 0) 
     return 0;
   vec_mult_scalar(n, &ray->direction, &diff);
-  //vec_print(stdout, &diff);
-  //printf("nnnnnnnnnnnnnn: %f\npoint: ");
+  /*vec_print(stdout, &diff); */
+  /*printf("nnnnnnnnnnnnnn: %f\npoint: "); */
   vec_add(&diff, &ray->origin, point);
 
   vec_subtract(point, &plane->origin, &diff);
-  float t = fabs(vec_dot(&diff, &plane->normal)) /
-    vec_dot(&diff, &diff);
+  float t = (fabs(vec_dot(&diff, &plane->normal)) /
+             vec_dot(&diff, &diff));
   if(t > 1) {
     vec_print(stdout, point);
     printf("\n");
     vec_print(stdout, &plane->normal);
     printf("\n");
     printf("t: %f\n", t);
-    // assert(1 == 0);
   }
   return 1;
 }
 #ifndef HEADER_GUARD_PLANE_H
 #define HEADER_GUARD_PLANE_H
 
-
 typedef struct
 {
   struct Primitive_Ops * ops;
+#include <stdio.h>
+#include <assert.h>
+
+#include "ray_types.h" 
+#include "primitive.h"
+
+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);
+}
+
+
+Material *
+get_material(Primitive * p)
+{
+  assert(p != NULL);
+  assert(p->ops != NULL);
+  assert(p->ops->get_material != NULL);
+  return (p->ops->get_material)(p);
+}
+
+int
+is_light(Primitive * p)
+{
+  assert(p != NULL);
+  assert(p->ops != NULL);
+  assert(p->ops->is_light != NULL);
+  return (p->ops->is_light)(p);
+}
+
+#ifndef HEADER_GUARD_PRIMITIVE_H
+#define HEADER_GUARD_PRIMITIVE_H
+
+/* #include "ray_types.h" */
+
+typedef struct 
+{
+  struct Primitive_Ops * ops;
+} Primitive;
+
+struct Primitive_Ops{
+  int (*intersects)(Primitive * prim,
+                    Ray * ray,
+                    Vec3 * point);
+  void (*get_normal)(Primitive * prim,
+                     Vec3* point,
+                     Vec3 * normal);
+  Material* (*get_material)(Primitive * prim);
+  int (*is_light)(Primitive * prim);
+};
+
+int 
+intersects(Primitive * p, Ray * r, Vec3 * po) ;
+
+void 
+get_normal(Primitive * p, Vec3 * r, Vec3 * po) ;
+
+Material *
+get_material(Primitive * p);
+
+int
+is_light(Primitive * p);
+
+#endif
 #include "dlog.h"
 
 #include "ray_types.h"
+#include "primitive.h"
 #include "sphere.h"
 #include "plane.h"
 #include "ray_trace.h"
 
 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);
-}
-
-
-Material *
-get_material(Primitive * p)
-{
-  assert(p != NULL);
-  assert(p->ops != NULL);
-  assert(p->ops->get_material != NULL);
-  return (p->ops->get_material)(p);
-}
-
-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 * scene, Ray * ray, 
           Color * color, int depth, Vec3 * point)
   int ii = 0;
 
   for(ii = 0; ii < scene->num_primitives; ++ii) {
-    // for each primitive
-    // printf("\na");
+    /* for each primitive */
+    /* printf("\na"); */
     prim = scene->primitives[ii];
 
-    // find distance
+    /* find distance */
     if(intersects(prim, ray, point)) {
       Vec3 dist;
       vec_subtract(&(ray->origin), point, &dist);
     *color = get_material(scene->primitives[best_prim])->color;
     return scene->primitives[best_prim];
   } else {
-    //sprintf(log_buffer,"hit primitive: %d, point: <%f,%f,%f>",
-    //best_prim, best_point.x, best_point.y, best_point.z);
-    //log_message(log_buffer,1,1);
+    /*sprintf(log_buffer,"hit primitive: %d, point: <%f,%f,%f>", */
+    /*best_prim, best_point.x, best_point.y, best_point.z); */
+    /*log_message(log_buffer,1,1); */
     prim = scene->primitives[best_prim];
     Material * prim_material = get_material(prim);
 
             vec_subtract(&np, &(tmp.origin),  &dist);
             float d = vec_dot(&dist, &dist);
             if(d < dist_to_light) {
-              //vec_print(stdout, &tmp.origin);
-              //              printf("\n");
-              //              vec_print(stdout, &to_light);
-              //              printf("\n");
-              //              vec_print(stdout, &best_point);
-              //              printf("\n");
-              //vec_print(stdout, &li->position);
-              //printf("\n");
-              //vec_print(stdout, &np);
-              //printf("\n");
+              /*vec_print(stdout, &tmp.origin); */
+              /*              printf("\n"); */
+              /*              vec_print(stdout, &to_light); */
+              /*              printf("\n"); */
+              /*              vec_print(stdout, &best_point); */
+              /*              printf("\n"); */
+              /*vec_print(stdout, &li->position); */
+              /*printf("\n"); */
+              /*vec_print(stdout, &np); */
+              /*printf("\n"); */
 
-              //printf(" best_prim: %d, jj: %d, d: %f, dist_to_light: %f\n",
-              //                     best_prim, jj, d, dist_to_light);
+              /*printf(" best_prim: %d, jj: %d, d: %f, dist_to_light:
+              //                     %f\n", */
+              /*                     best_prim, jj, d, dist_to_light); */
               shade = 0.0;
               break;
             }
 
       vec_norm(&to_light,&to_light);
 
-      // log_message("TRACE",1,1);
+      /* log_message("TRACE",1,1); */
 
 
 
           Color light_color = get_material(li_prim)->color;
           color_scalar_mult(&light_color, prim_material->specular * shade * dot, 
                             &light_color);
-          // color_mult(&prim_color, &light_color, &prim_color);
+          /* color_mult(&prim_color, &light_color, &prim_color); */
           color_add(color, &light_color, color);
         }
       }
         }
 
         color_scalar_mult( &rcol, reflection, &rcol);
-        // color_mult(&rcol, &prim_material->color, &rcol);
+        /* color_mult(&rcol, &prim_material->color, &rcol); */
         color_add(color, &rcol, color);
 
       }
     }
     return scene->primitives[best_prim];
   }
-  // log_message("TRACE2",1,1);
+  /* log_message("TRACE2",1,1); */
 }
 
 
 render(Scene * scene, 
           const char * filename) 
 {
+  assert(scene != NULL);
+  assert(filename != NULL);
+  assert(scene->num_primitives < 128);
   Image im;
   int i, j;
-  assert(scene != NULL);
+
   im.width = scene->view_width;
   im.height = scene->view_height;
   im.image = dmalloc(sizeof(Color) * scene->view_height * scene->view_width);
            
   for(j = 0; j < scene->view_height; ++j) {
    for(i = 0; i < scene->view_width; ++i) {
-     // if(do_log > 10) {
-     //fprintf(stdout, "i: %d, j: %d  ", i, j);
-       //log_message(log_buffer, 1,0);
-       // }
+     /* if(do_log > 10) { */
+     /*fprintf(stdout, "i: %d, j: %d  ", i, j); */
+     /*log_message(log_buffer, 1,0); */
+     /* } */
       {
         Vec3 a;
-        a.x = -(scene->view_width/2) + i; //+ 1.0*rand()/RAND_MAX ;;
-        a.y = (scene->view_height/2) - j;// + 1.0*rand()/RAND_MAX ;
+        a.x = -(scene->view_width/2) + i; /*+ 1.0*rand()/RAND_MAX ;; */
+        a.y = (scene->view_height/2) - j;/* + 1.0*rand()/RAND_MAX ; */
         a.z = -0;
-        // printf("point: ");
-        //        vec_print(stdout, &a);
-        //        printf("\n");
-        // printf("\n");
-        //printf("point:%f %f %f\n", a.x, a.y, a.z);
-        // printf("origin:%f %f %f\n", ray.origin.x, ray.origin.y,
-        // ray.origin.z);
+        /* printf("point: "); */
+        /*        vec_print(stdout, &a); */
+        /*        printf("\n"); */
+        /* printf("\n"); */
+        /*printf("point:%f %f %f\n", a.x, a.y, a.z); */
+        /* printf("origin:%f %f %f\n", ray.origin.x, ray.origin.y, */
+        /* ray.origin.z); */
         vec_subtract(&a, &ray.origin, &ray.direction);
-        //        printf("direction:%f %f %f\n", ray.direction.x, ray.direction.y,
-        //ray.direction.z);
+        /*        printf("direction:%f %f %f\n", ray.direction.x,
+        //ray.direction.y, */
+        /*ray.direction.z); */
         vec_norm(&ray.direction, &ray.direction);
-        // printf("direction:%f %f %f\n", ray.direction.x, ray.direction.y,
-        // ray.direction.z);
+        /* printf("direction:%f %f %f\n", ray.direction.x,
+        // ray.direction.y, */
+        /* ray.direction.z); */
       }
 
-      //sprintf(log_buffer,"ray_direction: <%f,%f,%f>",
-      //              ray.direction.x, ray.direction.y, ray.direction.z);
-      //      log_message(log_buffer, 1,1);
-      // Find the first intersection point
+      /*sprintf(log_buffer,"ray_direction: <%f,%f,%f>", */
+      /*              ray.direction.x, ray.direction.y,
+                      ray.direction.z); */
+      /*      log_message(log_buffer, 1,1); */
+      /* Find the first intersection point */
       Color color;
       color.r = 0;
       color.g = 0;

File ray_trace_main.c

 #include "dlog.h"
 
 #include "ray_types.h"
+#include "primitive.h"
 #include "sphere.h"
 #include "plane.h"
 #include "ray_trace.h"
 {
   
   Scene scene;
-  int bookmark = get_bookmark();
   scene.camera_position.x = 0;
   scene.camera_position.y = 0;
   scene.camera_position.z = -500;
   scene.view_width = 800;
 
   scene.num_primitives = 6;
-  // scene.primitives = dmalloc(scene.num_primitives*sizeof(Primitive*));
+  /* scene.primitives =
+     dmalloc(scene.num_primitives*sizeof(Primitive*)); */
 
   int i = 4;
   scene.primitives[i] = dmalloc(sizeof(Sphere));
   sphere_init((Sphere*)scene.primitives[i]);
   tmps = (Sphere *) scene.primitives[i];
   tmps->center.x = 5000;
-  tmps->center.y = 2000;
+  tmps->center.y = 5000;
   tmps->center.z = 15000;
-  tmps->radius = 3000.0;
+  tmps->radius = 4000.0;
   tmps->material.color.r = .9;
   tmps->material.color.g = .9;
   tmps->material.color.b = .9;
   sphere_init((Sphere*)scene.primitives[i]);
   tmps = (Sphere *) scene.primitives[i];
 
-  tmps->center.y = 6000;
-  tmps->center.x = -3000;
+  tmps->center.y = 9000;
+  tmps->center.x = -6000;
   tmps->center.z = 25000;
-  tmps->radius =   3000;
+  tmps->radius =   6000;
   tmps->material.color.r = .1;
   tmps->material.color.g = .1;
   tmps->material.color.b = .9;
   tmpp->material.specular = .3;
   tmpp->material.reflection = .2;
   printf("------------\n");
-
+  int bookmark = get_bookmark();
   render(&scene, ",output.ppm");
   /* 
   im.width = 256;
     n = im.width * im.height;
 ,    for(i = 0; i < (im.width * im.height); ++i) {
       im.image[i] = i / n;
-      // printf("%f\n", im.image[i]);
-    }
-  }
+      printf("%f\n", im.image[i]); 
+      }
+      }
 
   printf("image[0] = %f\n", im.image[0]);
 
 } Material;
 
 
-typedef struct 
-{
-  struct Primitive_Ops * ops;
-} Primitive;
-
-
-struct Primitive_Ops{
-  int (*intersects)(Primitive * prim,
-                    Ray * ray,
-                    Vec3 * point);
-  void (*get_normal)(Primitive * prim,
-                     Vec3* point,
-                     Vec3 * normal);
-  Material* (*get_material)(Primitive * prim);
-  int (*is_light)(Primitive * prim);
-
-};
-
-
-
-struct Prim_List_Struct
-{
-  Primitive * prim;
-  struct Prim_List_Struct * next;
-};
-
-typedef struct Prim_List_Struct PrimitiveList;
 
 #endif
 #include <stdio.h>
 
 #include "ray_types.h"
+#include "primitive.h"
 #include "sphere.h"
 
-static struct Primitive_Ops sphere_ops  =
-  {*sphere_intersects,
-   *sphere_normal,
-   *sphere_get_material,
-   *sphere_is_light};
-
+static struct Primitive_Ops sphere_ops  =  {*sphere_intersects,
+                                            *sphere_normal,
+                                            *sphere_get_material,
+                                            *sphere_is_light};
 
 void 
 sphere_init(Sphere * s)
   s->light = 0;
 }
 
-
-
 void
 sphere_normal(Primitive * p,
               Vec3* point,
               Vec3 * normal)
 {
-  Sphere * s ;
-  // assumes point is on sphere....
+  /* assumes point is on sphere.... */
   assert(p != NULL);
   assert(point != NULL);
   assert(normal != NULL);
-  s = (Sphere *) p;
+  Sphere * s = (Sphere *) p;
   vec_subtract(point, &s->center, normal);
   vec_norm(normal,normal);
 }
 int
 min_root(float a, float b, float c, float *x)
 {
+  /* Find the min positive root of a*x*x + b*x + c = 0 */
+
   int ans = 1;
+
   if(a == 0) {
     *x = (-c / b);
     ans = 1;
       ans = 0;
     } else {
       float disc_root = sqrt(disc);
-      float a1 = (-b + disc_root) / (2 * a);
-      float a2 = (-b - disc_root) / (2 * a);
+      float divisor = 1/(2*a);
+      float a1 = (-b + disc_root) * divisor;
+      float a2 = (-b - disc_root) * divisor;
       if (a1 < a2 || a2 < 0) {
         *x = a1;
       } else {
 }
 
 int 
-sphere_intersects(Primitive * p,
-           Ray * ray,
-           Vec3* point)
+sphere_intersects(Primitive * prim,
+                  Ray * ray,
+                  Vec3* point)
 {
-  float a, b, c;
-  float n1;
-  int ans;
-  assert(p != NULL);
+  assert(prim != NULL);
   assert(ray != NULL);
   assert(point != NULL);
-  Sphere * sphere = (Sphere *) p;
 
-
-  Vec3 diff;
+  Sphere * sphere = (Sphere *) prim;
+  Vec3 to_center;
   vec_subtract(&ray->origin, &sphere->center, 
-               &diff);
-  // printf("dir: %f %f %f\n", xr, yr, zr);
-
+               &to_center);
+  /* printf("dir: %f %f %f\n", xr, yr, zr); */
+  
+  float a, b, c;
   a = vec_dot(&ray->direction, 
               &ray->direction);
+  b = 2 * vec_dot(&to_center,
+                  &ray->direction);
+  c = (vec_dot(&to_center, &to_center) - sphere->radius*sphere->radius);
 
-  b = 2 * vec_dot(&diff,
-                  &ray->direction);
-  c = vec_dot(&diff, &diff) - sphere->radius*sphere->radius;
+  /* printf("abc: %f %f %f\n", a, b, c); */
+  float n;
 
-  // printf("abc: %f %f %f\n", a, b, c);
-  ans = 0;
-  if (min_root(a,b,c,&n1) == 1) {
-    if(n1 > 0) {
-      vec_mult_scalar(n1, &ray->direction, point);
+  if (min_root(a,b,c,&n) == 1) {
+    if(n > 0) {
+      vec_mult_scalar(n, &ray->direction, point);
       vec_add(point, &ray->origin, point);
-      ans = 1;
+      return 1;
     }
   }
-  return ans;
+  return 0;
 }
 
 Material *
 #include "dlog.h"
 
 #include "ray_types.h"
+#include "primitive.h"
 #include "sphere.h"
 #include "plane.h"
 
   buffer = dmalloc(1024 * sizeof(char));
   buffer = dmalloc(1024 * sizeof(char));
   
-  // for(i = 0; i < 2024; ++i)
-  // buffer[i] = 0xff;
+  /* for(i = 0; i < 2024; ++i) */
+  /* buffer[i] = 0xff; */
 
-  // buffer[1029] = 0;
+  /* buffer[1029] = 0; */
   end = get_bookmark();
   check_for_leaks(start,end);
 
 void
 vec_norm(Vec3* a, Vec3* x)
 {
-  float one_over_mag = 1.0/sqrt(vec_dot(a,a));
+  float one_over_mag = 1.0/sqrt(1e-6 + vec_dot(a,a));
   x->x = one_over_mag * a->x;
   x->y = one_over_mag * a->y;
   x->z = one_over_mag * a->z;