2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
38 ** Make m an identity matrix
40 static void __gluMakeIdentityd(GLdouble m
[16])
42 m
[0+4*0] = 1; m
[0+4*1] = 0; m
[0+4*2] = 0; m
[0+4*3] = 0;
43 m
[1+4*0] = 0; m
[1+4*1] = 1; m
[1+4*2] = 0; m
[1+4*3] = 0;
44 m
[2+4*0] = 0; m
[2+4*1] = 0; m
[2+4*2] = 1; m
[2+4*3] = 0;
45 m
[3+4*0] = 0; m
[3+4*1] = 0; m
[3+4*2] = 0; m
[3+4*3] = 1;
48 static void __gluMakeIdentityf(GLfloat m
[16])
50 m
[0+4*0] = 1; m
[0+4*1] = 0; m
[0+4*2] = 0; m
[0+4*3] = 0;
51 m
[1+4*0] = 0; m
[1+4*1] = 1; m
[1+4*2] = 0; m
[1+4*3] = 0;
52 m
[2+4*0] = 0; m
[2+4*1] = 0; m
[2+4*2] = 1; m
[2+4*3] = 0;
53 m
[3+4*0] = 0; m
[3+4*1] = 0; m
[3+4*2] = 0; m
[3+4*3] = 1;
56 /***********************************************************************
57 * gluOrtho2D (GLU32.@)
59 void WINAPI
gluOrtho2D( GLdouble left
, GLdouble right
, GLdouble bottom
, GLdouble top
)
61 glOrtho(left
, right
, bottom
, top
, -1, 1);
64 /***********************************************************************
65 * gluPerspective (GLU32.@)
67 void WINAPI
gluPerspective( GLdouble fovy
, GLdouble aspect
, GLdouble zNear
, GLdouble zFar
)
70 double sine
, cotangent
, deltaZ
;
71 double radians
= fovy
/ 2 * M_PI
/ 180;
73 deltaZ
= zFar
- zNear
;
75 if ((deltaZ
== 0) || (sine
== 0) || (aspect
== 0)) {
78 cotangent
= cos(radians
) / sine
;
80 __gluMakeIdentityd(&m
[0][0]);
81 m
[0][0] = cotangent
/ aspect
;
83 m
[2][2] = -(zFar
+ zNear
) / deltaZ
;
85 m
[3][2] = -2 * zNear
* zFar
/ deltaZ
;
87 glMultMatrixd(&m
[0][0]);
90 static void normalize(float v
[3])
94 r
= sqrt( v
[0]*v
[0] + v
[1]*v
[1] + v
[2]*v
[2] );
102 static void cross(float v1
[3], float v2
[3], float result
[3])
104 result
[0] = v1
[1]*v2
[2] - v1
[2]*v2
[1];
105 result
[1] = v1
[2]*v2
[0] - v1
[0]*v2
[2];
106 result
[2] = v1
[0]*v2
[1] - v1
[1]*v2
[0];
109 /***********************************************************************
110 * gluLookAt (GLU32.@)
112 void WINAPI
gluLookAt( GLdouble eyex
, GLdouble eyey
, GLdouble eyez
,
113 GLdouble centerx
, GLdouble centery
, GLdouble centerz
,
114 GLdouble upx
, GLdouble upy
, GLdouble upz
)
116 float forward
[3], side
[3], up
[3];
119 forward
[0] = centerx
- eyex
;
120 forward
[1] = centery
- eyey
;
121 forward
[2] = centerz
- eyez
;
129 /* Side = forward x up */
130 cross(forward
, up
, side
);
133 /* Recompute up as: up = side x forward */
134 cross(side
, forward
, up
);
136 __gluMakeIdentityf(&m
[0][0]);
145 m
[0][2] = -forward
[0];
146 m
[1][2] = -forward
[1];
147 m
[2][2] = -forward
[2];
149 glMultMatrixf(&m
[0][0]);
150 glTranslated(-eyex
, -eyey
, -eyez
);
153 static void __gluMultMatrixVecd(const GLdouble matrix
[16], const GLdouble in
[4],
158 for (i
=0; i
<4; i
++) {
160 in
[0] * matrix
[0*4+i
] +
161 in
[1] * matrix
[1*4+i
] +
162 in
[2] * matrix
[2*4+i
] +
163 in
[3] * matrix
[3*4+i
];
168 ** Invert 4x4 matrix.
169 ** Contributed by David Moore (See Mesa bug #6748)
171 static int __gluInvertMatrixd(const GLdouble m
[16], GLdouble invOut
[16])
176 inv
[0] = m
[5]*m
[10]*m
[15] - m
[5]*m
[11]*m
[14] - m
[9]*m
[6]*m
[15]
177 + m
[9]*m
[7]*m
[14] + m
[13]*m
[6]*m
[11] - m
[13]*m
[7]*m
[10];
178 inv
[4] = -m
[4]*m
[10]*m
[15] + m
[4]*m
[11]*m
[14] + m
[8]*m
[6]*m
[15]
179 - m
[8]*m
[7]*m
[14] - m
[12]*m
[6]*m
[11] + m
[12]*m
[7]*m
[10];
180 inv
[8] = m
[4]*m
[9]*m
[15] - m
[4]*m
[11]*m
[13] - m
[8]*m
[5]*m
[15]
181 + m
[8]*m
[7]*m
[13] + m
[12]*m
[5]*m
[11] - m
[12]*m
[7]*m
[9];
182 inv
[12] = -m
[4]*m
[9]*m
[14] + m
[4]*m
[10]*m
[13] + m
[8]*m
[5]*m
[14]
183 - m
[8]*m
[6]*m
[13] - m
[12]*m
[5]*m
[10] + m
[12]*m
[6]*m
[9];
184 inv
[1] = -m
[1]*m
[10]*m
[15] + m
[1]*m
[11]*m
[14] + m
[9]*m
[2]*m
[15]
185 - m
[9]*m
[3]*m
[14] - m
[13]*m
[2]*m
[11] + m
[13]*m
[3]*m
[10];
186 inv
[5] = m
[0]*m
[10]*m
[15] - m
[0]*m
[11]*m
[14] - m
[8]*m
[2]*m
[15]
187 + m
[8]*m
[3]*m
[14] + m
[12]*m
[2]*m
[11] - m
[12]*m
[3]*m
[10];
188 inv
[9] = -m
[0]*m
[9]*m
[15] + m
[0]*m
[11]*m
[13] + m
[8]*m
[1]*m
[15]
189 - m
[8]*m
[3]*m
[13] - m
[12]*m
[1]*m
[11] + m
[12]*m
[3]*m
[9];
190 inv
[13] = m
[0]*m
[9]*m
[14] - m
[0]*m
[10]*m
[13] - m
[8]*m
[1]*m
[14]
191 + m
[8]*m
[2]*m
[13] + m
[12]*m
[1]*m
[10] - m
[12]*m
[2]*m
[9];
192 inv
[2] = m
[1]*m
[6]*m
[15] - m
[1]*m
[7]*m
[14] - m
[5]*m
[2]*m
[15]
193 + m
[5]*m
[3]*m
[14] + m
[13]*m
[2]*m
[7] - m
[13]*m
[3]*m
[6];
194 inv
[6] = -m
[0]*m
[6]*m
[15] + m
[0]*m
[7]*m
[14] + m
[4]*m
[2]*m
[15]
195 - m
[4]*m
[3]*m
[14] - m
[12]*m
[2]*m
[7] + m
[12]*m
[3]*m
[6];
196 inv
[10] = m
[0]*m
[5]*m
[15] - m
[0]*m
[7]*m
[13] - m
[4]*m
[1]*m
[15]
197 + m
[4]*m
[3]*m
[13] + m
[12]*m
[1]*m
[7] - m
[12]*m
[3]*m
[5];
198 inv
[14] = -m
[0]*m
[5]*m
[14] + m
[0]*m
[6]*m
[13] + m
[4]*m
[1]*m
[14]
199 - m
[4]*m
[2]*m
[13] - m
[12]*m
[1]*m
[6] + m
[12]*m
[2]*m
[5];
200 inv
[3] = -m
[1]*m
[6]*m
[11] + m
[1]*m
[7]*m
[10] + m
[5]*m
[2]*m
[11]
201 - m
[5]*m
[3]*m
[10] - m
[9]*m
[2]*m
[7] + m
[9]*m
[3]*m
[6];
202 inv
[7] = m
[0]*m
[6]*m
[11] - m
[0]*m
[7]*m
[10] - m
[4]*m
[2]*m
[11]
203 + m
[4]*m
[3]*m
[10] + m
[8]*m
[2]*m
[7] - m
[8]*m
[3]*m
[6];
204 inv
[11] = -m
[0]*m
[5]*m
[11] + m
[0]*m
[7]*m
[9] + m
[4]*m
[1]*m
[11]
205 - m
[4]*m
[3]*m
[9] - m
[8]*m
[1]*m
[7] + m
[8]*m
[3]*m
[5];
206 inv
[15] = m
[0]*m
[5]*m
[10] - m
[0]*m
[6]*m
[9] - m
[4]*m
[1]*m
[10]
207 + m
[4]*m
[2]*m
[9] + m
[8]*m
[1]*m
[6] - m
[8]*m
[2]*m
[5];
209 det
= m
[0]*inv
[0] + m
[1]*inv
[4] + m
[2]*inv
[8] + m
[3]*inv
[12];
215 for (i
= 0; i
< 16; i
++)
216 invOut
[i
] = inv
[i
] * det
;
221 static void __gluMultMatricesd(const GLdouble a
[16], const GLdouble b
[16],
226 for (i
= 0; i
< 4; i
++) {
227 for (j
= 0; j
< 4; j
++) {
237 /***********************************************************************
238 * gluProject (GLU32.@)
240 GLint WINAPI
gluProject( GLdouble objx
, GLdouble objy
, GLdouble objz
, const GLdouble modelMatrix
[16],
241 const GLdouble projMatrix
[16], const GLint viewport
[4],
242 GLdouble
*winx
, GLdouble
*winy
, GLdouble
*winz
)
251 __gluMultMatrixVecd(modelMatrix
, in
, out
);
252 __gluMultMatrixVecd(projMatrix
, out
, in
);
253 if (in
[3] == 0.0) return(GL_FALSE
);
257 /* Map x, y and z to range 0-1 */
258 in
[0] = in
[0] * 0.5 + 0.5;
259 in
[1] = in
[1] * 0.5 + 0.5;
260 in
[2] = in
[2] * 0.5 + 0.5;
262 /* Map x,y to viewport */
263 in
[0] = in
[0] * viewport
[2] + viewport
[0];
264 in
[1] = in
[1] * viewport
[3] + viewport
[1];
272 /***********************************************************************
273 * gluUnProject (GLU32.@)
275 GLint WINAPI
gluUnProject( GLdouble winx
, GLdouble winy
, GLdouble winz
, const GLdouble modelMatrix
[16],
276 const GLdouble projMatrix
[16], const GLint viewport
[4],
277 GLdouble
*objx
, GLdouble
*objy
, GLdouble
*objz
)
279 double finalMatrix
[16];
283 __gluMultMatricesd(modelMatrix
, projMatrix
, finalMatrix
);
284 if (!__gluInvertMatrixd(finalMatrix
, finalMatrix
)) return(GL_FALSE
);
291 /* Map x and y from window coordinates */
292 in
[0] = (in
[0] - viewport
[0]) / viewport
[2];
293 in
[1] = (in
[1] - viewport
[1]) / viewport
[3];
295 /* Map to range -1 to 1 */
296 in
[0] = in
[0] * 2 - 1;
297 in
[1] = in
[1] * 2 - 1;
298 in
[2] = in
[2] * 2 - 1;
300 __gluMultMatrixVecd(finalMatrix
, in
, out
);
301 if (out
[3] == 0.0) return(GL_FALSE
);
311 /***********************************************************************
312 * gluPickMatrix (GLU32.@)
314 void WINAPI
gluPickMatrix( GLdouble x
, GLdouble y
, GLdouble deltax
, GLdouble deltay
, GLint viewport
[4] )
316 if (deltax
<= 0 || deltay
<= 0) {
320 /* Translate and scale the picked region to the entire window */
321 glTranslatef((viewport
[2] - 2 * (x
- viewport
[0])) / deltax
,
322 (viewport
[3] - 2 * (y
- viewport
[1])) / deltay
, 0);
323 glScalef(viewport
[2] / deltax
, viewport
[3] / deltay
, 1.0);