Windows NT 4.0 source code leak
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.

321 lines
7.5 KiB

5 years ago
  1. /*
  2. ** Copyright 1992, Silicon Graphics, Inc.
  3. ** All Rights Reserved.
  4. **
  5. ** This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6. ** the contents of this file may not be disclosed to third parties, copied or
  7. ** duplicated in any form, in whole or in part, without the prior written
  8. ** permission of Silicon Graphics, Inc.
  9. **
  10. ** RESTRICTED RIGHTS LEGEND:
  11. ** Use, duplication or disclosure by the Government is subject to restrictions
  12. ** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13. ** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14. ** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15. ** rights reserved under the Copyright Laws of the United States.
  16. **
  17. ** $Revision: 1.2 $
  18. ** $Date: 1995/06/23 21:27:54 $
  19. */
  20. #ifdef NT
  21. #include <glos.h>
  22. #endif
  23. #include <math.h>
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include "gluint.h"
  27. /*
  28. ** Make m an identity matrix
  29. */
  30. void __gluMakeIdentityd(GLdouble m[16])
  31. {
  32. m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;
  33. m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;
  34. m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
  35. m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
  36. }
  37. void __gluMakeIdentityf(GLfloat m[16])
  38. {
  39. m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;
  40. m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;
  41. m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
  42. m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
  43. }
  44. void APIENTRY gluOrtho2D(GLdouble left, GLdouble right,
  45. GLdouble bottom, GLdouble top)
  46. {
  47. glOrtho(left, right, bottom, top, -1, 1);
  48. }
  49. #define __glPi 3.14159265358979323846
  50. void APIENTRY gluPerspective(GLdouble fovy, GLdouble aspect,
  51. GLdouble zNear, GLdouble zFar)
  52. {
  53. GLdouble m[4][4];
  54. double sine, cotangent, deltaZ;
  55. double radians = fovy / 2 * __glPi / 180;
  56. deltaZ = zFar - zNear;
  57. sine = sin(radians);
  58. if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
  59. return;
  60. }
  61. cotangent = COS(radians) / sine;
  62. __gluMakeIdentityd(&m[0][0]);
  63. m[0][0] = cotangent / aspect;
  64. m[1][1] = cotangent;
  65. m[2][2] = -(zFar + zNear) / deltaZ;
  66. m[2][3] = -1;
  67. m[3][2] = -2 * zNear * zFar / deltaZ;
  68. m[3][3] = 0;
  69. glMultMatrixd(&m[0][0]);
  70. }
  71. static void normalize(float v[3])
  72. {
  73. float r;
  74. r = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
  75. if (r == 0.0) return;
  76. v[0] /= r;
  77. v[1] /= r;
  78. v[2] /= r;
  79. }
  80. static void cross(float v1[3], float v2[3], float result[3])
  81. {
  82. result[0] = v1[1]*v2[2] - v1[2]*v2[1];
  83. result[1] = v1[2]*v2[0] - v1[0]*v2[2];
  84. result[2] = v1[0]*v2[1] - v1[1]*v2[0];
  85. }
  86. void APIENTRY gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez,
  87. GLdouble centerx, GLdouble centery, GLdouble centerz,
  88. GLdouble upx, GLdouble upy, GLdouble upz)
  89. {
  90. int i;
  91. float forward[3], side[3], up[3];
  92. GLfloat m[4][4];
  93. forward[0] = centerx - eyex;
  94. forward[1] = centery - eyey;
  95. forward[2] = centerz - eyez;
  96. up[0] = upx;
  97. up[1] = upy;
  98. up[2] = upz;
  99. normalize(forward);
  100. /* Side = forward x up */
  101. cross(forward, up, side);
  102. normalize(side);
  103. /* Recompute up as: up = side x forward */
  104. cross(side, forward, up);
  105. __gluMakeIdentityf(&m[0][0]);
  106. m[0][0] = side[0];
  107. m[1][0] = side[1];
  108. m[2][0] = side[2];
  109. m[0][1] = up[0];
  110. m[1][1] = up[1];
  111. m[2][1] = up[2];
  112. m[0][2] = -forward[0];
  113. m[1][2] = -forward[1];
  114. m[2][2] = -forward[2];
  115. glMultMatrixf(&m[0][0]);
  116. glTranslated(-eyex, -eyey, -eyez);
  117. }
  118. void __gluMultMatrixVecd(const GLdouble matrix[16], const GLdouble in[4],
  119. GLdouble out[4])
  120. {
  121. int i;
  122. for (i=0; i<4; i++) {
  123. out[i] =
  124. in[0] * matrix[0*4+i] +
  125. in[1] * matrix[1*4+i] +
  126. in[2] * matrix[2*4+i] +
  127. in[3] * matrix[3*4+i];
  128. }
  129. }
  130. /*
  131. ** inverse = invert(src)
  132. */
  133. int __gluInvertMatrixd(const GLdouble src[16], GLdouble inverse[16])
  134. {
  135. int i, j, k, swap;
  136. double t;
  137. GLdouble temp[4][4];
  138. for (i=0; i<4; i++) {
  139. for (j=0; j<4; j++) {
  140. temp[i][j] = src[i*4+j];
  141. }
  142. }
  143. __gluMakeIdentityd(inverse);
  144. for (i = 0; i < 4; i++) {
  145. /*
  146. ** Look for largest element in column
  147. */
  148. swap = i;
  149. for (j = i + 1; j < 4; j++) {
  150. if (fabs(temp[j][i]) > fabs(temp[i][i])) {
  151. swap = j;
  152. }
  153. }
  154. if (swap != i) {
  155. /*
  156. ** Swap rows.
  157. */
  158. for (k = 0; k < 4; k++) {
  159. t = temp[i][k];
  160. temp[i][k] = temp[swap][k];
  161. temp[swap][k] = t;
  162. t = inverse[i*4+k];
  163. inverse[i*4+k] = inverse[swap*4+k];
  164. inverse[swap*4+k] = t;
  165. }
  166. }
  167. if (temp[i][i] == 0) {
  168. /*
  169. ** No non-zero pivot. The matrix is singular, which shouldn't
  170. ** happen. This means the user gave us a bad matrix.
  171. */
  172. return GL_FALSE;
  173. }
  174. t = temp[i][i];
  175. for (k = 0; k < 4; k++) {
  176. temp[i][k] /= t;
  177. inverse[i*4+k] /= t;
  178. }
  179. for (j = 0; j < 4; j++) {
  180. if (j != i) {
  181. t = temp[j][i];
  182. for (k = 0; k < 4; k++) {
  183. temp[j][k] -= temp[i][k]*t;
  184. inverse[j*4+k] -= inverse[i*4+k]*t;
  185. }
  186. }
  187. }
  188. }
  189. return GL_TRUE;
  190. }
  191. void __gluMultMatricesd(const GLdouble a[16], const GLdouble b[16], GLdouble r[16])
  192. {
  193. int i, j;
  194. for (i = 0; i < 4; i++) {
  195. for (j = 0; j < 4; j++) {
  196. r[i*4+j] =
  197. a[i*4+0]*b[0*4+j] +
  198. a[i*4+1]*b[1*4+j] +
  199. a[i*4+2]*b[2*4+j] +
  200. a[i*4+3]*b[3*4+j];
  201. }
  202. }
  203. }
  204. GLint APIENTRY gluProject(GLdouble objx, GLdouble objy, GLdouble objz,
  205. const GLdouble modelMatrix[16],
  206. const GLdouble projMatrix[16],
  207. const GLint viewport[4],
  208. GLdouble *winx, GLdouble *winy, GLdouble *winz)
  209. {
  210. double in[4];
  211. double out[4];
  212. in[0]=objx;
  213. in[1]=objy;
  214. in[2]=objz;
  215. in[3]=1.0;
  216. __gluMultMatrixVecd(modelMatrix, in, out);
  217. __gluMultMatrixVecd(projMatrix, out, in);
  218. if (in[3] == 0.0) return(GL_FALSE);
  219. in[0] /= in[3];
  220. in[1] /= in[3];
  221. in[2] /= in[3];
  222. /* Map x, y and z to range 0-1 */
  223. in[0] = in[0] * 0.5 + 0.5;
  224. in[1] = in[1] * 0.5 + 0.5;
  225. in[2] = in[2] * 0.5 + 0.5;
  226. /* Map x,y to viewport */
  227. in[0] = in[0] * viewport[2] + viewport[0];
  228. in[1] = in[1] * viewport[3] + viewport[1];
  229. *winx=in[0];
  230. *winy=in[1];
  231. *winz=in[2];
  232. return(GL_TRUE);
  233. }
  234. GLint APIENTRY gluUnProject(GLdouble winx, GLdouble winy, GLdouble winz,
  235. const GLdouble modelMatrix[16],
  236. const GLdouble projMatrix[16],
  237. const GLint viewport[4],
  238. GLdouble *objx, GLdouble *objy, GLdouble *objz)
  239. {
  240. double finalMatrix[16];
  241. double in[4];
  242. double out[4];
  243. __gluMultMatricesd(modelMatrix, projMatrix, finalMatrix);
  244. if (!__gluInvertMatrixd(finalMatrix, finalMatrix)) return(GL_FALSE);
  245. in[0]=winx;
  246. in[1]=winy;
  247. in[2]=winz;
  248. in[3]=1.0;
  249. /* Map x and y from window coordinates */
  250. in[0] = (in[0] - viewport[0]) / viewport[2];
  251. in[1] = (in[1] - viewport[1]) / viewport[3];
  252. /* Map to range -1 to 1 */
  253. in[0] = in[0] * 2 - 1;
  254. in[1] = in[1] * 2 - 1;
  255. in[2] = in[2] * 2 - 1;
  256. __gluMultMatrixVecd(finalMatrix, in, out);
  257. if (out[3] == 0.0) return(GL_FALSE);
  258. out[0] /= out[3];
  259. out[1] /= out[3];
  260. out[2] /= out[3];
  261. *objx = out[0];
  262. *objy = out[1];
  263. *objz = out[2];
  264. return(GL_TRUE);
  265. }
  266. void APIENTRY gluPickMatrix(GLdouble x, GLdouble y, GLdouble deltax, GLdouble deltay,
  267. GLint viewport[4])
  268. {
  269. if (deltax <= 0 || deltay <= 0) {
  270. return;
  271. }
  272. /* Translate and scale the picked region to the entire window */
  273. glTranslatef((viewport[2] - 2 * (x - viewport[0])) / deltax,
  274. (viewport[3] - 2 * (y - viewport[1])) / deltay, 0);
  275. glScalef(viewport[2] / deltax, viewport[3] / deltay, 1.0);
  276. }