PVRTVector.h 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  1. /******************************************************************************
  2. @File PVRTVector.h
  3. @Title PVRTVector
  4. @Version
  5. @Copyright Copyright (C) Imagination Technologies Limited.
  6. @Platform ANSI compatible
  7. @Description Vector and matrix mathematics library
  8. ******************************************************************************/
  9. #ifndef __PVRTVECTOR_H__
  10. #define __PVRTVECTOR_H__
  11. #include "assert.h"
  12. #include "PVRTGlobal.h"
  13. #include "PVRTFixedPoint.h"
  14. #include "PVRTMatrix.h"
  15. #include <string.h>
  16. #include <math.h>
  17. /*!***************************************************************************
  18. ** Forward Declarations for vector and matrix structs
  19. ****************************************************************************/
  20. struct PVRTVec4;
  21. struct PVRTVec3;
  22. struct PVRTMat3;
  23. struct PVRTMat4;
  24. /*!***************************************************************************
  25. @Function PVRTLinearEqSolve
  26. @Input pSrc 2D array of floats. 4 Eq linear problem is 5x4
  27. matrix, constants in first column
  28. @Input nCnt Number of equations to solve
  29. @Output pRes Result
  30. @Description Solves 'nCnt' simultaneous equations of 'nCnt' variables.
  31. pRes should be an array large enough to contain the
  32. results: the values of the 'nCnt' variables.
  33. This fn recursively uses Gaussian Elimination.
  34. *****************************************************************************/
  35. void PVRTLinearEqSolve(VERTTYPE * const pRes, VERTTYPE ** const pSrc, const int nCnt);
  36. /*!***************************************************************************
  37. ** PVRTVec2 2 component vector
  38. ****************************************************************************/
  39. struct PVRTVec2
  40. {
  41. VERTTYPE x, y;
  42. /*!***************************************************************************
  43. ** Constructors
  44. ****************************************************************************/
  45. /*!***************************************************************************
  46. @Function PVRTVec2
  47. @Description Blank constructor.
  48. *****************************************************************************/
  49. PVRTVec2() {}
  50. /*!***************************************************************************
  51. @Function PVRTVec2
  52. @Input fX X component of vector
  53. @Input fY Y component of vector
  54. @Description Simple constructor from 2 values.
  55. *****************************************************************************/
  56. PVRTVec2(VERTTYPE fX, VERTTYPE fY) : x(fX), y(fY) {}
  57. /*!***************************************************************************
  58. @Function PVRTVec2
  59. @Input fValue a component value
  60. @Description Constructor from a single value.
  61. *****************************************************************************/
  62. PVRTVec2(VERTTYPE fValue) : x(fValue), y(fValue) {}
  63. /*!***************************************************************************
  64. @Function PVRTVec2
  65. @Input pVec an array
  66. @Description Constructor from an array
  67. *****************************************************************************/
  68. PVRTVec2(const VERTTYPE* pVec) : x(pVec[0]), y(pVec[1]) {}
  69. /*!***************************************************************************
  70. @Function PVRTVec2
  71. @Input v3Vec a Vec3
  72. @Description Constructor from a Vec3
  73. *****************************************************************************/
  74. PVRTVec2(const PVRTVec3& v3Vec);
  75. /*!***************************************************************************
  76. ** Operators
  77. ****************************************************************************/
  78. /*!***************************************************************************
  79. @Function +
  80. @Input rhs another Vec2
  81. @Returns result of addition
  82. @Description componentwise addition operator for two Vec2s
  83. *****************************************************************************/
  84. PVRTVec2 operator+(const PVRTVec2& rhs) const
  85. {
  86. PVRTVec2 out(*this);
  87. return out += rhs;
  88. }
  89. /*!***************************************************************************
  90. @Function -
  91. @Input rhs another Vec2
  92. @Returns result of subtraction
  93. @Description componentwise subtraction operator for two Vec2s
  94. ****************************************************************************/
  95. PVRTVec2 operator-(const PVRTVec2& rhs) const
  96. {
  97. PVRTVec2 out(*this);
  98. return out -= rhs;
  99. }
  100. /*!***************************************************************************
  101. @Function +=
  102. @Input rhs another Vec2
  103. @Returns result of addition
  104. @Description componentwise addition and assignment operator for two Vec2s
  105. ****************************************************************************/
  106. PVRTVec2& operator+=(const PVRTVec2& rhs)
  107. {
  108. x += rhs.x;
  109. y += rhs.y;
  110. return *this;
  111. }
  112. /*!***************************************************************************
  113. @Function -=
  114. @Input rhs another Vec2
  115. @Returns result of subtraction
  116. @Description componentwise subtraction and assignment operator for two Vec2s
  117. ****************************************************************************/
  118. PVRTVec2& operator-=(const PVRTVec2& rhs)
  119. {
  120. x -= rhs.x;
  121. y -= rhs.y;
  122. return *this;
  123. }
  124. /*!***************************************************************************
  125. @Function -
  126. @Input rhs another Vec2
  127. @Returns result of negation
  128. @Description negation operator for a Vec2
  129. ****************************************************************************/
  130. friend PVRTVec2 operator- (const PVRTVec2& rhs) { return PVRTVec2(-rhs.x, -rhs.y); }
  131. /*!***************************************************************************
  132. @Function *
  133. @Input lhs scalar
  134. @Input rhs a Vec2
  135. @Returns result of multiplication
  136. @Description multiplication operator for a Vec2
  137. ****************************************************************************/
  138. friend PVRTVec2 operator*(const VERTTYPE lhs, const PVRTVec2& rhs)
  139. {
  140. PVRTVec2 out(lhs);
  141. return out *= rhs;
  142. }
  143. /*!***************************************************************************
  144. @Function /
  145. @Input lhs scalar
  146. @Input rhs a Vec2
  147. @Returns result of division
  148. @Description division operator for scalar and Vec2
  149. ****************************************************************************/
  150. friend PVRTVec2 operator/(const VERTTYPE lhs, const PVRTVec2& rhs)
  151. {
  152. PVRTVec2 out(lhs);
  153. return out /= rhs;
  154. }
  155. /*!***************************************************************************
  156. @Function *
  157. @Input rhs a scalar
  158. @Returns result of multiplication
  159. @Description componentwise multiplication by scalar for Vec2
  160. ****************************************************************************/
  161. PVRTVec2 operator*(const VERTTYPE& rhs) const
  162. {
  163. PVRTVec2 out(*this);
  164. return out *= rhs;
  165. }
  166. /*!***************************************************************************
  167. @Function *=
  168. @Input rhs a scalar
  169. @Returns result of multiplication and assignment
  170. @Description componentwise multiplication and assignment by scalar for Vec2
  171. ****************************************************************************/
  172. PVRTVec2& operator*=(const VERTTYPE& rhs)
  173. {
  174. x = VERTTYPEMUL(x, rhs);
  175. y = VERTTYPEMUL(y, rhs);
  176. return *this;
  177. }
  178. /*!***************************************************************************
  179. @Function *=
  180. @Input rhs a Vec2
  181. @Returns result of multiplication and assignment
  182. @Description componentwise multiplication and assignment by Vec2 for Vec2
  183. ****************************************************************************/
  184. PVRTVec2& operator*=(const PVRTVec2& rhs)
  185. {
  186. x = VERTTYPEMUL(x, rhs.x);
  187. y = VERTTYPEMUL(y, rhs.y);
  188. return *this;
  189. }
  190. /*!***************************************************************************
  191. @Function /
  192. @Input rhs a scalar
  193. @Returns result of division
  194. @Description componentwise division by scalar for Vec2
  195. ****************************************************************************/
  196. PVRTVec2 operator/(const VERTTYPE& rhs) const
  197. {
  198. PVRTVec2 out(*this);
  199. return out /= rhs;
  200. }
  201. /*!***************************************************************************
  202. @Function /=
  203. @Input rhs a scalar
  204. @Returns result of division and assignment
  205. @Description componentwise division and assignment by scalar for Vec2
  206. ****************************************************************************/
  207. PVRTVec2& operator/=(const VERTTYPE& rhs)
  208. {
  209. x = VERTTYPEDIV(x, rhs);
  210. y = VERTTYPEDIV(y, rhs);
  211. return *this;
  212. }
  213. /*!***************************************************************************
  214. @Function /=
  215. @Input rhs a Vec2
  216. @Returns result of division and assignment
  217. @Description componentwise division and assignment by Vec2 for Vec2
  218. ****************************************************************************/
  219. PVRTVec2& operator/=(const PVRTVec2& rhs)
  220. {
  221. x = VERTTYPEDIV(x, rhs.x);
  222. y = VERTTYPEDIV(y, rhs.y);
  223. return *this;
  224. }
  225. /*!***************************************************************************
  226. @Function ==
  227. @Input rhs a single value
  228. @Returns true if the two vectors are equal
  229. @Description PVRTVec2 equality operator
  230. ****************************************************************************/
  231. bool operator==(const PVRTVec2& rhs) const
  232. {
  233. return ((x == rhs.x) && (y == rhs.y));
  234. }
  235. /*!***************************************************************************
  236. @Function !=
  237. @Input rhs a single value
  238. @Returns true if the two vectors are not equal
  239. @Description PVRTVec2 inequality operator
  240. ****************************************************************************/
  241. bool operator!=(const PVRTVec2& rhs) const
  242. {
  243. return ((x != rhs.x) || (y != rhs.y));
  244. }
  245. // FUNCTIONS
  246. /*!***************************************************************************
  247. @Function lenSqr
  248. @Returns the square of the magnitude of the vector
  249. @Description calculates the square of the magnitude of the vector
  250. ****************************************************************************/
  251. VERTTYPE lenSqr() const
  252. {
  253. return VERTTYPEMUL(x,x)+VERTTYPEMUL(y,y);
  254. }
  255. /*!***************************************************************************
  256. @Function length
  257. @Returns the of the magnitude of the vector
  258. @Description calculates the magnitude of the vector
  259. ****************************************************************************/
  260. VERTTYPE length() const
  261. {
  262. return (VERTTYPE) f2vt(sqrt(vt2f(x)*vt2f(x) + vt2f(y)*vt2f(y)));
  263. }
  264. /*!***************************************************************************
  265. @Function normalize
  266. @Returns the normalized value of the vector
  267. @Description normalizes the vector
  268. ****************************************************************************/
  269. PVRTVec2 normalize()
  270. {
  271. return *this /= length();
  272. }
  273. /*!***************************************************************************
  274. @Function normalized
  275. @Returns returns the normalized value of the vector
  276. @Description returns a normalized vector of the same direction as this
  277. vector
  278. ****************************************************************************/
  279. PVRTVec2 normalized() const
  280. {
  281. PVRTVec2 out(*this);
  282. return out.normalize();
  283. }
  284. /*!***************************************************************************
  285. @Function rotated90
  286. @Returns returns the vector rotated 90°
  287. @Description returns the vector rotated 90°
  288. ****************************************************************************/
  289. PVRTVec2 rotated90() const
  290. {
  291. return PVRTVec2(-y, x);
  292. }
  293. /*!***************************************************************************
  294. @Function dot
  295. @Input rhs a single value
  296. @Returns scalar product
  297. @Description calculate the scalar product of two Vec3s
  298. ****************************************************************************/
  299. VERTTYPE dot(const PVRTVec2& rhs) const
  300. {
  301. return VERTTYPEMUL(x, rhs.x) + VERTTYPEMUL(y, rhs.y);
  302. }
  303. /*!***************************************************************************
  304. @Function ptr
  305. @Returns pointer
  306. @Description returns a pointer to memory containing the values of the
  307. Vec3
  308. ****************************************************************************/
  309. VERTTYPE *ptr() { return (VERTTYPE*)this; }
  310. };
  311. /*!***************************************************************************
  312. ** PVRTVec3 3 component vector
  313. ****************************************************************************/
  314. struct PVRTVec3 : public PVRTVECTOR3
  315. {
  316. /*!***************************************************************************
  317. ** Constructors
  318. ****************************************************************************/
  319. /*!***************************************************************************
  320. @Function PVRTVec3
  321. @Description Blank constructor.
  322. *****************************************************************************/
  323. PVRTVec3(){}
  324. /*!***************************************************************************
  325. @Function PVRTVec3
  326. @Input fX X component of vector
  327. @Input fY Y component of vector
  328. @Input fZ Z component of vector
  329. @Description Simple constructor from 3 values.
  330. *****************************************************************************/
  331. PVRTVec3(VERTTYPE fX, VERTTYPE fY, VERTTYPE fZ)
  332. {
  333. x = fX; y = fY; z = fZ;
  334. }
  335. /*!***************************************************************************
  336. @Function PVRTVec3
  337. @Input fValue a component value
  338. @Description Constructor from a single value.
  339. *****************************************************************************/
  340. PVRTVec3(const VERTTYPE fValue)
  341. {
  342. x = fValue; y = fValue; z = fValue;
  343. }
  344. /*!***************************************************************************
  345. @Function PVRTVec3
  346. @Input pVec an array
  347. @Description Constructor from an array
  348. *****************************************************************************/
  349. PVRTVec3(const VERTTYPE* pVec)
  350. {
  351. x = (*pVec++); y = (*pVec++); z = *pVec;
  352. }
  353. /*!***************************************************************************
  354. @Function PVRTVec3
  355. @Input v4Vec a PVRTVec4
  356. @Description Constructor from a PVRTVec4
  357. *****************************************************************************/
  358. PVRTVec3(const PVRTVec4& v4Vec);
  359. /*!***************************************************************************
  360. ** Operators
  361. ****************************************************************************/
  362. /*!***************************************************************************
  363. @Function +
  364. @Input rhs another PVRTVec3
  365. @Returns result of addition
  366. @Description componentwise addition operator for two PVRTVec3s
  367. *****************************************************************************/
  368. PVRTVec3 operator+(const PVRTVec3& rhs) const
  369. {
  370. PVRTVec3 out;
  371. out.x = x+rhs.x;
  372. out.y = y+rhs.y;
  373. out.z = z+rhs.z;
  374. return out;
  375. }
  376. /*!***************************************************************************
  377. @Function -
  378. @Input rhs another PVRTVec3
  379. @Returns result of subtraction
  380. @Description componentwise subtraction operator for two PVRTVec3s
  381. ****************************************************************************/
  382. PVRTVec3 operator-(const PVRTVec3& rhs) const
  383. {
  384. PVRTVec3 out;
  385. out.x = x-rhs.x;
  386. out.y = y-rhs.y;
  387. out.z = z-rhs.z;
  388. return out;
  389. }
  390. /*!***************************************************************************
  391. @Function +=
  392. @Input rhs another PVRTVec3
  393. @Returns result of addition
  394. @Description componentwise addition and assignement operator for two PVRTVec3s
  395. ****************************************************************************/
  396. PVRTVec3& operator+=(const PVRTVec3& rhs)
  397. {
  398. x +=rhs.x;
  399. y +=rhs.y;
  400. z +=rhs.z;
  401. return *this;
  402. }
  403. /*!***************************************************************************
  404. @Function -=
  405. @Input rhs another PVRTVec3
  406. @Returns result of subtraction
  407. @Description componentwise subtraction and assignement operator for two PVRTVec3s
  408. ****************************************************************************/
  409. PVRTVec3& operator-=(const PVRTVec3& rhs)
  410. {
  411. x -=rhs.x;
  412. y -=rhs.y;
  413. z -=rhs.z;
  414. return *this;
  415. }
  416. /*!***************************************************************************
  417. @Function -
  418. @Input rhs another PVRTVec3
  419. @Returns result of negation
  420. @Description negation operator for a PVRTVec3
  421. ****************************************************************************/
  422. friend PVRTVec3 operator - (const PVRTVec3& rhs) { return PVRTVec3(rhs) *= f2vt(-1); }
  423. /*!***************************************************************************
  424. @Function *
  425. @Input lhs single value
  426. @Input rhs a PVRTVec3
  427. @Returns result of multiplication
  428. @Description multiplication operator for a PVRTVec3
  429. ****************************************************************************/
  430. friend PVRTVec3 operator*(const VERTTYPE lhs, const PVRTVec3& rhs)
  431. {
  432. PVRTVec3 out;
  433. out.x = VERTTYPEMUL(lhs,rhs.x);
  434. out.y = VERTTYPEMUL(lhs,rhs.y);
  435. out.z = VERTTYPEMUL(lhs,rhs.z);
  436. return out;
  437. }
  438. /*!***************************************************************************
  439. @Function *
  440. @Input lhs single value
  441. @Input rhs a PVRTVec3
  442. @Returns result of negation
  443. @Description negation operator for a PVRTVec3
  444. ****************************************************************************/
  445. friend PVRTVec3 operator/(const VERTTYPE lhs, const PVRTVec3& rhs)
  446. {
  447. PVRTVec3 out;
  448. out.x = VERTTYPEDIV(lhs,rhs.x);
  449. out.y = VERTTYPEDIV(lhs,rhs.y);
  450. out.z = VERTTYPEDIV(lhs,rhs.z);
  451. return out;
  452. }
  453. /*!***************************************************************************
  454. @Function *
  455. @Input rhs a PVRTMat3
  456. @Returns result of multiplication
  457. @Description matrix multiplication operator PVRTVec3 and PVRTMat3
  458. ****************************************************************************/
  459. PVRTVec3 operator*(const PVRTMat3& rhs) const;
  460. /*!***************************************************************************
  461. @Function *=
  462. @Input rhs a PVRTMat3
  463. @Returns result of multiplication and assignment
  464. @Description matrix multiplication and assignment operator for PVRTVec3 and PVRTMat3
  465. ****************************************************************************/
  466. PVRTVec3& operator*=(const PVRTMat3& rhs);
  467. /*!***************************************************************************
  468. @Function *
  469. @Input rhs a single value
  470. @Returns result of multiplication
  471. @Description componentwise multiplication by single dimension value for PVRTVec3
  472. ****************************************************************************/
  473. PVRTVec3 operator*(const VERTTYPE& rhs) const
  474. {
  475. PVRTVec3 out;
  476. out.x = VERTTYPEMUL(x,rhs);
  477. out.y = VERTTYPEMUL(y,rhs);
  478. out.z = VERTTYPEMUL(z,rhs);
  479. return out;
  480. }
  481. /*!***************************************************************************
  482. @Function *
  483. @Input rhs a single value
  484. @Returns result of multiplication and assignment
  485. @Description componentwise multiplication and assignement by single
  486. dimension value for PVRTVec3
  487. ****************************************************************************/
  488. PVRTVec3& operator*=(const VERTTYPE& rhs)
  489. {
  490. x = VERTTYPEMUL(x,rhs);
  491. y = VERTTYPEMUL(y,rhs);
  492. z = VERTTYPEMUL(z,rhs);
  493. return *this;
  494. }
  495. /*!***************************************************************************
  496. @Function /
  497. @Input rhs a single value
  498. @Returns result of division
  499. @Description componentwise division by single
  500. dimension value for PVRTVec3
  501. ****************************************************************************/
  502. PVRTVec3 operator/(const VERTTYPE& rhs) const
  503. {
  504. PVRTVec3 out;
  505. out.x = VERTTYPEDIV(x,rhs);
  506. out.y = VERTTYPEDIV(y,rhs);
  507. out.z = VERTTYPEDIV(z,rhs);
  508. return out;
  509. }
  510. /*!***************************************************************************
  511. @Function /=
  512. @Input rhs a single value
  513. @Returns result of division and assignment
  514. @Description componentwise division and assignement by single
  515. dimension value for PVRTVec3
  516. ****************************************************************************/
  517. PVRTVec3& operator/=(const VERTTYPE& rhs)
  518. {
  519. x = VERTTYPEDIV(x,rhs);
  520. y = VERTTYPEDIV(y,rhs);
  521. z = VERTTYPEDIV(z,rhs);
  522. return *this;
  523. }
  524. /*!***************************************************************************
  525. @Function ==
  526. @Input rhs a single value
  527. @Returns true if the two vectors are equal
  528. @Description PVRTVec3 equality operator
  529. ****************************************************************************/
  530. bool operator==(const PVRTVec3& rhs) const
  531. {
  532. return ((x == rhs.x) && (y == rhs.y) && (z == rhs.z));
  533. }
  534. /*!***************************************************************************
  535. @Function !=
  536. @Input rhs a single value
  537. @Returns true if the two vectors are not equal
  538. @Description PVRTVec3 inequality operator
  539. ****************************************************************************/
  540. bool operator!=(const PVRTVec3& rhs) const
  541. {
  542. return ((x != rhs.x) || (y != rhs.y) || (z != rhs.z));
  543. }
  544. // FUNCTIONS
  545. /*!***************************************************************************
  546. @Function lenSqr
  547. @Returns the square of the magnitude of the vector
  548. @Description calculates the square of the magnitude of the vector
  549. ****************************************************************************/
  550. VERTTYPE lenSqr() const
  551. {
  552. return VERTTYPEMUL(x,x)+VERTTYPEMUL(y,y)+VERTTYPEMUL(z,z);
  553. }
  554. /*!***************************************************************************
  555. @Function length
  556. @Returns the of the magnitude of the vector
  557. @Description calculates the magnitude of the vector
  558. ****************************************************************************/
  559. VERTTYPE length() const
  560. {
  561. return (VERTTYPE) f2vt(sqrt(vt2f(x)*vt2f(x) + vt2f(y)*vt2f(y) + vt2f(z)*vt2f(z)));
  562. }
  563. /*!***************************************************************************
  564. @Function normalize
  565. @Returns the normalized value of the vector
  566. @Description normalizes the vector
  567. ****************************************************************************/
  568. PVRTVec3 normalize()
  569. {
  570. #if defined(PVRT_FIXED_POINT_ENABLE)
  571. // Scale vector by uniform value
  572. int n = PVRTABS(x) + PVRTABS(y) + PVRTABS(z);
  573. x = VERTTYPEDIV(x, n);
  574. y = VERTTYPEDIV(y, n);
  575. z = VERTTYPEDIV(z, n);
  576. // Calculate x2+y2+z2/sqrt(x2+y2+z2)
  577. int f = dot(*this);
  578. f = VERTTYPEDIV(PVRTF2X(1.0f), PVRTF2X(sqrt(PVRTX2F(f))));
  579. // Multiply vector components by f
  580. x = PVRTXMUL(x, f);
  581. y = PVRTXMUL(y, f);
  582. z = PVRTXMUL(z, f);
  583. #else
  584. VERTTYPE len = length();
  585. x =VERTTYPEDIV(x,len);
  586. y =VERTTYPEDIV(y,len);
  587. z =VERTTYPEDIV(z,len);
  588. #endif
  589. return *this;
  590. }
  591. /*!***************************************************************************
  592. @Function normalized
  593. @Returns returns the normalized value of the vector
  594. @Description returns a normalized vector of the same direction as this
  595. vector
  596. ****************************************************************************/
  597. PVRTVec3 normalized() const
  598. {
  599. PVRTVec3 out;
  600. #if defined(PVRT_FIXED_POINT_ENABLE)
  601. // Scale vector by uniform value
  602. int n = PVRTABS(x) + PVRTABS(y) + PVRTABS(z);
  603. out.x = VERTTYPEDIV(x, n);
  604. out.y = VERTTYPEDIV(y, n);
  605. out.z = VERTTYPEDIV(z, n);
  606. // Calculate x2+y2+z2/sqrt(x2+y2+z2)
  607. int f = out.dot(out);
  608. f = VERTTYPEDIV(PVRTF2X(1.0f), PVRTF2X(sqrt(PVRTX2F(f))));
  609. // Multiply vector components by f
  610. out.x = PVRTXMUL(out.x, f);
  611. out.y = PVRTXMUL(out.y, f);
  612. out.z = PVRTXMUL(out.z, f);
  613. #else
  614. VERTTYPE len = length();
  615. out.x =VERTTYPEDIV(x,len);
  616. out.y =VERTTYPEDIV(y,len);
  617. out.z =VERTTYPEDIV(z,len);
  618. #endif
  619. return out;
  620. }
  621. /*!***************************************************************************
  622. @Function dot
  623. @Input rhs a single value
  624. @Returns scalar product
  625. @Description calculate the scalar product of two PVRTVec3s
  626. ****************************************************************************/
  627. VERTTYPE dot(const PVRTVec3& rhs) const
  628. {
  629. return VERTTYPEMUL(x,rhs.x)+VERTTYPEMUL(y,rhs.y)+VERTTYPEMUL(z,rhs.z);
  630. }
  631. /*!***************************************************************************
  632. @Function cross
  633. @Returns returns three-dimensional vector
  634. @Description calculate the cross product of two PVRTVec3s
  635. ****************************************************************************/
  636. PVRTVec3 cross(const PVRTVec3& rhs) const
  637. {
  638. PVRTVec3 out;
  639. out.x = VERTTYPEMUL(y,rhs.z)-VERTTYPEMUL(z,rhs.y);
  640. out.y = VERTTYPEMUL(z,rhs.x)-VERTTYPEMUL(x,rhs.z);
  641. out.z = VERTTYPEMUL(x,rhs.y)-VERTTYPEMUL(y,rhs.x);
  642. return out;
  643. }
  644. /*!***************************************************************************
  645. @Function ptr
  646. @Returns pointer
  647. @Description returns a pointer to memory containing the values of the
  648. PVRTVec3
  649. ****************************************************************************/
  650. VERTTYPE *ptr() { return (VERTTYPE*)this; }
  651. };
  652. /*!***************************************************************************
  653. ** PVRTVec4 4 component vector
  654. ****************************************************************************/
  655. struct PVRTVec4 : public PVRTVECTOR4
  656. {
  657. /*!***************************************************************************
  658. ** Constructors
  659. ****************************************************************************/
  660. /*!***************************************************************************
  661. @Function PVRTVec4
  662. @Description Blank constructor.
  663. *****************************************************************************/
  664. PVRTVec4(){}
  665. /*!***************************************************************************
  666. @Function PVRTVec3
  667. @Description Blank constructor.
  668. *****************************************************************************/
  669. PVRTVec4(const VERTTYPE vec)
  670. {
  671. x = vec; y = vec; z = vec; w = vec;
  672. }
  673. /*!***************************************************************************
  674. @Function multiple value constructor
  675. @Input fX value of x component
  676. @Input fY value of y component
  677. @Input fZ value of z component
  678. @Input fW value of w component
  679. @Description Constructs a PVRTVec4 from 4 separate values
  680. ****************************************************************************/
  681. PVRTVec4(VERTTYPE fX, VERTTYPE fY, VERTTYPE fZ, VERTTYPE fW)
  682. {
  683. x = fX; y = fY; z = fZ; w = fW;
  684. }
  685. /*!***************************************************************************
  686. @Function constructor from PVRTVec3
  687. @Input vec3 a PVRTVec3
  688. @Input fW value of w component
  689. @Description Constructs a PVRTVec4 from a vec3 and a w component
  690. ****************************************************************************/
  691. PVRTVec4(const PVRTVec3& vec3, VERTTYPE fW)
  692. {
  693. x = vec3.x; y = vec3.y; z = vec3.z; w = fW;
  694. }
  695. /*!***************************************************************************
  696. @Function constructor from PVRTVec3
  697. @Input fX value of x component
  698. @Input vec3 a PVRTVec3
  699. @Description Constructs a vec4 from a vec3 and a w component
  700. ****************************************************************************/
  701. PVRTVec4(VERTTYPE fX, const PVRTVec3& vec3)
  702. {
  703. x = fX; y = vec3.x; z = vec3.y; w = vec3.z;
  704. }
  705. /*!***************************************************************************
  706. @Function constructor from array
  707. @Input pVec a pointer to an array of four values
  708. @Description Constructs a PVRTVec4 from a pointer to an array of four values
  709. ****************************************************************************/
  710. PVRTVec4(const VERTTYPE* pVec)
  711. {
  712. x = (*pVec++); y = (*pVec++); z= (*pVec++); w = *pVec;
  713. }
  714. /*!***************************************************************************
  715. ** PVRTVec4 Operators
  716. ****************************************************************************/
  717. /*!***************************************************************************
  718. @Function +
  719. @Input rhs another PVRTVec4
  720. @Returns result of addition
  721. @Description Addition operator for PVRTVec4
  722. ****************************************************************************/
  723. PVRTVec4 operator+(const PVRTVec4& rhs) const
  724. {
  725. PVRTVec4 out;
  726. out.x = x+rhs.x;
  727. out.y = y+rhs.y;
  728. out.z = z+rhs.z;
  729. out.w = w+rhs.w;
  730. return out;
  731. }
  732. /*!***************************************************************************
  733. @Function -
  734. @Input rhs another PVRTVec4
  735. @Returns result of subtraction
  736. @Description Subtraction operator for PVRTVec4
  737. ****************************************************************************/
  738. PVRTVec4 operator-(const PVRTVec4& rhs) const
  739. {
  740. PVRTVec4 out;
  741. out.x = x-rhs.x;
  742. out.y = y-rhs.y;
  743. out.z = z-rhs.z;
  744. out.w = w-rhs.w;
  745. return out;
  746. }
  747. /*!***************************************************************************
  748. @Function +=
  749. @Input rhs another PVRTVec4
  750. @Returns result of addition and assignment
  751. @Description Addition and assignment operator for PVRTVec4
  752. ****************************************************************************/
  753. PVRTVec4& operator+=(const PVRTVec4& rhs)
  754. {
  755. x +=rhs.x;
  756. y +=rhs.y;
  757. z +=rhs.z;
  758. w +=rhs.w;
  759. return *this;
  760. }
  761. /*!***************************************************************************
  762. @Function -=
  763. @Input rhs another PVRTVec4
  764. @Returns result of subtraction and assignment
  765. @Description Subtraction and assignment operator for PVRTVec4
  766. ****************************************************************************/
  767. PVRTVec4& operator-=(const PVRTVec4& rhs)
  768. {
  769. x -=rhs.x;
  770. y -=rhs.y;
  771. z -=rhs.z;
  772. w -=rhs.w;
  773. return *this;
  774. }
  775. /*!***************************************************************************
  776. @Function *
  777. @Input rhs a PVRTMat4
  778. @Returns result of multiplication
  779. @Description matrix multiplication for PVRTVec4 and PVRTMat4
  780. ****************************************************************************/
  781. PVRTVec4 operator*(const PVRTMat4& rhs) const;
  782. /*!***************************************************************************
  783. @Function *=
  784. @Input rhs a PVRTMat4
  785. @Returns result of multiplication and assignement
  786. @Description matrix multiplication and assignment for PVRTVec4 and PVRTMat4
  787. ****************************************************************************/
  788. PVRTVec4& operator*=(const PVRTMat4& rhs);
  789. /*!***************************************************************************
  790. @Function *
  791. @Input rhs a single dimension value
  792. @Returns result of multiplication
  793. @Description componentwise multiplication of a PVRTVec4 by a single value
  794. ****************************************************************************/
  795. PVRTVec4 operator*(const VERTTYPE& rhs) const
  796. {
  797. PVRTVec4 out;
  798. out.x = VERTTYPEMUL(x,rhs);
  799. out.y = VERTTYPEMUL(y,rhs);
  800. out.z = VERTTYPEMUL(z,rhs);
  801. out.w = VERTTYPEMUL(w,rhs);
  802. return out;
  803. }
  804. /*!***************************************************************************
  805. @Function *=
  806. @Input rhs a single dimension value
  807. @Returns result of multiplication and assignment
  808. @Description componentwise multiplication and assignment of a PVRTVec4 by
  809. a single value
  810. ****************************************************************************/
  811. PVRTVec4& operator*=(const VERTTYPE& rhs)
  812. {
  813. x = VERTTYPEMUL(x,rhs);
  814. y = VERTTYPEMUL(y,rhs);
  815. z = VERTTYPEMUL(z,rhs);
  816. w = VERTTYPEMUL(w,rhs);
  817. return *this;
  818. }
  819. /*!***************************************************************************
  820. @Function /
  821. @Input rhs a single dimension value
  822. @Returns result of division
  823. @Description componentwise division of a PVRTVec4 by a single value
  824. ****************************************************************************/
  825. PVRTVec4 operator/(const VERTTYPE& rhs) const
  826. {
  827. PVRTVec4 out;
  828. out.x = VERTTYPEDIV(x,rhs);
  829. out.y = VERTTYPEDIV(y,rhs);
  830. out.z = VERTTYPEDIV(z,rhs);
  831. out.w = VERTTYPEDIV(w,rhs);
  832. return out;
  833. }
  834. /*!***************************************************************************
  835. @Function /=
  836. @Input rhs a single dimension value
  837. @Returns result of division and assignment
  838. @Description componentwise division and assignment of a PVRTVec4 by
  839. a single value
  840. ****************************************************************************/
  841. PVRTVec4& operator/=(const VERTTYPE& rhs)
  842. {
  843. x = VERTTYPEDIV(x,rhs);
  844. y = VERTTYPEDIV(y,rhs);
  845. z = VERTTYPEDIV(z,rhs);
  846. w = VERTTYPEDIV(w,rhs);
  847. return *this;
  848. }
  849. /*!***************************************************************************
  850. @Function *
  851. @Input lhs a single dimension value
  852. @Input rhs a PVRTVec4
  853. @Returns result of muliplication
  854. @Description componentwise multiplication of a PVRTVec4 by
  855. a single value
  856. ****************************************************************************/
  857. friend PVRTVec4 operator*(const VERTTYPE lhs, const PVRTVec4& rhs)
  858. {
  859. PVRTVec4 out;
  860. out.x = VERTTYPEMUL(lhs,rhs.x);
  861. out.y = VERTTYPEMUL(lhs,rhs.y);
  862. out.z = VERTTYPEMUL(lhs,rhs.z);
  863. out.w = VERTTYPEMUL(lhs,rhs.w);
  864. return out;
  865. }
  866. /*!***************************************************************************
  867. @Function ==
  868. @Input rhs a single value
  869. @Returns true if the two vectors are equal
  870. @Description PVRTVec4 equality operator
  871. ****************************************************************************/
  872. bool operator==(const PVRTVec4& rhs) const
  873. {
  874. return ((x == rhs.x) && (y == rhs.y) && (z == rhs.z) && (w == rhs.w));
  875. }
  876. /*!***************************************************************************
  877. @Function !=
  878. @Input rhs a single value
  879. @Returns true if the two vectors are not equal
  880. @Description PVRTVec4 inequality operator
  881. ****************************************************************************/
  882. bool operator!=(const PVRTVec4& rhs) const
  883. {
  884. return ((x != rhs.x) || (y != rhs.y) || (z != rhs.z) || (w != rhs.w));
  885. }
  886. /*!***************************************************************************
  887. ** Functions
  888. ****************************************************************************/
  889. /*!***************************************************************************
  890. @Function lenSqr
  891. @Returns square of the magnitude of the vector
  892. @Description calculates the square of the magnitude of the vector
  893. ****************************************************************************/
  894. VERTTYPE lenSqr() const
  895. {
  896. return VERTTYPEMUL(x,x)+VERTTYPEMUL(y,y)+VERTTYPEMUL(z,z)+VERTTYPEMUL(w,w);
  897. }
  898. /*!***************************************************************************
  899. @Function length
  900. @Returns the magnitude of the vector
  901. @Description calculates the magnitude of the vector
  902. ****************************************************************************/
  903. VERTTYPE length() const
  904. {
  905. return (VERTTYPE) f2vt(sqrt(vt2f(x)*vt2f(x) + vt2f(y)*vt2f(y) + vt2f(z)*vt2f(z) + vt2f(w)*vt2f(w)));
  906. }
  907. /*!***************************************************************************
  908. @Function normalize
  909. @Returns normalized vector
  910. @Description calculates the normalized value of a PVRTVec4
  911. ****************************************************************************/
  912. PVRTVec4 normalize()
  913. {
  914. VERTTYPE len = length();
  915. x =VERTTYPEDIV(x,len);
  916. y =VERTTYPEDIV(y,len);
  917. z =VERTTYPEDIV(z,len);
  918. w =VERTTYPEDIV(w,len);
  919. return *this;
  920. }
  921. /*!***************************************************************************
  922. @Function normalized
  923. @Returns normalized vector
  924. @Description returns a normalized vector of the same direction as this
  925. vector
  926. ****************************************************************************/
  927. PVRTVec4 normalized() const
  928. {
  929. PVRTVec4 out;
  930. VERTTYPE len = length();
  931. out.x =VERTTYPEDIV(x,len);
  932. out.y =VERTTYPEDIV(y,len);
  933. out.z =VERTTYPEDIV(z,len);
  934. out.w =VERTTYPEDIV(w,len);
  935. return out;
  936. }
  937. /*!***************************************************************************
  938. @Function dot
  939. @Returns scalar product
  940. @Description returns a normalized vector of the same direction as this
  941. vector
  942. ****************************************************************************/
  943. VERTTYPE dot(const PVRTVec4& rhs) const
  944. {
  945. return VERTTYPEMUL(x,rhs.x)+VERTTYPEMUL(y,rhs.y)+VERTTYPEMUL(z,rhs.z)+VERTTYPEMUL(w,rhs.w);
  946. }
  947. /*!***************************************************************************
  948. @Function ptr
  949. @Returns pointer to vector values
  950. @Description returns a pointer to memory containing the values of the
  951. PVRTVec3
  952. ****************************************************************************/
  953. VERTTYPE *ptr() { return (VERTTYPE*)this; }
  954. };
  955. /*!***************************************************************************
  956. ** PVRTMat3 3x3 Matrix
  957. ****************************************************************************/
  958. struct PVRTMat3 : public PVRTMATRIX3
  959. {
  960. /*!***************************************************************************
  961. ** Constructors
  962. ****************************************************************************/
  963. /*!***************************************************************************
  964. @Function PVRTMat3
  965. @Description Blank constructor.
  966. *****************************************************************************/
  967. PVRTMat3(){}
  968. /*!***************************************************************************
  969. @Function PVRTMat3
  970. @Input pMat an array of values for the matrix
  971. @Description Constructor from array.
  972. *****************************************************************************/
  973. PVRTMat3(const VERTTYPE* pMat)
  974. {
  975. VERTTYPE* ptr = f;
  976. for(int i=0;i<9;i++)
  977. {
  978. (*ptr++)=(*pMat++);
  979. }
  980. }
  981. /*!***************************************************************************
  982. @Function PVRTMat3
  983. @Input m0 matrix value
  984. @Input m1 matrix value
  985. @Input m2 matrix value
  986. @Input m3 matrix value
  987. @Input m4 matrix value
  988. @Input m5 matrix value
  989. @Input m6 matrix value
  990. @Input m7 matrix value
  991. @Input m8 matrix value
  992. @Description Constructor from distinct values.
  993. *****************************************************************************/
  994. PVRTMat3(VERTTYPE m0,VERTTYPE m1,VERTTYPE m2,
  995. VERTTYPE m3,VERTTYPE m4,VERTTYPE m5,
  996. VERTTYPE m6,VERTTYPE m7,VERTTYPE m8)
  997. {
  998. f[0]=m0;f[1]=m1;f[2]=m2;
  999. f[3]=m3;f[4]=m4;f[5]=m5;
  1000. f[6]=m6;f[7]=m7;f[8]=m8;
  1001. }
  1002. /*!***************************************************************************
  1003. @Function PVRTMat3
  1004. @Input mat - a PVRTMat4
  1005. @Description Constructor from 4x4 matrix - uses top left values
  1006. *****************************************************************************/
  1007. PVRTMat3(const PVRTMat4& mat);
  1008. /****************************************************************************
  1009. ** PVRTMat3 OPERATORS
  1010. ****************************************************************************/
  1011. /*!***************************************************************************
  1012. @Function ()
  1013. @Input row row of matrix
  1014. @Input column column of matrix
  1015. @Returns value of element
  1016. @Description Returns the value of the element at the specified row and column
  1017. of the PVRTMat3
  1018. *****************************************************************************/
  1019. VERTTYPE& operator()(const int row, const int column)
  1020. {
  1021. return f[column*3+row];
  1022. }
  1023. /*!***************************************************************************
  1024. @Function ()
  1025. @Input row row of matrix
  1026. @Input column column of matrix
  1027. @Returns value of element
  1028. @Description Returns the value of the element at the specified row and column
  1029. of the PVRTMat3
  1030. *****************************************************************************/
  1031. const VERTTYPE& operator()(const int row, const int column) const
  1032. {
  1033. return f[column*3+row];
  1034. }
  1035. /*!***************************************************************************
  1036. @Function *
  1037. @Input rhs another PVRTMat3
  1038. @Returns result of multiplication
  1039. @Description Matrix multiplication of two 3x3 matrices.
  1040. *****************************************************************************/
  1041. PVRTMat3 operator*(const PVRTMat3& rhs) const
  1042. {
  1043. PVRTMat3 out;
  1044. // col 1
  1045. out.f[0] = VERTTYPEMUL(f[0],rhs.f[0])+VERTTYPEMUL(f[3],rhs.f[1])+VERTTYPEMUL(f[6],rhs.f[2]);
  1046. out.f[1] = VERTTYPEMUL(f[1],rhs.f[0])+VERTTYPEMUL(f[4],rhs.f[1])+VERTTYPEMUL(f[7],rhs.f[2]);
  1047. out.f[2] = VERTTYPEMUL(f[2],rhs.f[0])+VERTTYPEMUL(f[5],rhs.f[1])+VERTTYPEMUL(f[8],rhs.f[2]);
  1048. // col 2
  1049. out.f[3] = VERTTYPEMUL(f[0],rhs.f[3])+VERTTYPEMUL(f[3],rhs.f[4])+VERTTYPEMUL(f[6],rhs.f[5]);
  1050. out.f[4] = VERTTYPEMUL(f[1],rhs.f[3])+VERTTYPEMUL(f[4],rhs.f[4])+VERTTYPEMUL(f[7],rhs.f[5]);
  1051. out.f[5] = VERTTYPEMUL(f[2],rhs.f[3])+VERTTYPEMUL(f[5],rhs.f[4])+VERTTYPEMUL(f[8],rhs.f[5]);
  1052. // col3
  1053. out.f[6] = VERTTYPEMUL(f[0],rhs.f[6])+VERTTYPEMUL(f[3],rhs.f[7])+VERTTYPEMUL(f[6],rhs.f[8]);
  1054. out.f[7] = VERTTYPEMUL(f[1],rhs.f[6])+VERTTYPEMUL(f[4],rhs.f[7])+VERTTYPEMUL(f[7],rhs.f[8]);
  1055. out.f[8] = VERTTYPEMUL(f[2],rhs.f[6])+VERTTYPEMUL(f[5],rhs.f[7])+VERTTYPEMUL(f[8],rhs.f[8]);
  1056. return out;
  1057. }
  1058. /*!***************************************************************************
  1059. @Function +
  1060. @Input rhs another PVRTMat3
  1061. @Returns result of addition
  1062. @Description element by element addition operator.
  1063. *****************************************************************************/
  1064. PVRTMat3 operator+(const PVRTMat3& rhs) const
  1065. {
  1066. PVRTMat3 out;
  1067. VERTTYPE const *lptr = f, *rptr = rhs.f;
  1068. VERTTYPE *outptr = out.f;
  1069. for(int i=0;i<9;i++)
  1070. {
  1071. (*outptr++) = (*lptr++) + (*rptr++);
  1072. }
  1073. return out;
  1074. }
  1075. /*!***************************************************************************
  1076. @Function -
  1077. @Input rhs another PVRTMat3
  1078. @Returns result of subtraction
  1079. @Description element by element subtraction operator.
  1080. *****************************************************************************/
  1081. PVRTMat3 operator-(const PVRTMat3& rhs) const
  1082. {
  1083. PVRTMat3 out;
  1084. VERTTYPE const *lptr = f, *rptr = rhs.f;
  1085. VERTTYPE *outptr = out.f;
  1086. for(int i=0;i<9;i++)
  1087. {
  1088. (*outptr++) = (*lptr++) - (*rptr++);
  1089. }
  1090. return out;
  1091. }
  1092. /*!***************************************************************************
  1093. @Function +=
  1094. @Input rhs another PVRTMat3
  1095. @Returns result of addition and assignment
  1096. @Description element by element addition and assignment operator.
  1097. *****************************************************************************/
  1098. PVRTMat3& operator+=(const PVRTMat3& rhs)
  1099. {
  1100. VERTTYPE *lptr = f;
  1101. VERTTYPE const *rptr = rhs.f;
  1102. for(int i=0;i<9;i++)
  1103. {
  1104. (*lptr++) += (*rptr++);
  1105. }
  1106. return *this;
  1107. }
  1108. /*!***************************************************************************
  1109. @Function -=
  1110. @Input rhs another PVRTMat3
  1111. @Returns result of subtraction and assignment
  1112. @Description element by element subtraction and assignment operator.
  1113. *****************************************************************************/
  1114. PVRTMat3& operator-=(const PVRTMat3& rhs)
  1115. {
  1116. VERTTYPE *lptr = f;
  1117. VERTTYPE const *rptr = rhs.f;
  1118. for(int i=0;i<9;i++)
  1119. {
  1120. (*lptr++) -= (*rptr++);
  1121. }
  1122. return *this;
  1123. }
  1124. /*!***************************************************************************
  1125. @Function *=
  1126. @Input rhs another PVRTMat3
  1127. @Returns result of multiplication and assignment
  1128. @Description Matrix multiplication and assignment of two 3x3 matrices.
  1129. *****************************************************************************/
  1130. PVRTMat3& operator*=(const PVRTMat3& rhs)
  1131. {
  1132. PVRTMat3 out;
  1133. // col 1
  1134. out.f[0] = VERTTYPEMUL(f[0],rhs.f[0])+VERTTYPEMUL(f[3],rhs.f[1])+VERTTYPEMUL(f[6],rhs.f[2]);
  1135. out.f[1] = VERTTYPEMUL(f[1],rhs.f[0])+VERTTYPEMUL(f[4],rhs.f[1])+VERTTYPEMUL(f[7],rhs.f[2]);
  1136. out.f[2] = VERTTYPEMUL(f[2],rhs.f[0])+VERTTYPEMUL(f[5],rhs.f[1])+VERTTYPEMUL(f[8],rhs.f[2]);
  1137. // col 2
  1138. out.f[3] = VERTTYPEMUL(f[0],rhs.f[3])+VERTTYPEMUL(f[3],rhs.f[4])+VERTTYPEMUL(f[6],rhs.f[5]);
  1139. out.f[4] = VERTTYPEMUL(f[1],rhs.f[3])+VERTTYPEMUL(f[4],rhs.f[4])+VERTTYPEMUL(f[7],rhs.f[5]);
  1140. out.f[5] = VERTTYPEMUL(f[2],rhs.f[3])+VERTTYPEMUL(f[5],rhs.f[4])+VERTTYPEMUL(f[8],rhs.f[5]);
  1141. // col3
  1142. out.f[6] = VERTTYPEMUL(f[0],rhs.f[6])+VERTTYPEMUL(f[3],rhs.f[7])+VERTTYPEMUL(f[6],rhs.f[8]);
  1143. out.f[7] = VERTTYPEMUL(f[1],rhs.f[6])+VERTTYPEMUL(f[4],rhs.f[7])+VERTTYPEMUL(f[7],rhs.f[8]);
  1144. out.f[8] = VERTTYPEMUL(f[2],rhs.f[6])+VERTTYPEMUL(f[5],rhs.f[7])+VERTTYPEMUL(f[8],rhs.f[8]);
  1145. *this = out;
  1146. return *this;
  1147. }
  1148. /*!***************************************************************************
  1149. @Function *
  1150. @Input rhs a single value
  1151. @Returns result of multiplication and assignment
  1152. @Description element multiplication by a single value.
  1153. *****************************************************************************/
  1154. PVRTMat3& operator*(const VERTTYPE rhs)
  1155. {
  1156. for (int i=0; i<9; ++i)
  1157. {
  1158. f[i]*=rhs;
  1159. }
  1160. return *this;
  1161. }
  1162. /*!***************************************************************************
  1163. @Function *=
  1164. @Input rhs a single value
  1165. @Returns result of multiplication and assignment
  1166. @Description element multiplication and assignment by a single value.
  1167. *****************************************************************************/
  1168. PVRTMat3& operator*=(const VERTTYPE rhs)
  1169. {
  1170. for (int i=0; i<9; ++i)
  1171. {
  1172. f[i]*=rhs;
  1173. }
  1174. return *this;
  1175. }
  1176. /*!***************************************************************************
  1177. @Function *
  1178. @Input rhs another PVRTVec3
  1179. @Returns result of multiplication
  1180. @Description Matrix multiplication of 3x3 matrix and vec3
  1181. *****************************************************************************/
  1182. PVRTVec3 operator*(const PVRTVec3& rhs) const
  1183. {
  1184. PVRTVec3 out;
  1185. out.x = VERTTYPEMUL(rhs.x,f[0])+VERTTYPEMUL(rhs.y,f[3])+VERTTYPEMUL(rhs.z,f[6]);
  1186. out.y = VERTTYPEMUL(rhs.x,f[1])+VERTTYPEMUL(rhs.y,f[4])+VERTTYPEMUL(rhs.z,f[7]);
  1187. out.z = VERTTYPEMUL(rhs.x,f[2])+VERTTYPEMUL(rhs.y,f[5])+VERTTYPEMUL(rhs.z,f[8]);
  1188. return out;
  1189. }
  1190. // FUNCTIONS
  1191. /*!***************************************************************************
  1192. ** Functions
  1193. *****************************************************************************/
  1194. /*!***************************************************************************
  1195. @Function determinant
  1196. @Returns result of multiplication
  1197. @Description Matrix multiplication and assignment of 3x3 matrix and vec3
  1198. *****************************************************************************/
  1199. VERTTYPE determinant() const
  1200. {
  1201. return VERTTYPEMUL(f[0],(VERTTYPEMUL(f[4],f[8])-VERTTYPEMUL(f[7],f[5])))
  1202. - VERTTYPEMUL(f[3],(VERTTYPEMUL(f[1],f[8])-VERTTYPEMUL(f[7],f[2])))
  1203. + VERTTYPEMUL(f[6],(VERTTYPEMUL(f[1],f[5])-VERTTYPEMUL(f[4],f[2])));
  1204. }
  1205. /*!***************************************************************************
  1206. @Function inverse
  1207. @Returns inverse mat3
  1208. @Description Calculates multiplicative inverse of this matrix
  1209. *****************************************************************************/
  1210. PVRTMat3 inverse() const
  1211. {
  1212. PVRTMat3 out;
  1213. VERTTYPE recDet = determinant();
  1214. _ASSERT(recDet!=0);
  1215. recDet = VERTTYPEDIV(f2vt(1.0f),recDet);
  1216. //TODO: deal with singular matrices with more than just an assert
  1217. // inverse is 1/det * adjoint of M
  1218. // adjoint is transpose of cofactor matrix
  1219. // do transpose and cofactors in one step
  1220. out.f[0] = VERTTYPEMUL(f[4],f[8]) - VERTTYPEMUL(f[5],f[7]);
  1221. out.f[1] = VERTTYPEMUL(f[2],f[7]) - VERTTYPEMUL(f[1],f[8]);
  1222. out.f[2] = VERTTYPEMUL(f[1],f[5]) - VERTTYPEMUL(f[2],f[4]);
  1223. out.f[3] = VERTTYPEMUL(f[5],f[6]) - VERTTYPEMUL(f[3],f[8]);
  1224. out.f[4] = VERTTYPEMUL(f[0],f[8]) - VERTTYPEMUL(f[2],f[6]);
  1225. out.f[5] = VERTTYPEMUL(f[2],f[3]) - VERTTYPEMUL(f[0],f[5]);
  1226. out.f[6] = VERTTYPEMUL(f[3],f[7]) - VERTTYPEMUL(f[4],f[6]);
  1227. out.f[7] = VERTTYPEMUL(f[1],f[6]) - VERTTYPEMUL(f[0],f[7]);
  1228. out.f[8] = VERTTYPEMUL(f[0],f[4]) - VERTTYPEMUL(f[1],f[3]);
  1229. out *= recDet;
  1230. return out;
  1231. }
  1232. /*!***************************************************************************
  1233. @Function transpose
  1234. @Returns transpose 3x3 matrix
  1235. @Description Calculates the transpose of this matrix
  1236. *****************************************************************************/
  1237. PVRTMat3 transpose() const
  1238. {
  1239. PVRTMat3 out;
  1240. out.f[0] = f[0]; out.f[3] = f[1]; out.f[6] = f[2];
  1241. out.f[1] = f[3]; out.f[4] = f[4]; out.f[7] = f[5];
  1242. out.f[2] = f[6]; out.f[5] = f[7]; out.f[8] = f[8];
  1243. return out;
  1244. }
  1245. /*!***************************************************************************
  1246. @Function ptr
  1247. @Returns pointer to an array of the elements of this PVRTMat3
  1248. @Description Calculates transpose of this matrix
  1249. *****************************************************************************/
  1250. VERTTYPE *ptr() { return (VERTTYPE*)&f; }
  1251. /*!***************************************************************************
  1252. ** Static factory functions
  1253. *****************************************************************************/
  1254. /*!***************************************************************************
  1255. @Function Identity
  1256. @Returns a PVRTMat3 representation of the 3x3 identity matrix
  1257. @Description Generates an identity matrix
  1258. *****************************************************************************/
  1259. static PVRTMat3 Identity()
  1260. {
  1261. PVRTMat3 out;
  1262. out.f[0] = 1;out.f[1] = 0;out.f[2] = 0;
  1263. out.f[3] = 0;out.f[4] = 1;out.f[5] = 0;
  1264. out.f[6] = 0;out.f[7] = 0;out.f[8] = 1;
  1265. return out;
  1266. }
  1267. /*!***************************************************************************
  1268. @Function RotationX
  1269. @Returns a PVRTMat3 corresponding to the requested rotation
  1270. @Description Calculates a matrix corresponding to a rotation of angle
  1271. degrees about the X axis
  1272. *****************************************************************************/
  1273. static PVRTMat3 RotationX(VERTTYPE angle);
  1274. /*!***************************************************************************
  1275. @Function RotationY
  1276. @Returns a PVRTMat3 corresponding to the requested rotation
  1277. @Description Calculates a matrix corresponding to a rotation of angle
  1278. degrees about the Y axis
  1279. *****************************************************************************/
  1280. static PVRTMat3 RotationY(VERTTYPE angle);
  1281. /*!***************************************************************************
  1282. @Function RotationZ
  1283. @Returns a PVRTMat3 corresponding to the requested rotation
  1284. @Description Calculates a matrix corresponding to a rotation of angle
  1285. degrees about the Z axis
  1286. *****************************************************************************/
  1287. static PVRTMat3 RotationZ(VERTTYPE angle);
  1288. /*!***************************************************************************
  1289. @Function Rotation2D
  1290. @Returns a PVRTMat3 corresponding to the requested rotation
  1291. @Description Calculates a matrix corresponding to a rotation of angle
  1292. degrees about the Z axis
  1293. *****************************************************************************/
  1294. static PVRTMat3 Rotation2D(VERTTYPE angle)
  1295. {
  1296. return RotationZ(angle);
  1297. }
  1298. /*!***************************************************************************
  1299. @Function Scale
  1300. @Returns a PVRTMat3 corresponding to the requested scaling transformation
  1301. @Description Calculates a matrix corresponding to scaling of fx, fy and fz
  1302. times in each axis.
  1303. *****************************************************************************/
  1304. static PVRTMat3 Scale(const VERTTYPE fx,const VERTTYPE fy,const VERTTYPE fz)
  1305. {
  1306. return PVRTMat3(fx,0,0,
  1307. 0,fy,0,
  1308. 0,0,fz);
  1309. }
  1310. /*!***************************************************************************
  1311. @Function Scale2D
  1312. @Returns a PVRTMat3 corresponding to the requested scaling transformation
  1313. @Description Calculates a matrix corresponding to scaling of fx, fy and fz
  1314. times in each axis.
  1315. *****************************************************************************/
  1316. static PVRTMat3 Scale2D(const VERTTYPE fx,const VERTTYPE fy)
  1317. {
  1318. return PVRTMat3(fx,0,0,
  1319. 0,fy,0,
  1320. 0,0,f2vt(1));
  1321. }
  1322. /*!***************************************************************************
  1323. @Function Translation2D
  1324. @Returns a PVRTMat3 corresponding to the requested translation
  1325. @Description Calculates a matrix corresponding to a transformation
  1326. of tx and ty times in each axis.
  1327. *****************************************************************************/
  1328. static PVRTMat3 Translation2D(const VERTTYPE tx, const VERTTYPE ty)
  1329. {
  1330. return PVRTMat3( f2vt(1), 0, 0,
  1331. 0, f2vt(1), 0,
  1332. tx, ty, f2vt(1));
  1333. }
  1334. };
  1335. /*!***************************************************************************
  1336. ** PVRTMat4 4x4 Matrix
  1337. ****************************************************************************/
  1338. struct PVRTMat4 : public PVRTMATRIX
  1339. {
  1340. /*!***************************************************************************
  1341. ** Constructors
  1342. ****************************************************************************/
  1343. /*!***************************************************************************
  1344. @Function PVRTMat4
  1345. @Description Blank constructor.
  1346. *****************************************************************************/
  1347. PVRTMat4(){}
  1348. /*!***************************************************************************
  1349. @Function PVRTMat4
  1350. @Input m0 matrix value
  1351. @Input m1 matrix value
  1352. @Input m2 matrix value
  1353. @Input m3 matrix value
  1354. @Input m4 matrix value
  1355. @Input m5 matrix value
  1356. @Input m6 matrix value
  1357. @Input m7 matrix value
  1358. @Input m8 matrix value
  1359. @Input m9 matrix value
  1360. @Input m10 matrix value
  1361. @Input m11 matrix value
  1362. @Input m12 matrix value
  1363. @Input m13 matrix value
  1364. @Input m14 matrix value
  1365. @Input m15 matrix value
  1366. @Description Constructor from array.
  1367. *****************************************************************************/
  1368. PVRTMat4(VERTTYPE m0,VERTTYPE m1,VERTTYPE m2,VERTTYPE m3,
  1369. VERTTYPE m4,VERTTYPE m5,VERTTYPE m6,VERTTYPE m7,
  1370. VERTTYPE m8,VERTTYPE m9,VERTTYPE m10,VERTTYPE m11,
  1371. VERTTYPE m12,VERTTYPE m13,VERTTYPE m14,VERTTYPE m15)
  1372. {
  1373. f[0]=m0;f[1]=m1;f[2]=m2;f[3]=m3;
  1374. f[4]=m4;f[5]=m5;f[6]=m6;f[7]=m7;
  1375. f[8]=m8;f[9]=m9;f[10]=m10;f[11]=m11;
  1376. f[12]=m12;f[13]=m13;f[14]=m14;f[15]=m15;
  1377. }
  1378. /*!***************************************************************************
  1379. @Function PVRTMat4
  1380. @Input mat A pointer to an array of 16 VERTTYPEs
  1381. @Description Constructor from distinct values.
  1382. *****************************************************************************/
  1383. PVRTMat4(const VERTTYPE* mat)
  1384. {
  1385. VERTTYPE* ptr = f;
  1386. for(int i=0;i<16;i++)
  1387. {
  1388. (*ptr++)=(*mat++);
  1389. }
  1390. }
  1391. /****************************************************************************
  1392. ** PVRTMat4 OPERATORS
  1393. ****************************************************************************/
  1394. /*!***************************************************************************
  1395. @Function ()
  1396. @Input r - row of matrix
  1397. @Input c - column of matrix
  1398. @Returns value of element
  1399. @Description Returns value of the element at row r and colun c of the
  1400. PVRTMat4
  1401. *****************************************************************************/
  1402. VERTTYPE& operator()(const int r, const int c)
  1403. {
  1404. return f[c*4+r];
  1405. }
  1406. /*!***************************************************************************
  1407. @Function ()
  1408. @Input r - row of matrix
  1409. @Input c - column of matrix
  1410. @Returns value of element
  1411. @Description Returns value of the element at row r and colun c of the
  1412. PVRTMat4
  1413. *****************************************************************************/
  1414. const VERTTYPE& operator()(const int r, const int c) const
  1415. {
  1416. return f[c*4+r];
  1417. }
  1418. /*!***************************************************************************
  1419. @Function *
  1420. @Input rhs another PVRTMat4
  1421. @Returns result of multiplication
  1422. @Description Matrix multiplication of two 4x4 matrices.
  1423. *****************************************************************************/
  1424. PVRTMat4 operator*(const PVRTMat4& rhs) const;
  1425. /*!***************************************************************************
  1426. @Function +
  1427. @Input rhs another PVRTMat4
  1428. @Returns result of addition
  1429. @Description element by element addition operator.
  1430. *****************************************************************************/
  1431. PVRTMat4 operator+(const PVRTMat4& rhs) const
  1432. {
  1433. PVRTMat4 out;
  1434. VERTTYPE const *lptr = f, *rptr = rhs.f;
  1435. VERTTYPE *outptr = out.f;
  1436. for(int i=0;i<16;i++)
  1437. {
  1438. (*outptr++) = (*lptr++) + (*rptr++);
  1439. }
  1440. return out;
  1441. }
  1442. /*!***************************************************************************
  1443. @Function -
  1444. @Input rhs another PVRTMat4
  1445. @Returns result of subtraction
  1446. @Description element by element subtraction operator.
  1447. *****************************************************************************/
  1448. PVRTMat4 operator-(const PVRTMat4& rhs) const
  1449. {
  1450. PVRTMat4 out;
  1451. for(int i=0;i<16;i++)
  1452. {
  1453. out.f[i] = f[i]-rhs.f[i];
  1454. }
  1455. return out;
  1456. }
  1457. /*!***************************************************************************
  1458. @Function +=
  1459. @Input rhs another PVRTMat4
  1460. @Returns result of addition and assignment
  1461. @Description element by element addition and assignment operator.
  1462. *****************************************************************************/
  1463. PVRTMat4& operator+=(const PVRTMat4& rhs)
  1464. {
  1465. VERTTYPE *lptr = f;
  1466. VERTTYPE const *rptr = rhs.f;
  1467. for(int i=0;i<16;i++)
  1468. {
  1469. (*lptr++) += (*rptr++);
  1470. }
  1471. return *this;
  1472. }
  1473. /*!***************************************************************************
  1474. @Function -=
  1475. @Input rhs another PVRTMat4
  1476. @Returns result of subtraction and assignment
  1477. @Description element by element subtraction and assignment operator.
  1478. *****************************************************************************/
  1479. PVRTMat4& operator-=(const PVRTMat4& rhs)
  1480. {
  1481. VERTTYPE *lptr = f;
  1482. VERTTYPE const *rptr = rhs.f;
  1483. for(int i=0;i<16;i++)
  1484. {
  1485. (*lptr++) -= (*rptr++);
  1486. }
  1487. return *this;
  1488. }
  1489. /*!***************************************************************************
  1490. @Function *=
  1491. @Input rhs another PVRTMat4
  1492. @Returns result of multiplication and assignment
  1493. @Description Matrix multiplication and assignment of two 4x4 matrices.
  1494. *****************************************************************************/
  1495. PVRTMat4& operator*=(const PVRTMat4& rhs)
  1496. {
  1497. PVRTMat4 result;
  1498. // col 0
  1499. result.f[0] = VERTTYPEMUL(f[0],rhs.f[0])+VERTTYPEMUL(f[4],rhs.f[1])+VERTTYPEMUL(f[8],rhs.f[2])+VERTTYPEMUL(f[12],rhs.f[3]);
  1500. result.f[1] = VERTTYPEMUL(f[1],rhs.f[0])+VERTTYPEMUL(f[5],rhs.f[1])+VERTTYPEMUL(f[9],rhs.f[2])+VERTTYPEMUL(f[13],rhs.f[3]);
  1501. result.f[2] = VERTTYPEMUL(f[2],rhs.f[0])+VERTTYPEMUL(f[6],rhs.f[1])+VERTTYPEMUL(f[10],rhs.f[2])+VERTTYPEMUL(f[14],rhs.f[3]);
  1502. result.f[3] = VERTTYPEMUL(f[3],rhs.f[0])+VERTTYPEMUL(f[7],rhs.f[1])+VERTTYPEMUL(f[11],rhs.f[2])+VERTTYPEMUL(f[15],rhs.f[3]);
  1503. // col 1
  1504. result.f[4] = VERTTYPEMUL(f[0],rhs.f[4])+VERTTYPEMUL(f[4],rhs.f[5])+VERTTYPEMUL(f[8],rhs.f[6])+VERTTYPEMUL(f[12],rhs.f[7]);
  1505. result.f[5] = VERTTYPEMUL(f[1],rhs.f[4])+VERTTYPEMUL(f[5],rhs.f[5])+VERTTYPEMUL(f[9],rhs.f[6])+VERTTYPEMUL(f[13],rhs.f[7]);
  1506. result.f[6] = VERTTYPEMUL(f[2],rhs.f[4])+VERTTYPEMUL(f[6],rhs.f[5])+VERTTYPEMUL(f[10],rhs.f[6])+VERTTYPEMUL(f[14],rhs.f[7]);
  1507. result.f[7] = VERTTYPEMUL(f[3],rhs.f[4])+VERTTYPEMUL(f[7],rhs.f[5])+VERTTYPEMUL(f[11],rhs.f[6])+VERTTYPEMUL(f[15],rhs.f[7]);
  1508. // col 2
  1509. result.f[8] = VERTTYPEMUL(f[0],rhs.f[8])+VERTTYPEMUL(f[4],rhs.f[9])+VERTTYPEMUL(f[8],rhs.f[10])+VERTTYPEMUL(f[12],rhs.f[11]);
  1510. result.f[9] = VERTTYPEMUL(f[1],rhs.f[8])+VERTTYPEMUL(f[5],rhs.f[9])+VERTTYPEMUL(f[9],rhs.f[10])+VERTTYPEMUL(f[13],rhs.f[11]);
  1511. result.f[10] = VERTTYPEMUL(f[2],rhs.f[8])+VERTTYPEMUL(f[6],rhs.f[9])+VERTTYPEMUL(f[10],rhs.f[10])+VERTTYPEMUL(f[14],rhs.f[11]);
  1512. result.f[11] = VERTTYPEMUL(f[3],rhs.f[8])+VERTTYPEMUL(f[7],rhs.f[9])+VERTTYPEMUL(f[11],rhs.f[10])+VERTTYPEMUL(f[15],rhs.f[11]);
  1513. // col 3
  1514. result.f[12] = VERTTYPEMUL(f[0],rhs.f[12])+VERTTYPEMUL(f[4],rhs.f[13])+VERTTYPEMUL(f[8],rhs.f[14])+VERTTYPEMUL(f[12],rhs.f[15]);
  1515. result.f[13] = VERTTYPEMUL(f[1],rhs.f[12])+VERTTYPEMUL(f[5],rhs.f[13])+VERTTYPEMUL(f[9],rhs.f[14])+VERTTYPEMUL(f[13],rhs.f[15]);
  1516. result.f[14] = VERTTYPEMUL(f[2],rhs.f[12])+VERTTYPEMUL(f[6],rhs.f[13])+VERTTYPEMUL(f[10],rhs.f[14])+VERTTYPEMUL(f[14],rhs.f[15]);
  1517. result.f[15] = VERTTYPEMUL(f[3],rhs.f[12])+VERTTYPEMUL(f[7],rhs.f[13])+VERTTYPEMUL(f[11],rhs.f[14])+VERTTYPEMUL(f[15],rhs.f[15]);
  1518. *this = result;
  1519. return *this;
  1520. }
  1521. /*!***************************************************************************
  1522. @Function *
  1523. @Input rhs a single value
  1524. @Returns result of multiplication and assignment
  1525. @Description element multiplication by a single value.
  1526. *****************************************************************************/
  1527. PVRTMat4& operator*(const VERTTYPE rhs)
  1528. {
  1529. for (int i=0; i<16; ++i)
  1530. {
  1531. f[i]*=rhs;
  1532. }
  1533. return *this;
  1534. }
  1535. /*!***************************************************************************
  1536. @Function *=
  1537. @Input rhs a single value
  1538. @Returns result of multiplication and assignment
  1539. @Description element multiplication and assignment by a single value.
  1540. *****************************************************************************/
  1541. PVRTMat4& operator*=(const VERTTYPE rhs)
  1542. {
  1543. for (int i=0; i<16; ++i)
  1544. {
  1545. f[i]*=rhs;
  1546. }
  1547. return *this;
  1548. }
  1549. /*!***************************************************************************
  1550. @Function =
  1551. @Input rhs another PVRTMat4
  1552. @Returns result of assignment
  1553. @Description element assignment operator.
  1554. *****************************************************************************/
  1555. PVRTMat4& operator=(const PVRTMat4& rhs)
  1556. {
  1557. for (int i=0; i<16; ++i)
  1558. {
  1559. f[i] =rhs.f[i];
  1560. }
  1561. return *this;
  1562. }
  1563. /*!***************************************************************************
  1564. @Function *
  1565. @Input rhs a PVRTVec4
  1566. @Returns result of multiplication
  1567. @Description Matrix multiplication of 4x4 matrix and vec3
  1568. *****************************************************************************/
  1569. PVRTVec4 operator*(const PVRTVec4& rhs) const
  1570. {
  1571. PVRTVec4 out;
  1572. out.x = VERTTYPEMUL(rhs.x,f[0])+VERTTYPEMUL(rhs.y,f[4])+VERTTYPEMUL(rhs.z,f[8])+VERTTYPEMUL(rhs.w,f[12]);
  1573. out.y = VERTTYPEMUL(rhs.x,f[1])+VERTTYPEMUL(rhs.y,f[5])+VERTTYPEMUL(rhs.z,f[9])+VERTTYPEMUL(rhs.w,f[13]);
  1574. out.z = VERTTYPEMUL(rhs.x,f[2])+VERTTYPEMUL(rhs.y,f[6])+VERTTYPEMUL(rhs.z,f[10])+VERTTYPEMUL(rhs.w,f[14]);
  1575. out.w = VERTTYPEMUL(rhs.x,f[3])+VERTTYPEMUL(rhs.y,f[7])+VERTTYPEMUL(rhs.z,f[11])+VERTTYPEMUL(rhs.w,f[15]);
  1576. return out;
  1577. }
  1578. /*!***************************************************************************
  1579. @Function *=
  1580. @Input rhs a PVRTVec4
  1581. @Returns result of multiplication and assignment
  1582. @Description Matrix multiplication and assignment of 4x4 matrix and vec3
  1583. *****************************************************************************/
  1584. PVRTVec4 operator*=(const PVRTVec4& rhs) const
  1585. {
  1586. PVRTVec4 out;
  1587. out.x = VERTTYPEMUL(rhs.x,f[0])+VERTTYPEMUL(rhs.y,f[4])+VERTTYPEMUL(rhs.z,f[8])+VERTTYPEMUL(rhs.w,f[12]);
  1588. out.y = VERTTYPEMUL(rhs.x,f[1])+VERTTYPEMUL(rhs.y,f[5])+VERTTYPEMUL(rhs.z,f[9])+VERTTYPEMUL(rhs.w,f[13]);
  1589. out.z = VERTTYPEMUL(rhs.x,f[2])+VERTTYPEMUL(rhs.y,f[6])+VERTTYPEMUL(rhs.z,f[10])+VERTTYPEMUL(rhs.w,f[14]);
  1590. out.w = VERTTYPEMUL(rhs.x,f[3])+VERTTYPEMUL(rhs.y,f[7])+VERTTYPEMUL(rhs.z,f[11])+VERTTYPEMUL(rhs.w,f[15]);
  1591. return out;
  1592. }
  1593. /*!***************************************************************************
  1594. @Function inverse
  1595. @Returns inverse mat4
  1596. @Description Calculates multiplicative inverse of this matrix
  1597. The matrix must be of the form :
  1598. A 0
  1599. C 1
  1600. Where A is a 3x3 matrix and C is a 1x3 matrix.
  1601. *****************************************************************************/
  1602. PVRTMat4 inverse() const;
  1603. /*!***************************************************************************
  1604. @Function inverseEx
  1605. @Returns inverse mat4
  1606. @Description Calculates multiplicative inverse of this matrix
  1607. Uses a linear equation solver and the knowledge that M.M^-1=I.
  1608. Use this fn to calculate the inverse of matrices that
  1609. inverse() cannot.
  1610. *****************************************************************************/
  1611. PVRTMat4 inverseEx() const
  1612. {
  1613. PVRTMat4 out;
  1614. VERTTYPE *ppRows[4];
  1615. VERTTYPE pRes[4];
  1616. VERTTYPE pIn[20];
  1617. int i, j;
  1618. for(i = 0; i < 4; ++i)
  1619. ppRows[i] = &pIn[i * 5];
  1620. /* Solve 4 sets of 4 linear equations */
  1621. for(i = 0; i < 4; ++i)
  1622. {
  1623. for(j = 0; j < 4; ++j)
  1624. {
  1625. ppRows[j][0] = PVRTMat4::Identity().f[i + 4 * j];
  1626. memcpy(&ppRows[j][1], &f[j * 4], 4 * sizeof(VERTTYPE));
  1627. }
  1628. PVRTLinearEqSolve(pRes, (VERTTYPE**)ppRows, 4);
  1629. for(j = 0; j < 4; ++j)
  1630. {
  1631. out.f[i + 4 * j] = pRes[j];
  1632. }
  1633. }
  1634. return out;
  1635. }
  1636. /*!***************************************************************************
  1637. @Function transpose
  1638. @Returns transpose mat4
  1639. @Description Calculates transpose of this matrix
  1640. *****************************************************************************/
  1641. PVRTMat4 transpose() const
  1642. {
  1643. PVRTMat4 out;
  1644. out.f[0] = f[0]; out.f[1] = f[4]; out.f[2] = f[8]; out.f[3] = f[12];
  1645. out.f[4] = f[1]; out.f[5] = f[5]; out.f[6] = f[9]; out.f[7] = f[13];
  1646. out.f[8] = f[2]; out.f[9] = f[6]; out.f[10] = f[10]; out.f[11] = f[14];
  1647. out.f[12] = f[3]; out.f[13] = f[7]; out.f[14] = f[11]; out.f[15] = f[15];
  1648. return out;
  1649. }
  1650. /*!***************************************************************************
  1651. @Function postTranslate
  1652. @Input tx distance of translation in x axis
  1653. @Input ty distance of translation in y axis
  1654. @Input tz distance of translation in z axis
  1655. @Returns Returns this
  1656. @Description Alters the translation component of the transformation matrix.
  1657. *****************************************************************************/
  1658. PVRTMat4& postTranslate(VERTTYPE tx, VERTTYPE ty, VERTTYPE tz)
  1659. {
  1660. f[12] += VERTTYPEMUL(tx,f[0])+VERTTYPEMUL(ty,f[4])+VERTTYPEMUL(tz,f[8]);
  1661. f[13] += VERTTYPEMUL(tx,f[1])+VERTTYPEMUL(ty,f[5])+VERTTYPEMUL(tz,f[9]);
  1662. f[14] += VERTTYPEMUL(tx,f[2])+VERTTYPEMUL(ty,f[6])+VERTTYPEMUL(tz,f[10]);
  1663. f[15] += VERTTYPEMUL(tx,f[3])+VERTTYPEMUL(ty,f[7])+VERTTYPEMUL(tz,f[11]);
  1664. // col(3) += tx * col(0) + ty * col(1) + tz * col(2);
  1665. return *this;
  1666. }
  1667. /*!***************************************************************************
  1668. @Function postTranslate
  1669. @Input tvec translation vector
  1670. @Returns Returns this
  1671. @Description Alters the translation component of the transformation matrix.
  1672. *****************************************************************************/
  1673. PVRTMat4& postTranslate(const PVRTVec3& tvec)
  1674. {
  1675. return postTranslate(tvec.x, tvec.y, tvec.z);
  1676. }
  1677. /*!***************************************************************************
  1678. @Function postTranslate
  1679. @Input tx distance of translation in x axis
  1680. @Input ty distance of translation in y axis
  1681. @Input tz distance of translation in z axis
  1682. @Returns Returns this
  1683. @Description Translates the matrix from the passed parameters
  1684. *****************************************************************************/
  1685. PVRTMat4& preTranslate(VERTTYPE tx, VERTTYPE ty, VERTTYPE tz)
  1686. {
  1687. f[0]+=VERTTYPEMUL(f[3],tx); f[4]+=VERTTYPEMUL(f[7],tx); f[8]+=VERTTYPEMUL(f[11],tx); f[12]+=VERTTYPEMUL(f[15],tx);
  1688. f[1]+=VERTTYPEMUL(f[3],ty); f[5]+=VERTTYPEMUL(f[7],ty); f[9]+=VERTTYPEMUL(f[11],ty); f[13]+=VERTTYPEMUL(f[15],ty);
  1689. f[2]+=VERTTYPEMUL(f[3],tz); f[6]+=VERTTYPEMUL(f[7],tz); f[10]+=VERTTYPEMUL(f[11],tz); f[14]+=VERTTYPEMUL(f[15],tz);
  1690. // row(0) += tx * row(3);
  1691. // row(1) += ty * row(3);
  1692. // row(2) += tz * row(3);
  1693. return *this;
  1694. }
  1695. /*!***************************************************************************
  1696. @Function postTranslate
  1697. @Input tvec translation vector
  1698. @Returns Returns the translation defined by the passed parameters
  1699. @Description Translates the matrix from the passed parameters
  1700. *****************************************************************************/
  1701. PVRTMat4& preTranslate(const PVRTVec3& tvec)
  1702. {
  1703. return preTranslate(tvec.x, tvec.y, tvec.z);
  1704. }
  1705. /*!***************************************************************************
  1706. @Function ptr
  1707. @Returns pointer to an array of the elements of this PVRTMat4
  1708. @Description Calculates transpose of this matrix
  1709. *****************************************************************************/
  1710. VERTTYPE *ptr() { return (VERTTYPE*)&f; }
  1711. /*!***************************************************************************
  1712. ** Static factory functions
  1713. *****************************************************************************/
  1714. /*!***************************************************************************
  1715. @Function Identity
  1716. @Returns a PVRTMat4 representation of the 4x4 identity matrix
  1717. @Description Generates an identity matrix
  1718. *****************************************************************************/
  1719. static PVRTMat4 Identity()
  1720. {
  1721. PVRTMat4 out;
  1722. out.f[0] = f2vt(1);out.f[1] = 0;out.f[2] = 0;out.f[3] = 0;
  1723. out.f[4] = 0;out.f[5] = f2vt(1);out.f[6] = 0;out.f[7] = 0;
  1724. out.f[8] = 0;out.f[9] = 0;out.f[10] = f2vt(1);out.f[11] = 0;
  1725. out.f[12] = 0;out.f[13] = 0;out.f[14] = 0;out.f[15] = f2vt(1);
  1726. return out;
  1727. }
  1728. /*!***************************************************************************
  1729. @Function RotationX
  1730. @Returns a PVRTMat3 corresponding to the requested rotation
  1731. @Description Calculates a matrix corresponding to a rotation of angle
  1732. degrees about the X axis
  1733. *****************************************************************************/
  1734. static PVRTMat4 RotationX(VERTTYPE angle);
  1735. /*!***************************************************************************
  1736. @Function RotationY
  1737. @Returns a PVRTMat3 corresponding to the requested rotation
  1738. @Description Calculates a matrix corresponding to a rotation of angle
  1739. degrees about the Y axis
  1740. *****************************************************************************/
  1741. static PVRTMat4 RotationY(VERTTYPE angle);
  1742. /*!***************************************************************************
  1743. @Function RotationZ
  1744. @Returns a PVRTMat3 corresponding to the requested rotation
  1745. @Description Calculates a matrix corresponding to a rotation of angle
  1746. degrees about the Z axis
  1747. *****************************************************************************/
  1748. static PVRTMat4 RotationZ(VERTTYPE angle);
  1749. /*!***************************************************************************
  1750. @Function Scale
  1751. @Returns a PVRTMat3 corresponding to the requested scaling transformation
  1752. @Description Calculates a matrix corresponding to scaling of fx, fy and fz
  1753. times in each axis.
  1754. *****************************************************************************/
  1755. static PVRTMat4 Scale(const VERTTYPE fx,const VERTTYPE fy,const VERTTYPE fz)
  1756. {
  1757. return PVRTMat4(fx,0,0,0,
  1758. 0,fy,0,0,
  1759. 0,0,fz,0,
  1760. 0,0,0,f2vt(1));
  1761. }
  1762. /*!***************************************************************************
  1763. @Function Translation
  1764. @Returns a PVRTMat4 corresponding to the requested translation
  1765. @Description Calculates a 4x4 matrix corresponding to a transformation
  1766. of tx, ty and tz distance in each axis.
  1767. *****************************************************************************/
  1768. static PVRTMat4 Translation(const VERTTYPE tx, const VERTTYPE ty, const VERTTYPE tz)
  1769. {
  1770. return PVRTMat4(f2vt(1),0,0,0,
  1771. 0,f2vt(1),0,0,
  1772. 0,0,f2vt(1),0,
  1773. tx,ty,tz,f2vt(1));
  1774. }
  1775. /*!***************************************************************************
  1776. ** Clipspace enum
  1777. ** Determines whether clip space Z ranges from -1 to 1 (OGL) or from 0 to 1 (D3D)
  1778. *****************************************************************************/
  1779. enum eClipspace { OGL, D3D };
  1780. /*!***************************************************************************
  1781. @Function Ortho
  1782. @Input left view plane
  1783. @Input top view plane
  1784. @Input right view plane
  1785. @Input bottom view plane
  1786. @Input nearPlane the near rendering plane
  1787. @Input farPlane the far rendering plane
  1788. @Input cs which clipspace convention is being used
  1789. @Input bRotate is the viewport in portrait or landscape mode
  1790. @Returns Returns the orthogonal projection matrix defined by the passed parameters
  1791. @Description Translates the matrix from the passed parameters
  1792. *****************************************************************************/
  1793. static PVRTMat4 Ortho(VERTTYPE left, VERTTYPE top, VERTTYPE right,
  1794. VERTTYPE bottom, VERTTYPE nearPlane, VERTTYPE farPlane, const eClipspace cs, bool bRotate = false)
  1795. {
  1796. VERTTYPE rcplmr = VERTTYPEDIV(VERTTYPE(1),(left - right));
  1797. VERTTYPE rcpbmt = VERTTYPEDIV(VERTTYPE(1),(bottom - top));
  1798. VERTTYPE rcpnmf = VERTTYPEDIV(VERTTYPE(1),(nearPlane - farPlane));
  1799. PVRTMat4 result;
  1800. if (bRotate)
  1801. {
  1802. result.f[0]=0; result.f[4]=VERTTYPEMUL(2,rcplmr); result.f[8]=0; result.f[12]=VERTTYPEMUL(-(right+left),rcplmr);
  1803. result.f[1]=VERTTYPEMUL(-2,rcpbmt); result.f[5]=0; result.f[9]=0; result.f[13]=VERTTYPEMUL((top+bottom),rcpbmt);
  1804. }
  1805. else
  1806. {
  1807. result.f[0]=VERTTYPEMUL(-2,rcplmr); result.f[4]=0; result.f[8]=0; result.f[12]=VERTTYPEMUL(right+left,rcplmr);
  1808. result.f[1]=0; result.f[5]=VERTTYPEMUL(-2,rcpbmt); result.f[9]=0; result.f[13]=VERTTYPEMUL((top+bottom),rcpbmt);
  1809. }
  1810. if (cs == D3D)
  1811. {
  1812. result.f[2]=0; result.f[6]=0; result.f[10]=-rcpnmf; result.f[14]=VERTTYPEMUL(nearPlane,rcpnmf);
  1813. }
  1814. else
  1815. {
  1816. result.f[2]=0; result.f[6]=0; result.f[10]=VERTTYPEMUL(-2,rcpnmf); result.f[14]=VERTTYPEMUL(nearPlane + farPlane,rcpnmf);
  1817. }
  1818. result.f[3]=0; result.f[7]=0; result.f[11]=0; result.f[15]=1;
  1819. return result;
  1820. }
  1821. /*!***************************************************************************
  1822. @Function LookAtRH
  1823. @Input vEye position of 'camera'
  1824. @Input vAt target that camera points at
  1825. @Input vUp up vector for camera
  1826. @Returns Returns the view matrix defined by the passed parameters
  1827. @Description Create a look-at view matrix for a right hand coordinate
  1828. system
  1829. *****************************************************************************/
  1830. static PVRTMat4 LookAtRH(const PVRTVec3& vEye, const PVRTVec3& vAt, const PVRTVec3& vUp)
  1831. { return LookAt(vEye, vAt, vUp, true); }
  1832. /*!***************************************************************************
  1833. @Function LookAtLH
  1834. @Input vEye position of 'camera'
  1835. @Input vAt target that camera points at
  1836. @Input vUp up vector for camera
  1837. @Returns Returns the view matrix defined by the passed parameters
  1838. @Description Create a look-at view matrix for a left hand coordinate
  1839. system
  1840. *****************************************************************************/
  1841. static PVRTMat4 LookAtLH(const PVRTVec3& vEye, const PVRTVec3& vAt, const PVRTVec3& vUp)
  1842. { return LookAt(vEye, vAt, vUp, false); }
  1843. /*!***************************************************************************
  1844. @Function PerspectiveRH
  1845. @Input width width of viewplane
  1846. @Input height height of viewplane
  1847. @Input nearPlane near clipping distance
  1848. @Input farPlane far clipping distance
  1849. @Input cs cs which clipspace convention is being used
  1850. @Input bRotate is the viewport in portrait or landscape mode
  1851. @Returns Perspective matrix
  1852. @Description Create a perspective matrix for a right hand coordinate
  1853. system
  1854. *****************************************************************************/
  1855. static PVRTMat4 PerspectiveRH(VERTTYPE width, VERTTYPE height, VERTTYPE nearPlane,
  1856. VERTTYPE farPlane, const eClipspace cs, bool bRotate = false)
  1857. { return Perspective(width, height, nearPlane, farPlane, cs, true, bRotate); }
  1858. /*!***************************************************************************
  1859. @Function PerspectiveLH
  1860. @Input width width of viewplane
  1861. @Input height height of viewplane
  1862. @Input nearPlane near clipping distance
  1863. @Input farPlane far clipping distance
  1864. @Input cs cs which clipspace convention is being used
  1865. @Input bRotate is the viewport in portrait or landscape mode
  1866. @Returns Perspective matrix
  1867. @Description Create a perspective matrix for a left hand coordinate
  1868. system
  1869. *****************************************************************************/
  1870. static PVRTMat4 PerspectiveLH(VERTTYPE width, VERTTYPE height, VERTTYPE nearPlane, VERTTYPE farPlane, const eClipspace cs, bool bRotate = false)
  1871. { return Perspective(width, height, nearPlane, farPlane, cs, false, bRotate); }
  1872. /*!***************************************************************************
  1873. @Function PerspectiveFloatDepthRH
  1874. @Input width width of viewplane
  1875. @Input height height of viewplane
  1876. @Input nearPlane near clipping distance
  1877. @Input cs cs which clipspace convention is being used
  1878. @Input bRotate is the viewport in portrait or landscape mode
  1879. @Returns Perspective matrix
  1880. @Description Create a perspective matrix for a right hand coordinate
  1881. system
  1882. *****************************************************************************/
  1883. static PVRTMat4 PerspectiveFloatDepthRH(VERTTYPE width, VERTTYPE height, VERTTYPE nearPlane, const eClipspace cs, bool bRotate = false)
  1884. { return PerspectiveFloatDepth(width, height, nearPlane, cs, true, bRotate); }
  1885. /*!***************************************************************************
  1886. @Function PerspectiveFloatDepthLH
  1887. @Input width width of viewplane
  1888. @Input height height of viewplane
  1889. @Input nearPlane near clipping distance
  1890. @Input cs cs which clipspace convention is being used
  1891. @Input bRotate is the viewport in portrait or landscape mode
  1892. @Returns Perspective matrix
  1893. @Description Create a perspective matrix for a left hand coordinate
  1894. system
  1895. *****************************************************************************/
  1896. static PVRTMat4 PerspectiveFloatDepthLH(VERTTYPE width, VERTTYPE height, VERTTYPE nearPlane, const eClipspace cs, bool bRotate = false)
  1897. { return PerspectiveFloatDepth(width, height, nearPlane, cs, false, bRotate); }
  1898. /*!***************************************************************************
  1899. @Function PerspectiveFovRH
  1900. @Input fovy angle of view (vertical)
  1901. @Input aspect aspect ratio of view
  1902. @Input nearPlane near clipping distance
  1903. @Input farPlane far clipping distance
  1904. @Input cs cs which clipspace convention is being used
  1905. @Input bRotate is the viewport in portrait or landscape mode
  1906. @Returns Perspective matrix
  1907. @Description Create a perspective matrix for a right hand coordinate
  1908. system
  1909. *****************************************************************************/
  1910. static PVRTMat4 PerspectiveFovRH(VERTTYPE fovy, VERTTYPE aspect, VERTTYPE nearPlane, VERTTYPE farPlane, const eClipspace cs, bool bRotate = false)
  1911. { return PerspectiveFov(fovy, aspect, nearPlane, farPlane, cs, true, bRotate); }
  1912. /*!***************************************************************************
  1913. @Function PerspectiveFovLH
  1914. @Input fovy angle of view (vertical)
  1915. @Input aspect aspect ratio of view
  1916. @Input nearPlane near clipping distance
  1917. @Input farPlane far clipping distance
  1918. @Input cs cs which clipspace convention is being used
  1919. @Input bRotate is the viewport in portrait or landscape mode
  1920. @Returns Perspective matrix
  1921. @Description Create a perspective matrix for a left hand coordinate
  1922. system
  1923. *****************************************************************************/
  1924. static PVRTMat4 PerspectiveFovLH(VERTTYPE fovy, VERTTYPE aspect, VERTTYPE nearPlane, VERTTYPE farPlane, const eClipspace cs, bool bRotate = false)
  1925. { return PerspectiveFov(fovy, aspect, nearPlane, farPlane, cs, false, bRotate); }
  1926. /*!***************************************************************************
  1927. @Function PerspectiveFovRH
  1928. @Input fovy angle of view (vertical)
  1929. @Input aspect aspect ratio of view
  1930. @Input nearPlane near clipping distance
  1931. @Input cs cs which clipspace convention is being used
  1932. @Input bRotate is the viewport in portrait or landscape mode
  1933. @Returns Perspective matrix
  1934. @Description Create a perspective matrix for a right hand coordinate
  1935. system
  1936. *****************************************************************************/
  1937. static PVRTMat4 PerspectiveFovFloatDepthRH(VERTTYPE fovy, VERTTYPE aspect, VERTTYPE nearPlane, const eClipspace cs, bool bRotate = false)
  1938. { return PerspectiveFovFloatDepth(fovy, aspect, nearPlane, cs, true, bRotate); }
  1939. /*!***************************************************************************
  1940. @Function PerspectiveFovLH
  1941. @Input fovy angle of view (vertical)
  1942. @Input aspect aspect ratio of view
  1943. @Input nearPlane near clipping distance
  1944. @Input cs cs which clipspace convention is being used
  1945. @Input bRotate is the viewport in portrait or landscape mode
  1946. @Returns Perspective matrix
  1947. @Description Create a perspective matrix for a left hand coordinate
  1948. system
  1949. *****************************************************************************/
  1950. static PVRTMat4 PerspectiveFovFloatDepthLH(VERTTYPE fovy, VERTTYPE aspect, VERTTYPE nearPlane, const eClipspace cs, bool bRotate = false)
  1951. { return PerspectiveFovFloatDepth(fovy, aspect, nearPlane, cs, false, bRotate); }
  1952. /*!***************************************************************************
  1953. @Function LookAt
  1954. @Input vEye position of 'camera'
  1955. @Input vAt target that camera points at
  1956. @Input vUp up vector for camera
  1957. @Input bRightHanded handedness of coordinate system
  1958. @Returns Returns the view matrix defined by the passed parameters
  1959. @Description Create a look-at view matrix
  1960. *****************************************************************************/
  1961. static PVRTMat4 LookAt(const PVRTVec3& vEye, const PVRTVec3& vAt, const PVRTVec3& vUp, bool bRightHanded)
  1962. {
  1963. PVRTVec3 vForward, vUpNorm, vSide;
  1964. PVRTMat4 result;
  1965. vForward = (bRightHanded) ? vEye - vAt : vAt - vEye;
  1966. vForward.normalize();
  1967. vUpNorm = vUp.normalized();
  1968. vSide = vUpNorm.cross( vForward);
  1969. vUpNorm = vForward.cross(vSide);
  1970. result.f[0]=vSide.x; result.f[4]=vSide.y; result.f[8]=vSide.z; result.f[12]=0;
  1971. result.f[1]=vUpNorm.x; result.f[5]=vUpNorm.y; result.f[9]=vUpNorm.z; result.f[13]=0;
  1972. result.f[2]=vForward.x; result.f[6]=vForward.y; result.f[10]=vForward.z; result.f[14]=0;
  1973. result.f[3]=0; result.f[7]=0; result.f[11]=0; result.f[15]=f2vt(1);
  1974. result.postTranslate(-vEye);
  1975. return result;
  1976. }
  1977. /*!***************************************************************************
  1978. @Function Perspective
  1979. @Input width width of viewplane
  1980. @Input height height of viewplane
  1981. @Input nearPlane near clipping distance
  1982. @Input farPlane far clipping distance
  1983. @Input cs which clipspace convention is being used
  1984. @Input bRightHanded handedness of coordinate system
  1985. @Input bRotate is the viewport in portrait or landscape mode
  1986. @Returns Perspective matrix
  1987. @Description Create a perspective matrix
  1988. *****************************************************************************/
  1989. static PVRTMat4 Perspective(
  1990. VERTTYPE width, VERTTYPE height,
  1991. VERTTYPE nearPlane, VERTTYPE farPlane,
  1992. const eClipspace cs,
  1993. bool bRightHanded,
  1994. bool bRotate = false)
  1995. {
  1996. VERTTYPE n2 = VERTTYPEMUL(f2vt(2),nearPlane);
  1997. VERTTYPE rcpnmf = VERTTYPEDIV(f2vt(1),(nearPlane - farPlane));
  1998. PVRTMat4 result;
  1999. if (bRotate)
  2000. {
  2001. result.f[0] = 0; result.f[4]=VERTTYPEDIV(-n2,width); result.f[8]=0; result.f[12]=0;
  2002. result.f[1] = VERTTYPEDIV(n2,height); result.f[5]=0; result.f[9]=0; result.f[13]=0;
  2003. }
  2004. else
  2005. {
  2006. result.f[0] = VERTTYPEDIV(n2,width); result.f[4]=0; result.f[8]=0; result.f[12]=0;
  2007. result.f[1] = 0; result.f[5]=VERTTYPEDIV(n2,height); result.f[9]=0; result.f[13]=0;
  2008. }
  2009. if (cs == D3D)
  2010. {
  2011. result.f[2] = 0; result.f[6]=0; result.f[10]=VERTTYPEMUL(farPlane,rcpnmf); result.f[14]=VERTTYPEMUL(VERTTYPEMUL(farPlane,rcpnmf),nearPlane);
  2012. }
  2013. else
  2014. {
  2015. result.f[2] = 0; result.f[6]=0; result.f[10]=VERTTYPEMUL(farPlane+nearPlane,rcpnmf); result.f[14]=VERTTYPEMUL(VERTTYPEMUL(farPlane,rcpnmf),n2);
  2016. }
  2017. result.f[3] = 0; result.f[7]=0; result.f[11]=f2vt(-1); result.f[15]=0;
  2018. if (!bRightHanded)
  2019. {
  2020. result.f[10] = VERTTYPEMUL(result.f[10], f2vt(-1));
  2021. result.f[11] = f2vt(1);
  2022. }
  2023. return result;
  2024. }
  2025. /*!***************************************************************************
  2026. @Function Perspective
  2027. @Input width width of viewplane
  2028. @Input height height of viewplane
  2029. @Input nearPlane near clipping distance
  2030. @Input cs which clipspace convention is being used
  2031. @Input bRightHanded handedness of coordinate system
  2032. @Input bRotate is the viewport in portrait or landscape mode
  2033. @Returns Perspective matrix
  2034. @Description perspective calculation where far plane is assumed to be at an infinite distance and the screen
  2035. space Z is inverted
  2036. *****************************************************************************/
  2037. static PVRTMat4 PerspectiveFloatDepth(
  2038. VERTTYPE width, VERTTYPE height,
  2039. VERTTYPE nearPlane,
  2040. const eClipspace cs,
  2041. bool bRightHanded,
  2042. bool bRotate = false)
  2043. {
  2044. VERTTYPE n2 = VERTTYPEMUL(2,nearPlane);
  2045. PVRTMat4 result;
  2046. if (bRotate)
  2047. {
  2048. result.f[0] = 0; result.f[4]=VERTTYPEDIV(-n2,width); result.f[8]=0; result.f[12]=0;
  2049. result.f[1] = VERTTYPEDIV(n2,height); result.f[5]=0; result.f[9]=0; result.f[13]=0;
  2050. }
  2051. else
  2052. {
  2053. result.f[0] = VERTTYPEDIV(n2,width); result.f[4]=0; result.f[8]=0; result.f[12]=0;
  2054. result.f[1] = 0; result.f[5]=VERTTYPEDIV(n2,height); result.f[9]=0; result.f[13]=0;
  2055. }
  2056. if (cs == D3D)
  2057. {
  2058. result.f[2] = 0; result.f[6]=0; result.f[10]=0; result.f[14]=nearPlane;
  2059. }
  2060. else
  2061. {
  2062. result.f[2] = 0; result.f[6]=0; result.f[10]=(bRightHanded?(VERTTYPE)1:(VERTTYPE)-1); result.f[14]=n2;
  2063. }
  2064. result.f[3] = (VERTTYPE)0; result.f[7]=(VERTTYPE)0; result.f[11]= (bRightHanded?(VERTTYPE)-1:(VERTTYPE)1); result.f[15]=(VERTTYPE)0;
  2065. return result;
  2066. }
  2067. /*!***************************************************************************
  2068. @Function Perspective
  2069. @Input fovy angle of view (vertical)
  2070. @Input aspect aspect ratio of view
  2071. @Input nearPlane near clipping distance
  2072. @Input farPlane far clipping distance
  2073. @Input cs cs which clipspace convention is being used
  2074. @Input bRightHanded handedness of coordinate system
  2075. @Input bRotate is the viewport in portrait or landscape mode
  2076. @Returns Perspective matrix
  2077. @Description perspective calculation where field of view is used instead of near plane dimensions
  2078. *****************************************************************************/
  2079. static PVRTMat4 PerspectiveFov(
  2080. VERTTYPE fovy, VERTTYPE aspect,
  2081. VERTTYPE nearPlane, VERTTYPE farPlane,
  2082. const eClipspace cs,
  2083. bool bRightHanded,
  2084. bool bRotate = false)
  2085. {
  2086. VERTTYPE height = VERTTYPEMUL(VERTTYPEMUL(f2vt(2.0f),nearPlane),PVRTTAN(VERTTYPEMUL(fovy,f2vt(0.5f))));
  2087. if (bRotate) return Perspective(height, VERTTYPEDIV(height,aspect), nearPlane, farPlane, cs, bRightHanded, bRotate);
  2088. return Perspective(VERTTYPEMUL(height,aspect), height, nearPlane, farPlane, cs, bRightHanded, bRotate);
  2089. }
  2090. /*!***************************************************************************
  2091. @Function Perspective
  2092. @Input fovy angle of view (vertical)
  2093. @Input aspect aspect ratio of view
  2094. @Input nearPlane near clipping distance
  2095. @Input cs cs which clipspace convention is being used
  2096. @Input bRightHanded handedness of coordinate system
  2097. @Input bRotate is the viewport in portrait or landscape mode
  2098. @Returns Perspective matrix
  2099. @Description perspective calculation where field of view is used instead of near plane dimensions
  2100. and far plane is assumed to be at an infinite distance with inverted Z range
  2101. *****************************************************************************/
  2102. static PVRTMat4 PerspectiveFovFloatDepth(
  2103. VERTTYPE fovy, VERTTYPE aspect,
  2104. VERTTYPE nearPlane,
  2105. const eClipspace cs,
  2106. bool bRightHanded,
  2107. bool bRotate = false)
  2108. {
  2109. VERTTYPE height = VERTTYPEMUL(VERTTYPEMUL(2,nearPlane), PVRTTAN(VERTTYPEMUL(fovy,0.5)));
  2110. if (bRotate) return PerspectiveFloatDepth(height, VERTTYPEDIV(height,aspect), nearPlane, cs, bRightHanded, bRotate);
  2111. return PerspectiveFloatDepth(VERTTYPEMUL(height,aspect), height, nearPlane, cs, bRightHanded, bRotate);
  2112. }
  2113. };
  2114. #endif /*__PVRTVECTOR_H__*/
  2115. /*****************************************************************************
  2116. End of file (PVRTVector.h)
  2117. *****************************************************************************/