matrixSet.h
Engine/source/math/util/matrixSet.h
Classes:
class
Public Variables
dALIGN_BEGIN class MatrixSet
Detailed Description
Public Variables
dALIGN_BEGIN class MatrixSet dALIGN_END
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23#ifndef _MATRIXSET_H_ 24#define _MATRIXSET_H_ 25 26#ifndef _MMATRIX_H_ 27#include "math/mMatrix.h" 28#endif 29#ifndef _UTIL_DELEGATE_H_ 30#include "core/util/delegate.h" 31#endif 32#ifndef _MATRIXSETDELEGATES_H_ 33#include "math/util/matrixSetDelegateMethods.h" 34#endif 35 36 37dALIGN_BEGIN 38 39class MatrixSet 40{ 41 typedef Delegate<const MatrixF &()> MatrixEvalDelegate; 42 enum _Transforms 43 { 44 ObjectToWorld = 0, // World 45 WorldToCamera, // View 46 CameraToScreen, // Projection 47 ScreenToCamera, // Projection^-1 48 ObjectToScreen, // World * View * Proj 49 ObjectToCamera, // World * View 50 WorldToObject, // World^-1 51 CameraToWorld, // View^-1 52 CameraToObject, // (World * View)^-1 53 WorldToScreen, // View * Projection 54 SceneView, // The View matrix for the SceneState 55 SceneProjection, // The Projection matrix for the SceneState 56 NumTransforms, 57 }; 58 59 MatrixF mTransform[NumTransforms]; 60 MatrixEvalDelegate mEvalDelegate[NumTransforms]; 61 62 const MatrixF *mViewSource; 63 const MatrixF *mProjectionSource; 64 65 MATRIX_SET_GET_VALUE(ObjectToWorld); 66 MATRIX_SET_GET_VALUE(WorldToCamera); 67 MATRIX_SET_GET_VALUE(CameraToScreen); 68 MATRIX_SET_GET_VALUE(ScreenToCamera); 69 MATRIX_SET_GET_VALUE(ObjectToCamera); 70 MATRIX_SET_GET_VALUE(WorldToObject); 71 MATRIX_SET_GET_VALUE(CameraToWorld); 72 MATRIX_SET_GET_VALUE(ObjectToScreen); 73 MATRIX_SET_GET_VALUE(CameraToObject); 74 MATRIX_SET_GET_VALUE(WorldToScreen); 75 MATRIX_SET_GET_VALUE(SceneView); 76 MATRIX_SET_GET_VALUE(SceneProjection); 77 78 MATRIX_SET_IS_INVERSE_OF(WorldToObject, ObjectToWorld); 79 MATRIX_SET_IS_INVERSE_OF(CameraToWorld, WorldToCamera); 80 81 MATRIX_SET_MULT_ASSIGN(WorldToCamera, ObjectToWorld, ObjectToCamera); 82 MATRIX_SET_IS_INVERSE_OF(CameraToObject, ObjectToCamera); 83 84 MATRIX_SET_MULT_ASSIGN(CameraToScreen, WorldToCamera, WorldToScreen); 85 86 MATRIX_SET_MULT_ASSIGN(WorldToScreen, ObjectToWorld, ObjectToScreen); 87 88 MATRIX_SET_IS_INVERSE_OF(ScreenToCamera, CameraToScreen); 89 90public: 91 MatrixSet(); 92 93 // Direct accessors 94 inline const MatrixF &getObjectToWorld() const { return mTransform[ObjectToWorld]; } 95 inline const MatrixF &getWorldToCamera() const { return mTransform[WorldToCamera]; } 96 inline const MatrixF &getCameraToScreen() const { return mTransform[CameraToScreen]; } 97 inline const MatrixF &getScreenToCamera() const { return mTransform[ScreenToCamera]; } 98 99 // Delegate driven, lazy-evaluation accessors 100 inline const MatrixF &getWorldToScreen() const { return mEvalDelegate[WorldToScreen](); } 101 inline const MatrixF &getWorldViewProjection() const { return mEvalDelegate[ObjectToScreen](); } 102 inline const MatrixF &getWorldToObject() const { return mEvalDelegate[WorldToObject](); } 103 inline const MatrixF &getCameraToWorld() const { return mEvalDelegate[CameraToWorld](); } 104 inline const MatrixF &getObjectToCamera() const { return mEvalDelegate[ObjectToCamera](); } 105 inline const MatrixF &getCameraToObject() const { return mEvalDelegate[CameraToObject](); } 106 107 // Assignment for the world/view/projection matrices 108 inline void setWorld(const MatrixF &world) 109 { 110 mTransform[ObjectToWorld] = world; 111 mEvalDelegate[WorldToObject].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(WorldToObject, ObjectToWorld)); 112 mEvalDelegate[ObjectToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToScreen, ObjectToWorld, ObjectToScreen)); 113 mEvalDelegate[ObjectToCamera].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToCamera, ObjectToWorld, ObjectToCamera)); 114 mEvalDelegate[CameraToObject].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(CameraToObject, ObjectToCamera)); 115 } 116 117 inline void setView(const MatrixF &view) 118 { 119 if(&view == mViewSource) 120 return; 121 122 mViewSource = &view; 123 mTransform[WorldToCamera] = view; 124 mEvalDelegate[CameraToWorld].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(CameraToWorld, WorldToCamera)); 125 mEvalDelegate[ObjectToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToScreen, ObjectToWorld, ObjectToScreen)); 126 mEvalDelegate[WorldToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(CameraToScreen, WorldToCamera, WorldToScreen)); 127 mEvalDelegate[ObjectToCamera].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToCamera, ObjectToWorld, ObjectToCamera)); 128 mEvalDelegate[CameraToObject].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(CameraToObject, ObjectToCamera)); 129 } 130 131 inline void setProjection(const MatrixF &projection) 132 { 133 if(&projection == mProjectionSource) 134 return; 135 136 mProjectionSource = &projection; 137 mTransform[CameraToScreen] = projection; 138 mEvalDelegate[ObjectToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToScreen, ObjectToWorld, ObjectToScreen)); 139 mEvalDelegate[WorldToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(CameraToScreen, WorldToCamera, WorldToScreen)); 140 mEvalDelegate[ScreenToCamera].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(ScreenToCamera, CameraToScreen)); 141 } 142 143 void setSceneView(const MatrixF &view) 144 { 145 mViewSource = NULL; 146 setView(view); 147 mViewSource = &mTransform[WorldToCamera]; 148 mTransform[SceneView] = view; 149 } 150 151 void setSceneProjection(const MatrixF &projection) 152 { 153 mProjectionSource = NULL; 154 setProjection(projection); 155 mProjectionSource = &mTransform[CameraToScreen]; 156 mTransform[SceneProjection] = projection; 157 } 158 159 void restoreSceneViewProjection() 160 { 161 mViewSource = NULL; 162 mProjectionSource = NULL; 163 setView(mTransform[SceneView]); 164 setProjection(mTransform[SceneProjection]); 165 mViewSource = &mTransform[WorldToCamera]; 166 mProjectionSource = &mTransform[CameraToScreen]; 167 } 168} 169 170dALIGN_END; 171 172#endif // _MATRIXSET_H_ 173