Vector.h (35354B)
1/* 2Oolong Engine for the iPhone / iPod touch 3Copyright (c) 2007-2008 Wolfgang Engel http://code.google.com/p/oolongengine/ 4 5This software is provided 'as-is', without any express or implied warranty. 6In no event will the authors be held liable for any damages arising from the use of this software. 7Permission is granted to anyone to use this software for any purpose, 8including commercial applications, and to alter it and redistribute it freely, 9subject to the following restrictions: 10 111. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 133. This notice may not be removed or altered from any source distribution. 14*/ 15#ifndef VECTOR_H_ 16#define VECTOR_H_ 17 18#include <OpenGLES/ES1/gl.h> 19#include <math.h> 20 21//#include "Matrix.h" 22 23 24#define VERTTYPE GLfloat 25#define VERTTYPEENUM GL_FLOAT 26 27// Floating-point operations 28#define VERTTYPEMUL(a,b) ( (VERTTYPE)((a)*(b)) ) 29#define VERTTYPEDIV(a,b) ( (VERTTYPE)((a)/(b)) ) 30#define VERTTYPEABS(a) ( (VERTTYPE)(fabs(a)) ) 31 32#define f2vt(x) (x) 33#define vt2f(x) (x) 34 35#define PIOVERTWO PIOVERTWOf 36#define PI PIf 37#define TWOPI TWOPIf 38#define ONE ONEf 39 40#define X2F(x) ((float)(x)/65536.0f) 41#define XMUL(a,b) ( (int)( ((INT64BIT)(a)*(b)) / 65536 ) ) 42#define XDIV(a,b) ( (int)( (((INT64BIT)(a))<<16)/(b) ) ) 43#define _ABS(a) ((a) <= 0 ? -(a) : (a) ) 44 45 46// Define a 64-bit type for various platforms 47#if defined(__int64) || defined(WIN32) 48#define INT64BIT __int64 49#elif defined(TInt64) 50#define INT64BIT TInt64 51#else 52#define INT64BIT long long int 53#endif 54 55typedef struct _LARGE_INTEGER 56 { 57 union 58 { 59 struct 60 { 61 unsigned long LowPart; 62 long HighPart; 63 }; 64 INT64BIT QuadPart; 65 }; 66 } LARGE_INTEGER, *PLARGE_INTEGER; 67 68/**************************************************************************** 69 ** Typedefs 70 ****************************************************************************/ 71/*!*************************************************************************** 72 2D floating point vector 73 *****************************************************************************/ 74typedef struct 75 { 76 float x; /*!< x coordinate */ 77 float y; /*!< y coordinate */ 78 } VECTOR2; 79 80 81/*!*************************************************************************** 82 3D floating point vector 83 *****************************************************************************/ 84typedef struct 85 { 86 float x; /*!< x coordinate */ 87 float y; /*!< y coordinate */ 88 float z; /*!< z coordinate */ 89 } VECTOR3; 90 91 92/*!*************************************************************************** 93 4D floating point vector 94 *****************************************************************************/ 95typedef struct 96 { 97 float x; /*!< x coordinate */ 98 float y; /*!< y coordinate */ 99 float z; /*!< z coordinate */ 100 float w; /*!< w coordinate */ 101 } VECTOR4; 102 103/*!*************************************************************************** 104** Forward Declarations for vector and matrix structs 105****************************************************************************/ 106//struct VECTOR4; 107//struct VECTOR3; 108 109/*!*************************************************************************** 110 ** Vec2 2 component vector 111 ****************************************************************************/ 112struct Vec2 113{ 114 VERTTYPE x, y; 115 /*!*************************************************************************** 116 ** Constructors 117 ****************************************************************************/ 118 /*!*************************************************************************** 119 @Function Vec2 120 @Description Blank constructor. 121 *****************************************************************************/ 122 Vec2() {} 123 /*!*************************************************************************** 124 @Function Vec2 125 @Input fX X component of vector 126 @Input fY Y component of vector 127 @Description Simple constructor from 2 values. 128 *****************************************************************************/ 129 Vec2(VERTTYPE fX, VERTTYPE fY) : x(fX), y(fY) {} 130 /*!*************************************************************************** 131 @Function Vec2 132 @Input fValue a component value 133 @Description Constructor from a single value. 134 *****************************************************************************/ 135 Vec2(VERTTYPE fValue) : x(fValue), y(fValue) {} 136 /*!*************************************************************************** 137 @Function Vec2 138 @Input pVec an array 139 @Description Constructor from an array 140 *****************************************************************************/ 141 Vec2(const VERTTYPE* pVec) : x(pVec[0]), y(pVec[1]) {} 142 /*!*************************************************************************** 143 @Function Vec2 144 @Input v3Vec a Vec3 145 @Description Constructor from a Vec3 146 *****************************************************************************/ 147// Vec2(const Vec3& v3Vec); 148 /*!*************************************************************************** 149 ** Operators 150 ****************************************************************************/ 151 /*!*************************************************************************** 152 @Function + 153 @Input rhs another Vec2 154 @Returns result of addition 155 @Description componentwise addition operator for two Vec2s 156 *****************************************************************************/ 157 Vec2 operator+(const Vec2& rhs) const 158 { 159 Vec2 out(*this); 160 return out += rhs; 161 } 162 /*!*************************************************************************** 163 @Function - 164 @Input rhs another Vec2 165 @Returns result of subtraction 166 @Description componentwise subtraction operator for two Vec2s 167 ****************************************************************************/ 168 Vec2 operator-(const Vec2& rhs) const 169 { 170 Vec2 out(*this); 171 return out -= rhs; 172 } 173 174 /*!*************************************************************************** 175 @Function += 176 @Input rhs another Vec2 177 @Returns result of addition 178 @Description componentwise addition and assignment operator for two Vec2s 179 ****************************************************************************/ 180 Vec2& operator+=(const Vec2& rhs) 181 { 182 x += rhs.x; 183 y += rhs.y; 184 return *this; 185 } 186 187 /*!*************************************************************************** 188 @Function -= 189 @Input rhs another Vec2 190 @Returns result of subtraction 191 @Description componentwise subtraction and assignment operator for two Vec2s 192 ****************************************************************************/ 193 Vec2& operator-=(const Vec2& rhs) 194 { 195 x -= rhs.x; 196 y -= rhs.y; 197 return *this; 198 } 199 200 /*!*************************************************************************** 201 @Function - 202 @Input rhs another Vec2 203 @Returns result of negation 204 @Description negation operator for a Vec2 205 ****************************************************************************/ 206 friend Vec2 operator- (const Vec2& rhs) { return Vec2(-rhs.x, -rhs.y); } 207 208 /*!*************************************************************************** 209 @Function * 210 @Input lhs scalar 211 @Input rhs a Vec2 212 @Returns result of negation 213 @Description negation operator for a Vec2 214 ****************************************************************************/ 215 friend Vec2 operator*(const VERTTYPE lhs, const Vec2& rhs) 216 { 217 Vec2 out(lhs); 218 return out *= rhs; 219 } 220 221 /*!*************************************************************************** 222 @Function / 223 @Input lhs scalar 224 @Input rhs a Vec2 225 @Returns result of division 226 @Description division operator for scalar and Vec2 227 ****************************************************************************/ 228 friend Vec2 operator/(const VERTTYPE lhs, const Vec2& rhs) 229 { 230 Vec2 out(lhs); 231 return out /= rhs; 232 } 233 234 /*!*************************************************************************** 235 @Function * 236 @Input rhs a scalar 237 @Returns result of multiplication 238 @Description componentwise multiplication by scalar for Vec2 239 ****************************************************************************/ 240 Vec2 operator*(const VERTTYPE& rhs) const 241 { 242 Vec2 out(*this); 243 return out *= rhs; 244 } 245 246 /*!*************************************************************************** 247 @Function *= 248 @Input rhs a scalar 249 @Returns result of multiplication and assignment 250 @Description componentwise multiplication and assignment by scalar for Vec2 251 ****************************************************************************/ 252 Vec2& operator*=(const VERTTYPE& rhs) 253 { 254 x = VERTTYPEMUL(x, rhs); 255 y = VERTTYPEMUL(y, rhs); 256 return *this; 257 } 258 259 /*!*************************************************************************** 260 @Function *= 261 @Input rhs a Vec2 262 @Returns result of multiplication and assignment 263 @Description componentwise multiplication and assignment by Vec2 for Vec2 264 ****************************************************************************/ 265 Vec2& operator*=(const Vec2& rhs) 266 { 267 x = VERTTYPEMUL(x, rhs.x); 268 y = VERTTYPEMUL(y, rhs.y); 269 return *this; 270 } 271 272 /*!*************************************************************************** 273 @Function / 274 @Input rhs a scalar 275 @Returns result of division 276 @Description componentwise division by scalar for Vec2 277 ****************************************************************************/ 278 Vec2 operator/(const VERTTYPE& rhs) const 279 { 280 Vec2 out(*this); 281 return out /= rhs; 282 } 283 284 /*!*************************************************************************** 285 @Function /= 286 @Input rhs a scalar 287 @Returns result of division and assignment 288 @Description componentwise division and assignment by scalar for Vec2 289 ****************************************************************************/ 290 Vec2& operator/=(const VERTTYPE& rhs) 291 { 292 x = VERTTYPEDIV(x, rhs); 293 y = VERTTYPEDIV(y, rhs); 294 return *this; 295 } 296 297 /*!*************************************************************************** 298 @Function /= 299 @Input rhs a Vec2 300 @Returns result of division and assignment 301 @Description componentwise division and assignment by Vec2 for Vec2 302 ****************************************************************************/ 303 Vec2& operator/=(const Vec2& rhs) 304 { 305 x = VERTTYPEDIV(x, rhs.x); 306 y = VERTTYPEDIV(y, rhs.y); 307 return *this; 308 } 309 // FUNCTIONS 310 /*!*************************************************************************** 311 @Function lenSqr 312 @Returns the square of the magnitude of the vector 313 @Description calculates the square of the magnitude of the vector 314 ****************************************************************************/ 315 VERTTYPE lenSqr() const 316 { 317 return VERTTYPEMUL(x,x)+VERTTYPEMUL(y,y); 318 } 319 320 /*!*************************************************************************** 321 @Function length 322 @Returns the of the magnitude of the vector 323 @Description calculates the magnitude of the vector 324 ****************************************************************************/ 325 VERTTYPE length() const 326 { 327 return (VERTTYPE) f2vt(sqrt(vt2f(x)*vt2f(x) + vt2f(y)*vt2f(y))); 328 } 329 330 /*!*************************************************************************** 331 @Function normalize 332 @Returns the normalized value of the vector 333 @Description normalizes the vector 334 ****************************************************************************/ 335 Vec2 normalize() 336 { 337 return *this /= length(); 338 } 339 340 /*!*************************************************************************** 341 @Function normalized 342 @Returns returns the normalized value of the vector 343 @Description returns a normalized vector of the same direction as this 344 vector 345 ****************************************************************************/ 346 Vec2 normalized() const 347 { 348 Vec2 out(*this); 349 return out.normalize(); 350 } 351 352 /*!*************************************************************************** 353 @Function rotated90 354 @Returns returns the vector rotated 90ᄚ 355 @Description returns the vector rotated 90ᄚ 356 ****************************************************************************/ 357 Vec2 rotated90() const 358 { 359 return Vec2(-y, x); 360 } 361 362 /*!*************************************************************************** 363 @Function dot 364 @Input rhs a single value 365 @Returns scalar product 366 @Description calculate the scalar product of two Vec3s 367 ****************************************************************************/ 368 VERTTYPE dot(const Vec2& rhs) 369 { 370 return VERTTYPEMUL(x, rhs.x) + VERTTYPEMUL(y, rhs.y); 371 } 372 373 /*!*************************************************************************** 374 @Function ptr 375 @Returns pointer 376 @Description returns a pointer to memory containing the values of the 377 Vec3 378 ****************************************************************************/ 379 VERTTYPE *ptr() { return (VERTTYPE*)this; } 380}; 381 382/*!*************************************************************************** 383** Vec3 3 component vector 384****************************************************************************/ 385struct Vec3 : public VECTOR3 386{ 387/*!*************************************************************************** 388** Constructors 389****************************************************************************/ 390/*!*************************************************************************** 391 @Function Vec3 392 @Description Blank constructor. 393*****************************************************************************/ 394 Vec3(){} 395/*!*************************************************************************** 396 @Function Vec3 397 @Input fX X component of vector 398 @Input fY Y component of vector 399 @Input fZ Z component of vector 400 @Description Simple constructor from 3 values. 401*****************************************************************************/ 402 Vec3(VERTTYPE fX, VERTTYPE fY, VERTTYPE fZ) 403 { 404 x = fX; y = fY; z = fZ; 405 } 406/*!*************************************************************************** 407 @Function Vec3 408 @Input fValue a component value 409 @Description Constructor from a single value. 410*****************************************************************************/ 411 Vec3(const VERTTYPE fValue) 412 { 413 x = fValue; y = fValue; z = fValue; 414 } 415/*!*************************************************************************** 416 @Function Vec3 417 @Input pVec an array 418 @Description Constructor from an array 419*****************************************************************************/ 420 Vec3(const VERTTYPE* pVec) 421 { 422 x = (*pVec++); y = (*pVec++); z = *pVec; 423 } 424/*!*************************************************************************** 425 @Function Vec3 426 @Input v4Vec a Vec4 427 @Description Constructor from a Vec4 428*****************************************************************************/ 429// Vec3(const Vec4& v4Vec); 430/*!*************************************************************************** 431** Operators 432****************************************************************************/ 433/*!*************************************************************************** 434 @Function + 435 @Input rhs another Vec3 436 @Returns result of addition 437 @Description componentwise addition operator for two VECTOR3s 438*****************************************************************************/ 439 Vec3 operator+(const Vec3& rhs) const 440 { 441 Vec3 out; 442 out.x = x+rhs.x; 443 out.y = y+rhs.y; 444 out.z = z+rhs.z; 445 return out; 446 } 447/*!*************************************************************************** 448 @Function - 449 @Input rhs another Vec3 450 @Returns result of subtraction 451 @Description componentwise subtraction operator for two VECTOR3s 452****************************************************************************/ 453 Vec3 operator-(const Vec3& rhs) const 454 { 455 Vec3 out; 456 out.x = x-rhs.x; 457 out.y = y-rhs.y; 458 out.z = z-rhs.z; 459 return out; 460 } 461 462/*!*************************************************************************** 463 @Function += 464 @Input rhs another Vec3 465 @Returns result of addition 466 @Description componentwise addition and assignement operator for two VECTOR3s 467****************************************************************************/ 468 Vec3& operator+=(const Vec3& rhs) 469 { 470 x +=rhs.x; 471 y +=rhs.y; 472 z +=rhs.z; 473 return *this; 474 } 475 476/*!*************************************************************************** 477 @Function -= 478 @Input rhs another Vec3 479 @Returns result of subtraction 480 @Description componentwise subtraction and assignement operator for two VECTOR3s 481****************************************************************************/ 482 Vec3& operator-=(const Vec3& rhs) 483 { 484 x -=rhs.x; 485 y -=rhs.y; 486 z -=rhs.z; 487 return *this; 488 } 489 490/*!*************************************************************************** 491 @Function - 492 @Input rhs another Vec3 493 @Returns result of negation 494 @Description negation operator for a Vec3 495****************************************************************************/ 496 friend Vec3 operator - (const Vec3& rhs) { return Vec3(rhs) *= f2vt(-1); } 497 498/*!*************************************************************************** 499 @Function * 500 @Input lhs single value 501 @Input rhs a Vec3 502 @Returns result of negation 503 @Description negation operator for a Vec3 504****************************************************************************/ 505 friend Vec3 operator*(const VERTTYPE lhs, const Vec3& rhs) 506 { 507 Vec3 out; 508 out.x = VERTTYPEMUL(lhs,rhs.x); 509 out.y = VERTTYPEMUL(lhs,rhs.y); 510 out.z = VERTTYPEMUL(lhs,rhs.z); 511 return out; 512 } 513 514/*!*************************************************************************** 515 @Function * 516 @Input lhs single value 517 @Input rhs a Vec3 518 @Returns result of negation 519 @Description negation operator for a Vec3 520****************************************************************************/ 521 friend Vec3 operator/(const VERTTYPE lhs, const Vec3& rhs) 522 { 523 Vec3 out; 524 out.x = VERTTYPEDIV(lhs,rhs.x); 525 out.y = VERTTYPEDIV(lhs,rhs.y); 526 out.z = VERTTYPEDIV(lhs,rhs.z); 527 return out; 528 } 529 530/*!*************************************************************************** 531 @Function * 532 @Input rhs a PVRTMat3 533 @Returns result of multiplication 534 @Description matrix multiplication operator Vec3 and PVRTMat3 535****************************************************************************/ 536// Vec3 operator*(const PVRTMat3& rhs) const; 537 538/*!*************************************************************************** 539 @Function *= 540 @Input rhs a PVRTMat3 541 @Returns result of multiplication and assignment 542 @Description matrix multiplication and assignment operator for Vec3 and PVRTMat3 543****************************************************************************/ 544// Vec3& operator*=(const PVRTMat3& rhs); 545 546/*!*************************************************************************** 547 @Function * 548 @Input rhs a single value 549 @Returns result of multiplication 550 @Description componentwise multiplication by single dimension value for Vec3 551****************************************************************************/ 552 Vec3 operator*(const VERTTYPE& rhs) const 553 { 554 Vec3 out; 555 out.x = VERTTYPEMUL(x,rhs); 556 out.y = VERTTYPEMUL(y,rhs); 557 out.z = VERTTYPEMUL(z,rhs); 558 return out; 559 } 560 561/*!*************************************************************************** 562 @Function * 563 @Input rhs a single value 564 @Returns result of multiplication and assignment 565 @Description componentwise multiplication and assignement by single 566 dimension value for Vec3 567****************************************************************************/ 568 Vec3& operator*=(const VERTTYPE& rhs) 569 { 570 x = VERTTYPEMUL(x,rhs); 571 y = VERTTYPEMUL(y,rhs); 572 z = VERTTYPEMUL(z,rhs); 573 return *this; 574 } 575 576/*!*************************************************************************** 577 @Function / 578 @Input rhs a single value 579 @Returns result of division 580 @Description componentwise division by single 581 dimension value for Vec3 582****************************************************************************/ 583 Vec3 operator/(const VERTTYPE& rhs) const 584 { 585 Vec3 out; 586 out.x = VERTTYPEDIV(x,rhs); 587 out.y = VERTTYPEDIV(y,rhs); 588 out.z = VERTTYPEDIV(z,rhs); 589 return out; 590 } 591 592/*!*************************************************************************** 593 @Function /= 594 @Input rhs a single value 595 @Returns result of division and assignment 596 @Description componentwise division and assignement by single 597 dimension value for Vec3 598****************************************************************************/ 599 Vec3& operator/=(const VERTTYPE& rhs) 600 { 601 x = VERTTYPEDIV(x,rhs); 602 y = VERTTYPEDIV(y,rhs); 603 z = VERTTYPEDIV(z,rhs); 604 return *this; 605 } 606 607 // FUNCTIONS 608/*!*************************************************************************** 609 @Function lenSqr 610 @Returns the square of the magnitude of the vector 611 @Description calculates the square of the magnitude of the vector 612****************************************************************************/ 613 VERTTYPE lenSqr() const 614 { 615 return VERTTYPEMUL(x,x)+VERTTYPEMUL(y,y)+VERTTYPEMUL(z,z); 616 } 617 618/*!*************************************************************************** 619 @Function length 620 @Returns the of the magnitude of the vector 621 @Description calculates the magnitude of the vector 622****************************************************************************/ 623 VERTTYPE length() const 624 { 625 return (VERTTYPE) f2vt(sqrt(vt2f(x)*vt2f(x) + vt2f(y)*vt2f(y) + vt2f(z)*vt2f(z))); 626 } 627 628/*!*************************************************************************** 629 @Function normalize 630 @Returns the normalized value of the vector 631 @Description normalizes the vector 632****************************************************************************/ 633 Vec3 normalize() 634 { 635#if defined(PVRT_FIXED_POINT_ENABLE) 636 // Scale vector by uniform value 637 int n = PVRTABS(x) + PVRTABS(y) + PVRTABS(z); 638 x = VERTTYPEDIV(x, n); 639 y = VERTTYPEDIV(y, n); 640 z = VERTTYPEDIV(z, n); 641 642 // Calculate x2+y2+z2/sqrt(x2+y2+z2) 643 int f = dot(*this); 644 f = VERTTYPEDIV(PVRTF2X(1.0f), PVRTF2X(sqrt(PVRTX2F(f)))); 645 646 // Multiply vector components by f 647 x = PVRTXMUL(x, f); 648 y = PVRTXMUL(y, f); 649 z = PVRTXMUL(z, f); 650#else 651 VERTTYPE len = length(); 652 x =VERTTYPEDIV(x,len); 653 y =VERTTYPEDIV(y,len); 654 z =VERTTYPEDIV(z,len); 655#endif 656 return *this; 657 } 658 659/*!*************************************************************************** 660 @Function normalized 661 @Returns returns the normalized value of the vector 662 @Description returns a normalized vector of the same direction as this 663 vector 664****************************************************************************/ 665 Vec3 normalized() const 666 { 667 Vec3 out; 668#if defined(PVRT_FIXED_POINT_ENABLE) 669 // Scale vector by uniform value 670 int n = PVRTABS(x) + PVRTABS(y) + PVRTABS(z); 671 out.x = VERTTYPEDIV(x, n); 672 out.y = VERTTYPEDIV(y, n); 673 out.z = VERTTYPEDIV(z, n); 674 675 // Calculate x2+y2+z2/sqrt(x2+y2+z2) 676 int f = out.dot(out); 677 f = VERTTYPEDIV(PVRTF2X(1.0f), PVRTF2X(sqrt(PVRTX2F(f)))); 678 679 // Multiply vector components by f 680 out.x = PVRTXMUL(out.x, f); 681 out.y = PVRTXMUL(out.y, f); 682 out.z = PVRTXMUL(out.z, f); 683#else 684 VERTTYPE len = length(); 685 out.x =VERTTYPEDIV(x,len); 686 out.y =VERTTYPEDIV(y,len); 687 out.z =VERTTYPEDIV(z,len); 688#endif 689 return out; 690 } 691 692/*!*************************************************************************** 693 @Function dot 694 @Input rhs a single value 695 @Returns scalar product 696 @Description calculate the scalar product of two VECTOR3s 697****************************************************************************/ 698 VERTTYPE dot(const Vec3& rhs) 699 { 700 return VERTTYPEMUL(x,rhs.x)+VERTTYPEMUL(y,rhs.y)+VERTTYPEMUL(z,rhs.z); 701 } 702 703/*!*************************************************************************** 704 @Function dot 705 @Returns scalar product 706 @Description calculate the scalar product of two VECTOR3s 707****************************************************************************/ 708 Vec3 cross(const Vec3& rhs) 709 { 710 Vec3 out; 711 out.x = VERTTYPEMUL(y,rhs.z)-VERTTYPEMUL(z,rhs.y); 712 out.y = VERTTYPEMUL(z,rhs.x)-VERTTYPEMUL(x,rhs.z); 713 out.z = VERTTYPEMUL(x,rhs.y)-VERTTYPEMUL(y,rhs.x); 714 return out; 715 } 716 717/*!*************************************************************************** 718 @Function ptr 719 @Returns pointer 720 @Description returns a pointer to memory containing the values of the 721 Vec3 722****************************************************************************/ 723 VERTTYPE *ptr() { return (VERTTYPE*)this; } 724}; 725 726/*!*************************************************************************** 727** Vec4 4 component vector 728****************************************************************************/ 729struct Vec4 : public VECTOR4 730{ 731/*!*************************************************************************** 732** Constructors 733****************************************************************************/ 734/*!*************************************************************************** 735 @Function Vec4 736 @Description Blank constructor. 737*****************************************************************************/ 738 Vec4(){} 739 740/*!*************************************************************************** 741 @Function Vec3 742 @Description Blank constructor. 743*****************************************************************************/ 744 Vec4(const VERTTYPE vec) 745 { 746 x = vec; y = vec; z = vec; w = vec; 747 } 748 749/*!*************************************************************************** 750 @Function multiple value constructor 751 @Input fX value of x component 752 @Input fY value of y component 753 @Input fZ value of z component 754 @Input fW value of w component 755 @Description Constructs a Vec4 from 4 separate values 756****************************************************************************/ 757 Vec4(VERTTYPE fX, VERTTYPE fY, VERTTYPE fZ, VERTTYPE fW) 758 { 759 x = fX; y = fY; z = fZ; w = fW; 760 } 761 762/*!*************************************************************************** 763 @Function constructor from Vec3 764 @Input vec3 a Vec3 765 @Input fW value of w component 766 @Description Constructs a Vec4 from a vec3 and a w component 767****************************************************************************/ 768 Vec4(const Vec3& vec3, VERTTYPE fW) 769 { 770 x = vec3.x; y = vec3.y; z = vec3.z; w = fW; 771 } 772 773/*!*************************************************************************** 774 @Function constructor from Vec3 775 @Input fX value of x component 776 @Input vec3 a Vec3 777 @Description Constructs a vec4 from a vec3 and a w component 778****************************************************************************/ 779 Vec4(VERTTYPE fX, const Vec3& vec3) 780 { 781 x = fX; y = vec3.x; z = vec3.y; w = vec3.z; 782 } 783 784/*!*************************************************************************** 785 @Function constructor from array 786 @Input pVec a pointer to an array of four values 787 @Description Constructs a Vec4 from a pointer to an array of four values 788****************************************************************************/ 789 Vec4(const VERTTYPE* pVec) 790 { 791 x = (*pVec++); y = (*pVec++); z= (*pVec++); w = *pVec; 792 } 793 794/*!*************************************************************************** 795** Vec4 Operators 796****************************************************************************/ 797/*!*************************************************************************** 798 @Function + 799 @Input rhs another Vec4 800 @Returns result of addition 801 @Description Addition operator for Vec4 802****************************************************************************/ 803 Vec4 operator+(const Vec4& rhs) const 804 { 805 Vec4 out; 806 out.x = x+rhs.x; 807 out.y = y+rhs.y; 808 out.z = z+rhs.z; 809 out.w = w+rhs.w; 810 return out; 811 } 812 813/*!*************************************************************************** 814 @Function - 815 @Input rhs another Vec4 816 @Returns result of subtraction 817 @Description Subtraction operator for Vec4 818****************************************************************************/ 819 Vec4 operator-(const Vec4& rhs) const 820 { 821 Vec4 out; 822 out.x = x-rhs.x; 823 out.y = y-rhs.y; 824 out.z = z-rhs.z; 825 out.w = w-rhs.w; 826 return out; 827 } 828 829/*!*************************************************************************** 830 @Function += 831 @Input rhs another Vec4 832 @Returns result of addition and assignment 833 @Description Addition and assignment operator for Vec4 834****************************************************************************/ 835 Vec4& operator+=(const Vec4& rhs) 836 { 837 x +=rhs.x; 838 y +=rhs.y; 839 z +=rhs.z; 840 w +=rhs.w; 841 return *this; 842 } 843 844/*!*************************************************************************** 845 @Function -= 846 @Input rhs another Vec4 847 @Returns result of subtraction and assignment 848 @Description Subtraction and assignment operator for Vec4 849****************************************************************************/ 850 Vec4& operator-=(const Vec4& rhs) 851 { 852 x -=rhs.x; 853 y -=rhs.y; 854 z -=rhs.z; 855 w -=rhs.w; 856 return *this; 857 } 858 859/*!*************************************************************************** 860 @Function * 861 @Input rhs a PVRTMat4 862 @Returns result of multiplication 863 @Description matrix multiplication for Vec4 and PVRTMat4 864****************************************************************************/ 865// Vec4 operator*(const MATRIX& rhs) const; 866 867/*!*************************************************************************** 868 @Function *= 869 @Input rhs a PVRTMat4 870 @Returns result of multiplication and assignement 871 @Description matrix multiplication and assignment for Vec4 and PVRTMat4 872****************************************************************************/ 873// Vec4& operator*=(const MATRIX& rhs); 874 875/*!*************************************************************************** 876 @Function * 877 @Input rhs a single dimension value 878 @Returns result of multiplication 879 @Description componentwise multiplication of a Vec4 by a single value 880****************************************************************************/ 881 Vec4 operator*(const VERTTYPE& rhs) const 882 { 883 Vec4 out; 884 out.x = VERTTYPEMUL(x,rhs); 885 out.y = VERTTYPEMUL(y,rhs); 886 out.z = VERTTYPEMUL(z,rhs); 887 out.w = VERTTYPEMUL(w,rhs); 888 return out; 889 } 890 891/*!*************************************************************************** 892 @Function *= 893 @Input rhs a single dimension value 894 @Returns result of multiplication and assignment 895 @Description componentwise multiplication and assignment of a Vec4 by 896 a single value 897****************************************************************************/ 898 Vec4& operator*=(const VERTTYPE& rhs) 899 { 900 x = VERTTYPEMUL(x,rhs); 901 y = VERTTYPEMUL(y,rhs); 902 z = VERTTYPEMUL(z,rhs); 903 w = VERTTYPEMUL(w,rhs); 904 return *this; 905 } 906 907/*!*************************************************************************** 908 @Function / 909 @Input rhs a single dimension value 910 @Returns result of division 911 @Description componentwise division of a Vec4 by a single value 912****************************************************************************/ 913 Vec4 operator/(const VERTTYPE& rhs) const 914 { 915 Vec4 out; 916 out.x = VERTTYPEDIV(x,rhs); 917 out.y = VERTTYPEDIV(y,rhs); 918 out.z = VERTTYPEDIV(z,rhs); 919 out.w = VERTTYPEDIV(w,rhs); 920 return out; 921 } 922 923/*!*************************************************************************** 924 @Function /= 925 @Input rhs a single dimension value 926 @Returns result of division and assignment 927 @Description componentwise division and assignment of a Vec4 by 928 a single value 929****************************************************************************/ 930 Vec4& operator/=(const VERTTYPE& rhs) 931 { 932 x = VERTTYPEDIV(x,rhs); 933 y = VERTTYPEDIV(y,rhs); 934 z = VERTTYPEDIV(z,rhs); 935 w = VERTTYPEDIV(w,rhs); 936 return *this; 937 } 938 939/*!*************************************************************************** 940 @Function * 941 @Input lhs a single dimension value 942 @Input rhs a Vec4 943 @Returns result of muliplication 944 @Description componentwise multiplication of a Vec4 by 945 a single value 946****************************************************************************/ 947friend Vec4 operator*(const VERTTYPE lhs, const Vec4& rhs) 948{ 949 Vec4 out; 950 out.x = VERTTYPEMUL(lhs,rhs.x); 951 out.y = VERTTYPEMUL(lhs,rhs.y); 952 out.z = VERTTYPEMUL(lhs,rhs.z); 953 out.w = VERTTYPEMUL(lhs,rhs.w); 954 return out; 955} 956 957/*!*************************************************************************** 958** Functions 959****************************************************************************/ 960/*!*************************************************************************** 961 @Function lenSqr 962 @Returns square of the magnitude of the vector 963 @Description calculates the square of the magnitude of the vector 964****************************************************************************/ 965 VERTTYPE lenSqr() const 966 { 967 return VERTTYPEMUL(x,x)+VERTTYPEMUL(y,y)+VERTTYPEMUL(z,z)+VERTTYPEMUL(w,w); 968 } 969 970/*!*************************************************************************** 971 @Function length 972 @Returns the magnitude of the vector 973 @Description calculates the magnitude of the vector 974****************************************************************************/ 975 VERTTYPE length() const 976 { 977 return (VERTTYPE) f2vt(sqrt(vt2f(x)*vt2f(x) + vt2f(y)*vt2f(y) + vt2f(z)*vt2f(z) + vt2f(w)*vt2f(w))); 978 } 979 980/*!*************************************************************************** 981 @Function normalize 982 @Returns normalized vector 983 @Description calculates the normalized value of a Vec4 984****************************************************************************/ 985 Vec4 normalize() 986 { 987 VERTTYPE len = length(); 988 x =VERTTYPEDIV(x,len); 989 y =VERTTYPEDIV(y,len); 990 z =VERTTYPEDIV(z,len); 991 w =VERTTYPEDIV(w,len); 992 return *this; 993 } 994/*!*************************************************************************** 995 @Function normalized 996 @Returns normalized vector 997 @Description returns a normalized vector of the same direction as this 998 vector 999****************************************************************************/ 1000 Vec4 normalized() const 1001 { 1002 Vec4 out; 1003 VERTTYPE len = length(); 1004 out.x =VERTTYPEDIV(x,len); 1005 out.y =VERTTYPEDIV(y,len); 1006 out.z =VERTTYPEDIV(z,len); 1007 out.w =VERTTYPEDIV(w,len); 1008 return out; 1009 } 1010 1011/*!*************************************************************************** 1012 @Function dot 1013 @Returns scalar product 1014 @Description returns a normalized vector of the same direction as this 1015 vector 1016****************************************************************************/ 1017 VERTTYPE dot(const Vec4& rhs) 1018 { 1019 return VERTTYPEMUL(x,rhs.x)+VERTTYPEMUL(y,rhs.y)+VERTTYPEMUL(z,rhs.z)+VERTTYPEMUL(w,rhs.w); 1020 } 1021 1022/*!*************************************************************************** 1023 @Function ptr 1024 @Returns pointer to vector values 1025 @Description returns a pointer to memory containing the values of the 1026 Vec3 1027****************************************************************************/ 1028 VERTTYPE *ptr() { return (VERTTYPE*)this; } 1029}; 1030 1031 1032 1033#endif // VECTOR_H_