2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
10 ** http://oss.sgi.com/projects/FreeB
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
34 ** $Date: 2001/09/18 16:39:38 $ $Revision: 1.4 $
35 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libutil/project.c,v 1.4 2001/09/18 16:39:38 kschultz Exp $
45 ** Make m an identity matrix
47 static void __gluMakeIdentityd(GLdouble m
[16])
49 m
[0+4*0] = 1; m
[0+4*1] = 0; m
[0+4*2] = 0; m
[0+4*3] = 0;
50 m
[1+4*0] = 0; m
[1+4*1] = 1; m
[1+4*2] = 0; m
[1+4*3] = 0;
51 m
[2+4*0] = 0; m
[2+4*1] = 0; m
[2+4*2] = 1; m
[2+4*3] = 0;
52 m
[3+4*0] = 0; m
[3+4*1] = 0; m
[3+4*2] = 0; m
[3+4*3] = 1;
55 static void __gluMakeIdentityf(GLfloat m
[16])
57 m
[0+4*0] = 1; m
[0+4*1] = 0; m
[0+4*2] = 0; m
[0+4*3] = 0;
58 m
[1+4*0] = 0; m
[1+4*1] = 1; m
[1+4*2] = 0; m
[1+4*3] = 0;
59 m
[2+4*0] = 0; m
[2+4*1] = 0; m
[2+4*2] = 1; m
[2+4*3] = 0;
60 m
[3+4*0] = 0; m
[3+4*1] = 0; m
[3+4*2] = 0; m
[3+4*3] = 1;
64 gluOrtho2D(GLdouble left
, GLdouble right
, GLdouble bottom
, GLdouble top
)
66 glOrtho(left
, right
, bottom
, top
, -1, 1);
69 #define __glPi 3.14159265358979323846
72 gluPerspective(GLdouble fovy
, GLdouble aspect
, GLdouble zNear
, GLdouble zFar
)
75 double sine
, cotangent
, deltaZ
;
76 double radians
= fovy
/ 2 * __glPi
/ 180;
78 deltaZ
= zFar
- zNear
;
80 if ((deltaZ
== 0) || (sine
== 0) || (aspect
== 0)) {
83 cotangent
= COS(radians
) / sine
;
85 __gluMakeIdentityd(&m
[0][0]);
86 m
[0][0] = cotangent
/ aspect
;
88 m
[2][2] = -(zFar
+ zNear
) / deltaZ
;
90 m
[3][2] = -2 * zNear
* zFar
/ deltaZ
;
92 glMultMatrixd(&m
[0][0]);
95 static void normalize(float v
[3])
99 r
= sqrt( v
[0]*v
[0] + v
[1]*v
[1] + v
[2]*v
[2] );
100 if (r
== 0.0) return;
107 static void cross(float v1
[3], float v2
[3], float result
[3])
109 result
[0] = v1
[1]*v2
[2] - v1
[2]*v2
[1];
110 result
[1] = v1
[2]*v2
[0] - v1
[0]*v2
[2];
111 result
[2] = v1
[0]*v2
[1] - v1
[1]*v2
[0];
115 gluLookAt(GLdouble eyex
, GLdouble eyey
, GLdouble eyez
, GLdouble centerx
,
116 GLdouble centery
, GLdouble centerz
, GLdouble upx
, GLdouble upy
,
120 float forward
[3], side
[3], up
[3];
123 forward
[0] = centerx
- eyex
;
124 forward
[1] = centery
- eyey
;
125 forward
[2] = centerz
- eyez
;
133 /* Side = forward x up */
134 cross(forward
, up
, side
);
137 /* Recompute up as: up = side x forward */
138 cross(side
, forward
, up
);
140 __gluMakeIdentityf(&m
[0][0]);
149 m
[0][2] = -forward
[0];
150 m
[1][2] = -forward
[1];
151 m
[2][2] = -forward
[2];
153 glMultMatrixf(&m
[0][0]);
154 glTranslated(-eyex
, -eyey
, -eyez
);
157 static void __gluMultMatrixVecd(const GLdouble matrix
[16], const GLdouble in
[4],
162 for (i
=0; i
<4; i
++) {
164 in
[0] * matrix
[0*4+i
] +
165 in
[1] * matrix
[1*4+i
] +
166 in
[2] * matrix
[2*4+i
] +
167 in
[3] * matrix
[3*4+i
];
172 ** inverse = invert(src)
174 static int __gluInvertMatrixd(const GLdouble src
[16], GLdouble inverse
[16])
180 for (i
=0; i
<4; i
++) {
181 for (j
=0; j
<4; j
++) {
182 temp
[i
][j
] = src
[i
*4+j
];
185 __gluMakeIdentityd(inverse
);
187 for (i
= 0; i
< 4; i
++) {
189 ** Look for largest element in column
192 for (j
= i
+ 1; j
< 4; j
++) {
193 if (fabs(temp
[j
][i
]) > fabs(temp
[i
][i
])) {
202 for (k
= 0; k
< 4; k
++) {
204 temp
[i
][k
] = temp
[swap
][k
];
208 inverse
[i
*4+k
] = inverse
[swap
*4+k
];
209 inverse
[swap
*4+k
] = t
;
213 if (temp
[i
][i
] == 0) {
215 ** No non-zero pivot. The matrix is singular, which shouldn't
216 ** happen. This means the user gave us a bad matrix.
222 for (k
= 0; k
< 4; k
++) {
226 for (j
= 0; j
< 4; j
++) {
229 for (k
= 0; k
< 4; k
++) {
230 temp
[j
][k
] -= temp
[i
][k
]*t
;
231 inverse
[j
*4+k
] -= inverse
[i
*4+k
]*t
;
239 static void __gluMultMatricesd(const GLdouble a
[16], const GLdouble b
[16],
244 for (i
= 0; i
< 4; i
++) {
245 for (j
= 0; j
< 4; j
++) {
256 gluProject(GLdouble objx
, GLdouble objy
, GLdouble objz
,
257 const GLdouble modelMatrix
[16],
258 const GLdouble projMatrix
[16],
259 const GLint viewport
[4],
260 GLdouble
*winx
, GLdouble
*winy
, GLdouble
*winz
)
269 __gluMultMatrixVecd(modelMatrix
, in
, out
);
270 __gluMultMatrixVecd(projMatrix
, out
, in
);
271 if (in
[3] == 0.0) return(GL_FALSE
);
275 /* Map x, y and z to range 0-1 */
276 in
[0] = in
[0] * 0.5 + 0.5;
277 in
[1] = in
[1] * 0.5 + 0.5;
278 in
[2] = in
[2] * 0.5 + 0.5;
280 /* Map x,y to viewport */
281 in
[0] = in
[0] * viewport
[2] + viewport
[0];
282 in
[1] = in
[1] * viewport
[3] + viewport
[1];
291 gluUnProject(GLdouble winx
, GLdouble winy
, GLdouble winz
,
292 const GLdouble modelMatrix
[16],
293 const GLdouble projMatrix
[16],
294 const GLint viewport
[4],
295 GLdouble
*objx
, GLdouble
*objy
, GLdouble
*objz
)
297 double finalMatrix
[16];
301 __gluMultMatricesd(modelMatrix
, projMatrix
, finalMatrix
);
302 if (!__gluInvertMatrixd(finalMatrix
, finalMatrix
)) return(GL_FALSE
);
309 /* Map x and y from window coordinates */
310 in
[0] = (in
[0] - viewport
[0]) / viewport
[2];
311 in
[1] = (in
[1] - viewport
[1]) / viewport
[3];
313 /* Map to range -1 to 1 */
314 in
[0] = in
[0] * 2 - 1;
315 in
[1] = in
[1] * 2 - 1;
316 in
[2] = in
[2] * 2 - 1;
318 __gluMultMatrixVecd(finalMatrix
, in
, out
);
319 if (out
[3] == 0.0) return(GL_FALSE
);
330 gluUnProject4(GLdouble winx
, GLdouble winy
, GLdouble winz
, GLdouble clipw
,
331 const GLdouble modelMatrix
[16],
332 const GLdouble projMatrix
[16],
333 const GLint viewport
[4],
334 GLclampd nearVal
, GLclampd farVal
,
335 GLdouble
*objx
, GLdouble
*objy
, GLdouble
*objz
,
338 double finalMatrix
[16];
342 __gluMultMatricesd(modelMatrix
, projMatrix
, finalMatrix
);
343 if (!__gluInvertMatrixd(finalMatrix
, finalMatrix
)) return(GL_FALSE
);
350 /* Map x and y from window coordinates */
351 in
[0] = (in
[0] - viewport
[0]) / viewport
[2];
352 in
[1] = (in
[1] - viewport
[1]) / viewport
[3];
353 in
[2] = (in
[2] - nearVal
) / (farVal
- nearVal
);
355 /* Map to range -1 to 1 */
356 in
[0] = in
[0] * 2 - 1;
357 in
[1] = in
[1] * 2 - 1;
358 in
[2] = in
[2] * 2 - 1;
360 __gluMultMatrixVecd(finalMatrix
, in
, out
);
361 if (out
[3] == 0.0) return(GL_FALSE
);
370 gluPickMatrix(GLdouble x
, GLdouble y
, GLdouble deltax
, GLdouble deltay
,
373 if (deltax
<= 0 || deltay
<= 0) {
377 /* Translate and scale the picked region to the entire window */
378 glTranslatef((viewport
[2] - 2 * (x
- viewport
[0])) / deltax
,
379 (viewport
[3] - 2 * (y
- viewport
[1])) / deltay
, 0);
380 glScalef(viewport
[2] / deltax
, viewport
[3] / deltay
, 1.0);