71a2d52f1afc21d7a9f07237ad7109dc73763dfc
[platform/core/uifw/dali-core.git] / dali / internal / update / render-tasks / scene-graph-camera.cpp
1 /*
2  * Copyright (c) 2018 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/public-api/common/dali-common.h>
27 #include <dali/public-api/math/math-utils.h>
28 #include <dali/internal/update/nodes/node.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 }
39
40 namespace Dali
41 {
42
43 namespace Internal
44 {
45
46 namespace SceneGraph
47 {
48
49 namespace
50 {
51
52 template< typename T >
53 T Sign( T value )
54 {
55   return T( T(0) < value ) - T( value < T(0) );
56 }
57
58 void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
59 {
60   Vector3 vZ = target - eye;
61   vZ.Normalize();
62
63   Vector3 vX = up.Cross(vZ);
64   vX.Normalize();
65
66   Vector3 vY = vZ.Cross(vX);
67   vY.Normalize();
68
69   result.SetInverseTransformComponents(vX, vY, vZ, eye);
70 }
71
72 void Frustum(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
73 {
74   float deltaZ = far - near;
75   if ((near <= 0.0f) || (far <= 0.0f) || Equals(right, left) || Equals(bottom, top) || (deltaZ <= 0.0f))
76   {
77     DALI_LOG_ERROR("Invalid parameters passed into Frustum!\n");
78     DALI_ASSERT_DEBUG("Invalid parameters passed into Frustum!");
79     return;
80   }
81
82   float deltaX = right - left;
83   float deltaY = invertYAxis ? bottom - top : top - bottom;
84
85   result.SetIdentity();
86
87   float* m = result.AsFloat();
88   m[0] = -2.0f * near / deltaX;
89   m[1] = m[2] = m[3] = 0.0f;
90
91   m[5] = -2.0f * near / deltaY;
92   m[4] = m[6] = m[7] = 0.0f;
93
94   m[8] = (right + left) / deltaX;
95   m[9] = (top + bottom) / deltaY;
96   m[10] = (near + far) / deltaZ;
97   m[11] = 1.0f;
98
99   m[14] = -2.0f * near * far / deltaZ;
100   m[12] = m[13] = m[15] = 0.0f;
101 }
102
103 void Perspective(Matrix& result, float fovy, float aspect, float near, float far, bool invertYAxis )
104 {
105   float frustumH = tanf( fovy * 0.5f ) * near;
106   float frustumW = frustumH * aspect;
107
108   Frustum(result, -frustumW, frustumW, -frustumH, frustumH, near, far, invertYAxis);
109 }
110
111 void Orthographic(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
112 {
113   if ( Equals(right, left) || Equals(top, bottom) || Equals(far, near) )
114   {
115     DALI_LOG_ERROR( "Cannot create orthographic projection matrix with a zero dimension.\n" );
116     DALI_ASSERT_DEBUG( "Cannot create orthographic projection matrix with a zero dimension." );
117     return;
118   }
119
120   float deltaX = right - left;
121   float deltaY = invertYAxis ? bottom - top : top - bottom;
122   float deltaZ = far - near;
123
124   float *m = result.AsFloat();
125   m[0] = -2.0f / deltaX;
126   m[1] = 0.0f;
127   m[2] = 0.0f;
128   m[3] = 0.0f;
129
130   m[4] = 0.0f;
131   m[5] = -2.0f / deltaY;
132   m[6] = 0.0f;
133   m[7] = 0.0f;
134
135   m[8] = 0.0f;
136   m[9] = 0.0f;
137   m[10] = 2.0f / deltaZ;
138   m[11] = 0.0f;
139   m[12] = -(right + left) / deltaX;
140   m[13] = -(top + bottom) / deltaY;
141   m[14] = -(near + far)   / deltaZ;
142   m[15] = 1.0f;
143 }
144
145 } // unnamed namespace
146
147 const Dali::Camera::Type Camera::DEFAULT_TYPE( Dali::Camera::FREE_LOOK );
148 const Dali::Camera::ProjectionMode Camera::DEFAULT_MODE( Dali::Camera::PERSPECTIVE_PROJECTION );
149 const bool  Camera::DEFAULT_INVERT_Y_AXIS( false );
150 const float Camera::DEFAULT_FIELD_OF_VIEW( 45.0f*(Math::PI/180.0f) );
151 const float Camera::DEFAULT_ASPECT_RATIO( 4.0f/3.0f );
152 const float Camera::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
153 const float Camera::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
154 const float Camera::DEFAULT_TOP_CLIPPING_PLANE(-400.0f);
155 const float Camera::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
156 const float Camera::DEFAULT_NEAR_CLIPPING_PLANE( 800.0f ); // default height of the screen
157 const float Camera::DEFAULT_FAR_CLIPPING_PLANE( DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE );
158 const Vector3 Camera::DEFAULT_TARGET_POSITION( 0.0f, 0.0f, 0.0f );
159
160
161 Camera::Camera()
162 : mUpdateViewFlag( UPDATE_COUNT ),
163   mUpdateProjectionFlag( UPDATE_COUNT ),
164   mNode( NULL ),
165   mType( DEFAULT_TYPE ),
166   mProjectionMode( DEFAULT_MODE ),
167   mInvertYAxis( DEFAULT_INVERT_Y_AXIS ),
168   mFieldOfView( DEFAULT_FIELD_OF_VIEW ),
169   mAspectRatio( DEFAULT_ASPECT_RATIO ),
170   mLeftClippingPlane( DEFAULT_LEFT_CLIPPING_PLANE ),
171   mRightClippingPlane( DEFAULT_RIGHT_CLIPPING_PLANE ),
172   mTopClippingPlane( DEFAULT_TOP_CLIPPING_PLANE ),
173   mBottomClippingPlane( DEFAULT_BOTTOM_CLIPPING_PLANE ),
174   mNearClippingPlane( DEFAULT_NEAR_CLIPPING_PLANE ),
175   mFarClippingPlane( DEFAULT_FAR_CLIPPING_PLANE ),
176   mTargetPosition( DEFAULT_TARGET_POSITION ),
177   mViewMatrix(),
178   mProjectionMatrix(),
179   mInverseViewProjection( Matrix::IDENTITY )
180 {
181 }
182
183 Camera* Camera::New()
184 {
185   return new Camera();
186 }
187
188 Camera::~Camera()
189 {
190 }
191
192 void Camera::SetNode( const Node* node )
193 {
194   mNode = node;
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
269
270 void VectorReflectedByPlane(Vector4 &out, Vector4 &in, Vector4 &plane)
271 {
272   float d = float(2.0) * plane.Dot(in);
273   out.x = static_cast<float>(in.x - plane.x*d);
274   out.y = static_cast<float>(in.y - plane.y*d);
275   out.z = static_cast<float>(in.z - plane.z*d);
276   out.w = static_cast<float>(in.w - plane.w*d);
277 }
278
279 void Camera::AdjustNearPlaneForPerspective( Matrix& perspective, const Vector4& clipPlane )
280 {
281   Vector4    q;
282   float* v = perspective.AsFloat();
283
284   q.x = (Sign(clipPlane.x) + v[8]) / v[0];
285   q.y = (Sign(clipPlane.y) + v[9]) / v[5];
286   q.z = -1.0f;
287   q.w = (1.0f + v[10]) / v[14];
288
289   // Calculate the scaled plane vector
290   Vector4 c = clipPlane * (REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_A / q.Dot( clipPlane));
291
292   // Replace the third row of the projection v
293   v[2] = c.x;
294   v[6] = c.y;
295   v[10] = c.z + REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_D;
296   v[14] = c.w;
297 }
298
299 void Camera::SetReflectByPlane( const Vector4& plane )
300 {
301   float* v = mReflectionMtx.AsFloat();
302   float _2ab = -2.0f * plane.x * plane.y;
303   float _2ac = -2.0f * plane.x * plane.z;
304   float _2bc = -2.0f * plane.y * plane.z;
305
306   v[0] = 1.0f - 2.0f * plane.x * plane.x;
307   v[1] = _2ab;
308   v[2] = _2ac;
309   v[3] = 0.0f;
310
311   v[4] = _2ab;
312   v[5] = 1.0f - 2.0f * plane.y * plane.y;
313   v[6] = _2bc;
314   v[7] = 0.0f;
315
316   v[8] = _2ac;
317   v[9] = _2bc;
318   v[10] = 1.0f - 2.0f * plane.z * plane.z;
319   v[11] = 0.0f;
320
321   v[12] =    - 2 * plane.x * plane.w;
322   v[13] =    - 2 * plane.y * plane.w;
323   v[14] =    - 2 * plane.z * plane.w;
324   v[15] = 1.0f;
325
326   mUseReflection = true;
327   mReflectionPlane = plane;
328   mUpdateViewFlag = UPDATE_COUNT;
329 }
330
331 const Matrix& Camera::GetProjectionMatrix( BufferIndex bufferIndex ) const
332 {
333   return mProjectionMatrix[ bufferIndex ];
334 }
335
336 const Matrix& Camera::GetViewMatrix( BufferIndex bufferIndex ) const
337 {
338   return mViewMatrix[ bufferIndex ];
339 }
340
341 const Matrix& Camera::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
342 {
343   return mInverseViewProjection[ bufferIndex ];
344 }
345
346 const PropertyInputImpl* Camera::GetProjectionMatrix() const
347 {
348   return &mProjectionMatrix;
349 }
350
351 const PropertyInputImpl* Camera::GetViewMatrix() const
352 {
353   return &mViewMatrix;
354 }
355
356 void Camera::Update( BufferIndex updateBufferIndex )
357 {
358   // if owning node has changes in world position we need to update camera for next 2 frames
359   if( mNode->IsLocalMatrixDirty() )
360   {
361     mUpdateViewFlag = UPDATE_COUNT;
362   }
363   if( mNode->GetDirtyFlags() & NodePropertyFlags::VISIBLE )
364   {
365     // If the visibility changes, the projection matrix needs to be re-calculated.
366     // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
367     // in the following update the node will be skipped leaving the projection matrix (double buffered)
368     // with the Identity.
369     mUpdateProjectionFlag = UPDATE_COUNT;
370   }
371
372   // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
373   uint32_t viewUpdateCount = UpdateViewMatrix( updateBufferIndex );
374   uint32_t projectionUpdateCount = UpdateProjection( updateBufferIndex );
375
376   // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
377   if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
378   {
379     // either has actually changed so recalculate
380     Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
381     UpdateFrustum( updateBufferIndex );
382
383     // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
384     static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
385   }
386   else if( viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX )
387   {
388     // neither has actually changed, but we might copied previous frames value so need to
389     // copy the previous inverse and frustum as well
390     mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
391     mFrustum[ updateBufferIndex ] = mFrustum[ updateBufferIndex ? 0 : 1 ];
392   }
393 }
394
395 bool Camera::ViewMatrixUpdated()
396 {
397   return 0u != mUpdateViewFlag;
398 }
399
400 uint32_t Camera::UpdateViewMatrix( BufferIndex updateBufferIndex )
401 {
402   uint32_t retval( mUpdateViewFlag );
403   if( 0u != mUpdateViewFlag )
404   {
405     if( COPY_PREVIOUS_MATRIX == mUpdateViewFlag )
406     {
407       // The projection matrix was updated in the previous frame; copy it
408       mViewMatrix.CopyPrevious( updateBufferIndex );
409     }
410     else // UPDATE_COUNT == mUpdateViewFlag
411     {
412       switch( mType )
413       {
414         // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
415         case Dali::Camera::FREE_LOOK:
416         {
417           Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
418           viewMatrix = mNode->GetWorldMatrix( updateBufferIndex );
419
420           if (mUseReflection)
421           {
422             const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
423             Vector3 position{}, scale{};
424             Quaternion orientation{};
425             owningNodeMatrix.GetTransformComponents( position, orientation, scale );
426             mReflectionEye = position;
427             mUseReflectionClip = true;
428
429             Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
430             Matrix oldViewMatrix( viewMatrix );
431             Matrix::Multiply(viewMatrix, oldViewMatrix, mReflectionMtx);
432           }
433
434           viewMatrix.Invert();
435           mViewMatrix.SetDirty( updateBufferIndex );
436           break;
437         }
438
439         // camera orientation constrained to look at a target
440         case Dali::Camera::LOOK_AT_TARGET:
441         {
442           const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
443           Vector3 position, scale;
444           Quaternion orientation;
445           owningNodeMatrix.GetTransformComponents( position, orientation, scale );
446           Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
447
448           if (mUseReflection)
449           {
450             Vector3 up = orientation.Rotate( Vector3::YAXIS );
451             Vector4 position4 = Vector4(position);
452             Vector4 target4 = Vector4(mTargetPosition);
453             Vector4 up4 = Vector4(up);
454             Vector4 positionNew;
455             Vector4 targetNew;
456             Vector4 upNew;
457             Vector3 positionNew3;
458             Vector3 targetNewVector3;
459             Vector3 upNew3;
460
461             // eye
462             VectorReflectedByPlane(positionNew, position4, mReflectionPlane);
463             VectorReflectedByPlane(targetNew, target4, mReflectionPlane);
464             VectorReflectedByPlane(upNew, up4, mReflectionPlane);
465
466             positionNew3     = Vector3(positionNew);
467             targetNewVector3 = Vector3(targetNew);
468             upNew3           = Vector3(upNew);
469             LookAt(viewMatrix, positionNew3, targetNewVector3, upNew3 );
470
471             Matrix oldViewMatrix( viewMatrix );
472             Matrix tmp;
473             tmp.SetIdentityAndScale(Vector3(-1.0, 1.0,1.0));
474             Matrix::Multiply(viewMatrix, oldViewMatrix, tmp);
475
476             mReflectionEye = positionNew;
477             mUseReflectionClip = true;
478           }
479           else
480           {
481             LookAt( viewMatrix, position, mTargetPosition, orientation.Rotate( Vector3::YAXIS ) );
482           }
483           mViewMatrix.SetDirty( updateBufferIndex );
484           break;
485         }
486       }
487     }
488     --mUpdateViewFlag;
489   }
490   return retval;
491 }
492
493 void Camera::UpdateFrustum( BufferIndex updateBufferIndex, bool normalize )
494 {
495
496   // Extract the clip matrix planes
497   Matrix clipMatrix;
498   Matrix::Multiply( clipMatrix, mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
499
500   const float* cm = clipMatrix.AsFloat();
501   FrustumPlanes& planes = mFrustum[ updateBufferIndex ];
502
503   // Left
504   planes.mPlanes[ 0 ].mNormal.x = cm[ 3 ]  + cm[ 0 ]; // column 4 + column 1
505   planes.mPlanes[ 0 ].mNormal.y = cm[ 7 ]  + cm[ 4 ];
506   planes.mPlanes[ 0 ].mNormal.z = cm[ 11 ] + cm[ 8 ];
507   planes.mPlanes[ 0 ].mDistance = cm[ 15 ] + cm[ 12 ];
508
509   // Right
510   planes.mPlanes[ 1 ].mNormal.x = cm[ 3 ]  - cm[ 0 ]; // column 4 - column 1
511   planes.mPlanes[ 1 ].mNormal.y = cm[ 7 ]  - cm[ 4 ];
512   planes.mPlanes[ 1 ].mNormal.z = cm[ 11 ] - cm[ 8 ];
513   planes.mPlanes[ 1 ].mDistance = cm[ 15 ] - cm[ 12 ];
514
515   // Bottom
516   planes.mPlanes[ 2 ].mNormal.x = cm[ 3 ]  + cm[ 1 ]; // column 4 + column 2
517   planes.mPlanes[ 2 ].mNormal.y = cm[ 7 ]  + cm[ 5 ];
518   planes.mPlanes[ 2 ].mNormal.z = cm[ 11 ] + cm[ 9 ];
519   planes.mPlanes[ 2 ].mDistance = cm[ 15 ] + cm[ 13 ];
520
521   // Top
522   planes.mPlanes[ 3 ].mNormal.x = cm[ 3 ]  - cm[ 1 ]; // column 4 - column 2
523   planes.mPlanes[ 3 ].mNormal.y = cm[ 7 ]  - cm[ 5 ];
524   planes.mPlanes[ 3 ].mNormal.z = cm[ 11 ] - cm[ 9 ];
525   planes.mPlanes[ 3 ].mDistance = cm[ 15 ] - cm[ 13 ];
526
527   // Near
528   planes.mPlanes[ 4 ].mNormal.x = cm[ 3 ]  + cm[ 2 ]; // column 4 + column 3
529   planes.mPlanes[ 4 ].mNormal.y = cm[ 7 ]  + cm[ 6 ];
530   planes.mPlanes[ 4 ].mNormal.z = cm[ 11 ] + cm[ 10 ];
531   planes.mPlanes[ 4 ].mDistance = cm[ 15 ] + cm[ 14 ];
532
533   // Far
534   planes.mPlanes[ 5 ].mNormal.x = cm[ 3 ]  - cm[ 2 ]; // column 4 - column 3
535   planes.mPlanes[ 5 ].mNormal.y = cm[ 7 ]  - cm[ 6 ];
536   planes.mPlanes[ 5 ].mNormal.z = cm[ 11 ] - cm[ 10 ];
537   planes.mPlanes[ 5 ].mDistance = cm[ 15 ] - cm[ 14 ];
538
539   if ( normalize )
540   {
541     for ( uint32_t i = 0; i < 6; ++i )
542     {
543       // Normalize planes to ensure correct bounding distance checking
544       Plane& plane = planes.mPlanes[ i ];
545       float l = 1.0f / plane.mNormal.Length();
546       plane.mNormal *= l;
547       plane.mDistance *= l;
548
549       planes.mSign[i] = Vector3( Sign(plane.mNormal.x), Sign(plane.mNormal.y), Sign(plane.mNormal.z) );
550     }
551   }
552   else
553   {
554     for ( uint32_t i = 0; i < 6; ++i )
555     {
556       planes.mSign[i] = Vector3( Sign(planes.mPlanes[ i ].mNormal.x), Sign(planes.mPlanes[ i ].mNormal.y), Sign(planes.mPlanes[ i ].mNormal.z) );
557     }
558   }
559   mFrustum[ updateBufferIndex ? 0 : 1 ] = planes;
560 }
561
562 bool Camera::CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius )
563 {
564   const FrustumPlanes& planes = mFrustum[ bufferIndex ];
565   for ( uint32_t i = 0; i < 6; ++i )
566   {
567     if ( ( planes.mPlanes[ i ].mDistance + planes.mPlanes[ i ].mNormal.Dot( origin ) ) < -radius )
568     {
569       return false;
570     }
571   }
572   return true;
573 }
574
575 bool Camera::CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents )
576 {
577   const FrustumPlanes& planes = mFrustum[ bufferIndex ];
578   for ( uint32_t i = 0; i < 6; ++i )
579   {
580     if( planes.mPlanes[ i ].mNormal.Dot( origin + (halfExtents * planes.mSign[i]) ) > -(planes.mPlanes[ i ].mDistance) )
581     {
582       continue;
583     }
584
585     return false;
586   }
587   return true;
588 }
589
590 uint32_t Camera::UpdateProjection( BufferIndex updateBufferIndex )
591 {
592   uint32_t retval( mUpdateProjectionFlag );
593   // Early-exit if no update required
594   if ( 0u != mUpdateProjectionFlag )
595   {
596     if ( COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag )
597     {
598       // The projection matrix was updated in the previous frame; copy it
599       mProjectionMatrix.CopyPrevious( updateBufferIndex );
600     }
601     else // UPDATE_COUNT == mUpdateProjectionFlag
602     {
603       switch( mProjectionMode )
604       {
605         case Dali::Camera::PERSPECTIVE_PROJECTION:
606         {
607           Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
608           Perspective( projectionMatrix,
609                        mFieldOfView,
610                        mAspectRatio,
611                        mNearClippingPlane,
612                        mFarClippingPlane,
613                        mInvertYAxis );
614
615           //need to apply custom clipping plane
616           if (mUseReflectionClip)
617           {
618             Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
619             Matrix viewInv = viewMatrix;
620             viewInv.Invert();
621             viewInv.Transpose();
622
623             Dali::Vector4 adjReflectPlane = mReflectionPlane;
624             float d = mReflectionPlane.Dot(mReflectionEye);
625             if (d < 0)
626             {
627               adjReflectPlane.w = -adjReflectPlane.w;
628             }
629
630             Vector4 customClipping = viewInv * adjReflectPlane;
631             AdjustNearPlaneForPerspective(projectionMatrix, customClipping);
632
633             // Invert Z
634             Matrix matZ;
635             matZ.SetIdentity();
636             float* vZ = matZ.AsFloat();
637             vZ[10] = -vZ[10];
638             Matrix::Multiply(projectionMatrix, projectionMatrix , matZ);
639           }
640           break;
641         }
642         case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
643         {
644           Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
645           Orthographic( projectionMatrix,
646                         mLeftClippingPlane,   mRightClippingPlane,
647                         mBottomClippingPlane, mTopClippingPlane,
648                         mNearClippingPlane,   mFarClippingPlane,
649                         mInvertYAxis );
650           break;
651         }
652       }
653
654       mProjectionMatrix.SetDirty( updateBufferIndex );
655     }
656     --mUpdateProjectionFlag;
657   }
658   return retval;
659 }
660
661 } // namespace SceneGraph
662
663 } // namespace Internal
664
665 } // namespace Dali