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.

181 lines
4.7 KiB

  1. /*
  2. * (c) Copyright 1993, Silicon Graphics, Inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. */
  37. /* Routines to manipulate 3 dimensional vectors. All these routines
  38. * should work even if the input and output vectors are the same.
  39. */
  40. #include <windows.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <math.h>
  44. #include <GL/gl.h>
  45. #include "3d.h"
  46. #include "tk.h"
  47. #define static
  48. void (*errfunc)(char *) = 0;
  49. void seterrorfunc(void (*func)(char *))
  50. {
  51. errfunc = func;
  52. }
  53. void error(char *s)
  54. {
  55. if (errfunc)
  56. (*errfunc)(s);
  57. else {
  58. MESSAGEBOX(GetFocus(), s, "Error", MB_OK);
  59. exit(1);
  60. }
  61. }
  62. void diff3(GLdouble p[3], GLdouble q[3], GLdouble diff[3])
  63. {
  64. diff[0] = p[0] - q[0];
  65. diff[1] = p[1] - q[1];
  66. diff[2] = p[2] - q[2];
  67. }
  68. void add3(GLdouble p[3], GLdouble q[3], GLdouble sum[3])
  69. {
  70. sum[0] = p[0] + q[0];
  71. sum[1] = p[1] + q[1];
  72. sum[2] = p[2] + q[2];
  73. }
  74. void scalarmult(GLdouble s, GLdouble v[3], GLdouble vout[3])
  75. {
  76. vout[0] = v[0]*s;
  77. vout[1] = v[1]*s;
  78. vout[2] = v[2]*s;
  79. }
  80. GLdouble dot3(GLdouble p[3], GLdouble q[3])
  81. {
  82. return p[0]*q[0] + p[1]*q[1] + p[2]*q[2];
  83. }
  84. GLdouble length3(GLdouble v[3])
  85. {
  86. return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  87. }
  88. GLdouble dist3(GLdouble p[3], GLdouble q[3])
  89. {
  90. GLdouble d[3];
  91. diff3(p, q, d);
  92. return length3(d);
  93. }
  94. void copy3(GLdouble old[3], GLdouble new[3])
  95. {
  96. new[0] = old[0], new[1] = old[1], new[2] = old[2];
  97. }
  98. void crossprod(GLdouble v1[3], GLdouble v2[3], GLdouble prod[3])
  99. {
  100. GLdouble p[3]; /* in case prod == v1 or v2 */
  101. p[0] = v1[1]*v2[2] - v2[1]*v1[2];
  102. p[1] = v1[2]*v2[0] - v2[2]*v1[0];
  103. p[2] = v1[0]*v2[1] - v2[0]*v1[1];
  104. prod[0] = p[0]; prod[1] = p[1]; prod[2] = p[2];
  105. }
  106. void normalize(GLdouble v[3])
  107. {
  108. GLdouble d;
  109. d = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
  110. if (d == (GLdouble)0.0) {
  111. error("normalize: zero length vector");
  112. v[0] = d = (GLdouble)1.0;
  113. }
  114. d = (GLdouble)1/d;
  115. v[0] *= d; v[1] *= d; v[2] *= d;
  116. }
  117. void identifymat3(GLdouble m[3][3])
  118. {
  119. int i, j;
  120. for (i=0; i<3; i++)
  121. for (j=0; j<3; j++)
  122. m[i][j] = (i == j) ? (GLdouble)1.0 : (GLdouble)0.0;
  123. }
  124. void copymat3(GLdouble *to, GLdouble *from)
  125. {
  126. int i;
  127. for (i=0; i<9; i++) {
  128. *to++ = *from++;
  129. }
  130. }
  131. void xformvec3(GLdouble v[3], GLdouble m[3][3], GLdouble vm[3])
  132. {
  133. GLdouble result[3]; /* in case v == vm */
  134. int i;
  135. for (i=0; i<3; i++) {
  136. result[i] = v[0]*m[0][i] + v[1]*m[1][i] + v[2]*m[2][i];
  137. }
  138. for (i=0; i<3; i++) {
  139. vm[i] = result[i];
  140. }
  141. }
  142. long samepoint(GLdouble p1[3], GLdouble p2[3])
  143. {
  144. if (p1[0] == p2[0] && p1[1] == p2[1] && p1[2] == p2[2])
  145. return 1;
  146. return 0;
  147. }
  148. void perpnorm(GLdouble p1[3], GLdouble p2[3], GLdouble p3[3], GLdouble n[3])
  149. {
  150. GLdouble d1[3], d2[3];
  151. diff3(p2, p1, d1);
  152. diff3(p2, p3, d2);
  153. crossprod(d1, d2, n);
  154. normalize(n);
  155. }