home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / nextrad.lha / NeXtRad / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-22  |  5.5 KB  |  227 lines

  1. /* misc.c */
  2. /* written by : Jason R. Wilson */
  3.  
  4. /* this package provides misc. routines such as normal computation */
  5.  
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include "datastruct.h"
  9.  
  10.  
  11. /***********************************************************************/
  12.  
  13. void IdentifyVertices (ObjectCell *Object)
  14. {
  15.  
  16.    /* this routine goes through all the vertices and associates a unique
  17.       integer with each one (it also counts them and stores the result
  18.       in NoofVertices) */
  19.    
  20.    VertexCell *traverse;
  21.    int count;
  22.    
  23.    count = 0;
  24.    traverse = Object->VertexHead;
  25.    
  26.    while (!(traverse == NULL))
  27.    {
  28.       count++;
  29.       traverse->Number = count;
  30.       traverse = traverse->Next;
  31.    }
  32.    
  33.    Object->NoofVertices = count;
  34. }
  35.  
  36.  
  37.  
  38. /***********************************************************************/
  39.  
  40. int IdentifyPolys (ObjectCell *ObjectHead)
  41.  
  42. /* labels the polygons with a unique integer */
  43. /* returns the total number of polygons */
  44.  
  45. {
  46.    ObjectCell *Traverse;
  47.    PolygonCell *CurrentPoly;
  48.    int Total;
  49.  
  50.    Traverse = ObjectHead;
  51.    Total = 0;
  52.  
  53.    while (!(Traverse == NULL))
  54.    {
  55.       CurrentPoly = Traverse->PolygonHead;
  56.       while (!(CurrentPoly == NULL))
  57.       {
  58.      Total++;
  59.      CurrentPoly->ID = Total;
  60.      CurrentPoly = CurrentPoly->Next;
  61.       }
  62.       Traverse = Traverse->Next;
  63.    }
  64.  
  65.    return Total;
  66. }
  67.  
  68. /***********************************************************************/
  69.  
  70.  
  71. int IdentifyVerts (ObjectCell *ObjectHead)
  72.  
  73. /* labels the vertices with a unique integer */
  74. /* returns the total number of vertices */
  75.  
  76. {
  77.    ObjectCell *Traverse; 
  78.    VertexCell *CurrentVert;
  79.    int Total;
  80.  
  81.    Traverse = ObjectHead;
  82.    Total = 0;
  83.  
  84.    while (!(Traverse == NULL))
  85.    {
  86.       CurrentVert = Traverse->VertexHead;
  87.       while (!(CurrentVert == NULL))
  88.       {
  89.      Total++;
  90.      CurrentVert->Number = Total;
  91.      CurrentVert = CurrentVert->Next;
  92.       }
  93.       Traverse = Traverse->Next;
  94.    }
  95.  
  96.    return Total;
  97. }
  98.  
  99.  
  100.  
  101. /*----------------------------------------------------------------------*/
  102.  
  103.  
  104. void ComputeNormals (ObjectCell *Object)
  105.      
  106.      /* This routine computes the normals of the polygons and vertices of the 
  107.     object passed in */
  108. {
  109.    PolygonCell *CurrentPoly;
  110.    VertexCell *CurrentVert;
  111.    VertexListCell *CurrentVertList;
  112.    PolygonListCell *CurrentPolyList;
  113.  
  114.    int loop;
  115.    int numpolys;
  116.  
  117.    Point pointi,pointj;
  118.  
  119.    /*------------------------------------------------------------*/
  120.    /* First, compute polygon normals */
  121.    CurrentPoly = Object->PolygonHead;
  122.    while (!(CurrentPoly == NULL))
  123.    {
  124.       /*-------------------------------------------------------*/
  125.       /* init. polygon normal */
  126.       CurrentPoly->Normal.dx = 0.0;
  127.       CurrentPoly->Normal.dy = 0.0;
  128.       CurrentPoly->Normal.dz = 0.0;
  129.       /*-------------------------------------------------------*/
  130.  
  131.       /* Compute Normal of Polygon using Newell's Method */
  132.       CurrentVertList = CurrentPoly->Vertices;
  133.       while (!(CurrentVertList == NULL))
  134.     {
  135.      pointi = CurrentVertList->Vertex->WorldPosition;
  136.      if (CurrentVertList->Rest == NULL)
  137.         pointj = CurrentPoly->Vertices->Vertex->WorldPosition;
  138.      else
  139.         pointj = CurrentVertList->Rest->Vertex->WorldPosition;
  140.      CurrentPoly->Normal.dx += (pointi.y - pointj.y)*(pointi.z+pointj.z);
  141.      CurrentPoly->Normal.dy += (pointi.z - pointj.z)*(pointi.x+pointj.x);
  142.      CurrentPoly->Normal.dz += (pointi.x - pointj.x)*(pointi.y+pointj.y);
  143.      CurrentVertList = CurrentVertList->Rest;
  144.        }
  145.         
  146.       /*-------------------------------------------------------*/
  147.  
  148.       CurrentPoly = CurrentPoly->Next;
  149.    }
  150.  
  151.    /*-------------------------------------------------------------*/
  152.    /* Next, Compute Vertex Normals */
  153.  
  154.    CurrentVert = Object->VertexHead;
  155.    while (!(CurrentVert == NULL))
  156.    {
  157.       /* init. vertex normal to zero (and numpolys) */
  158.       CurrentVert->Normal.dx = 0.0;
  159.       CurrentVert->Normal.dy = 0.0;
  160.       CurrentVert->Normal.dz = 0.0;
  161.       numpolys = 0;
  162.  
  163.       /*-------------------------------------------------------*/
  164.       CurrentPolyList = CurrentVert->Polygons;
  165.       while (!(CurrentPolyList == NULL))
  166.       {
  167.      numpolys++;
  168.      CurrentVert->Normal.dx += CurrentPolyList->Polygon->Normal.dx;
  169.      CurrentVert->Normal.dy += CurrentPolyList->Polygon->Normal.dy;
  170.      CurrentVert->Normal.dz += CurrentPolyList->Polygon->Normal.dz;
  171.      CurrentPolyList = CurrentPolyList->Rest;
  172.       }
  173.       CurrentVert->Normal.dx /= numpolys;
  174.       CurrentVert->Normal.dy /= numpolys;
  175.       CurrentVert->Normal.dz /= numpolys;
  176.       
  177.       CurrentVert = CurrentVert->Next;
  178.    }
  179.    /*-------------------------------------------------------------*/
  180.  
  181. }
  182.  
  183.  
  184. /***********************************************************************/
  185.  
  186. void CullBackFaces (ObjectCell *ObjectHead,double Eye,Point VRP,Vector n)
  187. /* marks the backfacing polygons culled */
  188. {
  189.    Point WorldEye,q;
  190.    PolygonCell *CurrentPoly;
  191.    Vector nf;
  192.    ObjectCell *Object;
  193.  
  194.    /* find eye in world coordinates */
  195.  
  196.    WorldEye.x = Eye*n.dx + VRP.x;
  197.    WorldEye.y = Eye*n.dy + VRP.y;
  198.    WorldEye.z = Eye*n.dz + VRP.z;
  199.  
  200.    /* go through polygons of all objects */
  201.  
  202.    Object = ObjectHead;
  203.    while (!(Object == NULL))
  204.    {
  205.       CurrentPoly = Object->PolygonHead;
  206.       while (!(CurrentPoly == NULL))
  207.       {
  208.      CurrentPoly->Culled = false; /* assume that it is not culled */
  209.      nf = CurrentPoly->Normal;
  210.      q  = CurrentPoly->Vertices->Vertex->WorldPosition;
  211.      /* just pick the first one */
  212.      
  213.      if ((nf.dx*(WorldEye.x - q.x) + nf.dy*(WorldEye.y - q.y) +
  214.           nf.dz*(WorldEye.z - q.z)) <= 0.0)
  215.         CurrentPoly->Culled = true;
  216.      CurrentPoly = CurrentPoly->Next;
  217.       }
  218.       Object = Object->Next;
  219.    }
  220. }
  221.  
  222. /***********************************************************************/
  223.  
  224.  
  225.  
  226.  
  227.