home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vrml2gl.zip / LIB / glUtils.c < prev    next >
C/C++ Source or Header  |  1997-02-06  |  3KB  |  95 lines

  1. #include "glMathUtils.h"
  2. /*===========================================================================*/
  3. /* Reduces a normal vector specified as a set of three coordinates, to a unit*/
  4. /* normal vector of length one.                                              */
  5. /*===========================================================================*/
  6. void  glMathNormalizefv ( float vector[3] )
  7. {
  8.    static   float length;
  9.    
  10.    /* Calculate the length of the vector. */
  11.    length = sqrt( (vector[0]*vector[0]) + 
  12.                   (vector[1]*vector[1]) +
  13.                   (vector[2]*vector[2]) );
  14.  
  15.    /*  Keep the program from blowing up by providing an exceptable */
  16.    /* value for vectors that may calculated too close to zero.     */
  17.    if ( length == 0.0 )
  18.       length = 1.0;
  19.  
  20.    /*  Dividing each element by the length will result in a */
  21.    /* unit normal vector.                                   */
  22.    vector[0] /= length;
  23.    vector[1] /= length;
  24.    vector[2] /= length;
  25. }
  26.  
  27. /*===========================================================================*/
  28. /* Points p1, p2, & p3 specified in counter clock-wise order.                */
  29. /*===========================================================================*/
  30. void  glMathGetNormal2fv   ( float v[3][3], float out[3] )
  31. {
  32.    static   float  v1[3], 
  33.                      v2[3];
  34.  
  35.    /* Calculate two vectors from the three points. */
  36.    v1[0] = v[0][0] - v[1][0];
  37.    v1[1] = v[0][1] - v[1][1];
  38.    v1[2] = v[0][2] - v[1][2];
  39.  
  40.    v2[0] = v[1][0] - v[2][0];
  41.    v2[1] = v[1][1] - v[2][1];
  42.    v2[2] = v[1][2] - v[2][2];
  43.  
  44.    /* Take the cross product of the two vectors to get */
  45.    /* the normal vector which will be stored in out.   */
  46.    out[0] = v1[1]*v2[2] - v1[2]*v2[1];
  47.    out[1] = v1[2]*v2[0] - v1[0]*v2[2];
  48.    out[2] = v1[0]*v2[1] - v1[1]*v2[0];
  49.  
  50.    /* Normalize the vector (shorten length to one). */
  51.    glMathNormalizefv( out );
  52. }
  53.  
  54. /*===========================================================================*/
  55. /* Points p1, p2, & p3 specified in counter clock-wise order.                */
  56. /*===========================================================================*/
  57. void     glMathGetNormalfv3f( float v1[3], float v2[3], float v3[3], float out[3] )
  58. {
  59.    static   float vector1[3],
  60.                   vector2[3];
  61.  
  62.    /* Calculate two vectors from the three points. */
  63.    vector1[0] = v1[0] - v2[0];
  64.    vector1[1] = v1[1] - v2[1];
  65.    vector1[2] = v1[2] - v2[2];
  66.  
  67.    vector2[0] = v2[0] - v3[0];
  68.    vector2[1] = v2[1] - v3[1];
  69.    vector2[2] = v2[2] - v3[2];
  70.  
  71.    /* Take the cross product of the two vectors to get */
  72.    /* the normal vector which will be stored in out.   */
  73.    out[0] = vector1[1]*vector2[2] - vector1[2]*vector2[1];
  74.    out[1] = vector1[2]*vector2[0] - vector1[0]*vector2[2];
  75.    out[2] = vector1[0]*vector2[1] - vector1[1]*vector2[0];
  76.  
  77.    /* Normalize the vector (shorten length to one). */
  78.    glMathNormalizefv( out );
  79. }
  80. /*===========================================================================*/
  81. /* This function will give the right angle for 'fovy' given the objects size */ 
  82. /*===========================================================================*/
  83. float glMathGetFovy2f   ( float size, float distance )
  84. {
  85.    float radtheta, 
  86.          degtheta;
  87.  
  88.    radtheta = 2.0 * atan2( (size/2.0), distance );
  89.    degtheta = ( 180.0 * radtheta ) / PIE;
  90.  
  91.    return degtheta;
  92. }
  93.  
  94.  
  95.