[Tizen] Add screen and client rotation itself function
[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   mProjectionRotation( 0 ),
165   mNode( NULL ),
166   mType( DEFAULT_TYPE ),
167   mProjectionMode( DEFAULT_MODE ),
168   mInvertYAxis( DEFAULT_INVERT_Y_AXIS ),
169   mFieldOfView( DEFAULT_FIELD_OF_VIEW ),
170   mAspectRatio( DEFAULT_ASPECT_RATIO ),
171   mLeftClippingPlane( DEFAULT_LEFT_CLIPPING_PLANE ),
172   mRightClippingPlane( DEFAULT_RIGHT_CLIPPING_PLANE ),
173   mTopClippingPlane( DEFAULT_TOP_CLIPPING_PLANE ),
174   mBottomClippingPlane( DEFAULT_BOTTOM_CLIPPING_PLANE ),
175   mNearClippingPlane( DEFAULT_NEAR_CLIPPING_PLANE ),
176   mFarClippingPlane( DEFAULT_FAR_CLIPPING_PLANE ),
177   mTargetPosition( DEFAULT_TARGET_POSITION ),
178   mViewMatrix(),
179   mProjectionMatrix(),
180   mInverseViewProjection( Matrix::IDENTITY ),
181   mFinalProjection( Matrix::IDENTITY )
182 {
183 }
184
185 Camera* Camera::New()
186 {
187   return new Camera();
188 }
189
190 Camera::~Camera()
191 {
192 }
193
194 void Camera::SetNode( const Node* node )
195 {
196   mNode = node;
197 }
198
199 void Camera::SetType( Dali::Camera::Type type )
200 {
201   mType = type;
202 }
203
204 void Camera::SetProjectionMode( Dali::Camera::ProjectionMode mode )
205 {
206   mProjectionMode = mode;
207   mUpdateProjectionFlag = UPDATE_COUNT;
208 }
209
210 void Camera::SetInvertYAxis( bool invertYAxis )
211 {
212   mInvertYAxis = invertYAxis;
213   mUpdateProjectionFlag = UPDATE_COUNT;
214 }
215
216 void Camera::SetFieldOfView( float fieldOfView )
217 {
218   mFieldOfView = fieldOfView;
219   mUpdateProjectionFlag = UPDATE_COUNT;
220 }
221
222 void Camera::SetAspectRatio( float aspectRatio )
223 {
224   mAspectRatio = aspectRatio;
225   mUpdateProjectionFlag = UPDATE_COUNT;
226 }
227
228 void Camera::SetLeftClippingPlane( float leftClippingPlane )
229 {
230   mLeftClippingPlane = leftClippingPlane;
231   mUpdateProjectionFlag = UPDATE_COUNT;
232 }
233
234 void Camera::SetRightClippingPlane( float rightClippingPlane )
235 {
236   mRightClippingPlane = rightClippingPlane;
237   mUpdateProjectionFlag = UPDATE_COUNT;
238 }
239
240 void Camera::SetTopClippingPlane( float topClippingPlane )
241 {
242   mTopClippingPlane = topClippingPlane;
243   mUpdateProjectionFlag = UPDATE_COUNT;
244 }
245
246 void Camera::SetBottomClippingPlane( float bottomClippingPlane )
247 {
248   mBottomClippingPlane = bottomClippingPlane;
249   mUpdateProjectionFlag = UPDATE_COUNT;
250 }
251
252 void Camera::SetNearClippingPlane( float nearClippingPlane )
253 {
254   mNearClippingPlane = nearClippingPlane;
255   mUpdateProjectionFlag = UPDATE_COUNT;
256 }
257
258 void Camera::SetFarClippingPlane( float farClippingPlane )
259 {
260   mFarClippingPlane = farClippingPlane;
261   mUpdateProjectionFlag = UPDATE_COUNT;
262 }
263
264 void Camera::SetTargetPosition( const Vector3& targetPosition )
265 {
266   mTargetPosition = targetPosition;
267   mUpdateViewFlag = UPDATE_COUNT;
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 void Camera::RotateProjection( int rotationAngle )
332 {
333   mProjectionRotation = rotationAngle;
334   mUpdateViewFlag = UPDATE_COUNT;
335 }
336
337 const Matrix& Camera::GetProjectionMatrix( BufferIndex bufferIndex ) const
338 {
339   return mProjectionMatrix[ bufferIndex ];
340 }
341
342 const Matrix& Camera::GetViewMatrix( BufferIndex bufferIndex ) const
343 {
344   return mViewMatrix[ bufferIndex ];
345 }
346
347 const Matrix& Camera::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
348 {
349   return mInverseViewProjection[ bufferIndex ];
350 }
351
352 const Matrix& Camera::GetFinalProjectionMatrix( BufferIndex bufferIndex ) const
353 {
354   return mFinalProjection[ bufferIndex ];
355 }
356
357 const PropertyInputImpl* Camera::GetProjectionMatrix() const
358 {
359   return &mProjectionMatrix;
360 }
361
362 const PropertyInputImpl* Camera::GetViewMatrix() const
363 {
364   return &mViewMatrix;
365 }
366
367 void Camera::Update( BufferIndex updateBufferIndex )
368 {
369   // if owning node has changes in world position we need to update camera for next 2 frames
370   if( mNode->IsLocalMatrixDirty() )
371   {
372     mUpdateViewFlag = UPDATE_COUNT;
373   }
374   if( mNode->GetDirtyFlags() & NodePropertyFlags::VISIBLE )
375   {
376     // If the visibility changes, the projection matrix needs to be re-calculated.
377     // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
378     // in the following update the node will be skipped leaving the projection matrix (double buffered)
379     // with the Identity.
380     mUpdateProjectionFlag = UPDATE_COUNT;
381   }
382
383   // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
384   uint32_t viewUpdateCount = UpdateViewMatrix( updateBufferIndex );
385   uint32_t projectionUpdateCount = UpdateProjection( updateBufferIndex );
386
387   // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
388   if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
389   {
390     // either has actually changed so recalculate
391     Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
392     UpdateFrustum( updateBufferIndex );
393
394     // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
395     static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
396   }
397   else if( viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX )
398   {
399     // neither has actually changed, but we might copied previous frames value so need to
400     // copy the previous inverse and frustum as well
401     mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
402     mFrustum[ updateBufferIndex ] = mFrustum[ updateBufferIndex ? 0 : 1 ];
403   }
404 }
405
406 bool Camera::ViewMatrixUpdated()
407 {
408   return 0u != mUpdateViewFlag;
409 }
410
411 uint32_t Camera::UpdateViewMatrix( BufferIndex updateBufferIndex )
412 {
413   uint32_t retval( mUpdateViewFlag );
414   if( 0u != mUpdateViewFlag )
415   {
416     if( COPY_PREVIOUS_MATRIX == mUpdateViewFlag )
417     {
418       // The projection matrix was updated in the previous frame; copy it
419       mViewMatrix.CopyPrevious( updateBufferIndex );
420     }
421     else // UPDATE_COUNT == mUpdateViewFlag
422     {
423       switch( mType )
424       {
425         // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
426         case Dali::Camera::FREE_LOOK:
427         {
428           Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
429           viewMatrix = mNode->GetWorldMatrix( updateBufferIndex );
430
431           if (mUseReflection)
432           {
433             const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
434             Vector3 position{}, scale{};
435             Quaternion orientation{};
436             owningNodeMatrix.GetTransformComponents( position, orientation, scale );
437             mReflectionEye = position;
438             mUseReflectionClip = true;
439
440             Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
441             Matrix oldViewMatrix( viewMatrix );
442             Matrix::Multiply(viewMatrix, oldViewMatrix, mReflectionMtx);
443           }
444
445           viewMatrix.Invert();
446           mViewMatrix.SetDirty( updateBufferIndex );
447           break;
448         }
449
450         // camera orientation constrained to look at a target
451         case Dali::Camera::LOOK_AT_TARGET:
452         {
453           const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
454           Vector3 position, scale;
455           Quaternion orientation;
456           owningNodeMatrix.GetTransformComponents( position, orientation, scale );
457           Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
458
459           if (mUseReflection)
460           {
461             Vector3 up = orientation.Rotate( Vector3::YAXIS );
462             Vector4 position4 = Vector4(position);
463             Vector4 target4 = Vector4(mTargetPosition);
464             Vector4 up4 = Vector4(up);
465             Vector4 positionNew;
466             Vector4 targetNew;
467             Vector4 upNew;
468             Vector3 positionNew3;
469             Vector3 targetNewVector3;
470             Vector3 upNew3;
471
472             // eye
473             VectorReflectedByPlane(positionNew, position4, mReflectionPlane);
474             VectorReflectedByPlane(targetNew, target4, mReflectionPlane);
475             VectorReflectedByPlane(upNew, up4, mReflectionPlane);
476
477             positionNew3     = Vector3(positionNew);
478             targetNewVector3 = Vector3(targetNew);
479             upNew3           = Vector3(upNew);
480             LookAt(viewMatrix, positionNew3, targetNewVector3, upNew3 );
481
482             Matrix oldViewMatrix( viewMatrix );
483             Matrix tmp;
484             tmp.SetIdentityAndScale(Vector3(-1.0, 1.0,1.0));
485             Matrix::Multiply(viewMatrix, oldViewMatrix, tmp);
486
487             mReflectionEye = positionNew;
488             mUseReflectionClip = true;
489           }
490           else
491           {
492             LookAt( viewMatrix, position, mTargetPosition, orientation.Rotate( Vector3::YAXIS ) );
493           }
494           mViewMatrix.SetDirty( updateBufferIndex );
495           break;
496         }
497       }
498     }
499     --mUpdateViewFlag;
500   }
501   return retval;
502 }
503
504 void Camera::UpdateFrustum( BufferIndex updateBufferIndex, bool normalize )
505 {
506
507   // Extract the clip matrix planes
508   Matrix clipMatrix;
509   Matrix::Multiply( clipMatrix, mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
510
511   const float* cm = clipMatrix.AsFloat();
512   FrustumPlanes& planes = mFrustum[ updateBufferIndex ];
513
514   // Left
515   planes.mPlanes[ 0 ].mNormal.x = cm[ 3 ]  + cm[ 0 ]; // column 4 + column 1
516   planes.mPlanes[ 0 ].mNormal.y = cm[ 7 ]  + cm[ 4 ];
517   planes.mPlanes[ 0 ].mNormal.z = cm[ 11 ] + cm[ 8 ];
518   planes.mPlanes[ 0 ].mDistance = cm[ 15 ] + cm[ 12 ];
519
520   // Right
521   planes.mPlanes[ 1 ].mNormal.x = cm[ 3 ]  - cm[ 0 ]; // column 4 - column 1
522   planes.mPlanes[ 1 ].mNormal.y = cm[ 7 ]  - cm[ 4 ];
523   planes.mPlanes[ 1 ].mNormal.z = cm[ 11 ] - cm[ 8 ];
524   planes.mPlanes[ 1 ].mDistance = cm[ 15 ] - cm[ 12 ];
525
526   // Bottom
527   planes.mPlanes[ 2 ].mNormal.x = cm[ 3 ]  + cm[ 1 ]; // column 4 + column 2
528   planes.mPlanes[ 2 ].mNormal.y = cm[ 7 ]  + cm[ 5 ];
529   planes.mPlanes[ 2 ].mNormal.z = cm[ 11 ] + cm[ 9 ];
530   planes.mPlanes[ 2 ].mDistance = cm[ 15 ] + cm[ 13 ];
531
532   // Top
533   planes.mPlanes[ 3 ].mNormal.x = cm[ 3 ]  - cm[ 1 ]; // column 4 - column 2
534   planes.mPlanes[ 3 ].mNormal.y = cm[ 7 ]  - cm[ 5 ];
535   planes.mPlanes[ 3 ].mNormal.z = cm[ 11 ] - cm[ 9 ];
536   planes.mPlanes[ 3 ].mDistance = cm[ 15 ] - cm[ 13 ];
537
538   // Near
539   planes.mPlanes[ 4 ].mNormal.x = cm[ 3 ]  + cm[ 2 ]; // column 4 + column 3
540   planes.mPlanes[ 4 ].mNormal.y = cm[ 7 ]  + cm[ 6 ];
541   planes.mPlanes[ 4 ].mNormal.z = cm[ 11 ] + cm[ 10 ];
542   planes.mPlanes[ 4 ].mDistance = cm[ 15 ] + cm[ 14 ];
543
544   // Far
545   planes.mPlanes[ 5 ].mNormal.x = cm[ 3 ]  - cm[ 2 ]; // column 4 - column 3
546   planes.mPlanes[ 5 ].mNormal.y = cm[ 7 ]  - cm[ 6 ];
547   planes.mPlanes[ 5 ].mNormal.z = cm[ 11 ] - cm[ 10 ];
548   planes.mPlanes[ 5 ].mDistance = cm[ 15 ] - cm[ 14 ];
549
550   if ( normalize )
551   {
552     for ( uint32_t i = 0; i < 6; ++i )
553     {
554       // Normalize planes to ensure correct bounding distance checking
555       Plane& plane = planes.mPlanes[ i ];
556       float l = 1.0f / plane.mNormal.Length();
557       plane.mNormal *= l;
558       plane.mDistance *= l;
559
560       planes.mSign[i] = Vector3( Sign(plane.mNormal.x), Sign(plane.mNormal.y), Sign(plane.mNormal.z) );
561     }
562   }
563   else
564   {
565     for ( uint32_t i = 0; i < 6; ++i )
566     {
567       planes.mSign[i] = Vector3( Sign(planes.mPlanes[ i ].mNormal.x), Sign(planes.mPlanes[ i ].mNormal.y), Sign(planes.mPlanes[ i ].mNormal.z) );
568     }
569   }
570   mFrustum[ updateBufferIndex ? 0 : 1 ] = planes;
571 }
572
573 bool Camera::CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius )
574 {
575   const FrustumPlanes& planes = mFrustum[ bufferIndex ];
576   for ( uint32_t i = 0; i < 6; ++i )
577   {
578     if ( ( planes.mPlanes[ i ].mDistance + planes.mPlanes[ i ].mNormal.Dot( origin ) ) < -radius )
579     {
580       return false;
581     }
582   }
583   return true;
584 }
585
586 bool Camera::CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents )
587 {
588   const FrustumPlanes& planes = mFrustum[ bufferIndex ];
589   for ( uint32_t i = 0; i < 6; ++i )
590   {
591     if( planes.mPlanes[ i ].mNormal.Dot( origin + (halfExtents * planes.mSign[i]) ) > -(planes.mPlanes[ i ].mDistance) )
592     {
593       continue;
594     }
595
596     return false;
597   }
598   return true;
599 }
600
601 uint32_t Camera::UpdateProjection( BufferIndex updateBufferIndex )
602 {
603   uint32_t retval( mUpdateProjectionFlag );
604   // Early-exit if no update required
605   if ( 0u != mUpdateProjectionFlag )
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     else // UPDATE_COUNT == mUpdateProjectionFlag
613     {
614       switch( mProjectionMode )
615       {
616         case Dali::Camera::PERSPECTIVE_PROJECTION:
617         {
618           Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
619           Perspective( projectionMatrix,
620                        mFieldOfView,
621                        mAspectRatio,
622                        mNearClippingPlane,
623                        mFarClippingPlane,
624                        mInvertYAxis );
625
626           //need to apply custom clipping plane
627           if (mUseReflectionClip)
628           {
629             Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
630             Matrix viewInv = viewMatrix;
631             viewInv.Invert();
632             viewInv.Transpose();
633
634             Dali::Vector4 adjReflectPlane = mReflectionPlane;
635             float d = mReflectionPlane.Dot(mReflectionEye);
636             if (d < 0)
637             {
638               adjReflectPlane.w = -adjReflectPlane.w;
639             }
640
641             Vector4 customClipping = viewInv * adjReflectPlane;
642             AdjustNearPlaneForPerspective(projectionMatrix, customClipping);
643
644             // Invert Z
645             Matrix matZ;
646             matZ.SetIdentity();
647             float* vZ = matZ.AsFloat();
648             vZ[10] = -vZ[10];
649             Matrix::Multiply(projectionMatrix, projectionMatrix , matZ);
650           }
651           break;
652         }
653         case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
654         {
655           Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
656           Orthographic( projectionMatrix,
657                         mLeftClippingPlane,   mRightClippingPlane,
658                         mBottomClippingPlane, mTopClippingPlane,
659                         mNearClippingPlane,   mFarClippingPlane,
660                         mInvertYAxis );
661           break;
662         }
663       }
664
665       mProjectionMatrix.SetDirty( updateBufferIndex );
666
667       Matrix &finalProjection = mFinalProjection[ updateBufferIndex ];
668       finalProjection.SetIdentity();
669
670       Quaternion rotationAngle;
671       switch( mProjectionRotation )
672       {
673         case 90:
674         {
675           rotationAngle = Quaternion( Dali::ANGLE_90, Vector3::ZAXIS );
676           break;
677         }
678         case 180:
679         {
680           rotationAngle = Quaternion( Dali::ANGLE_180, Vector3::ZAXIS );
681           break;
682         }
683         case 270:
684         {
685           rotationAngle = Quaternion( Dali::ANGLE_270, Vector3::ZAXIS );
686           break;
687         }
688         default:
689           rotationAngle = Quaternion( Dali::ANGLE_0, Vector3::ZAXIS );
690           break;
691       }
692
693       Matrix rotation;
694       rotation.SetIdentity();
695       rotation.SetTransformComponents( Vector3( 1.0f, 1.0f, 1.0f ), rotationAngle, Vector3( 0.0f, 0.0f, 0.0f ) );
696
697       Matrix::Multiply( finalProjection, mProjectionMatrix.Get( updateBufferIndex ), rotation );
698     }
699     --mUpdateProjectionFlag;
700   }
701   return retval;
702 }
703
704 } // namespace SceneGraph
705
706 } // namespace Internal
707
708 } // namespace Dali