| 1 |
/* $Id: glu.c,v 1.2 2000/11/02 00:28:54 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.2 |
| 6 |
* Copyright (C) 1995-1996 Brian Paul |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the Free |
| 20 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <math.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include "gluP.h" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/* |
| 31 |
* Miscellaneous utility functions |
| 32 |
*/ |
| 33 |
|
| 34 |
|
| 35 |
#ifndef M_PI |
| 36 |
#define M_PI 3.1415926536 |
| 37 |
#endif |
| 38 |
#define EPS 0.00001 |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
void gluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez, |
| 44 |
GLdouble centerx, GLdouble centery, GLdouble centerz, |
| 45 |
GLdouble upx, GLdouble upy, GLdouble upz ) |
| 46 |
{ |
| 47 |
GLdouble m[16]; |
| 48 |
GLdouble x[3], y[3], z[3]; |
| 49 |
GLdouble mag; |
| 50 |
|
| 51 |
/* Make rotation matrix */ |
| 52 |
|
| 53 |
/* Z vector */ |
| 54 |
z[0] = eyex - centerx; |
| 55 |
z[1] = eyey - centery; |
| 56 |
z[2] = eyez - centerz; |
| 57 |
mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] ); |
| 58 |
if (mag) { /* mpichler, 19950515 */ |
| 59 |
z[0] /= mag; |
| 60 |
z[1] /= mag; |
| 61 |
z[2] /= mag; |
| 62 |
} |
| 63 |
|
| 64 |
/* Y vector */ |
| 65 |
y[0] = upx; |
| 66 |
y[1] = upy; |
| 67 |
y[2] = upz; |
| 68 |
|
| 69 |
/* X vector = Y cross Z */ |
| 70 |
x[0] = y[1]*z[2] - y[2]*z[1]; |
| 71 |
x[1] = -y[0]*z[2] + y[2]*z[0]; |
| 72 |
x[2] = y[0]*z[1] - y[1]*z[0]; |
| 73 |
|
| 74 |
/* Recompute Y = Z cross X */ |
| 75 |
y[0] = z[1]*x[2] - z[2]*x[1]; |
| 76 |
y[1] = -z[0]*x[2] + z[2]*x[0]; |
| 77 |
y[2] = z[0]*x[1] - z[1]*x[0]; |
| 78 |
|
| 79 |
/* mpichler, 19950515 */ |
| 80 |
/* cross product gives area of parallelogram, which is < 1.0 for |
| 81 |
* non-perpendicular unit-length vectors; so normalize x, y here |
| 82 |
*/ |
| 83 |
|
| 84 |
mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] ); |
| 85 |
if (mag) { |
| 86 |
x[0] /= mag; |
| 87 |
x[1] /= mag; |
| 88 |
x[2] /= mag; |
| 89 |
} |
| 90 |
|
| 91 |
mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] ); |
| 92 |
if (mag) { |
| 93 |
y[0] /= mag; |
| 94 |
y[1] /= mag; |
| 95 |
y[2] /= mag; |
| 96 |
} |
| 97 |
|
| 98 |
#define M(row,col) m[col*4+row] |
| 99 |
M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0; |
| 100 |
M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0; |
| 101 |
M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0; |
| 102 |
M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0; |
| 103 |
#undef M |
| 104 |
glMultMatrixd( m ); |
| 105 |
|
| 106 |
/* Translate Eye to Origin */ |
| 107 |
glTranslated( -eyex, -eyey, -eyez ); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
void gluOrtho2D( GLdouble left, GLdouble right, |
| 114 |
GLdouble bottom, GLdouble top ) |
| 115 |
{ |
| 116 |
glOrtho( left, right, bottom, top, -1.0, 1.0 ); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
void gluPerspective( GLdouble fovy, GLdouble aspect, |
| 122 |
GLdouble zNear, GLdouble zFar ) |
| 123 |
{ |
| 124 |
GLdouble xmin, xmax, ymin, ymax; |
| 125 |
|
| 126 |
ymax = zNear * tan( fovy * M_PI / 360.0 ); |
| 127 |
ymin = -ymax; |
| 128 |
|
| 129 |
xmin = ymin * aspect; |
| 130 |
xmax = ymax * aspect; |
| 131 |
|
| 132 |
glFrustum( xmin, xmax, ymin, ymax, zNear, zFar ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
void gluPickMatrix( GLdouble x, GLdouble y, |
| 138 |
GLdouble width, GLdouble height, |
| 139 |
GLint viewport[4] ) |
| 140 |
{ |
| 141 |
GLfloat m[16]; |
| 142 |
GLfloat sx, sy; |
| 143 |
GLfloat tx, ty; |
| 144 |
|
| 145 |
sx = viewport[2] / width; |
| 146 |
sy = viewport[3] / height; |
| 147 |
tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width; |
| 148 |
ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height; |
| 149 |
|
| 150 |
#define M(row,col) m[col*4+row] |
| 151 |
M(0,0) = sx; M(0,1) = 0.0; M(0,2) = 0.0; M(0,3) = tx; |
| 152 |
M(1,0) = 0.0; M(1,1) = sy; M(1,2) = 0.0; M(1,3) = ty; |
| 153 |
M(2,0) = 0.0; M(2,1) = 0.0; M(2,2) = 1.0; M(2,3) = 0.0; |
| 154 |
M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0; |
| 155 |
#undef M |
| 156 |
|
| 157 |
glMultMatrixf( m ); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
const GLubyte* gluErrorString( GLenum errorCode ) |
| 163 |
{ |
| 164 |
static char *tess_error[] = { |
| 165 |
"missing gluEndPolygon", |
| 166 |
"missing gluBeginPolygon", |
| 167 |
"misoriented contour", |
| 168 |
"vertex/edge intersection", |
| 169 |
"misoriented or self-intersecting loops", |
| 170 |
"coincident vertices", |
| 171 |
"colinear vertices", |
| 172 |
"intersecting edges", |
| 173 |
"not coplanar contours" |
| 174 |
}; |
| 175 |
static char *nurbs_error[] = { |
| 176 |
"spline order un-supported", |
| 177 |
"too few knots", |
| 178 |
"valid knot range is empty", |
| 179 |
"decreasing knot sequence knot", |
| 180 |
"knot multiplicity greater than order of spline", |
| 181 |
"endcurve() must follow bgncurve()", |
| 182 |
"bgncurve() must precede endcurve()", |
| 183 |
"missing or extra geometric data", |
| 184 |
"can't draw pwlcurves", |
| 185 |
"missing bgncurve()", |
| 186 |
"missing bgnsurface()", |
| 187 |
"endtrim() must precede endsurface()", |
| 188 |
"bgnsurface() must precede endsurface()", |
| 189 |
"curve of improper type passed as trim curve", |
| 190 |
"bgnsurface() must precede bgntrim()", |
| 191 |
"endtrim() must follow bgntrim()", |
| 192 |
"bgntrim() must precede endtrim()", |
| 193 |
"invalid or missing trim curve", |
| 194 |
"bgntrim() must precede pwlcurve()", |
| 195 |
"pwlcurve referenced twice", |
| 196 |
"pwlcurve and nurbscurve mixed", |
| 197 |
"improper usage of trim data type", |
| 198 |
"nurbscurve referenced twice", |
| 199 |
"nurbscurve and pwlcurve mixed", |
| 200 |
"nurbssurface referenced twice", |
| 201 |
"invalid property", |
| 202 |
"endsurface() must follow bgnsurface()", |
| 203 |
"misoriented trim curves", |
| 204 |
"intersecting trim curves", |
| 205 |
"UNUSED", |
| 206 |
"unconnected trim curves", |
| 207 |
"unknown knot error", |
| 208 |
"negative vertex count encountered", |
| 209 |
"negative byte-stride encounteed", |
| 210 |
"unknown type descriptor", |
| 211 |
"null control array or knot vector", |
| 212 |
"duplicate point on pwlcurve" |
| 213 |
}; |
| 214 |
|
| 215 |
/* GL Errors */ |
| 216 |
if (errorCode==GL_NO_ERROR) { |
| 217 |
return (GLubyte *) "no error"; |
| 218 |
} |
| 219 |
else if (errorCode==GL_INVALID_VALUE) { |
| 220 |
return (GLubyte *) "invalid value"; |
| 221 |
} |
| 222 |
else if (errorCode==GL_INVALID_ENUM) { |
| 223 |
return (GLubyte *) "invalid enum"; |
| 224 |
} |
| 225 |
else if (errorCode==GL_INVALID_OPERATION) { |
| 226 |
return (GLubyte *) "invalid operation"; |
| 227 |
} |
| 228 |
else if (errorCode==GL_STACK_OVERFLOW) { |
| 229 |
return (GLubyte *) "stack overflow"; |
| 230 |
} |
| 231 |
else if (errorCode==GL_STACK_UNDERFLOW) { |
| 232 |
return (GLubyte *) "stack underflow"; |
| 233 |
} |
| 234 |
else if (errorCode==GL_OUT_OF_MEMORY) { |
| 235 |
return (GLubyte *) "out of memory"; |
| 236 |
} |
| 237 |
/* GLU Errors */ |
| 238 |
else if (errorCode==GLU_NO_ERROR) { |
| 239 |
return (GLubyte *) "no error"; |
| 240 |
} |
| 241 |
else if (errorCode==GLU_INVALID_ENUM) { |
| 242 |
return (GLubyte *) "invalid enum"; |
| 243 |
} |
| 244 |
else if (errorCode==GLU_INVALID_VALUE) { |
| 245 |
return (GLubyte *) "invalid value"; |
| 246 |
} |
| 247 |
else if (errorCode==GLU_OUT_OF_MEMORY) { |
| 248 |
return (GLubyte *) "out of memory"; |
| 249 |
} |
| 250 |
else if (errorCode==GLU_INCOMPATIBLE_GL_VERSION) { |
| 251 |
return (GLubyte *) "incompatible GL version"; |
| 252 |
} |
| 253 |
else if (errorCode>=GLU_TESS_ERROR1 && errorCode<=GLU_TESS_ERROR9) { |
| 254 |
return (GLubyte *) tess_error[errorCode-GLU_TESS_ERROR1]; |
| 255 |
} |
| 256 |
else if (errorCode>=GLU_NURBS_ERROR1 && errorCode<=GLU_NURBS_ERROR37) { |
| 257 |
return (GLubyte *) nurbs_error[errorCode-GLU_NURBS_ERROR1]; |
| 258 |
} |
| 259 |
else { |
| 260 |
return NULL; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
/* |
| 267 |
* New in GLU 1.1 |
| 268 |
*/ |
| 269 |
|
| 270 |
const GLubyte* gluGetString( GLenum name ) |
| 271 |
{ |
| 272 |
static char *extensions = ""; |
| 273 |
static char *version = "1.1 Mesa 2.2"; |
| 274 |
|
| 275 |
switch (name) { |
| 276 |
case GLU_EXTENSIONS: |
| 277 |
return (GLubyte *) extensions; |
| 278 |
case GLU_VERSION: |
| 279 |
return (GLubyte *) version; |
| 280 |
default: |
| 281 |
return NULL; |
| 282 |
} |
| 283 |
} |
| 284 |
|