home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vectoper.zip / RTNrmOps.cpp < prev    next >
C/C++ Source or Header  |  1996-09-19  |  4KB  |  159 lines

  1.  
  2.  
  3. #include <stdio.h>
  4.  
  5. #include "RTTypes.h"
  6. #include "RTVecOps.h"
  7.  
  8.  
  9.  
  10. /*
  11.  *******************************************************************************
  12.  *
  13.  *  Name:  surf_tri_normals
  14.  *
  15.  *  Purpose: this routine determines the normals for a set of triangles 
  16.  *           specified by a surface structure.  The vertices of the tri-
  17.  *           angles are already in eye coordinates when this routine is
  18.  *           called due to a caching mechanism for data points in eye
  19.  *           coordinates.
  20.  *
  21.  *
  22.  *
  23.  *  Input Parameters
  24.  *
  25.  *     data - data points in eye coordinates
  26.  *
  27.  *
  28.  *  Output Parameters
  29.  *
  30.  *     sptr - ptr to structure defining surface
  31.  *
  32.  *******************************************************************************
  33.  */
  34. void
  35.     surf_tri_norms(Surface *sptr, Vector *data)
  36.       {
  37.  
  38.        Triangle *tptr;     /* pointer to current triangle structure */
  39.        Vector pts[3];      /* array to make code more understandable */
  40.        Vector Pa, Pb;      /* vectors used to find normal for triangle */
  41.        Vector N_vec;       /* normal vector for current triangle */
  42.  
  43.        int i, j;
  44.  
  45.  
  46. /*
  47.  *     cycle through each triangle in the surface
  48.  */
  49.        for (i=0; i<sptr->num_triang; i++)  {
  50.            tptr = &sptr->triangs[i];
  51.            vset (&tptr->ce,  (float) 0.0,  (float) 0.0,  (float) 0.0);
  52.  
  53. /*
  54.  *         put triangle vertices in a separate array
  55.  *         (it just makes code more readable to me)
  56.  */
  57.            for (j=0; j<3; j++)  {
  58.                pts[j] = data[tptr->v[j]];
  59.                tptr->ce = Vector_Sum(&tptr->ce, &pts[j]);
  60.               }
  61.  
  62. /*
  63.  *         calculate centroid of triangle
  64.  */
  65.            tptr->ce = VectorScaler_Division(&tptr->ce, (float)3.0);
  66.  
  67. /*
  68.  *         calculate normal vector for triangle
  69.  */
  70.            Pa = Vector_Difference( &pts[1], &pts[0] );
  71.            Pb = Vector_Difference( &pts[2], &pts[0] );
  72.  
  73.            Cross_Product(&Pa, &Pb, &N_vec);
  74.            tptr->normal = VectorScaler_Division(&N_vec, vector_len(&N_vec));
  75.           }
  76.  
  77.       }
  78.  
  79.  
  80.  
  81. /*
  82.  *******************************************************************************
  83.  *
  84.  *  Name:  surf_tri_normals
  85.  *
  86.  *  Purpose:  this routine determines the approximate normals for each vertex
  87.  *            in the triangles for a specified surface.  Since a vertex may
  88.  *            be in more than one surface, the is a point normals array for
  89.  *            each surface in an object.
  90.  *
  91.  *
  92.  *
  93.  *  Input Parameters
  94.  *
  95.  *     data - data points in eye coordinates
  96.  *
  97.  *
  98.  *  Output Parameters
  99.  *
  100.  *     sptr - ptr to structure defining surface
  101.  *
  102.  *******************************************************************************
  103.  */
  104. void
  105.     surf_pt_norms(Surface *sptr, Vector *pnorms, int nvert)
  106.       {
  107.        Triangle *tptr;
  108.  
  109.        int i, vertex[501];
  110.  
  111.  
  112. /*
  113.  *     initialize point normals array
  114.  */
  115.  
  116.        for (i=0; i<500; i++)
  117.            vertex[i] = 0;
  118.  
  119.        for (i=0; i<nvert; i++)
  120.            vset(&pnorms[i],  (float) 0.0,  (float) 0.0,  (float) 0.0);
  121.  
  122. /*
  123.  +++++++++++++++++++++++++++++++++++++++++++++++++
  124.  *  cycle through each triangle in the surface
  125.  +++++++++++++++++++++++++++++++++++++++++++++++++
  126.  */
  127.        for (i=0; i<sptr->num_triang; i++)  {
  128.            tptr = &sptr->triangs[i];
  129.  
  130. /*
  131.  *         add triangle normal into normal for each vertex for that triangle
  132.  */           
  133.            for (int j=0; j<3; j++)  {
  134.                pnorms[tptr->v[j]] = Vector_Sum(&pnorms[tptr->v[j]], &tptr->normal);
  135.  
  136.               }
  137.  
  138.            vertex[tptr->v[0]] +=1;
  139.            vertex[tptr->v[1]] +=1;
  140.            vertex[tptr->v[2]] +=1;
  141.           }
  142.  
  143. /*
  144.  +++++++++++++++++++++++++++++++++++++++++++++++++
  145.  +++++++++++++++++++++++++++++++++++++++++++++++++
  146.  * 
  147.  *     normalize all point normals for surface
  148.  */
  149.        for (i=0; i<nvert; i++)  {
  150.            if (vector_len(&pnorms[i]) != 0.0)
  151.                pnorms[i] = VectorScaler_Division(&pnorms[i],
  152.                                                         vector_len(&pnorms[i]));
  153.          }
  154.  
  155.  }
  156.  
  157.  
  158.  
  159.