matrixSet.h

Engine/source/math/util/matrixSet.h

More...

Classes:

Public Variables

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      ObjectToScreen,      // World * View * Proj
 48      ObjectToCamera,      // World * View
 49      WorldToObject,       // World^-1
 50      CameraToWorld,       // View^-1
 51      CameraToObject,      // (World * View)^-1
 52      WorldToScreen,       // View * Projection
 53      SceneView,           // The View matrix for the SceneState
 54      SceneProjection,     // The Projection matrix for the SceneState
 55      NumTransforms,
 56   };
 57
 58   MatrixF mTransform[NumTransforms];
 59   MatrixEvalDelegate mEvalDelegate[NumTransforms];
 60
 61   const MatrixF *mViewSource;
 62   const MatrixF *mProjectionSource;
 63
 64   MATRIX_SET_GET_VALUE(ObjectToWorld);
 65   MATRIX_SET_GET_VALUE(WorldToCamera);
 66   MATRIX_SET_GET_VALUE(CameraToScreen);
 67   MATRIX_SET_GET_VALUE(ObjectToCamera);
 68   MATRIX_SET_GET_VALUE(WorldToObject);
 69   MATRIX_SET_GET_VALUE(CameraToWorld);
 70   MATRIX_SET_GET_VALUE(ObjectToScreen);
 71   MATRIX_SET_GET_VALUE(CameraToObject);
 72   MATRIX_SET_GET_VALUE(WorldToScreen);
 73   MATRIX_SET_GET_VALUE(SceneView);
 74   MATRIX_SET_GET_VALUE(SceneProjection);
 75
 76   MATRIX_SET_IS_INVERSE_OF(WorldToObject, ObjectToWorld);
 77   MATRIX_SET_IS_INVERSE_OF(CameraToWorld, WorldToCamera);
 78
 79   MATRIX_SET_MULT_ASSIGN(WorldToCamera, ObjectToWorld, ObjectToCamera);
 80   MATRIX_SET_IS_INVERSE_OF(CameraToObject, ObjectToCamera);
 81
 82   MATRIX_SET_MULT_ASSIGN(CameraToScreen, WorldToCamera, WorldToScreen);
 83
 84   MATRIX_SET_MULT_ASSIGN(WorldToScreen, ObjectToWorld, ObjectToScreen);
 85
 86public:
 87   MatrixSet();
 88
 89   // Direct accessors
 90   inline const MatrixF &getObjectToWorld() const { return mTransform[ObjectToWorld]; }
 91   inline const MatrixF &getWorldToCamera() const { return mTransform[WorldToCamera]; }
 92   inline const MatrixF &getCameraToScreen() const { return mTransform[CameraToScreen]; }
 93
 94   // Delegate driven, lazy-evaluation accessors
 95   inline const MatrixF &getWorldToScreen() const { return mEvalDelegate[WorldToScreen](); }
 96   inline const MatrixF &getWorldViewProjection() const { return mEvalDelegate[ObjectToScreen](); }
 97   inline const MatrixF &getWorldToObject() const { return mEvalDelegate[WorldToObject](); }   
 98   inline const MatrixF &getCameraToWorld() const { return mEvalDelegate[CameraToWorld](); }
 99   inline const MatrixF &getObjectToCamera() const { return mEvalDelegate[ObjectToCamera](); }
100   inline const MatrixF &getCameraToObject() const { return mEvalDelegate[CameraToObject](); }
101
102   // Assignment for the world/view/projection matrices
103   inline void setWorld(const MatrixF &world)
104   {
105      mTransform[ObjectToWorld] = world;
106      mEvalDelegate[WorldToObject].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(WorldToObject, ObjectToWorld));
107      mEvalDelegate[ObjectToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToScreen, ObjectToWorld, ObjectToScreen));
108      mEvalDelegate[ObjectToCamera].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToCamera, ObjectToWorld, ObjectToCamera));
109      mEvalDelegate[CameraToObject].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(CameraToObject, ObjectToCamera));
110   }
111
112   inline void setView(const MatrixF &view)
113   {
114      if(&view == mViewSource)
115         return;
116
117      mViewSource = &view;
118      mTransform[WorldToCamera] = view;
119      mEvalDelegate[CameraToWorld].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(CameraToWorld, WorldToCamera)); 
120      mEvalDelegate[ObjectToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToScreen, ObjectToWorld, ObjectToScreen));
121      mEvalDelegate[WorldToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(CameraToScreen, WorldToCamera, WorldToScreen));
122      mEvalDelegate[ObjectToCamera].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToCamera, ObjectToWorld, ObjectToCamera));
123      mEvalDelegate[CameraToObject].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(CameraToObject, ObjectToCamera));
124   }
125
126   inline void setProjection(const MatrixF &projection)
127   {
128      if(&projection == mProjectionSource)
129         return;
130
131      mProjectionSource = &projection;
132      mTransform[CameraToScreen] = projection;
133      mEvalDelegate[ObjectToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToScreen, ObjectToWorld, ObjectToScreen));
134      mEvalDelegate[WorldToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(CameraToScreen, WorldToCamera, WorldToScreen));
135   }
136
137   void setSceneView(const MatrixF &view)
138   {
139      mViewSource = NULL;
140      setView(view);
141      mViewSource = &mTransform[WorldToCamera];
142      mTransform[SceneView] = view;
143   }
144
145   void setSceneProjection(const MatrixF &projection)
146   {
147      mProjectionSource = NULL;
148      setProjection(projection);
149      mProjectionSource = &mTransform[CameraToScreen];
150      mTransform[SceneProjection] = projection;
151   }
152
153   void restoreSceneViewProjection()
154   {
155      mViewSource = NULL;
156      mProjectionSource = NULL;
157      setView(mTransform[SceneView]);
158      setProjection(mTransform[SceneProjection]);
159      mViewSource = &mTransform[WorldToCamera];
160      mProjectionSource = &mTransform[CameraToScreen];
161   }
162}
163
164dALIGN_END;
165
166#endif // _MATRIXSET_H_
167