00001
00002 /*
00003 TEDDY - General graphics application library
00004 Copyright (C) 1999, 2000, 2001 Timo Suoranta
00005 tksuoran@cc.helsinki.fi
00006
00007 This library is free software; you can redistribute it and/or
00008 modify it under the terms of the GNU Lesser General Public
00009 License as published by the Free Software Foundation; either
00010 version 2.1 of the License, or (at your option) any later version.
00011
00012 This library is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015 Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public
00018 License along with this library; if not, write to the Free Software
00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00020
00021 $Id: Atmosphere.h,v 1.1 2002/01/08 20:47:04 tksuoran Exp $
00022 */
00023
00024
00029 #ifndef TEDDY__SPACE_GAME__ATMOSPHERE_H
00030 #define TEDDY__SPACE_GAME__ATMOSPHERE_H
00031
00032
00033 #include "Teddy/Maths/Matrix.h"
00034 #include "Teddy/Maths/Plane.h"
00035 #include "Teddy/Maths/Vector.h"
00036 using namespace Teddy::Maths;
00037
00038
00039 namespace Teddy {
00040 namespace SpaceGame {
00041
00042
00053 class Ellipsoid {
00054 public:
00055 Matrix A; // 3x3
00056 Vector B; // 3x1
00057 float C; // scalar
00058 };
00059
00060
00071 class Ellipse {
00072 public:
00073 Matrix A; // 2x2
00074 Vector B; // 2x1
00075 float C;
00076 };
00077
00078
00098 extern void PerspProjEllipsoid (
00099 const Ellipsoid &ellipsoid,
00100 const Vector &eye,
00101 const Plane &plane,
00102 Ellipse &ellipse
00103 );
00104
00105
00110 extern void ConvertEllipse (
00111 Ellipse &ellipse,
00112 Vector ¢er,
00113 Vector &axis0,
00114 Vector &axis1,
00115 float &halfLength0,
00116 float &halfLength1
00117 );
00118
00119
00120 }; // namespace SpaceGame
00121 }; // namespace Teddy
00122
00123
00124 #endif // TEDDY__SPACE_GAME__ATMOSPHERE
00125
00126
00127 /*
00128
00129 > How do I get ellipsoid in the form the code
00130 > takes input from my sphere which has radius r
00131 > and location specified as OpenGL modelview
00132 > matrix?
00133
00134 This discussion assumes that you have already transformed
00135 your sphere into eye coordinates.
00136
00137 The GeneralEllipsoid class is based on the ellipsoid
00138 representation
00139
00140 X^T*A*X + B^T*X + C = 0
00141
00142 A is a 3x3 symmetric matrix,
00143 B is a 3x1 vector,
00144 C is a scalar.
00145
00146 The superscript T means "transpose".
00147
00148 The equation for a sphere is
00149
00150 (X-K)^T*(X-K) = R^2.
00151
00152 center K
00153 radius R
00154
00155 Multiplying the left-hand side and
00156 subtracting R^2 to the left produces
00157
00158 X^T*I*X - 2*K^T*X + K^T*K - R^2 = 0
00159
00160 I is the 3x3 identity matrix.
00161
00162 In setting up the GeneralEllipsoid object
00163
00164 X^T*A*X + B^T*X + C = 0
00165
00166 A is a 3x3 symmetric matrix,
00167 B is a 3x1 vector,
00168 C is a scalar.
00169
00170 you need
00171
00172 A = I 3x3 Matrix: Identity
00173 B = -2*K 3x1 Vector: -2 * center
00174 C = K^T*K - R^2 1 Scalar: center transpose ^ center - radius * radius ?
00175
00176
00177
00178 Since you are in eye coordinates, the 'eye' input to the
00179 projector is the zero vector (0,0,0). The view plane has
00180 normal N = (0,0,1) and plane constant n, the near value
00181 for the frustum. That is, the plane is Dot(N,X) = n.
00182
00183 The returned GeneralEllipse object (a 2D object) is in the
00184 form
00185
00186 Y^T*A*Y + B^T*Y + C = 0
00187
00188 (not the same A, B,C in the ellipsoid).
00189
00190 Matrix A is a symmetric 2x2 matrix,
00191 B is a 2x1 vector, and
00192 C is a scalar.
00193
00194 The 2D plane in which the ellipse is defined is the near plane.
00195 The origin of that plane is the center of the frustum near face.
00196
00197 If you need to GeneralEllipse in factored form:
00198 Complete the square,
00199
00200 (Y-U)^t A (Y-U) = U^t A U - C where
00201
00202 U = -0.5 A^{-1} B.
00203
00204 Define M = A/(U^t A U - C).
00205 The ellipse is
00206
00207 (Y-U)^t M (Y-U) = 1.
00208
00209 Factor M = R^t D R (can do this with an eigensolver)
00210 where R is orthonormal and D is diagonal with
00211 positive // diagonal terms.
00212
00213 If Z = R(Y-U) = (z1,z2), and D = diag(d1,d2), then the
00214 ellipse is
00215
00216 1 = Z^t D Z = d1*z1^2+d2*z2^2.
00217
00218
00219 The silhouette is a hyperbola whenever part
00220 of the sphere is behind the viewer. (E.g,
00221 when the viewer is standing at the shore
00222 looking out over the Pacific Ocean at the
00223 horizon.) It's a parabola when the sphere
00224 is tangent to the view plane (the plane
00225 containing the view point, with normal equal
00226 to the view direction.)
00227
00228 */
00229