Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: math.c
  3. *
  4. * Useful math routines.
  5. *
  6. * Created: 14-Mar-1995 22:35:54
  7. * Author: Gilman Wong [gilmanw]
  8. *
  9. * Copyright (c) 1995 Microsoft Corporation
  10. *
  11. \**************************************************************************/
  12. #include <windows.h>
  13. #include <math.h>
  14. #include <gl/gl.h>
  15. #include "global.h"
  16. /******************************Public*Routine******************************\
  17. * calcNorm
  18. *
  19. * Compute the normal vector for the given three vertices. Assume CCW
  20. * ordering.
  21. *
  22. * History:
  23. * 14-Mar-1995 -by- Gilman Wong [gilmanw]
  24. * Taken from Otto's screen saver code.
  25. \**************************************************************************/
  26. void calcNorm(GLfloat *norm, GLfloat *p1, GLfloat *p2, GLfloat *p3)
  27. {
  28. GLfloat crossX, crossY, crossZ;
  29. GLfloat abX, abY, abZ;
  30. GLfloat acX, acY, acZ;
  31. GLfloat sqrLength;
  32. GLfloat invLength;
  33. if (!norm || !p1 || !p2 || !p3)
  34. {
  35. LBprintf("calcNorm: bad array (0x%lx, 0x%lx, 0x%lx, 0x%lx)",
  36. norm, p1, p2, p3);
  37. return;
  38. }
  39. abX = p2[0] - p1[0]; // calculate p2 - p1
  40. abY = p2[1] - p1[1];
  41. abZ = p2[2] - p1[2];
  42. acX = p3[0] - p1[0]; // calculate p3 - p1
  43. acY = p3[1] - p1[1];
  44. acZ = p3[2] - p1[2];
  45. crossX = (abY * acZ) - (abZ * acY); // get cross product
  46. crossY = (abZ * acX) - (abX * acZ); // (p2 - p1) X (p3 - p1)
  47. crossZ = (abX * acY) - (abY * acX);
  48. sqrLength = (crossX * crossX) + (crossY * crossY) +
  49. (crossZ * crossZ);
  50. if (sqrLength > ZERO_EPS)
  51. invLength = (float) (1.0 / sqrt(sqrLength));
  52. else
  53. invLength = 1.0f;
  54. norm[0] = crossX * invLength;
  55. norm[1] = crossY * invLength;
  56. norm[2] = crossZ * invLength;
  57. }