Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenproject.c
Go to the documentation of this file.
00001 /* 00002 ** License Applicability. Except to the extent portions of this file are 00003 ** made subject to an alternative license as permitted in the SGI Free 00004 ** Software License B, Version 1.1 (the "License"), the contents of this 00005 ** file are subject only to the provisions of the License. You may not use 00006 ** this file except in compliance with the License. You may obtain a copy 00007 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600 00008 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at: 00009 ** 00010 ** http://oss.sgi.com/projects/FreeB 00011 ** 00012 ** Note that, as provided in the License, the Software is distributed on an 00013 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS 00014 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND 00015 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A 00016 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT. 00017 ** 00018 ** Original Code. The Original Code is: OpenGL Sample Implementation, 00019 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics, 00020 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc. 00021 ** Copyright in any portions created by third parties is as indicated 00022 ** elsewhere herein. All Rights Reserved. 00023 ** 00024 ** Additional Notice Provisions: The application programming interfaces 00025 ** established by SGI in conjunction with the Original Code are The 00026 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released 00027 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version 00028 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X 00029 ** Window System(R) (Version 1.3), released October 19, 1998. This software 00030 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation 00031 ** published by SGI, but has not been independently verified as being 00032 ** compliant with the OpenGL(R) version 1.2.1 Specification. 00033 ** 00034 ** $Date: 2007-10-19 23:21:45 +0000 (Fri, 19 Oct 2007) $ $Revision: 1.1 $ 00035 ** $Header: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/glu32/libutil/project.c,v 1.1 2004/02/02 16:39:16 navaraf Exp $ 00036 */ 00037 00038 #include "gluos.h" 00039 #include <math.h> 00040 #include <GL/gl.h> 00041 #include <GL/glu.h> 00042 #include "gluint.h" 00043 00044 /* 00045 ** Make m an identity matrix 00046 */ 00047 static void __gluMakeIdentityd(GLdouble m[16]) 00048 { 00049 m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0; 00050 m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0; 00051 m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0; 00052 m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1; 00053 } 00054 00055 static void __gluMakeIdentityf(GLfloat m[16]) 00056 { 00057 m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0; 00058 m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0; 00059 m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0; 00060 m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1; 00061 } 00062 00063 void GLAPIENTRY 00064 gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) 00065 { 00066 glOrtho(left, right, bottom, top, -1, 1); 00067 } 00068 00069 #define __glPi 3.14159265358979323846 00070 00071 void GLAPIENTRY 00072 gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) 00073 { 00074 GLdouble m[4][4]; 00075 double sine, cotangent, deltaZ; 00076 double radians = fovy / 2 * __glPi / 180; 00077 00078 deltaZ = zFar - zNear; 00079 sine = sin(radians); 00080 if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) { 00081 return; 00082 } 00083 cotangent = COS(radians) / sine; 00084 00085 __gluMakeIdentityd(&m[0][0]); 00086 m[0][0] = cotangent / aspect; 00087 m[1][1] = cotangent; 00088 m[2][2] = -(zFar + zNear) / deltaZ; 00089 m[2][3] = -1; 00090 m[3][2] = -2 * zNear * zFar / deltaZ; 00091 m[3][3] = 0; 00092 glMultMatrixd(&m[0][0]); 00093 } 00094 00095 static void normalize(float v[3]) 00096 { 00097 float r; 00098 00099 r = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] ); 00100 if (r == 0.0) return; 00101 00102 v[0] /= r; 00103 v[1] /= r; 00104 v[2] /= r; 00105 } 00106 00107 static void cross(float v1[3], float v2[3], float result[3]) 00108 { 00109 result[0] = v1[1]*v2[2] - v1[2]*v2[1]; 00110 result[1] = v1[2]*v2[0] - v1[0]*v2[2]; 00111 result[2] = v1[0]*v2[1] - v1[1]*v2[0]; 00112 } 00113 00114 void GLAPIENTRY 00115 gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, 00116 GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, 00117 GLdouble upz) 00118 { 00119 float forward[3], side[3], up[3]; 00120 GLfloat m[4][4]; 00121 00122 forward[0] = centerx - eyex; 00123 forward[1] = centery - eyey; 00124 forward[2] = centerz - eyez; 00125 00126 up[0] = upx; 00127 up[1] = upy; 00128 up[2] = upz; 00129 00130 normalize(forward); 00131 00132 /* Side = forward x up */ 00133 cross(forward, up, side); 00134 normalize(side); 00135 00136 /* Recompute up as: up = side x forward */ 00137 cross(side, forward, up); 00138 00139 __gluMakeIdentityf(&m[0][0]); 00140 m[0][0] = side[0]; 00141 m[1][0] = side[1]; 00142 m[2][0] = side[2]; 00143 00144 m[0][1] = up[0]; 00145 m[1][1] = up[1]; 00146 m[2][1] = up[2]; 00147 00148 m[0][2] = -forward[0]; 00149 m[1][2] = -forward[1]; 00150 m[2][2] = -forward[2]; 00151 00152 glMultMatrixf(&m[0][0]); 00153 glTranslated(-eyex, -eyey, -eyez); 00154 } 00155 00156 static void __gluMultMatrixVecd(const GLdouble matrix[16], const GLdouble in[4], 00157 GLdouble out[4]) 00158 { 00159 int i; 00160 00161 for (i=0; i<4; i++) { 00162 out[i] = 00163 in[0] * matrix[0*4+i] + 00164 in[1] * matrix[1*4+i] + 00165 in[2] * matrix[2*4+i] + 00166 in[3] * matrix[3*4+i]; 00167 } 00168 } 00169 00170 /* 00171 ** inverse = invert(src) 00172 */ 00173 static int __gluInvertMatrixd(const GLdouble src[16], GLdouble inverse[16]) 00174 { 00175 int i, j, k, swap; 00176 double t; 00177 GLdouble temp[4][4]; 00178 00179 for (i=0; i<4; i++) { 00180 for (j=0; j<4; j++) { 00181 temp[i][j] = src[i*4+j]; 00182 } 00183 } 00184 __gluMakeIdentityd(inverse); 00185 00186 for (i = 0; i < 4; i++) { 00187 /* 00188 ** Look for largest element in column 00189 */ 00190 swap = i; 00191 for (j = i + 1; j < 4; j++) { 00192 if (fabs(temp[j][i]) > fabs(temp[i][i])) { 00193 swap = j; 00194 } 00195 } 00196 00197 if (swap != i) { 00198 /* 00199 ** Swap rows. 00200 */ 00201 for (k = 0; k < 4; k++) { 00202 t = temp[i][k]; 00203 temp[i][k] = temp[swap][k]; 00204 temp[swap][k] = t; 00205 00206 t = inverse[i*4+k]; 00207 inverse[i*4+k] = inverse[swap*4+k]; 00208 inverse[swap*4+k] = t; 00209 } 00210 } 00211 00212 if (temp[i][i] == 0) { 00213 /* 00214 ** No non-zero pivot. The matrix is singular, which shouldn't 00215 ** happen. This means the user gave us a bad matrix. 00216 */ 00217 return GL_FALSE; 00218 } 00219 00220 t = temp[i][i]; 00221 for (k = 0; k < 4; k++) { 00222 temp[i][k] /= t; 00223 inverse[i*4+k] /= t; 00224 } 00225 for (j = 0; j < 4; j++) { 00226 if (j != i) { 00227 t = temp[j][i]; 00228 for (k = 0; k < 4; k++) { 00229 temp[j][k] -= temp[i][k]*t; 00230 inverse[j*4+k] -= inverse[i*4+k]*t; 00231 } 00232 } 00233 } 00234 } 00235 return GL_TRUE; 00236 } 00237 00238 static void __gluMultMatricesd(const GLdouble a[16], const GLdouble b[16], 00239 GLdouble r[16]) 00240 { 00241 int i, j; 00242 00243 for (i = 0; i < 4; i++) { 00244 for (j = 0; j < 4; j++) { 00245 r[i*4+j] = 00246 a[i*4+0]*b[0*4+j] + 00247 a[i*4+1]*b[1*4+j] + 00248 a[i*4+2]*b[2*4+j] + 00249 a[i*4+3]*b[3*4+j]; 00250 } 00251 } 00252 } 00253 00254 GLint GLAPIENTRY 00255 gluProject(GLdouble objx, GLdouble objy, GLdouble objz, 00256 const GLdouble modelMatrix[16], 00257 const GLdouble projMatrix[16], 00258 const GLint viewport[4], 00259 GLdouble *winx, GLdouble *winy, GLdouble *winz) 00260 { 00261 double in[4]; 00262 double out[4]; 00263 00264 in[0]=objx; 00265 in[1]=objy; 00266 in[2]=objz; 00267 in[3]=1.0; 00268 __gluMultMatrixVecd(modelMatrix, in, out); 00269 __gluMultMatrixVecd(projMatrix, out, in); 00270 if (in[3] == 0.0) return(GL_FALSE); 00271 in[0] /= in[3]; 00272 in[1] /= in[3]; 00273 in[2] /= in[3]; 00274 /* Map x, y and z to range 0-1 */ 00275 in[0] = in[0] * 0.5 + 0.5; 00276 in[1] = in[1] * 0.5 + 0.5; 00277 in[2] = in[2] * 0.5 + 0.5; 00278 00279 /* Map x,y to viewport */ 00280 in[0] = in[0] * viewport[2] + viewport[0]; 00281 in[1] = in[1] * viewport[3] + viewport[1]; 00282 00283 *winx=in[0]; 00284 *winy=in[1]; 00285 *winz=in[2]; 00286 return(GL_TRUE); 00287 } 00288 00289 GLint GLAPIENTRY 00290 gluUnProject(GLdouble winx, GLdouble winy, GLdouble winz, 00291 const GLdouble modelMatrix[16], 00292 const GLdouble projMatrix[16], 00293 const GLint viewport[4], 00294 GLdouble *objx, GLdouble *objy, GLdouble *objz) 00295 { 00296 double finalMatrix[16]; 00297 double in[4]; 00298 double out[4]; 00299 00300 __gluMultMatricesd(modelMatrix, projMatrix, finalMatrix); 00301 if (!__gluInvertMatrixd(finalMatrix, finalMatrix)) return(GL_FALSE); 00302 00303 in[0]=winx; 00304 in[1]=winy; 00305 in[2]=winz; 00306 in[3]=1.0; 00307 00308 /* Map x and y from window coordinates */ 00309 in[0] = (in[0] - viewport[0]) / viewport[2]; 00310 in[1] = (in[1] - viewport[1]) / viewport[3]; 00311 00312 /* Map to range -1 to 1 */ 00313 in[0] = in[0] * 2 - 1; 00314 in[1] = in[1] * 2 - 1; 00315 in[2] = in[2] * 2 - 1; 00316 00317 __gluMultMatrixVecd(finalMatrix, in, out); 00318 if (out[3] == 0.0) return(GL_FALSE); 00319 out[0] /= out[3]; 00320 out[1] /= out[3]; 00321 out[2] /= out[3]; 00322 *objx = out[0]; 00323 *objy = out[1]; 00324 *objz = out[2]; 00325 return(GL_TRUE); 00326 } 00327 00328 GLint GLAPIENTRY 00329 gluUnProject4(GLdouble winx, GLdouble winy, GLdouble winz, GLdouble clipw, 00330 const GLdouble modelMatrix[16], 00331 const GLdouble projMatrix[16], 00332 const GLint viewport[4], 00333 GLclampd nearVal, GLclampd farVal, 00334 GLdouble *objx, GLdouble *objy, GLdouble *objz, 00335 GLdouble *objw) 00336 { 00337 double finalMatrix[16]; 00338 double in[4]; 00339 double out[4]; 00340 00341 __gluMultMatricesd(modelMatrix, projMatrix, finalMatrix); 00342 if (!__gluInvertMatrixd(finalMatrix, finalMatrix)) return(GL_FALSE); 00343 00344 in[0]=winx; 00345 in[1]=winy; 00346 in[2]=winz; 00347 in[3]=clipw; 00348 00349 /* Map x and y from window coordinates */ 00350 in[0] = (in[0] - viewport[0]) / viewport[2]; 00351 in[1] = (in[1] - viewport[1]) / viewport[3]; 00352 in[2] = (in[2] - nearVal) / (farVal - nearVal); 00353 00354 /* Map to range -1 to 1 */ 00355 in[0] = in[0] * 2 - 1; 00356 in[1] = in[1] * 2 - 1; 00357 in[2] = in[2] * 2 - 1; 00358 00359 __gluMultMatrixVecd(finalMatrix, in, out); 00360 if (out[3] == 0.0) return(GL_FALSE); 00361 *objx = out[0]; 00362 *objy = out[1]; 00363 *objz = out[2]; 00364 *objw = out[3]; 00365 return(GL_TRUE); 00366 } 00367 00368 void GLAPIENTRY 00369 gluPickMatrix(GLdouble x, GLdouble y, GLdouble deltax, GLdouble deltay, 00370 GLint viewport[4]) 00371 { 00372 if (deltax <= 0 || deltay <= 0) { 00373 return; 00374 } 00375 00376 /* Translate and scale the picked region to the entire window */ 00377 glTranslatef((viewport[2] - 2 * (x - viewport[0])) / deltax, 00378 (viewport[3] - 2 * (y - viewport[1])) / deltay, 0); 00379 glScalef(viewport[2] / deltax, viewport[3] / deltay, 1.0); 00380 } Generated on Fri May 25 2012 04:21:58 for ReactOS by
1.7.6.1
|