d2d1/tests: Add hwnd render target pixel format tests.
[wine.git] / dlls / glu32 / project.c
blob0a71eabd98cd6a1bbea6e0234e6c4953a5495d7f
1 /*
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
23 * SOFTWARE.
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.
31 #include <math.h>
33 #include "windef.h"
34 #include "wine/wgl.h"
35 #include "wine/glu.h"
38 ** Make m an identity matrix
40 static void __gluMakeIdentityd(GLdouble m[4][4])
42 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
43 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
44 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = 0;
45 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
48 static void __gluMakeIdentityf(GLfloat m[4][4])
50 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
51 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
52 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = 0;
53 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][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 )
69 GLdouble m[4][4];
70 double sine, cotangent, deltaZ;
71 double radians = fovy / 2 * M_PI / 180;
73 deltaZ = zFar - zNear;
74 sine = sin(radians);
75 if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
76 return;
78 cotangent = cos(radians) / sine;
80 __gluMakeIdentityd(m);
81 m[0][0] = cotangent / aspect;
82 m[1][1] = cotangent;
83 m[2][2] = -(zFar + zNear) / deltaZ;
84 m[2][3] = -1;
85 m[3][2] = -2 * zNear * zFar / deltaZ;
86 m[3][3] = 0;
87 glMultMatrixd(&m[0][0]);
90 static void normalize(float v[3])
92 float r;
94 r = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
95 if (r == 0.0) return;
97 v[0] /= r;
98 v[1] /= r;
99 v[2] /= r;
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];
117 GLfloat m[4][4];
119 forward[0] = centerx - eyex;
120 forward[1] = centery - eyey;
121 forward[2] = centerz - eyez;
123 up[0] = upx;
124 up[1] = upy;
125 up[2] = upz;
127 normalize(forward);
129 /* Side = forward x up */
130 cross(forward, up, side);
131 normalize(side);
133 /* Recompute up as: up = side x forward */
134 cross(side, forward, up);
136 __gluMakeIdentityf(m);
137 m[0][0] = side[0];
138 m[1][0] = side[1];
139 m[2][0] = side[2];
141 m[0][1] = up[0];
142 m[1][1] = up[1];
143 m[2][1] = up[2];
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],
154 GLdouble out[4])
156 int i;
158 for (i=0; i<4; i++) {
159 out[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])
173 double inv[16], det;
174 int i;
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];
210 if (det == 0)
211 return GL_FALSE;
213 det = 1.0 / det;
215 for (i = 0; i < 16; i++)
216 invOut[i] = inv[i] * det;
218 return GL_TRUE;
221 static void __gluMultMatricesd(const GLdouble a[16], const GLdouble b[16],
222 GLdouble r[16])
224 int i, j;
226 for (i = 0; i < 4; i++) {
227 for (j = 0; j < 4; j++) {
228 r[i*4+j] =
229 a[i*4+0]*b[0*4+j] +
230 a[i*4+1]*b[1*4+j] +
231 a[i*4+2]*b[2*4+j] +
232 a[i*4+3]*b[3*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 )
244 double in[4];
245 double out[4];
247 in[0]=objx;
248 in[1]=objy;
249 in[2]=objz;
250 in[3]=1.0;
251 __gluMultMatrixVecd(modelMatrix, in, out);
252 __gluMultMatrixVecd(projMatrix, out, in);
253 if (in[3] == 0.0) return(GL_FALSE);
254 in[0] /= in[3];
255 in[1] /= in[3];
256 in[2] /= in[3];
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];
266 *winx=in[0];
267 *winy=in[1];
268 *winz=in[2];
269 return(GL_TRUE);
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];
280 double in[4];
281 double out[4];
283 __gluMultMatricesd(modelMatrix, projMatrix, finalMatrix);
284 if (!__gluInvertMatrixd(finalMatrix, finalMatrix)) return(GL_FALSE);
286 in[0]=winx;
287 in[1]=winy;
288 in[2]=winz;
289 in[3]=1.0;
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);
302 out[0] /= out[3];
303 out[1] /= out[3];
304 out[2] /= out[3];
305 *objx = out[0];
306 *objy = out[1];
307 *objz = out[2];
308 return(GL_TRUE);
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) {
317 return;
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);