All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
InteriorRenderable.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Interiors.h"
6 #include "CulledMeshRenderable.h"
7 #include "LayerIds.h"
8 #include "Rendering.h"
9 #include "Culling.h"
10 #include "VectorMath.h"
11 #include "InteriorsStencilMirrorType.h"
12 #include "InteriorsRenderableLayer.h"
13 
14 #include <vector>
15 #include <string>
16 
17 namespace Eegeo
18 {
19  namespace Rendering
20  {
21  namespace Renderables
22  {
24  {
25  public:
26  struct RenderState
27  {
28  RenderState()
29  : m_cameraLocationEcef(dv3::Zero())
30  , m_positionUnpackTransform(m44::CreateIdentity())
31  , m_positionModelTransform(m44::CreateIdentity())
32  , m_normalModelTransform(m44::CreateIdentity())
33  , m_viewTransform(m44::CreateIdentity())
34  , m_projectionTransform(m44::CreateIdentity())
35  , m_worldToEcefTangentTransform(m44::CreateIdentity())
36  , m_cameraRelativeTransform(m44::CreateIdentity())
37  {}
38 
39 
40  RenderState(const dv3& cameraLocationEcef,
41  const m44& positionUnpackTransform,
42  const m44& positionModelTransform,
43  const m44& normalModelTransform,
44  const m44& viewTransform,
45  const m44& projectionTransform,
46  const m44& worldToEcefTangentTransform,
47  const m44& cameraRelativeTransform,
48  const v3& cameraRelativeSortOrigin)
49  : m_cameraLocationEcef(cameraLocationEcef)
50  , m_positionUnpackTransform(positionUnpackTransform)
51  , m_positionModelTransform(positionModelTransform)
52  , m_normalModelTransform(normalModelTransform)
53  , m_viewTransform(viewTransform)
54  , m_projectionTransform(projectionTransform)
55  , m_worldToEcefTangentTransform(worldToEcefTangentTransform)
56  , m_cameraRelativeTransform(cameraRelativeTransform)
57  , m_cameraRelativeSortOrigin(cameraRelativeSortOrigin)
58  {}
59 
60  dv3 CameraLocationEcef() const { return m_cameraLocationEcef; }
61  m44 PositionUnpackTransform() const { return m_positionUnpackTransform; }
62  m44 PositionModelTransform() const { return m_positionModelTransform; }
63  m44 NormalModelTransform() const { return m_normalModelTransform; }
64  m44 ViewTransform() const { return m_viewTransform; }
65  m44 ProjectionTransform() const { return m_projectionTransform; }
66  m44 WorldToEcefTangentTransform() const { return m_worldToEcefTangentTransform; }
67  m44 CameraRelativeTransform() const { return m_cameraRelativeTransform; }
68  const v3& CameraRelativeSortOrigin() const { return m_cameraRelativeSortOrigin; }
69 
70  m44 UnpackModel() const;
71  m44 UnpackMVP() const;
72  v3 WorldViewDirection() const;
73 
74  private:
75  dv3 m_cameraLocationEcef;
76  m44 m_positionUnpackTransform;
77  m44 m_positionModelTransform;
78  m44 m_normalModelTransform;
79  m44 m_viewTransform;
80  m44 m_projectionTransform;
81  m44 m_worldToEcefTangentTransform;
82  m44 m_cameraRelativeTransform;
83  v3 m_cameraRelativeSortOrigin;
84  };
85 
86 
87  InteriorRenderable(LayerIds::Values layerId,
88  int floorIndex,
89  Materials::IMaterial* material,
90  Rendering::Mesh* pMesh,
91  Culling::CullingVolumeTree* pCullingVolumeTree,
92  const VertexLayouts::VertexBinding& vertexBinding,
93  Eegeo::dv3 ecefLocalOrigin,
94  Eegeo::v4 positionBoundsMin,
95  Eegeo::v4 positionBoundsMax,
96  Eegeo::v2 uvBoundsMin,
97  Eegeo::v2 uvBoundsMax,
98  const std::string& renderableId,
100  const float interiorTerrainHeight,
101  bool ownsMesh,
102  const std::string& interiorIdentifier);
103 
104  virtual ~InteriorRenderable();
105 
106  virtual void OnMaterialChanged(const Rendering::Materials::IMaterial* pMaterial, Rendering::VertexLayouts::VertexBindingPool& vertexBindingPool);
107 
108  const std::string& GetRenderableId() const;
109 
110  virtual void SetColor(const v4& color);
111  const v4& GetColor() const;
112  void SetSaturation(float saturation);
113  float GetSaturation() const { return m_saturation; }
114  virtual bool NeedsStencilMirrorDuplicates() const { return m_stencilMirrorType == Resources::Interiors::StencilMirror_Mirror; }
115  bool IsStencilMirrorMask() const { return m_stencilMirrorType == Resources::Interiors::StencilMirror_Mask; }
116  Resources::Interiors::InteriorsStencilMirrorType GetStencilMirrorType() const { return m_stencilMirrorType; }
117  v4 GetStencilMirrorClearColor() const;
118  bool NeedsBlend() const;
119  float GetInteriorTerrainHeight() const { return m_interiorTerrainHeight; }
120  const std::string& GetInteriorId() const;
121 
122  int GetFloorIndex() const { return m_floorIndex; }
123  Resources::Interiors::InteriorsRenderableLayer::Type GetMaterialRenderableLayer() const { return m_materialRenderableLayer; }
124 
125  const Eegeo::v2& GetUVBoundsMin() const { return m_uvBoundsMin; }
126  const Eegeo::v2& GetUVBoundsMax() const { return m_uvBoundsMax; }
127  const Eegeo::v4& GetPositionBoundsMin() const { return m_positionBoundsMin; }
128  const Eegeo::v4& GetPositionBoundsMax() const { return m_positionBoundsMax; }
129 
130  RenderState CalcRenderState(const Rendering::RenderContext& renderContext, float environmentFlatteningScale) const;
131 
132  void SetRenderState(const RenderState& renderState) { m_renderState = renderState; }
133  RenderState GetRenderState() const { return m_renderState; }
134 
135  virtual void SetModelTransform(const m44& modelToLocalEcefTransform);
136 
137  void SetNormalModelTransform(const m44& normalToLocalEcefTransform);
138 
139  void SetMaterialIsTranslucent(bool materialIsTranslucent);
140 
141  void SetMaterialRenderableLayer(Eegeo::Resources::Interiors::InteriorsRenderableLayer::Type materialRenderableLayer);
142 
143  void SetSortKeyTranslucency(bool depthSorted);
144 
145  void SetStencilMirrorType(Resources::Interiors::InteriorsStencilMirrorType stencilMirrorType);
146 
147  void SetStencilMirrorClearColor(const v3& stencilMirrorClearColor);
148 
149  virtual bool UpdateVisibility(const Eegeo::Geometry::Frustum& viewFrustum, Culling::CullingVolumeVisibilityUpdater& visibilityUpdater, float environmentScale);
150 
151  u32 CalcSortKeyDepthOpaque() const;
152 
153  static u32 CalcSortKeyDepthOpaque(int floorIndex, Resources::Interiors::InteriorsStencilMirrorType stencilMirrorType);
154 
155  u32 CalcSortKeyDepthBlended(int drawOrderDepth) const;
156 
157  static u32 CalcSortKeyDepthBlended(int floorIndex, Resources::Interiors::InteriorsRenderableLayer::Type renderableLayer, Resources::Interiors::InteriorsStencilMirrorType stencilMirrorType, int drawOrderDepth);
158 
159  bool IsMaterialTranslucent() const { return m_materialIsTranslucent; }
160 
161  m44 GetModelTransform() const { return m_modelTransform; }
162 
163  protected:
164  m44 CalcPositionUnpack() const;
165 
166  m44 GetNormalModelTransform() const { return m_normalModelTransform; }
167 
168  private:
169 
170  m44 CalcCameraRelativeTransform(const dv3& cameraLocationEcef) const;
171  m44 CalcCameraRelativeModelTransform(const m44& modelTransform, const m44& cameraRelativeTransform) const;
172 
173  // const state
174  const Eegeo::v4 m_positionBoundsMin;
175  const Eegeo::v4 m_positionBoundsMax;
176 
177  const Eegeo::v2 m_uvBoundsMin;
178  const Eegeo::v2 m_uvBoundsMax;
179 
180  const std::string m_renderableId;
181  const Resources::Interiors::IInteriorsMaterialAssignmentObserver& m_materialAssignmentObserver;
182  const int m_floorIndex;
183  const float m_interiorTerrainHeight;
184  const bool m_ownsMesh;
185 
186  // mutable canonical state
187  m44 m_modelTransform;
188  m44 m_normalModelTransform;
189  v3 m_stencilMirrorClearColor;
190  v4 m_color;
191  float m_saturation;
192  Resources::Interiors::InteriorsStencilMirrorType m_stencilMirrorType;
193  bool m_materialIsTranslucent;
194  Resources::Interiors::InteriorsRenderableLayer::Type m_materialRenderableLayer;
195  std::string m_interiorIdentifier;
196 
197  // mutable derived state
198  RenderState m_renderState;
199  };
200  }
201  }
202 }