home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_dos.lha / dos / sphsrc.v08 / sph_traverse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  6.7 KB  |  224 lines

  1. #include "HEADERS.h"
  2. #include "sphigslocal.h"
  3. #include "sph_draw.h"
  4. #include <string.h>
  5.  
  6. /** Transforming the normal from MV to UVN
  7. Here, we compute a matrix that can be used to transform a normal 
  8. from MV to UVN.  Based on John Hughes' magic!
  9. **/
  10.  
  11. static void ComputeNormalTransformer (void)
  12. {
  13. #  define a currentMCtoUVNxform[0][0]
  14. #  define b currentMCtoUVNxform[0][1]
  15. #  define c currentMCtoUVNxform[0][2]
  16. #  define d currentMCtoUVNxform[1][0]
  17. #  define e currentMCtoUVNxform[1][1]
  18. #  define f currentMCtoUVNxform[1][2]
  19. #  define p currentMCtoUVNxform[2][0]
  20. #  define q currentMCtoUVNxform[2][1]
  21. #  define r currentMCtoUVNxform[2][2]
  22.  
  23.    bzero (currentNormalMCtoUVNxform, sizeof(matrix));
  24.    currentNormalMCtoUVNxform[0][0] = e*r-q*f;
  25.    currentNormalMCtoUVNxform[0][1] = p*f-d*r;
  26.    currentNormalMCtoUVNxform[0][2] = d*q-p*e;
  27.    currentNormalMCtoUVNxform[1][0] = q*c-b*r;
  28.    currentNormalMCtoUVNxform[1][1] = a*r-p*c;
  29.    currentNormalMCtoUVNxform[1][2] = p*b-q*a;
  30.    currentNormalMCtoUVNxform[2][0] = b*f-c*e;
  31.    currentNormalMCtoUVNxform[2][1] = d*c-a*f;
  32.    currentNormalMCtoUVNxform[2][2] = a*e-d*b;
  33. }
  34.       
  35.  
  36.  
  37. /** SPH_traverse_struct_for_display
  38. **/
  39. void
  40. SPH__traverse_struct_for_display (int structID, 
  41.                  attribute_group *inherited_attrs)
  42. {
  43.    element *curel;
  44.    matrix_4by4 global_modxform, local_modxform;
  45.    matrix_4by4 save_compositeModxform, save_MCtoUVNxform, 
  46.                save_NormalMCtoUVNxform, save_TOTALxform;
  47.    attribute_group my_attrs;
  48.  
  49.    my_attrs = *inherited_attrs;
  50.  
  51.    /* Since a subordinate structure shouldn't affect its parent's 
  52.     *   composite modeling xform, each subordinate saves and restores it.
  53.     */
  54.    MAT3copy (save_compositeModxform, currentCompositeModxform);
  55.    MAT3copy (save_MCtoUVNxform, currentMCtoUVNxform);
  56.    MAT3copy (save_NormalMCtoUVNxform, currentNormalMCtoUVNxform);
  57.    MAT3copy (save_TOTALxform, currentTOTALxform);
  58.  
  59.    /* The local matrix for any structure is initially the identity matrix,
  60.     *   so its initial composite matrix is equal to the global matrix.
  61.     */
  62.    MAT3identity (local_modxform);
  63.    MAT3copy (global_modxform, currentCompositeModxform);
  64.  
  65.  
  66. /*!*/
  67.    for /* FOR EACH ELEMENT IN THE STRUCTURE, IN ORDER: */
  68.    (curel = SPH__structureTable[structID].first_element;
  69.     curel;
  70.     curel = curel->next)
  71.  
  72.       switch (curel->type) {
  73.  
  74.        case ELTYP__POLYHEDRON:
  75.      SPH__draw_polyhedron (curel->data.poly, &my_attrs);
  76.      break;
  77.        case ELTYP__FILL_AREA:
  78.      SPH__draw_fill_area (curel, &my_attrs);
  79.      break;
  80.        case ELTYP__POLYLINE:
  81.      SPH__draw_lines (curel, &my_attrs);
  82.      break;
  83.        case ELTYP__POLYMARKER:
  84.      SPH__draw_markers (curel, &my_attrs);
  85.      break;
  86.        case ELTYP__TEXT:
  87.      SPH__draw_text
  88.         (curel->data.point, curel->info.textstring, &my_attrs);
  89.      break;
  90.  
  91.        case ELTYP__EXECUTE_STRUCTURE:
  92.      SPH__traverse_struct_for_display (curel->data.value, &my_attrs);
  93.      break;
  94.  
  95.        case ELTYP__SET_INTERIOR_COLOR:
  96.      my_attrs.interior_color = curel->data.value;
  97.      break;
  98.  
  99.        case ELTYP__SET_EDGE_COLOR:
  100.      my_attrs.edge_color = curel->data.value;
  101.      break;
  102.        case ELTYP__SET_EDGE_WIDTH:
  103.      my_attrs.edge_width = curel->data.value;
  104.      break;
  105.        case ELTYP__SET_EDGE_STYLE:
  106.      my_attrs.edge_style = curel->data.value;
  107.      break;
  108.        case ELTYP__SET_EDGE_FLAG:
  109.      my_attrs.edge_flag = curel->data.value;
  110.      break;
  111.  
  112.        case ELTYP__SET_MARKER_COLOR:
  113.      my_attrs.marker_color = curel->data.value;
  114.      break;
  115.        case ELTYP__SET_MARKER_SIZE:
  116.      my_attrs.marker_size = curel->data.value;
  117.      break;
  118.        case ELTYP__SET_MARKER_STYLE:
  119.      my_attrs.marker_style = curel->data.value;
  120.      break;
  121.  
  122.        case ELTYP__SET_TEXT_COLOR:
  123.      my_attrs.text_color = curel->data.value;
  124.      break;
  125.        case ELTYP__SET_TEXT_FONT:
  126.      my_attrs.font = curel->data.value;
  127.      break;
  128.  
  129.        case ELTYP__SET_LINE_COLOR:
  130.      my_attrs.line_color = curel->data.value;
  131.      break;
  132.        case ELTYP__SET_LINE_WIDTH:
  133.      my_attrs.line_width = curel->data.value;
  134.      break;
  135.        case ELTYP__SET_LINE_STYLE:
  136.      my_attrs.line_style = curel->data.value;
  137.      break;
  138.  
  139.        case ELTYP__CLEAR_MODXFORM:
  140.              MAT3identity (local_modxform);
  141.       MAT3copy (currentCompositeModxform, save_compositeModxform);
  142.       MAT3copy (currentTOTALxform, save_TOTALxform);
  143.       MAT3copy (currentMCtoUVNxform, save_MCtoUVNxform);
  144.       MAT3copy (currentNormalMCtoUVNxform, save_NormalMCtoUVNxform);
  145.       break;
  146.             
  147.      
  148.        case ELTYP__SET_MODXFORM:
  149.      switch (curel->info.update_type) {
  150.          case ASSIGN:
  151.            MAT3copy (local_modxform, curel->data.matrix);
  152.                break;
  153.          case POSTCONCATENATE:
  154.            MAT3mult (local_modxform, curel->data.matrix, local_modxform);
  155.            break;
  156.          case PRECONCATENATE:
  157.            MAT3mult (local_modxform, local_modxform, curel->data.matrix);
  158.            break;
  159.         }
  160.      MAT3mult (currentCompositeModxform, local_modxform, global_modxform);
  161.      MAT3mult (currentMCtoUVNxform, currentCompositeModxform,
  162.            SPH_viewTable[currentViewIndex].vo_matrix);
  163.          MAT3mult (currentTOTALxform, currentCompositeModxform, 
  164.                SPH_viewTable[currentViewIndex].cammat);
  165.      if (SPH_viewTable[currentViewIndex].rendermode > WIREFRAME)
  166.         ComputeNormalTransformer();
  167.       }
  168.  
  169.    /* The restoration referred to earlier. */
  170.    MAT3copy (currentCompositeModxform, save_compositeModxform);
  171.    MAT3copy (currentTOTALxform, save_TOTALxform);
  172.    MAT3copy (currentMCtoUVNxform, save_MCtoUVNxform);
  173.    MAT3copy (currentNormalMCtoUVNxform, save_NormalMCtoUVNxform);
  174. }
  175.       
  176.  
  177.  
  178.  
  179. #define INITIALIZED_LATER  0
  180.  
  181. static attribute_group default_attribute_group = {
  182.    INITIALIZED_LATER /* black line color */,
  183.    LINE_WIDTH_UNIT_IN_PIXELS /* line width */, CONTINUOUS /* line style */,
  184.  
  185.    INITIALIZED_LATER /* black marker color */,
  186.    MARKER_SIZE_UNIT_IN_PIXELS /* marker size */, 
  187.    MARKER_CIRCLE /* marker style */,
  188.  
  189.    INITIALIZED_LATER /* white interior color */,
  190.  
  191.    1 /* edge width */, CONTINUOUS /* edge style */,
  192.    EDGE_VISIBLE /* edge_flag */, INITIALIZED_LATER /* black edge color */,
  193.  
  194.    INITIALIZED_LATER /* black text color */, 0 /* default SRGP font */
  195. };
  196.  
  197.  
  198. void SPH__initDefaultAttributeGroup (void)
  199. {
  200.    default_attribute_group.line_color = SRGP_BLACK;
  201.    default_attribute_group.marker_color = SRGP_BLACK;
  202.    default_attribute_group.text_color = SRGP_BLACK;
  203.    default_attribute_group.edge_color = SRGP_BLACK;
  204.    default_attribute_group.interior_color = SRGP_WHITE;
  205. }
  206.  
  207.  
  208.  
  209. /** SPH_traverse_network_for_display 
  210. **/
  211. void
  212. SPH__traverse_network_for_display (view_spec *viewSpec, root_header *network)
  213. {
  214.    MAT3identity (currentCompositeModxform);
  215.    MAT3copy (currentTOTALxform, viewSpec->cammat);
  216.    MAT3copy (currentMCtoUVNxform, viewSpec->vo_matrix);
  217.    if (viewSpec->rendermode > WIREFRAME)
  218.       ComputeNormalTransformer();
  219.  
  220.    /* NOW LET'S TRAVERSE THE NETWORK!!!! */
  221.    SPH__traverse_struct_for_display
  222.       (network->root_structID, &default_attribute_group);
  223. }
  224.