home *** CD-ROM | disk | FTP | other *** search
/ Amiga Magazin: Amiga-CD 2000 April & May / AMIGA_2000_04.iso / patches / mesa3.1 / mesa-aux.lha / src-aux.aos / xxform.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-05  |  2.3 KB  |  131 lines

  1. /*
  2.  * xform.c 
  3.  */
  4.  
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <GL/gl.h>
  8. #include "3d.h"
  9. #include "xxform.h"
  10.  
  11. #define STACKDEPTH 10
  12.  
  13. static const mat_t matstack[STACKDEPTH] =
  14. {
  15.   {
  16.     {
  17.       {1.0, 0.0, 0.0, 0.0},
  18.       {0.0, 1.0, 0.0, 0.0},
  19.       {0.0, 0.0, 1.0, 0.0},
  20.       {0.0, 0.0, 0.0, 1.0}},
  21.     {
  22.       {1.0, 0.0, 0.0},
  23.       {0.0, 1.0, 0.0},
  24.       {0.0, 0.0, 1.0}}}
  25. };
  26. static int identitymat = 1;
  27.  
  28. static int mattop = 0;
  29.  
  30. void m_xformpt(GLdouble pin[3], GLdouble pout[3],
  31.            GLdouble nin[3], GLdouble nout[3])
  32. {
  33.   int i;
  34.   GLdouble ptemp[3], ntemp[3];
  35.   mat_t *m = &matstack[mattop];
  36.  
  37.   if (identitymat) {
  38.     for (i = 0; i < 3; i++) {
  39.       pout[i] = pin[i];
  40.       nout[i] = nin[i];
  41.     }
  42.     return;
  43.   }
  44.   for (i = 0; i < 3; i++) {
  45.     ptemp[i] = pin[0] * m->mat[0][i] +
  46.       pin[1] * m->mat[1][i] +
  47.       pin[2] * m->mat[2][i] +
  48.       m->mat[3][i];
  49.     ntemp[i] = nin[0] * m->norm[0][i] +
  50.       nin[1] * m->norm[1][i] +
  51.       nin[2] * m->norm[2][i];
  52.   }
  53.   for (i = 0; i < 3; i++) {
  54.     pout[i] = ptemp[i];
  55.     nout[i] = ntemp[i];
  56.   }
  57.   normalize(nout);
  58. }
  59.  
  60. void m_xformptonly(GLdouble pin[3], GLdouble pout[3])
  61. {
  62.   int i;
  63.   GLdouble ptemp[3];
  64.   mat_t *m = &matstack[mattop];
  65.  
  66.   if (identitymat) {
  67.     for (i = 0; i < 3; i++) {
  68.       pout[i] = pin[i];
  69.     }
  70.     return;
  71.   }
  72.   for (i = 0; i < 3; i++) {
  73.     ptemp[i] = pin[0] * m->mat[0][i] +
  74.       pin[1] * m->mat[1][i] +
  75.       pin[2] * m->mat[2][i] +
  76.       m->mat[3][i];
  77.   }
  78.   for (i = 0; i < 3; i++) {
  79.     pout[i] = ptemp[i];
  80.   }
  81. }
  82.  
  83. void m_pushmatrix(void)
  84. {
  85.   if (mattop < STACKDEPTH - 1) {
  86.     matstack[mattop + 1] = matstack[mattop];
  87.     mattop++;
  88.   }
  89.   else
  90.     error("m_pushmatrix: stack overflow\n");
  91. }
  92.  
  93. void m_popmatrix(void)
  94. {
  95.   if (mattop > 0)
  96.     mattop--;
  97.   else
  98.     error("m_popmatrix: stack underflow\n");
  99. }
  100.  
  101. void m_translate(GLdouble x, GLdouble y, GLdouble z)
  102. {
  103.   int i;
  104.   mat_t *m = &matstack[mattop];
  105.  
  106.   identitymat = 0;
  107.   for (i = 0; i < 4; i++)
  108.     m->mat[3][i] = x * m->mat[0][i] +
  109.       y * m->mat[1][i] +
  110.       z * m->mat[2][i] +
  111.       m->mat[3][i];
  112. }
  113.  
  114. void m_scale(GLdouble x, GLdouble y, GLdouble z)
  115. {
  116.   int i;
  117.   mat_t *m = &matstack[mattop];
  118.  
  119.   identitymat = 0;
  120.   for (i = 0; i < 3; i++) {
  121.     m->mat[0][i] *= x;
  122.     m->mat[1][i] *= y;
  123.     m->mat[2][i] *= z;
  124.   }
  125.   for (i = 0; i < 3; i++) {
  126.     m->norm[0][i] /= x;
  127.     m->norm[1][i] /= y;
  128.     m->norm[2][i] /= z;
  129.   }
  130. }
  131.