f7b2b1c040d5a5178d6196e43c1b709b98f2f8c9
[platform/core/uifw/dali-core.git] / dali / internal / update / render-tasks / scene-graph-camera.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/render-tasks/scene-graph-camera.h>
20
21 // EXTERNAL INCLUDES
22 #include <stdint.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/update/nodes/node.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/math/math-utils.h>
29
30 namespace // unnamed namespace
31 {
32 const uint32_t UPDATE_COUNT         = 2u; // Update projection or view matrix this many frames after a change
33 const uint32_t COPY_PREVIOUS_MATRIX = 1u; // Copy view or projection matrix from previous frame
34
35 //For reflection and clipping plane
36 const float REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_A = 2.0f;
37 const float REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_D = 1.0f;
38 } // namespace
39
40 namespace Dali
41 {
42 namespace Internal
43 {
44 namespace SceneGraph
45 {
46 namespace
47 {
48 template<typename T>
49 T Sign(T value)
50 {
51   return T(T(0) < value) - T(value < T(0));
52 }
53
54 void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
55 {
56   Vector3 vZ = target - eye;
57   vZ.Normalize();
58
59   Vector3 vX = up.Cross(vZ);
60   vX.Normalize();
61
62   Vector3 vY = vZ.Cross(vX);
63   vY.Normalize();
64
65   result.SetInverseTransformComponents(vX, vY, vZ, eye);
66 }
67
68 void Frustum(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
69 {
70   float deltaZ = far - near;
71   if((near <= 0.0f) || (far <= 0.0f) || Equals(right, left) || Equals(bottom, top) || (deltaZ <= 0.0f))
72   {
73     DALI_LOG_ERROR("Invalid parameters passed into Frustum!\n");
74     DALI_ASSERT_DEBUG("Invalid parameters passed into Frustum!");
75     return;
76   }
77
78   float deltaX = right - left;
79   float deltaY = invertYAxis ? bottom - top : top - bottom;
80
81   result.SetIdentity();
82
83   float* m = result.AsFloat();
84   m[0]     = -2.0f * near / deltaX;
85   m[1] = m[2] = m[3] = 0.0f;
86
87   m[5] = -2.0f * near / deltaY;
88   m[4] = m[6] = m[7] = 0.0f;
89
90   m[8]  = (right + left) / deltaX;
91   m[9]  = (top + bottom) / deltaY;
92   m[10] = (near + far) / deltaZ;
93   m[11] = 1.0f;
94
95   m[14] = -2.0f * near * far / deltaZ;
96   m[12] = m[13] = m[15] = 0.0f;
97 }
98
99 void Perspective(Matrix& result, float fovy, float aspect, float near, float far, bool invertYAxis)
100 {
101   float frustumH = tanf(fovy * 0.5f) * near;
102   float frustumW = frustumH * aspect;
103
104   Frustum(result, -frustumW, frustumW, -frustumH, frustumH, near, far, invertYAxis);
105 }
106
107 void Orthographic(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
108 {
109   if(Equals(right, left) || Equals(top, bottom) || Equals(far, near))
110   {
111     DALI_LOG_ERROR("Cannot create orthographic projection matrix with a zero dimension.\n");
112     DALI_ASSERT_DEBUG("Cannot create orthographic projection matrix with a zero dimension.");
113     return;
114   }
115
116   float deltaX = right - left;
117   float deltaY = invertYAxis ? bottom - top : top - bottom;
118   float deltaZ = far - near;
119
120   float* m = result.AsFloat();
121   m[0]     = -2.0f / deltaX;
122   m[1]     = 0.0f;
123   m[2]     = 0.0f;
124   m[3]     = 0.0f;
125
126   m[4] = 0.0f;
127   m[5] = -2.0f / deltaY;
128   m[6] = 0.0f;
129   m[7] = 0.0f;
130
131   m[8]  = 0.0f;
132   m[9]  = 0.0f;
133   m[10] = 2.0f / deltaZ;
134   m[11] = 0.0f;
135   m[12] = -(right + left) / deltaX;
136   m[13] = -(top + bottom) / deltaY;
137   m[14] = -(near + far) / deltaZ;
138   m[15] = 1.0f;
139 }
140
141 } // unnamed namespace
142
143 const Dali::Camera::Type           Camera::DEFAULT_TYPE(Dali::Camera::FREE_LOOK);
144 const Dali::Camera::ProjectionMode Camera::DEFAULT_MODE(Dali::Camera::PERSPECTIVE_PROJECTION);
145 const bool                         Camera::DEFAULT_INVERT_Y_AXIS(false);
146 const float                        Camera::DEFAULT_FIELD_OF_VIEW(45.0f * (Math::PI / 180.0f));
147 const float                        Camera::DEFAULT_ASPECT_RATIO(4.0f / 3.0f);
148 const float                        Camera::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
149 const float                        Camera::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
150 const float                        Camera::DEFAULT_TOP_CLIPPING_PLANE(-400.0f);
151 const float                        Camera::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
152 const float                        Camera::DEFAULT_NEAR_CLIPPING_PLANE(800.0f); // default height of the screen
153 const float                        Camera::DEFAULT_FAR_CLIPPING_PLANE(DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE);
154 const Vector3                      Camera::DEFAULT_TARGET_POSITION(0.0f, 0.0f, 0.0f);
155
156 Camera::Camera()
157 : mUpdateViewFlag(UPDATE_COUNT),
158   mUpdateProjectionFlag(UPDATE_COUNT),
159   mProjectionRotation(0),
160   mNode(nullptr),
161   mType(DEFAULT_TYPE),
162   mProjectionMode(DEFAULT_MODE),
163   mInvertYAxis(DEFAULT_INVERT_Y_AXIS),
164   mFieldOfView(DEFAULT_FIELD_OF_VIEW),
165   mAspectRatio(DEFAULT_ASPECT_RATIO),
166   mLeftClippingPlane(DEFAULT_LEFT_CLIPPING_PLANE),
167   mRightClippingPlane(DEFAULT_RIGHT_CLIPPING_PLANE),
168   mTopClippingPlane(DEFAULT_TOP_CLIPPING_PLANE),
169   mBottomClippingPlane(DEFAULT_BOTTOM_CLIPPING_PLANE),
170   mNearClippingPlane(DEFAULT_NEAR_CLIPPING_PLANE),
171   mFarClippingPlane(DEFAULT_FAR_CLIPPING_PLANE),
172   mTargetPosition(DEFAULT_TARGET_POSITION),
173   mViewMatrix(),
174   mProjectionMatrix(),
175   mInverseViewProjection(Matrix::IDENTITY),
176   mFinalProjection(Matrix::IDENTITY)
177 {
178 }
179
180 Camera* Camera::New()
181 {
182   return new Camera();
183 }
184
185 Camera::~Camera() = default;
186
187 void Camera::SetNode(const Node* node)
188 {
189   mNode = node;
190 }
191
192 const Node* Camera::GetNode() const
193 {
194   return mNode;
195 }
196
197 void Camera::SetType(Dali::Camera::Type type)
198 {
199   mType = type;
200 }
201
202 void Camera::SetProjectionMode(Dali::Camera::ProjectionMode mode)
203 {
204   mProjectionMode       = mode;
205   mUpdateProjectionFlag = UPDATE_COUNT;
206 }
207
208 void Camera::SetInvertYAxis(bool invertYAxis)
209 {
210   mInvertYAxis          = invertYAxis;
211   mUpdateProjectionFlag = UPDATE_COUNT;
212 }
213
214 void Camera::SetFieldOfView(float fieldOfView)
215 {
216   mFieldOfView          = fieldOfView;
217   mUpdateProjectionFlag = UPDATE_COUNT;
218 }
219
220 void Camera::SetAspectRatio(float aspectRatio)
221 {
222   mAspectRatio          = aspectRatio;
223   mUpdateProjectionFlag = UPDATE_COUNT;
224 }
225
226 void Camera::SetLeftClippingPlane(float leftClippingPlane)
227 {
228   mLeftClippingPlane    = leftClippingPlane;
229   mUpdateProjectionFlag = UPDATE_COUNT;
230 }
231
232 void Camera::SetRightClippingPlane(float rightClippingPlane)
233 {
234   mRightClippingPlane   = rightClippingPlane;
235   mUpdateProjectionFlag = UPDATE_COUNT;
236 }
237
238 void Camera::SetTopClippingPlane(float topClippingPlane)
239 {
240   mTopClippingPlane     = topClippingPlane;
241   mUpdateProjectionFlag = UPDATE_COUNT;
242 }
243
244 void Camera::SetBottomClippingPlane(float bottomClippingPlane)
245 {
246   mBottomClippingPlane  = bottomClippingPlane;
247   mUpdateProjectionFlag = UPDATE_COUNT;
248 }
249
250 void Camera::SetNearClippingPlane(float nearClippingPlane)
251 {
252   mNearClippingPlane    = nearClippingPlane;
253   mUpdateProjectionFlag = UPDATE_COUNT;
254 }
255
256 void Camera::SetFarClippingPlane(float farClippingPlane)
257 {
258   mFarClippingPlane     = farClippingPlane;
259   mUpdateProjectionFlag = UPDATE_COUNT;
260 }
261
262 void Camera::SetTargetPosition(const Vector3& targetPosition)
263 {
264   mTargetPosition = targetPosition;
265   mUpdateViewFlag = UPDATE_COUNT;
266 }
267
268 void VectorReflectedByPlane(Vector4& out, Vector4& in, Vector4& plane)
269 {
270   float d = float(2.0) * plane.Dot(in);
271   out.x   = static_cast<float>(in.x - plane.x * d);
272   out.y   = static_cast<float>(in.y - plane.y * d);
273   out.z   = static_cast<float>(in.z - plane.z * d);
274   out.w   = static_cast<float>(in.w - plane.w * d);
275 }
276
277 void Camera::AdjustNearPlaneForPerspective(Matrix& perspective, const Vector4& clipPlane)
278 {
279   Vector4 q;
280   float*  v = perspective.AsFloat();
281
282   q.x = (Sign(clipPlane.x) + v[8]) / v[0];
283   q.y = (Sign(clipPlane.y) + v[9]) / v[5];
284   q.z = -1.0f;
285   q.w = (1.0f + v[10]) / v[14];
286
287   // Calculate the scaled plane vector
288   Vector4 c = clipPlane * (REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_A / q.Dot(clipPlane));
289
290   // Replace the third row of the projection v
291   v[2]  = c.x;
292   v[6]  = c.y;
293   v[10] = c.z + REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_D;
294   v[14] = c.w;
295 }
296
297 void Camera::SetReflectByPlane(const Vector4& plane)
298 {
299   float* v    = mReflectionMtx.AsFloat();
300   float  _2ab = -2.0f * plane.x * plane.y;
301   float  _2ac = -2.0f * plane.x * plane.z;
302   float  _2bc = -2.0f * plane.y * plane.z;
303
304   v[0] = 1.0f - 2.0f * plane.x * plane.x;
305   v[1] = _2ab;
306   v[2] = _2ac;
307   v[3] = 0.0f;
308
309   v[4] = _2ab;
310   v[5] = 1.0f - 2.0f * plane.y * plane.y;
311   v[6] = _2bc;
312   v[7] = 0.0f;
313
314   v[8]  = _2ac;
315   v[9]  = _2bc;
316   v[10] = 1.0f - 2.0f * plane.z * plane.z;
317   v[11] = 0.0f;
318
319   v[12] = -2 * plane.x * plane.w;
320   v[13] = -2 * plane.y * plane.w;
321   v[14] = -2 * plane.z * plane.w;
322   v[15] = 1.0f;
323
324   mUseReflection   = true;
325   mReflectionPlane = plane;
326   mUpdateViewFlag  = UPDATE_COUNT;
327 }
328
329 void Camera::RotateProjection(int rotationAngle)
330 {
331   mProjectionRotation   = rotationAngle;
332   mUpdateProjectionFlag = UPDATE_COUNT;
333 }
334
335 const Matrix& Camera::GetProjectionMatrix(BufferIndex bufferIndex) const
336 {
337   return mProjectionMatrix[bufferIndex];
338 }
339
340 const Matrix& Camera::GetViewMatrix(BufferIndex bufferIndex) const
341 {
342   return mViewMatrix[bufferIndex];
343 }
344
345 const Matrix& Camera::GetInverseViewProjectionMatrix(BufferIndex bufferIndex) const
346 {
347   return mInverseViewProjection[bufferIndex];
348 }
349
350 const Matrix& Camera::GetFinalProjectionMatrix(BufferIndex bufferIndex) const
351 {
352   return mFinalProjection[bufferIndex];
353 }
354
355 const PropertyInputImpl* Camera::GetProjectionMatrix() const
356 {
357   return &mProjectionMatrix;
358 }
359
360 const PropertyInputImpl* Camera::GetViewMatrix() const
361 {
362   return &mViewMatrix;
363 }
364
365 void Camera::Update(BufferIndex updateBufferIndex)
366 {
367   // if owning node has changes in world position we need to update camera for next 2 frames
368   if(mNode->IsLocalMatrixDirty())
369   {
370     mUpdateViewFlag = UPDATE_COUNT;
371   }
372   if(mNode->GetDirtyFlags() & NodePropertyFlags::VISIBLE)
373   {
374     // If the visibility changes, the projection matrix needs to be re-calculated.
375     // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
376     // in the following update the node will be skipped leaving the projection matrix (double buffered)
377     // with the Identity.
378     mUpdateProjectionFlag = UPDATE_COUNT;
379   }
380
381   // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
382   uint32_t viewUpdateCount       = UpdateViewMatrix(updateBufferIndex);
383   uint32_t projectionUpdateCount = UpdateProjection(updateBufferIndex);
384
385   // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
386   if(viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX)
387   {
388     // either has actually changed so recalculate
389     Matrix::Multiply(mInverseViewProjection[updateBufferIndex], mViewMatrix[updateBufferIndex], mProjectionMatrix[updateBufferIndex]);
390     UpdateFrustum(updateBufferIndex);
391
392     // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
393     static_cast<void>(mInverseViewProjection[updateBufferIndex].Invert());
394   }
395   else if(viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX)
396   {
397     // neither has actually changed, but we might copied previous frames value so need to
398     // copy the previous inverse and frustum as well
399     mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
400     mFrustum[updateBufferIndex]               = mFrustum[updateBufferIndex ? 0 : 1];
401   }
402 }
403
404 bool Camera::ViewMatrixUpdated()
405 {
406   return 0u != mUpdateViewFlag;
407 }
408
409 uint32_t Camera::UpdateViewMatrix(BufferIndex updateBufferIndex)
410 {
411   uint32_t retval(mUpdateViewFlag);
412   if(0u != mUpdateViewFlag)
413   {
414     if(COPY_PREVIOUS_MATRIX == mUpdateViewFlag)
415     {
416       // The projection matrix was updated in the previous frame; copy it
417       mViewMatrix.CopyPrevious(updateBufferIndex);
418     }
419     else // UPDATE_COUNT == mUpdateViewFlag
420     {
421       switch(mType)
422       {
423         // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
424         case Dali::Camera::FREE_LOOK:
425         {
426           Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
427           viewMatrix         = mNode->GetWorldMatrix(updateBufferIndex);
428
429           if(mUseReflection)
430           {
431             const Matrix& owningNodeMatrix(mNode->GetWorldMatrix(updateBufferIndex));
432             Vector3       position{}, scale{};
433             Quaternion    orientation{};
434             owningNodeMatrix.GetTransformComponents(position, orientation, scale);
435             mReflectionEye     = position;
436             mUseReflectionClip = true;
437
438             Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
439             Matrix  oldViewMatrix(viewMatrix);
440             Matrix::Multiply(viewMatrix, oldViewMatrix, mReflectionMtx);
441           }
442
443           viewMatrix.Invert();
444           mViewMatrix.SetDirty(updateBufferIndex);
445           break;
446         }
447
448         // camera orientation constrained to look at a target
449         case Dali::Camera::LOOK_AT_TARGET:
450         {
451           const Matrix& owningNodeMatrix(mNode->GetWorldMatrix(updateBufferIndex));
452           Vector3       position, scale;
453           Quaternion    orientation;
454           owningNodeMatrix.GetTransformComponents(position, orientation, scale);
455           Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
456
457           if(mUseReflection)
458           {
459             Vector3 up        = orientation.Rotate(Vector3::YAXIS);
460             Vector4 position4 = Vector4(position);
461             Vector4 target4   = Vector4(mTargetPosition);
462             Vector4 up4       = Vector4(up);
463             Vector4 positionNew;
464             Vector4 targetNew;
465             Vector4 upNew;
466             Vector3 positionNew3;
467             Vector3 targetNewVector3;
468             Vector3 upNew3;
469
470             // eye
471             VectorReflectedByPlane(positionNew, position4, mReflectionPlane);
472             VectorReflectedByPlane(targetNew, target4, mReflectionPlane);
473             VectorReflectedByPlane(upNew, up4, mReflectionPlane);
474
475             positionNew3     = Vector3(positionNew);
476             targetNewVector3 = Vector3(targetNew);
477             upNew3           = Vector3(upNew);
478             LookAt(viewMatrix, positionNew3, targetNewVector3, upNew3);
479
480             Matrix oldViewMatrix(viewMatrix);
481             Matrix tmp;
482             tmp.SetIdentityAndScale(Vector3(-1.0, 1.0, 1.0));
483             Matrix::Multiply(viewMatrix, oldViewMatrix, tmp);
484
485             mReflectionEye     = positionNew;
486             mUseReflectionClip = true;
487           }
488           else
489           {
490             LookAt(viewMatrix, position, mTargetPosition, orientation.Rotate(Vector3::YAXIS));
491           }
492           mViewMatrix.SetDirty(updateBufferIndex);
493           break;
494         }
495       }
496     }
497     --mUpdateViewFlag;
498   }
499   return retval;
500 }
501
502 void Camera::UpdateFrustum(BufferIndex updateBufferIndex, bool normalize)
503 {
504   // Extract the clip matrix planes
505   Matrix clipMatrix;
506   Matrix::Multiply(clipMatrix, mViewMatrix[updateBufferIndex], mProjectionMatrix[updateBufferIndex]);
507
508   const float*   cm     = clipMatrix.AsFloat();
509   FrustumPlanes& planes = mFrustum[updateBufferIndex];
510
511   // Left
512   planes.mPlanes[0].mNormal.x = cm[3] + cm[0]; // column 4 + column 1
513   planes.mPlanes[0].mNormal.y = cm[7] + cm[4];
514   planes.mPlanes[0].mNormal.z = cm[11] + cm[8];
515   planes.mPlanes[0].mDistance = cm[15] + cm[12];
516
517   // Right
518   planes.mPlanes[1].mNormal.x = cm[3] - cm[0]; // column 4 - column 1
519   planes.mPlanes[1].mNormal.y = cm[7] - cm[4];
520   planes.mPlanes[1].mNormal.z = cm[11] - cm[8];
521   planes.mPlanes[1].mDistance = cm[15] - cm[12];
522
523   // Bottom
524   planes.mPlanes[2].mNormal.x = cm[3] + cm[1]; // column 4 + column 2
525   planes.mPlanes[2].mNormal.y = cm[7] + cm[5];
526   planes.mPlanes[2].mNormal.z = cm[11] + cm[9];
527   planes.mPlanes[2].mDistance = cm[15] + cm[13];
528
529   // Top
530   planes.mPlanes[3].mNormal.x = cm[3] - cm[1]; // column 4 - column 2
531   planes.mPlanes[3].mNormal.y = cm[7] - cm[5];
532   planes.mPlanes[3].mNormal.z = cm[11] - cm[9];
533   planes.mPlanes[3].mDistance = cm[15] - cm[13];
534
535   // Near
536   planes.mPlanes[4].mNormal.x = cm[3] + cm[2]; // column 4 + column 3
537   planes.mPlanes[4].mNormal.y = cm[7] + cm[6];
538   planes.mPlanes[4].mNormal.z = cm[11] + cm[10];
539   planes.mPlanes[4].mDistance = cm[15] + cm[14];
540
541   // Far
542   planes.mPlanes[5].mNormal.x = cm[3] - cm[2]; // column 4 - column 3
543   planes.mPlanes[5].mNormal.y = cm[7] - cm[6];
544   planes.mPlanes[5].mNormal.z = cm[11] - cm[10];
545   planes.mPlanes[5].mDistance = cm[15] - cm[14];
546
547   if(normalize)
548   {
549     for(uint32_t i = 0; i < 6; ++i)
550     {
551       // Normalize planes to ensure correct bounding distance checking
552       Plane& plane = planes.mPlanes[i];
553       float  l     = 1.0f / plane.mNormal.Length();
554       plane.mNormal *= l;
555       plane.mDistance *= l;
556
557       planes.mSign[i] = Vector3(Sign(plane.mNormal.x), Sign(plane.mNormal.y), Sign(plane.mNormal.z));
558     }
559   }
560   else
561   {
562     for(uint32_t i = 0; i < 6; ++i)
563     {
564       planes.mSign[i] = Vector3(Sign(planes.mPlanes[i].mNormal.x), Sign(planes.mPlanes[i].mNormal.y), Sign(planes.mPlanes[i].mNormal.z));
565     }
566   }
567   mFrustum[updateBufferIndex ? 0 : 1] = planes;
568 }
569
570 bool Camera::CheckSphereInFrustum(BufferIndex bufferIndex, const Vector3& origin, float radius)
571 {
572   const FrustumPlanes& planes = mFrustum[bufferIndex];
573   for(uint32_t i = 0; i < 6; ++i)
574   {
575     if((planes.mPlanes[i].mDistance + planes.mPlanes[i].mNormal.Dot(origin)) < -radius)
576     {
577       return false;
578     }
579   }
580   return true;
581 }
582
583 bool Camera::CheckAABBInFrustum(BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents)
584 {
585   const FrustumPlanes& planes = mFrustum[bufferIndex];
586   for(uint32_t i = 0; i < 6; ++i)
587   {
588     if(planes.mPlanes[i].mNormal.Dot(origin + (halfExtents * planes.mSign[i])) > -(planes.mPlanes[i].mDistance))
589     {
590       continue;
591     }
592
593     return false;
594   }
595   return true;
596 }
597
598 uint32_t Camera::UpdateProjection(BufferIndex updateBufferIndex)
599 {
600   uint32_t retval(mUpdateProjectionFlag);
601   // Early-exit if no update required
602   if(0u != mUpdateProjectionFlag)
603   {
604     Matrix& finalProjection = mFinalProjection[updateBufferIndex];
605     finalProjection.SetIdentity();
606
607     if(COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag)
608     {
609       // The projection matrix was updated in the previous frame; copy it
610       mProjectionMatrix.CopyPrevious(updateBufferIndex);
611
612       finalProjection = mFinalProjection[updateBufferIndex ? 0 : 1];
613     }
614     else // UPDATE_COUNT == mUpdateProjectionFlag
615     {
616       switch(mProjectionMode)
617       {
618         case Dali::Camera::PERSPECTIVE_PROJECTION:
619         {
620           Matrix& projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
621           Perspective(projectionMatrix,
622                       mFieldOfView,
623                       mAspectRatio,
624                       mNearClippingPlane,
625                       mFarClippingPlane,
626                       mInvertYAxis);
627
628           //need to apply custom clipping plane
629           if(mUseReflectionClip)
630           {
631             Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
632             Matrix  viewInv    = viewMatrix;
633             viewInv.Invert();
634             viewInv.Transpose();
635
636             Dali::Vector4 adjReflectPlane = mReflectionPlane;
637             float         d               = mReflectionPlane.Dot(mReflectionEye);
638             if(d < 0)
639             {
640               adjReflectPlane.w = -adjReflectPlane.w;
641             }
642
643             Vector4 customClipping = viewInv * adjReflectPlane;
644             AdjustNearPlaneForPerspective(projectionMatrix, customClipping);
645
646             // Invert Z
647             Matrix matZ;
648             matZ.SetIdentity();
649             float* vZ = matZ.AsFloat();
650             vZ[10]    = -vZ[10];
651             Matrix::Multiply(projectionMatrix, projectionMatrix, matZ);
652           }
653           break;
654         }
655         case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
656         {
657           Matrix& projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
658           Orthographic(projectionMatrix,
659                        mLeftClippingPlane,
660                        mRightClippingPlane,
661                        mBottomClippingPlane,
662                        mTopClippingPlane,
663                        mNearClippingPlane,
664                        mFarClippingPlane,
665                        mInvertYAxis);
666           break;
667         }
668       }
669
670       mProjectionMatrix.SetDirty(updateBufferIndex);
671
672       Quaternion rotationAngle;
673       switch(mProjectionRotation)
674       {
675         case 90:
676         {
677           rotationAngle = Quaternion(Dali::ANGLE_90, Vector3::ZAXIS);
678           break;
679         }
680         case 180:
681         {
682           rotationAngle = Quaternion(Dali::ANGLE_180, Vector3::ZAXIS);
683           break;
684         }
685         case 270:
686         {
687           rotationAngle = Quaternion(Dali::ANGLE_270, Vector3::ZAXIS);
688           break;
689         }
690         default:
691           rotationAngle = Quaternion(Dali::ANGLE_0, Vector3::ZAXIS);
692           break;
693       }
694
695       Matrix rotation;
696       rotation.SetIdentity();
697       rotation.SetTransformComponents(Vector3(1.0f, 1.0f, 1.0f), rotationAngle, Vector3(0.0f, 0.0f, 0.0f));
698
699       Matrix::Multiply(finalProjection, mProjectionMatrix.Get(updateBufferIndex), rotation);
700     }
701     --mUpdateProjectionFlag;
702   }
703   return retval;
704 }
705
706 } // namespace SceneGraph
707
708 } // namespace Internal
709
710 } // namespace Dali