home *** CD-ROM | disk | FTP | other *** search
/ Fujiology Archive / fujiology_archive_v1_0.iso / !FALCON / !BONUS / GAMES / ENGINES / BM214A.ZIP / BM214A / ORIGINAL.SRC / TRIG.CPP < prev    next >
C/C++ Source or Header  |  1994-12-23  |  3KB  |  98 lines

  1. /********************************************************************
  2.  FILENAME: TRIG.CPP
  3.  AUTHOR  : JAKE HILL
  4.  DATE    : 12/1/94
  5.  
  6.  Copyright (c) 1994 by Jake Hill:
  7.  If you use any part of this code in your own project, please credit
  8.  me in your documentation and source code.  Thanks.
  9. ********************************************************************/
  10. #include "TRIG.HPP"
  11.  
  12. #include <dos.h>
  13. #include <math.h>
  14.  
  15. float pi = 3.141592654;
  16.  
  17. // These are the lookup tables.
  18. long sin_table[1024];
  19. long tan_table[1024];
  20. long cos_table[1024];
  21. long inv_cos_table[1024];
  22. long far inv_dist_table[10000];
  23.  
  24. // returns x * sin( a )
  25. short xSinA( short x, unsigned short a )
  26. {
  27.    long tx = x;
  28.    return (short) ( ( tx * sine(a) ) >> 16);
  29. };
  30.  
  31. // returns x * cos( a )
  32. short xCosA( short x, unsigned short a )
  33. {
  34.    long tx = x;
  35.    return (short) ( ( tx * cosine(a) ) >> 16);
  36. };
  37.  
  38. // returns sin( angle )
  39. // This function is only used during the trig initialization process.
  40. long Sine( long angle )
  41. {
  42.    double radians = ( angle * pi ) / 32768L;
  43.    return  (long) (sin( radians ) * 65536L);
  44. };
  45.  
  46. // returns cos( angle )
  47. // This function is only used during the trig initialization process.
  48. long Cosine( long angle )
  49. {
  50.    double radians = ( angle * pi ) / 32768L;
  51.    return (long) (cos( radians ) * 65536L);
  52. };
  53.  
  54. // returns tan( angle )
  55. // This function is only used during the trig initialization process.
  56. long Tangent( long angle )
  57. {
  58.    double radians = ( angle * pi ) / 32768L;
  59.    return (long) (tan( radians ) * 65536L);
  60. };
  61.  
  62. // returns 1/ ( cos(angle) )
  63. // This function is only used during the trig initialization process.
  64. long InvCosine( long angle )
  65. {
  66.    double radians = ( angle * pi ) / 32768L;
  67.  
  68. //   if ( cos( radians ) == 0 ) return 0;
  69.    if ( cos( radians ) == 0 ) return 65536L;
  70.    return (long) (65536L / cos( radians ));
  71. };
  72.  
  73. // Generate a lookup table for distances based on width of screen.
  74. // These values are used in the perspective calculations in order
  75. // to avoid doing a 32 bit division during the rendering process.
  76. // This function should be used whenever the screen width is changed.
  77. void GenInvDistTable(long screen_width)
  78. {
  79.    long numerator = (screen_width / 2) * 65536L;
  80.  
  81.    for (long z=0; z<10000; z++)
  82.       inv_dist_table[z] = numerator / (z+1);
  83. }
  84.  
  85. // Initialize all of the lookup tables for use in the renderer.
  86. void InitTrig(void)
  87. {
  88.    GenInvDistTable(320);
  89.  
  90.    for (long i=0; i<1024L; i++)
  91.    {
  92.       sin_table[i] = Sine( i*64L );
  93.       cos_table[i] = Cosine( i*64L );
  94.       tan_table[i] = Tangent( i*64L );
  95.       inv_cos_table[i] = InvCosine( i*64L );
  96.    }
  97. };
  98.