1
0

MATH.C 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /******************************************************************************
  2. Plush Version 1.2
  3. math.c
  4. Math and Matrix Control
  5. Copyright (c) 1996-2000, Justin Frankel
  6. ******************************************************************************/
  7. #include "plush.h"
  8. void plMatrixRotate(pl_Float matrix[], pl_uChar m, pl_Float Deg) {
  9. pl_uChar m1, m2;
  10. double c,s;
  11. double d= Deg * PL_PI / 180.0;
  12. memset(matrix,0,sizeof(pl_Float)*16);
  13. matrix[((m-1)<<2)+m-1] = matrix[15] = 1.0;
  14. m1 = (m % 3);
  15. m2 = ((m1+1) % 3);
  16. c = cos(d); s = sin(d);
  17. matrix[(m1<<2)+m1]=(pl_Float)c; matrix[(m1<<2)+m2]=(pl_Float)s;
  18. matrix[(m2<<2)+m2]=(pl_Float)c; matrix[(m2<<2)+m1]=(pl_Float)-s;
  19. }
  20. void plMatrixTranslate(pl_Float m[], pl_Float x, pl_Float y, pl_Float z) {
  21. memset(m,0,sizeof(pl_Float)*16);
  22. m[0] = m[4+1] = m[8+2] = m[12+3] = 1.0;
  23. m[0+3] = x; m[4+3] = y; m[8+3] = z;
  24. }
  25. void plMatrixMultiply(pl_Float *dest, pl_Float src[]) {
  26. pl_Float temp[16];
  27. pl_uInt i;
  28. memcpy(temp,dest,sizeof(pl_Float)*16);
  29. for (i = 0; i < 16; i += 4) {
  30. *dest++ = src[i+0]*temp[(0<<2)+0]+src[i+1]*temp[(1<<2)+0]+
  31. src[i+2]*temp[(2<<2)+0]+src[i+3]*temp[(3<<2)+0];
  32. *dest++ = src[i+0]*temp[(0<<2)+1]+src[i+1]*temp[(1<<2)+1]+
  33. src[i+2]*temp[(2<<2)+1]+src[i+3]*temp[(3<<2)+1];
  34. *dest++ = src[i+0]*temp[(0<<2)+2]+src[i+1]*temp[(1<<2)+2]+
  35. src[i+2]*temp[(2<<2)+2]+src[i+3]*temp[(3<<2)+2];
  36. *dest++ = src[i+0]*temp[(0<<2)+3]+src[i+1]*temp[(1<<2)+3]+
  37. src[i+2]*temp[(2<<2)+3]+src[i+3]*temp[(3<<2)+3];
  38. }
  39. }
  40. void plMatrixApply(pl_Float *m, pl_Float x, pl_Float y, pl_Float z,
  41. pl_Float *outx, pl_Float *outy, pl_Float *outz) {
  42. *outx = x*m[0] + y*m[1] + z*m[2] + m[3];
  43. *outy = x*m[4] + y*m[5] + z*m[6] + m[7];
  44. *outz = x*m[8] + y*m[9] + z*m[10] + m[11];
  45. }
  46. pl_Float plDotProduct(pl_Float x1, pl_Float y1, pl_Float z1,
  47. pl_Float x2, pl_Float y2, pl_Float z2) {
  48. return ((x1*x2)+(y1*y2)+(z1*z2));
  49. }
  50. void plNormalizeVector(pl_Float *x, pl_Float *y, pl_Float *z) {
  51. double length;
  52. length = (*x)*(*x)+(*y)*(*y)+(*z)*(*z);
  53. if (length > 0.0000000001) {
  54. pl_Float t = (pl_Float)sqrt(length);
  55. *x /= t;
  56. *y /= t;
  57. *z /= t;
  58. } else *x = *y = *z = 0.0;
  59. }