Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

RageTypes.h

Go to the documentation of this file.
00001 /* 00002 * RageTypes - vector and matrix types 00003 */ 00004 00005 #ifndef RAGETYPES_H 00006 #define RAGETYPES_H 00007 00008 enum BlendMode { BLEND_NORMAL, BLEND_ADD, BLEND_NO_EFFECT, BLEND_INVALID }; 00009 enum CullMode { CULL_BACK, CULL_FRONT, CULL_NONE }; 00010 enum ZTestMode { ZTEST_OFF, ZTEST_WRITE_ON_PASS, ZTEST_WRITE_ON_FAIL }; 00011 enum PolygonMode { POLYGON_FILL, POLYGON_LINE }; 00012 00013 00014 struct RageVector2 00015 { 00016 public: 00017 RageVector2() {} 00018 RageVector2( const float * f ) { x=f[0]; y=f[1]; } 00019 RageVector2( float x1, float y1 ) { x=x1; y=y1; } 00020 00021 // casting 00022 operator float* () { return &x; }; 00023 operator const float* () const { return &x; }; 00024 00025 // assignment operators 00026 RageVector2& operator += ( const RageVector2& other ) { x+=other.x; y+=other.y; return *this; } 00027 RageVector2& operator -= ( const RageVector2& other ) { x-=other.x; y-=other.y; return *this; } 00028 RageVector2& operator *= ( float f ) { x*=f; y*=f; return *this; } 00029 RageVector2& operator /= ( float f ) { x/=f; y/=f; return *this; } 00030 00031 // binary operators 00032 RageVector2 operator + ( const RageVector2& other ) const { return RageVector2( x+other.x, y+other.y ); } 00033 RageVector2 operator - ( const RageVector2& other ) const { return RageVector2( x-other.x, y-other.y ); } 00034 RageVector2 operator * ( float f ) const { return RageVector2( x*f, y*f ); } 00035 RageVector2 operator / ( float f ) const { return RageVector2( x/f, y/f ); } 00036 00037 friend RageVector2 operator * ( float f, const RageVector2& other ) { return other*f; } 00038 00039 float x, y; 00040 }; 00041 00042 00043 struct RageVector3 00044 { 00045 public: 00046 RageVector3() {} 00047 RageVector3( const float * f ) { x=f[0]; y=f[1]; z=f[2]; } 00048 RageVector3( float x1, float y1, float z1 ) { x=x1; y=y1; z=z1; } 00049 00050 // casting 00051 operator float* () { return &x; }; 00052 operator const float* () const { return &x; }; 00053 00054 // assignment operators 00055 RageVector3& operator += ( const RageVector3& other ) { x+=other.x; y+=other.y; z+=other.z; return *this; } 00056 RageVector3& operator -= ( const RageVector3& other ) { x-=other.x; y-=other.y; z-=other.z; return *this; } 00057 RageVector3& operator *= ( float f ) { x*=f; y*=f; z*=f; return *this; } 00058 RageVector3& operator /= ( float f ) { x/=f; y/=f; z/=f; return *this; } 00059 00060 // binary operators 00061 RageVector3 operator + ( const RageVector3& other ) const { return RageVector3( x+other.x, y+other.y, z+other.z ); } 00062 RageVector3 operator - ( const RageVector3& other ) const { return RageVector3( x-other.x, y-other.y, z-other.z ); } 00063 RageVector3 operator * ( float f ) const { return RageVector3( x*f, y*f, z*f ); } 00064 RageVector3 operator / ( float f ) const { return RageVector3( x/f, y/f, z/f ); } 00065 00066 friend RageVector3 operator * ( float f, const RageVector3& other ) { return other*f; } 00067 00068 float x, y, z; 00069 }; 00070 00071 00072 struct RageVector4 00073 { 00074 public: 00075 RageVector4() {} 00076 RageVector4( const float * f ) { x=f[0]; y=f[1]; z=f[2]; w=f[3]; } 00077 RageVector4( float x1, float y1, float z1, float w1 ) { x=x1; y=y1; z=z1; w=w1; } 00078 00079 // casting 00080 operator float* () { return &x; }; 00081 operator const float* () const { return &x; }; 00082 00083 // assignment operators 00084 RageVector4& operator += ( const RageVector4& other ) { x+=other.x; y+=other.y; z+=other.z; w+=other.w; return *this; } 00085 RageVector4& operator -= ( const RageVector4& other ) { x-=other.x; y-=other.y; z-=other.z; w-=other.w; return *this; } 00086 RageVector4& operator *= ( float f ) { x*=f; y*=f; z*=f; w*=f; return *this; } 00087 RageVector4& operator /= ( float f ) { x/=f; y/=f; z/=f; w/=f; return *this; } 00088 00089 // binary operators 00090 RageVector4 operator + ( const RageVector4& other ) const { return RageVector4( x+other.x, y+other.y, z+other.z, w+other.w ); } 00091 RageVector4 operator - ( const RageVector4& other ) const { return RageVector4( x-other.x, y-other.y, z-other.z, w-other.w ); } 00092 RageVector4 operator * ( float f ) const { return RageVector4( x*f, y*f, z*f, w*f ); } 00093 RageVector4 operator / ( float f ) const { return RageVector4( x/f, y/f, z/f, w/f ); } 00094 00095 friend RageVector4 operator * ( float f, const RageVector4& other ) { return other*f; } 00096 00097 float x, y, z, w; 00098 } ALIGN(16); 00099 00100 struct RageColor 00101 { 00102 public: 00103 RageColor() {} 00104 RageColor( const float * f ) { r=f[0]; g=f[1]; b=f[2]; a=f[3]; } 00105 RageColor( float r1, float g1, float b1, float a1 ) { r=r1; g=g1; b=b1; a=a1; } 00106 00107 // casting 00108 operator float* () { return &r; }; 00109 operator const float* () const { return &r; }; 00110 00111 // assignment operators 00112 RageColor& operator += ( const RageColor& other ) { r+=other.r; g+=other.g; b+=other.b; a+=other.a; return *this; } 00113 RageColor& operator -= ( const RageColor& other ) { r-=other.r; g-=other.g; b-=other.b; a-=other.a; return *this; } 00114 RageColor& operator *= ( const RageColor& other ) { r*=other.r; g*=other.g; b*=other.b; a*=other.a; return *this; } 00115 RageColor& operator *= ( float f ) { r*=f; g*=f; b*=f; a*=f; return *this; } 00116 /* Divide is rarely useful: you can always use multiplication, and you don't have to 00117 * worry about div/0. */ 00118 // RageColor& operator /= ( float f ) { r/=f; g/=f; b/=f; a/=f; return *this; } 00119 00120 // binary operators 00121 RageColor operator + ( const RageColor& other ) const { return RageColor( r+other.r, g+other.g, b+other.b, a+other.a ); } 00122 RageColor operator - ( const RageColor& other ) const { return RageColor( r-other.r, g-other.g, b-other.b, a-other.a ); } 00123 RageColor operator * ( const RageColor& other ) const { return RageColor( r*other.r, g*other.g, b*other.b, a*other.a ); } 00124 RageColor operator * ( float f ) const { return RageColor( r*f, g*f, b*f, a*f ); } 00125 // Divide is useful for using with the SCALE macro 00126 RageColor operator / ( float f ) const { return RageColor( r/f, g/f, b/f, a/f ); } 00127 00128 friend RageColor operator * ( float f, const RageColor& other ) { return other*f; } // What is this for? Did I add this? -Chris 00129 00130 bool FromString( const CString &str ) 00131 { 00132 int result = sscanf( str, "%f,%f,%f,%f", &r, &g, &b, &a ); 00133 if( result == 3 ) 00134 { 00135 a = 1; 00136 return true; 00137 } 00138 if( result == 4 ) 00139 return true; 00140 00141 int ir=255, ib=255, ig=255, ia=255; 00142 result = sscanf( str, "#%2x%2x%2x%2x", &ir, &ig, &ib, &ia ); 00143 if( result >= 3 ) 00144 { 00145 r = ir / 255.0f; g = ig / 255.0f; b = ib / 255.0f; 00146 if( result == 4 ) 00147 a = ia / 255.0f; 00148 else 00149 a = 1; 00150 return true; 00151 } 00152 00153 r=1; b=1; g=1; a=1; 00154 return false; 00155 } 00156 float r, g, b, a; 00157 } ALIGN(16); 00158 00159 /* Convert floating-point 0..1 value to integer 0..255 value. * 00160 * 00161 * As a test case, 00162 * 00163 * int cnts[1000]; memset(cnts, 0, sizeof(cnts)); 00164 * for( float n = 0; n <= 1.0; n += 0.0001 ) cnts[FTOC(n)]++; 00165 * for( int i = 0; i < 256; ++i ) printf("%i ", cnts[i]); 00166 * 00167 * should output the same value (+-1) 256 times. If this function is 00168 * incorrect, the first and/or last values may be biased. */ 00169 inline unsigned char FTOC(float a) 00170 { 00171 /* lfintf is much faster than C casts. We don't care which way negative values 00172 * are rounded, since we'll clamp them to zero below. Be sure to truncate (not 00173 * round) positive values. The highest value that should be converted to 1 is 00174 * roughly (1/256 - 0.00001); if we don't truncate, values up to (1/256 + 0.5) 00175 * will be converted to 1, which is wrong. */ 00176 int ret = lrintf(a*256.f - 0.5f); 00177 00178 /* Benchmarking shows that clamping here, as integers, is much faster than clamping 00179 * before the conversion, as floats. */ 00180 if( ret<0 ) return 0; 00181 else if( ret>255 ) return 255; 00182 else return (unsigned char) ret; 00183 } 00184 00185 /* Color type used only in vertex lists. OpenGL expects colors in 00186 * r, g, b, a order, independent of endianness, so storing them this 00187 * way avoids endianness problems. Don't try to manipulate this; only 00188 * manip RageColors. */ 00189 /* Perhaps the math in RageColor could be moved to RaveVColor. We don't need the 00190 * precision of a float for our calculations anyway. -Chris */ 00191 class RageVColor 00192 { 00193 public: 00194 uint8_t b,g,r,a; // specific ordering required by Direct3D 00195 00196 RageVColor() { } 00197 RageVColor(const RageColor &rc) { *this = rc; } 00198 RageVColor &operator= (const RageColor &rc) { 00199 r = FTOC(rc.r); g = FTOC(rc.g); b = FTOC(rc.b); a = FTOC(rc.a); 00200 return *this; 00201 } 00202 }; 00203 00204 00205 template <class T> 00206 class Rect 00207 { 00208 public: 00209 Rect() {}; 00210 Rect(T l, T t, T r, T b) { left = l, top = t, right = r, bottom = b; }; 00211 00212 T GetWidth() const { return right-left; }; 00213 T GetHeight() const { return bottom-top; }; 00214 T GetCenterX() const { return (left+right)/2; }; 00215 T GetCenterY() const { return (top+bottom)/2; }; 00216 00217 T left, top, right, bottom; 00218 }; 00219 00220 typedef Rect<int> RectI; 00221 typedef Rect<float> RectF; 00222 00223 /* Structure for our custom vertex type. Note that these data structes 00224 * have the same layout that D3D expects. */ 00225 struct RageSpriteVertex // has color 00226 { 00227 RageVector3 p; // position 00228 RageVector3 n; // normal 00229 RageVColor c; // diffuse color 00230 RageVector2 t; // texture coordinates 00231 }; 00232 00233 00234 struct RageModelVertex // doesn't have color. Relies on material color 00235 { 00236 /* Zero out by default. */ 00237 RageModelVertex(): 00238 p(0,0,0), 00239 n(0,0,0), 00240 t(0,0) 00241 { } 00242 RageVector3 p; // position 00243 RageVector3 n; // normal 00244 RageVector2 t; // texture coordinates 00245 int8_t bone; 00246 }; 00247 00248 00249 // RageMatrix elements are specified in row-major order. This 00250 // means that the translate terms are located in the fourth row and the 00251 // projection terms in the fourth column. This is consistent with the way 00252 // MAX, Direct3D, and OpenGL all handle matrices. Even though the OpenGL 00253 // documentation is in column-major form, the OpenGL code is designed to 00254 // handle matrix operations in row-major form. 00255 struct RageMatrix 00256 { 00257 public: 00258 RageMatrix() {}; 00259 RageMatrix( const float *f ) { for(int i=0; i<4; i++) for(int j=0; j<4; j++) m[j][i]=f[j*4+i]; } 00260 RageMatrix( const RageMatrix& other ) { for(int i=0; i<4; i++) for(int j=0; j<4; j++) m[j][i]=other.m[j][i]; } 00261 RageMatrix( float v00, float v01, float v02, float v03, 00262 float v10, float v11, float v12, float v13, 00263 float v20, float v21, float v22, float v23, 00264 float v30, float v31, float v32, float v33 ); 00265 00266 // access grants 00267 float& operator () ( int iRow, int iCol ) { return m[iCol][iRow]; } 00268 float operator () ( int iRow, int iCol ) const { return m[iCol][iRow]; } 00269 00270 // casting operators 00271 operator float* () { return m[0]; } 00272 operator const float* () const { return m[0]; } 00273 00274 RageMatrix GetTranspose() const; 00275 00276 float m[4][4]; 00277 } ALIGN(16); 00278 00279 RageColor scale( float x, float l1, float h1, const RageColor &a, const RageColor &b ); 00280 00281 #endif 00282 00283 /* 00284 * Copyright (c) 2001-2002 Chris Danford 00285 * All rights reserved. 00286 * 00287 * Permission is hereby granted, free of charge, to any person obtaining a 00288 * copy of this software and associated documentation files (the 00289 * "Software"), to deal in the Software without restriction, including 00290 * without limitation the rights to use, copy, modify, merge, publish, 00291 * distribute, and/or sell copies of the Software, and to permit persons to 00292 * whom the Software is furnished to do so, provided that the above 00293 * copyright notice(s) and this permission notice appear in all copies of 00294 * the Software and that both the above copyright notice(s) and this 00295 * permission notice appear in supporting documentation. 00296 * 00297 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00298 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00299 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00300 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00301 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00302 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00303 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00304 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00305 * PERFORMANCE OF THIS SOFTWARE. 00306 */

Generated on Thu Jan 27 20:57:30 2005 for StepMania by doxygen 1.3.7