Advertisement
Guest User

Untitled

a guest
Apr 15th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. typedef GLfloat vec4[4];
  2. inline void vec4_add(vec4 a, vec4 b)
  3. {
  4.     int i;
  5.     for(i=0; i<4; ++i)
  6.         a[i] += b[i];
  7. }
  8. inline void vec4_sub(vec4 a, vec4 b)
  9. {
  10.     int i;
  11.     for(i=0; i<4; ++i)
  12.         a[i] -= b[i];
  13. }
  14. inline void vec4_scale(vec4 v, GLfloat s)
  15. {
  16.     int i;
  17.     for(i=0; i<4; ++i)
  18.         v[i] *= s;
  19. }
  20. inline GLfloat vec4_inner_product(vec4 a, vec4 b)
  21. {
  22.     GLfloat p = 0.;
  23.     int i;
  24.     for(i=0; i<4; ++i)
  25.         p += b[i]*a[i];
  26.     return p;
  27. }
  28. inline GLfloat vec4_length(vec4 v)
  29. {
  30.     return sqrtf(vec4_inner_product(v,v));
  31. }
  32. inline void vec4_normalize(vec4 v)
  33. {
  34.     GLfloat k = 1.0 / vec4_length(v);
  35.     vec4_scale(v, k);
  36. }
  37. inline void vec4_cross(vec4 a, vec4 b)
  38. {
  39.     vec4 c;
  40.     c[0] = a[1]*b[2] - a[2]*b[1];
  41.     c[1] = a[2]*b[0] - a[0]*b[2];
  42.     c[2] = a[0]*b[1] - a[1]*b[0];
  43.     c[3] = 0.;
  44.     memcpy(a, c, sizeof(a));
  45. }
  46.  
  47. typedef vec4 mat4x4[4];
  48. inline void mat4x4_identity(mat4x4 M)
  49. {
  50.     int i, j;
  51.     M[0][0] = 1; M[1][0] = 0; M[2][0] = 0; M[3][0] = 0;
  52.     M[0][1] = 0; M[1][1] = 1; M[2][1] = 0; M[3][1] = 0;
  53.     M[0][2] = 0; M[1][2] = 0; M[2][2] = 1; M[3][2] = 0;
  54.     M[0][3] = 0; M[1][3] = 0; M[2][3] = 0; M[3][3] = 1;
  55.     /*for(j=0; j<4; ++j)
  56.         for(i=0; i<4; ++i) {
  57.             M[i][j] = i==j ? 1 : 0;
  58.     }*/
  59. }
  60. inline void mat4x4_cpy(mat4x4 M, mat4x4 N)
  61. {
  62.     int i, j;
  63.     for(j=0; j<4; ++j) {
  64.         for(i=0; i<4; ++i) {
  65.             M[i][j] = N[i][j];
  66.         }
  67.     }
  68. }
  69. inline void mat4x4_mul(mat4x4 M, mat4x4 b)
  70. {
  71.     mat4x4 a;
  72.     int i, j, k;
  73.     memcpy(a, M, sizeof(a));
  74.     for(j=0; j<4; ++j) {
  75.         for(i=0; i<4; ++i) {
  76.             M[i][j] = 0;
  77.             for(k=0; k<4; ++k) {
  78.                 M[i][j] += a[i][k]*b[k][j];
  79.             }
  80.         }
  81.     }
  82. }
  83. inline void mat4x4_trans(mat4x4 M, GLfloat x, GLfloat y, GLfloat z)
  84. {
  85.     mat4x4 T; mat4x4_identity(T);
  86.     T[3][0] = x;
  87.     T[3][1] = y;
  88.     T[3][2] = z;
  89.     mat4x4_mul(M, T);
  90. }
  91. inline void mat4x4_rot(mat4x4 M, GLfloat x, GLfloat y, GLfloat z, GLfloat angle)
  92. {
  93. }
  94. inline void mat4x4_rot_X(mat4x4 M, GLfloat angle)
  95. {
  96.     GLfloat s = sinf(angle);
  97.     GLfloat c = cosf(angle);
  98.     mat4x4 R = {
  99.         {1, 0, 0, 0},
  100.         {0, c, s, 0},
  101.         {0,-s, c, 0},
  102.         {0, 0, 0, 1}
  103.     };
  104.     mat4x4_mul(M, R);
  105. }
  106. inline void mat4x4_rot_Y(mat4x4 M, GLfloat angle)
  107. {
  108.     GLfloat s = sinf(angle);
  109.     GLfloat c = cosf(angle);
  110.     mat4x4 R = {
  111.         {c, 0, s, 0},
  112.         {0, 1, 0, 0},
  113.         {-s, 0, c, 0},
  114.         {0, 0, 0, 1}
  115.     };
  116.     mat4x4_mul(M, R);
  117. }
  118. inline void mat4x4_rot_Z(mat4x4 M, GLfloat angle)
  119. {
  120.     GLfloat s = sinf(angle);
  121.     GLfloat c = cosf(angle);
  122.     mat4x4 R = {
  123.         {c, s, 0, 0},
  124.         {-s, c, 0, 0},
  125.         {0, 0, 1, 0},
  126.         {0, 0, 0, 1}
  127.     };
  128.     mat4x4_mul(M, R);
  129. }
  130. inline void mat4x4_row(vec4 r, mat4x4 M, int i)
  131. {
  132.     int k;
  133.     for(k=0; k<4; ++k)
  134.         r[k] = M[k][i];
  135. }
  136. inline void mat4x4_col(vec4 r, mat4x4 M, int i)
  137. {
  138.     int k;
  139.     for(k=0; k<4; ++k)
  140.         r[k] = M[i][k];
  141. }
  142. inline void mat4x4_cpy_T(mat4x4 M, mat4x4 N)
  143. {
  144.     int i, j;
  145.     for(j=0; j<4; ++j) {
  146.         for(i=0; i<4; ++i) {
  147.             M[i][j] = N[j][i];
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement