]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/vec4f.hpp
Clouds3D crashes because there is no Light
[simgear.git] / simgear / scene / sky / clouds3d / vec4f.hpp
1 //-----------------------------------------------------------------------------
2 // File : vec4f.hpp
3 //-----------------------------------------------------------------------------
4 // GLVU : Copyright 1997 - 2002 
5 //        The University of North Carolina at Chapel Hill
6 //-----------------------------------------------------------------------------
7 // Permission to use, copy, modify, distribute and sell this software
8 // and its documentation for any purpose is hereby granted without
9 // fee, provided that the above copyright notice appear in all copies
10 // and that both that copyright notice and this permission notice
11 // appear in supporting documentation.  Binaries may be compiled with
12 // this software without any royalties or restrictions.
13 //
14 // The University of North Carolina at Chapel Hill makes no representations 
15 // about the suitability of this software for any purpose. It is provided 
16 // "as is" without express or implied warranty.
17
18 //==========================================================================
19 // vec4.hpp : 4d vector class template. Works for any integer or real type.
20 //==========================================================================
21
22 #ifndef VEC4_H
23 #define VEC4_H
24
25 #include <stdio.h>
26 #include <math.h>
27
28 template <class Type>
29 class Vec4
30 {
31   public:
32     Type x, y, z, w;
33
34     Vec4 (void) 
35       {};
36     Vec4 (const Type X, const Type Y, const Type Z, const Type W) 
37       { x=X; y=Y; z=Z; w=W; };
38     Vec4 (const Vec4& v) 
39       { x=v.x; y=v.y; z=v.z; w=v.w; };
40     Vec4 (const Type v[4])
41       { x=v[0]; y=v[1]; z=v[2]; w=v[3]; };
42     void Set (const Type X, const Type Y, const Type Z, const Type W)
43       { x=X; y=Y; z=Z; w=W; }
44     void Set (const Type v[4])
45       { x=v[0]; y=v[1]; z=v[2]; w=v[3]; };
46
47     operator Type*()                             // Type * CONVERSION
48       { return (Type *)&x; }
49     operator const Type*() const                 // CONST Type * CONVERSION
50       { return &x; }
51
52     Vec4& operator = (const Vec4& A)            // ASSIGNMENT (=)
53       { x=A.x; y=A.y; z=A.z; w=A.w; 
54         return(*this);  };
55
56     bool operator == (const Vec4& A) const        // COMPARISON (==)
57       { return (x==A.x && y==A.y && 
58         z==A.z && w==A.w); }
59     bool operator != (const Vec4& A) const        // COMPARISON (!=)
60       { return (x!=A.x || y!=A.y || 
61         z!=A.z || w!=A.w); }
62
63     Vec4 operator + (const Vec4& A) const       // ADDITION (+)
64       { Vec4 Sum(x+A.x, y+A.y, z+A.z, w+A.w); 
65         return(Sum); };
66     Vec4 operator - (const Vec4& A) const       // SUBTRACTION (-)
67       { Vec4 Diff(x-A.x, y-A.y, z-A.z, w-A.w);
68         return(Diff); };
69     Type operator * (const Vec4& A) const       // DOT-PRODUCT (*)
70       { Type DotProd = x*A.x+y*A.y+z*A.z+w*A.w; 
71         return(DotProd); };
72     Vec4 operator * (const Type s) const        // MULTIPLY BY SCALAR (*)
73       { Vec4 Scaled(x*s, y*s, z*s, w*s); 
74         return(Scaled); };
75     Vec4 operator / (const Type s) const        // DIVIDE BY SCALAR (/)
76       { Vec4 Scaled(x/s, y/s, z/s, w/s);
77         return(Scaled); };
78     Vec4 operator & (const Vec4& A) const       // COMPONENT MULTIPLY (&)
79       { Vec4 CompMult(x*A.x, y*A.y, z*A.z, w*A.w);
80         return(CompMult); }
81
82     friend inline Vec4 operator *(Type s, const Vec4& v)  // SCALAR MULT s*V
83       { return Vec4(v.x*s, v.y*s, v.z*s, v.w*s); }
84
85     Vec4& operator += (const Vec4& A)      // ACCUMULATED VECTOR ADDITION (+=)
86       { x+=A.x; y+=A.y; z+=A.z; w+=A.w; 
87         return *this; }
88     Vec4& operator -= (const Vec4& A)      // ACCUMULATED VECTOR SUBTRCT (-=)
89       { x-=A.x; y-=A.y; z-=A.z; w-=A.w;
90         return *this; }
91     Vec4& operator *= (const Type s)       // ACCUMULATED SCALAR MULT (*=)
92       { x*=s; y*=s; z*=s; w*=s; 
93         return *this; }
94     Vec4& operator /= (const Type s)       // ACCUMULATED SCALAR DIV (/=)
95       { x/=s; y/=s; z/=s; w/=s;
96         return *this; }
97     Vec4& operator &= (const Vec4& A)  // ACCUMULATED COMPONENT MULTIPLY (&=)
98       { x*=A.x; y*=A.y; z*=A.z; w*=A.w; return *this; }
99     Vec4 operator - (void) const          // NEGATION (-)
100       { Vec4 Negated(-x, -y, -z, -w);
101         return(Negated); };
102
103 /*
104     const Type& operator [] (const int i) const // ALLOWS VECTOR ACCESS AS AN ARRAY.
105       { return( (i==0)?x:((i==1)?y:((i==2)?z:w)) ); };
106     Type & operator [] (const int i)
107       { return( (i==0)?x:((i==1)?y:((i==2)?z:w)) ); };
108 */
109
110     Type Length (void) const                     // LENGTH OF VECTOR
111       { return ((Type)sqrt(x*x+y*y+z*z+w*w)); };
112     Type LengthSqr (void) const                  // LENGTH OF VECTOR (SQUARED)
113       { return (x*x+y*y+z*z+w*w); };
114     Vec4& Normalize (void)                       // NORMALIZE VECTOR
115       { Type L = Length();                       // CALCULATE LENGTH
116         if (L>0) { x/=L; y/=L; z/=L; w/=L; }
117         return *this;
118       };                                          // DIV COMPONENTS BY LENGTH
119
120     void Wdiv(void)
121       { x/=w; y/=w; z/=w; w=1; }
122
123     void Print() const
124       { printf("(%.3f, %.3f, %.3f, %.3f)\n",x, y, z, w); }
125
126     static Vec4 ZERO;
127 };
128
129 typedef Vec4<float> Vec4f;
130 typedef Vec4<double> Vec4d;
131
132 template<class Type> Vec4<Type> Vec4<Type>::ZERO = Vec4<Type>(0,0,0,0);
133
134 #endif
135
136