Refactored EventToUpdate into EventThreadServices
[platform/core/uifw/dali-core.git] / dali / internal / update / node-attachments / scene-graph-camera-attachment.cpp
1 /*
2  * Copyright (c) 2014 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/node-attachments/scene-graph-camera-attachment.h>
20
21 // INTERNAL HEADERS
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/internal/update/nodes/node.h>
24 #include <dali/internal/update/controllers/scene-controller.h>
25 #include <dali/internal/update/resources/resource-manager.h>
26 #include <dali/integration-api/debug.h>
27
28 namespace // unnamed namespace
29 {
30 const unsigned int UPDATE_COUNT        = 2u;  // Update projection or view matrix this many frames after a change
31 const unsigned int COPY_PREVIOUS_MATRIX = 1u; // Copy view or projection matrix from previous frame
32 }
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 namespace SceneGraph
41 {
42
43 namespace
44 {
45
46 void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
47 {
48   Vector3 vZ = target - eye;
49   vZ.Normalize();
50
51   Vector3 vX = up.Cross(vZ);
52   vX.Normalize();
53
54   Vector3 vY = vZ.Cross(vX);
55   vY.Normalize();
56
57   result.SetInverseTransformComponents(vX, vY, vZ, eye);
58 }
59
60
61 void Frustum(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
62 {
63   float deltaZ = far - near;
64   if ((near <= 0.0f) || (far <= 0.0f) || Equals(right, left) || Equals(bottom, top) || (deltaZ <= 0.0f))
65   {
66     DALI_LOG_ERROR("Invalid parameters passed into Frustum!");
67     DALI_ASSERT_DEBUG("Invalid parameters passed into Frustum!");
68     return;
69   }
70
71   float deltaX = right - left;
72   float deltaY = invertYAxis ? bottom - top : top - bottom;
73
74   result.SetIdentity();
75
76   float* m = result.AsFloat();
77   m[0] = -2.0f * near / deltaX;
78   m[1] = m[2] = m[3] = 0.0f;
79
80   m[5] = -2.0f * near / deltaY;
81   m[4] = m[6] = m[7] = 0.0f;
82
83   m[8] = (right + left) / deltaX;
84   m[9] = (top + bottom) / deltaY;
85   m[10] = (near + far) / deltaZ;
86   m[11] = 1.0f;
87
88   m[14] = -2.0f * near * far / deltaZ;
89   m[12] = m[13] = m[15] = 0.0f;
90 }
91
92 void Perspective(Matrix& result, float fovy, float aspect, float near, float far, bool invertYAxis, const Vector2& stereoBias )
93 {
94   float frustumH = tanf( fovy * 0.5f ) * near;
95   float frustumW = frustumH * aspect;
96   Vector2 bias = stereoBias * 0.5f;
97
98   Frustum(result, -(frustumW + bias.x), frustumW - bias.x, -(frustumH + bias.y), frustumH - bias.y, near, far, invertYAxis);
99 }
100
101 void Orthographic(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
102 {
103   if ( Equals(right, left) || Equals(top, bottom) || Equals(far, near) )
104   {
105     DALI_LOG_ERROR( "Cannot create orthographic projection matrix with a zero dimension." );
106     DALI_ASSERT_DEBUG( "Cannot create orthographic projection matrix with a zero dimension." );
107     return;
108   }
109
110   float deltaX = right - left;
111   float deltaY = invertYAxis ? bottom - top : top - bottom;
112   float deltaZ = far - near;
113
114   float *m = result.AsFloat();
115   m[0] = -2.0f / deltaX;
116   m[1] = 0.0f;
117   m[2] = 0.0f;
118   m[3] = 0.0f;
119
120   m[4] = 0.0f;
121   m[5] = -2.0f / deltaY;
122   m[6] = 0.0f;
123   m[7] = 0.0f;
124
125   m[8] = 0.0f;
126   m[9] = 0.0f;
127   m[10] = 2.0f / deltaZ;
128   m[11] = 0.0f;
129   m[12] = -(right + left) / deltaX;
130   m[13] = -(top + bottom) / deltaY;
131   m[14] = -(near + far)   / deltaZ;
132   m[15] = 1.0f;
133 }
134
135 } // unnamed namespace
136
137 const Dali::Camera::Type CameraAttachment::DEFAULT_TYPE( Dali::Camera::FREE_LOOK );
138 const Dali::Camera::ProjectionMode CameraAttachment::DEFAULT_MODE( Dali::Camera::PERSPECTIVE_PROJECTION );
139 const bool  CameraAttachment::DEFAULT_INVERT_Y_AXIS( false );
140 const float CameraAttachment::DEFAULT_FIELD_OF_VIEW( 45.0f*(M_PI/180.0f) );
141 const float CameraAttachment::DEFAULT_ASPECT_RATIO( 4.0f/3.0f );
142 const float CameraAttachment::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
143 const float CameraAttachment::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
144 const float CameraAttachment::DEFAULT_TOP_CLIPPING_PLANE(-400.0f);
145 const float CameraAttachment::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
146 const float CameraAttachment::DEFAULT_NEAR_CLIPPING_PLANE( 800.0f ); // default height of the screen
147 const float CameraAttachment::DEFAULT_FAR_CLIPPING_PLANE( DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE );
148 const Vector2 CameraAttachment::DEFAULT_STEREO_BIAS( 0.0f, 0.0f );
149 const Vector3 CameraAttachment::DEFAULT_TARGET_POSITION( 0.0f, 0.0f, 0.0f );
150
151
152 CameraAttachment::CameraAttachment()
153 : NodeAttachment(),
154   mUpdateViewFlag( UPDATE_COUNT ),
155   mUpdateProjectionFlag( UPDATE_COUNT ),
156   mType( DEFAULT_TYPE ),
157   mProjectionMode( DEFAULT_MODE ),
158   mInvertYAxis( DEFAULT_INVERT_Y_AXIS ),
159   mFieldOfView( DEFAULT_FIELD_OF_VIEW ),
160   mAspectRatio( DEFAULT_ASPECT_RATIO ),
161   mLeftClippingPlane( DEFAULT_LEFT_CLIPPING_PLANE ),
162   mRightClippingPlane( DEFAULT_RIGHT_CLIPPING_PLANE ),
163   mTopClippingPlane( DEFAULT_TOP_CLIPPING_PLANE ),
164   mBottomClippingPlane( DEFAULT_BOTTOM_CLIPPING_PLANE ),
165   mNearClippingPlane( DEFAULT_NEAR_CLIPPING_PLANE ),
166   mFarClippingPlane( DEFAULT_FAR_CLIPPING_PLANE ),
167   mStereoBias( DEFAULT_STEREO_BIAS ),
168   mTargetPosition( DEFAULT_TARGET_POSITION ),
169   mViewMatrix(),
170   mProjectionMatrix(),
171   mInverseViewProjection( Matrix::IDENTITY )
172 {
173 }
174
175 CameraAttachment* CameraAttachment::New()
176 {
177   return new CameraAttachment();
178 }
179
180 void CameraAttachment::ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex )
181 {
182   // do nothing
183 }
184
185 void CameraAttachment::OnDestroy()
186 {
187   // do nothing
188 }
189
190 CameraAttachment::~CameraAttachment()
191 {
192 }
193
194 RenderableAttachment* CameraAttachment::GetRenderable()
195 {
196   return NULL;
197 }
198
199 void CameraAttachment::SetType( Dali::Camera::Type type )
200 {
201   mType = type;
202 }
203
204 void CameraAttachment::SetProjectionMode( Dali::Camera::ProjectionMode mode )
205 {
206   mProjectionMode = mode;
207   mUpdateProjectionFlag = UPDATE_COUNT;
208 }
209
210 void CameraAttachment::SetInvertYAxis( bool invertYAxis )
211 {
212   mInvertYAxis = invertYAxis;
213   mUpdateProjectionFlag = UPDATE_COUNT;
214 }
215
216 void CameraAttachment::SetFieldOfView( float fieldOfView )
217 {
218   mFieldOfView = fieldOfView;
219   mUpdateProjectionFlag = UPDATE_COUNT;
220 }
221
222 void CameraAttachment::SetAspectRatio( float aspectRatio )
223 {
224   mAspectRatio = aspectRatio;
225   mUpdateProjectionFlag = UPDATE_COUNT;
226 }
227
228 void CameraAttachment::SetStereoBias( const Vector2& stereoBias )
229 {
230   mStereoBias = stereoBias;
231   mUpdateProjectionFlag = UPDATE_COUNT;
232 }
233
234 void CameraAttachment::SetLeftClippingPlane( float leftClippingPlane )
235 {
236   mLeftClippingPlane = leftClippingPlane;
237   mUpdateProjectionFlag = UPDATE_COUNT;
238 }
239
240 void CameraAttachment::SetRightClippingPlane( float rightClippingPlane )
241 {
242   mRightClippingPlane = rightClippingPlane;
243   mUpdateProjectionFlag = UPDATE_COUNT;
244 }
245
246 void CameraAttachment::SetTopClippingPlane( float topClippingPlane )
247 {
248   mTopClippingPlane = topClippingPlane;
249   mUpdateProjectionFlag = UPDATE_COUNT;
250 }
251
252 void CameraAttachment::SetBottomClippingPlane( float bottomClippingPlane )
253 {
254   mBottomClippingPlane = bottomClippingPlane;
255   mUpdateProjectionFlag = UPDATE_COUNT;
256 }
257
258 void CameraAttachment::SetNearClippingPlane( float nearClippingPlane )
259 {
260   mNearClippingPlane = nearClippingPlane;
261   mUpdateProjectionFlag = UPDATE_COUNT;
262 }
263
264 void CameraAttachment::SetFarClippingPlane( float farClippingPlane )
265 {
266   mFarClippingPlane = farClippingPlane;
267   mUpdateProjectionFlag = UPDATE_COUNT;
268 }
269
270 void CameraAttachment::SetTargetPosition( const Vector3& targetPosition )
271 {
272   mTargetPosition = targetPosition;
273   mUpdateViewFlag = UPDATE_COUNT;
274 }
275
276 const Matrix& CameraAttachment::GetProjectionMatrix( BufferIndex bufferIndex ) const
277 {
278   return mProjectionMatrix[ bufferIndex ];
279 }
280
281 const Matrix& CameraAttachment::GetViewMatrix( BufferIndex bufferIndex ) const
282 {
283   return mViewMatrix[ bufferIndex ];
284 }
285
286 const Matrix& CameraAttachment::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
287 {
288   return mInverseViewProjection[ bufferIndex ];
289 }
290
291 const PropertyInputImpl* CameraAttachment::GetProjectionMatrix() const
292 {
293   return &mProjectionMatrix;
294 }
295
296 const PropertyInputImpl* CameraAttachment::GetViewMatrix() const
297 {
298   return &mViewMatrix;
299 }
300
301 void CameraAttachment::Update( BufferIndex updateBufferIndex, const Node& owningNode, int nodeDirtyFlags )
302 {
303   // if owning node has changes in world position we need to update camera for next 2 frames
304   if( nodeDirtyFlags & TransformFlag )
305   {
306     mUpdateViewFlag = UPDATE_COUNT;
307   }
308   if( nodeDirtyFlags & VisibleFlag )
309   {
310     // If the visibility changes, the projection matrix needs to be re-calculated.
311     // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
312     // in the following update the node will be skipped leaving the projection matrix (double buffered)
313     // with the Identity.
314     mUpdateProjectionFlag = UPDATE_COUNT;
315   }
316
317   // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
318   unsigned int viewUpdateCount = UpdateViewMatrix( updateBufferIndex, owningNode );
319   unsigned int projectionUpdateCount = UpdateProjection( updateBufferIndex );
320
321   // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
322   if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
323   {
324     // either has actually changed so recalculate
325     Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
326     // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
327     static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
328   }
329   else if( viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX )
330   {
331     // neither has actually changed, but we might copied previous frames value so need to
332     // copy the previous inverse as well
333     mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
334   }
335 }
336
337 bool CameraAttachment::ViewMatrixUpdated()
338 {
339   return 0u != mUpdateViewFlag;
340 }
341
342 unsigned int CameraAttachment::UpdateViewMatrix( BufferIndex updateBufferIndex, const Node& owningNode )
343 {
344   unsigned int retval( mUpdateViewFlag );
345   if( 0u != mUpdateViewFlag )
346   {
347     if( COPY_PREVIOUS_MATRIX == mUpdateViewFlag )
348     {
349       // The projection matrix was updated in the previous frame; copy it
350       mViewMatrix.CopyPrevious( updateBufferIndex );
351     }
352     else // UPDATE_COUNT == mUpdateViewFlag
353     {
354       switch( mType )
355       {
356         // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
357         case Dali::Camera::FREE_LOOK:
358           {
359           Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
360           viewMatrix.SetInverseTransformComponents( Vector3::ONE, owningNode.GetWorldOrientation( updateBufferIndex ),
361                                                     owningNode.GetWorldPosition( updateBufferIndex ) );
362           mViewMatrix.SetDirty( updateBufferIndex );
363           break;
364         }
365           // camera orientation constrained to look at a target
366         case Dali::Camera::LOOK_AT_TARGET:
367           {
368           Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
369           LookAt( viewMatrix, owningNode.GetWorldPosition( updateBufferIndex ), mTargetPosition,
370                   owningNode.GetWorldOrientation( updateBufferIndex ).Rotate( Vector3::YAXIS ) );
371           mViewMatrix.SetDirty( updateBufferIndex );
372           break;
373         }
374       }
375     }
376     --mUpdateViewFlag;
377   }
378   return retval;
379 }
380
381 unsigned int CameraAttachment::UpdateProjection( BufferIndex updateBufferIndex )
382 {
383   unsigned int retval( mUpdateProjectionFlag );
384   // Early-exit if no update required
385   if ( 0u != mUpdateProjectionFlag )
386   {
387     if ( COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag )
388     {
389       // The projection matrix was updated in the previous frame; copy it
390       mProjectionMatrix.CopyPrevious( updateBufferIndex );
391     }
392     else // UPDATE_COUNT == mUpdateProjectionFlag
393     {
394       switch( mProjectionMode )
395       {
396         case Dali::Camera::PERSPECTIVE_PROJECTION:
397         {
398           Matrix &projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
399           Perspective( projectionMatrix,
400                        mFieldOfView,
401                        mAspectRatio,
402                        mNearClippingPlane,
403                        mFarClippingPlane,
404                        mInvertYAxis,
405                        mStereoBias );
406           break;
407         }
408         case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
409         {
410           Matrix &projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
411           Orthographic( projectionMatrix,
412                         mLeftClippingPlane,   mRightClippingPlane,
413                         mBottomClippingPlane, mTopClippingPlane,
414                         mNearClippingPlane,   mFarClippingPlane,
415                         mInvertYAxis );
416           break;
417         }
418       }
419
420       mProjectionMatrix.SetDirty(updateBufferIndex);
421     }
422     --mUpdateProjectionFlag;
423   }
424   return retval;
425 }
426
427 } // namespace SceneGraph
428
429 } // namespace Internal
430
431 } // namespace Dali