home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_12 / 9n12049a < prev    next >
Text File  |  1991-10-14  |  4KB  |  114 lines

  1. #include "solid.h"
  2.  
  3. void render_facet(struct solid_obj *obj_ptr,
  4.     struct facet *facet_ptr)
  5. /*  renders (shades) and displays a facet */
  6. {
  7.     struct vertex *vertex_ptr;
  8.     int x_first, y_first; /* screen coords for 1st
  9.         vertex of facet */
  10.     int vertex_ref_index;
  11.     float screen_x, screen_y; /* screen coords */
  12.     float x_normal, y_normal, z_normal; /* surface
  13.         normal vector */
  14.     float x_total, y_total, x_ave, y_ave;
  15.         /* coords to compute seed point for fill */
  16.     float light_vect_x, light_vect_y, light_vect_z;
  17.         /* unit vector from light source to first
  18.         vertex of facet */
  19.     struct vector normal; /* unit vector normal to
  20.         facet */
  21.     float unit; /* mult. factor to get unit vector */
  22.     float illum_x, illum_y, illum_z; /* illumination */
  23.     int illum;
  24.     char fill_pat[12][8] = {
  25.         {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
  26.             /*   0% */
  27.         {0x0, 0x20, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0},
  28.             /*   3% */
  29.         {0x20, 0x0, 0x2, 0x0, 0x80, 0x0, 0x8, 0x0},
  30.             /*   6% */
  31.         {0x20, 0x2, 0x80, 0x8, 0x20, 0x2, 0x80, 0x8},
  32.             /*  12% */
  33.         {0x44, 0x11, 0x44, 0x11, 0x44, 0x11, 0x44,
  34.             0x11}, /*  25% */
  35.         {0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44, 0xaa,
  36.             0x11}, /*  37% */
  37.         {0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55,
  38.             0xaa}, /*  50% */
  39.         {0x55, 0xbb, 0x55, 0xec, 0x55, 0xbb, 0x55,
  40.             0xec}, /*  62% */
  41.         {0xbb, 0xec, 0xbb, 0xec, 0xbb, 0xec, 0xbb,
  42.             0xec}, /*  75% */
  43.         {0xdf, 0xfd, 0x7f, 0xf7, 0xdf, 0xfd, 0x7f,
  44.             0xf7}, /*  87% */
  45.         {0xff, 0xdf, 0xff, 0xff, 0xff, 0xed, 0xff,
  46.             0xff}, /*  93% */
  47.         {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  48.             0xff}}; /* 100% */
  49.     unsigned line_pat[12] = {0x0000, 0x0000, 0x0100,
  50.         0x1010, 0x4444, 0x8a8a, 0xaaaa, 0xdada, 0xdddd,
  51.         0xdfdf, 0xefef, 0xffff};
  52.  
  53.     setcolor(WHITE); /* draw white border */
  54.     setlinestyle(SOLID_LINE, NULL, NORM_WIDTH);
  55.     draw_polygon(obj_ptr, facet_ptr);
  56.  
  57.     /* seed point for fill */
  58.     x_total = y_total = 0.0;
  59.     for (vertex_ref_index = 0; vertex_ref_index <
  60.         facet_ptr->vertex_count; ++vertex_ref_index) {
  61.         vertex_ptr = obj_ptr->vertex_first +
  62.             facet_ptr->vertex_index[vertex_ref_index];
  63.         x_total += proj_d * vertex_ptr->coord[0] /
  64.             proj_z;
  65.         y_total += proj_d * vertex_ptr->coord[1] /
  66.             proj_z;
  67.     }
  68.     x_ave = x_total / facet_ptr->vertex_count;
  69.     y_ave = y_total / facet_ptr->vertex_count;
  70.  
  71.     /* unit vector from 1st vertex to light source */
  72.     light_vect_x = light_x;
  73.     light_vect_y = light_y;
  74.     light_vect_z = light_z;
  75.     unit = 1.0 / sqrt(light_vect_x * light_vect_x +
  76.         light_vect_y * light_vect_y + light_vect_z *
  77.         light_vect_z);
  78.     light_vect_x *= unit;
  79.     light_vect_y *= unit;
  80.     light_vect_z *= unit;
  81.  
  82.     /* unit vector normal to facet */
  83.     normal = normal_vector(obj_ptr, facet_ptr);
  84.     unit = 1.0 / sqrt(normal.x * normal.x + normal.y *
  85.         normal.y + normal.z * normal.z);
  86.     normal.x *= unit;
  87.     normal.y *= unit;
  88.     normal.z *= unit;
  89.  
  90.     /* illumination and shading */
  91.     illum_x = light_vect_x * normal.x;
  92.     illum_y = light_vect_y * normal.y;
  93.     illum_z = light_vect_z * normal.z;
  94.     illum = 1 + (int)((((illum_x < 0.0) ? 0.0 :
  95.         illum_x) + ((illum_y < 0.0) ? 0.0 : illum_y) +
  96.         ((illum_z < 0.0) ? 0.0 : illum_z)) * 11.0 -
  97.         0.5);
  98.     setfillpattern(&fill_pat[illum][0],
  99.         obj_ptr->color); /* fill polygon */
  100.     floodfill((int)(((x_ave - screen_x_min) /
  101.         (screen_x_max - screen_x_min)) * maxx),
  102.         (int)(((y_ave - screen_y_min) /
  103.         (screen_y_max - screen_y_min)) * maxy), WHITE);
  104.     setcolor(color_bkgd); /* redraw border in
  105.         background color */
  106.     draw_polygon(obj_ptr, facet_ptr);
  107.     setcolor(obj_ptr->color); /* overlay border with
  108.         pattern */
  109.     setlinestyle(USERBIT_LINE, line_pat[illum],
  110.         NORM_WIDTH);
  111.     draw_polygon(obj_ptr, facet_ptr);
  112.     return;
  113. }
  114.