/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
namespace // unnamed namespace
{
-const unsigned int UPDATE_COUNT = 2u; // Update projection or view matrix this many frames after a change
-const unsigned int COPY_PREVIOUS_MATRIX = 1u; // Copy view or projection matrix from previous frame
+const uint32_t UPDATE_COUNT = 2u; // Update projection or view matrix this many frames after a change
+const uint32_t COPY_PREVIOUS_MATRIX = 1u; // Copy view or projection matrix from previous frame
+
+//For reflection and clipping plane
+const float REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_A = 2.0f;
+const float REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_D = 1.0f;
}
namespace Dali
namespace
{
+template< typename T >
+T Sign( T value )
+{
+ return T( T(0) < value ) - T( value < T(0) );
+}
+
void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
{
Vector3 vZ = target - eye;
result.SetInverseTransformComponents(vX, vY, vZ, eye);
}
-
void Frustum(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
{
float deltaZ = far - near;
m[12] = m[13] = m[15] = 0.0f;
}
-void Perspective(Matrix& result, float fovy, float aspect, float near, float far, bool invertYAxis, const Vector2& stereoBias )
+void Perspective(Matrix& result, float fovy, float aspect, float near, float far, bool invertYAxis )
{
float frustumH = tanf( fovy * 0.5f ) * near;
float frustumW = frustumH * aspect;
- Vector2 bias = stereoBias * 0.5f;
- Frustum(result, -(frustumW + bias.x), frustumW - bias.x, -(frustumH + bias.y), frustumH - bias.y, near, far, invertYAxis);
+ Frustum(result, -frustumW, frustumW, -frustumH, frustumH, near, far, invertYAxis);
}
void Orthographic(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
const Dali::Camera::Type Camera::DEFAULT_TYPE( Dali::Camera::FREE_LOOK );
const Dali::Camera::ProjectionMode Camera::DEFAULT_MODE( Dali::Camera::PERSPECTIVE_PROJECTION );
const bool Camera::DEFAULT_INVERT_Y_AXIS( false );
-const float Camera::DEFAULT_FIELD_OF_VIEW( 45.0f*(M_PI/180.0f) );
+const float Camera::DEFAULT_FIELD_OF_VIEW( 45.0f*(Math::PI/180.0f) );
const float Camera::DEFAULT_ASPECT_RATIO( 4.0f/3.0f );
const float Camera::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
const float Camera::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
const float Camera::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
const float Camera::DEFAULT_NEAR_CLIPPING_PLANE( 800.0f ); // default height of the screen
const float Camera::DEFAULT_FAR_CLIPPING_PLANE( DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE );
-const Vector2 Camera::DEFAULT_STEREO_BIAS( 0.0f, 0.0f );
const Vector3 Camera::DEFAULT_TARGET_POSITION( 0.0f, 0.0f, 0.0f );
Camera::Camera()
: mUpdateViewFlag( UPDATE_COUNT ),
mUpdateProjectionFlag( UPDATE_COUNT ),
+ mProjectionRotation(0),
+ mNode( nullptr ),
mType( DEFAULT_TYPE ),
mProjectionMode( DEFAULT_MODE ),
mInvertYAxis( DEFAULT_INVERT_Y_AXIS ),
mBottomClippingPlane( DEFAULT_BOTTOM_CLIPPING_PLANE ),
mNearClippingPlane( DEFAULT_NEAR_CLIPPING_PLANE ),
mFarClippingPlane( DEFAULT_FAR_CLIPPING_PLANE ),
- mStereoBias( DEFAULT_STEREO_BIAS ),
mTargetPosition( DEFAULT_TARGET_POSITION ),
mViewMatrix(),
mProjectionMatrix(),
- mInverseViewProjection( Matrix::IDENTITY )
+ mInverseViewProjection( Matrix::IDENTITY ),
+ mFinalProjection(Matrix::IDENTITY)
{
}
return new Camera();
}
-Camera::~Camera()
+Camera::~Camera() = default;
+
+void Camera::SetNode( const Node* node )
+{
+ mNode = node;
+}
+
+const Node* Camera::GetNode() const
{
+ return mNode;
}
void Camera::SetType( Dali::Camera::Type type )
mUpdateProjectionFlag = UPDATE_COUNT;
}
-void Camera::SetStereoBias( const Vector2& stereoBias )
-{
- mStereoBias = stereoBias;
- mUpdateProjectionFlag = UPDATE_COUNT;
-}
-
void Camera::SetLeftClippingPlane( float leftClippingPlane )
{
mLeftClippingPlane = leftClippingPlane;
mUpdateViewFlag = UPDATE_COUNT;
}
+void VectorReflectedByPlane(Vector4 &out, Vector4 &in, Vector4 &plane)
+{
+ float d = float(2.0) * plane.Dot(in);
+ out.x = static_cast<float>(in.x - plane.x*d);
+ out.y = static_cast<float>(in.y - plane.y*d);
+ out.z = static_cast<float>(in.z - plane.z*d);
+ out.w = static_cast<float>(in.w - plane.w*d);
+}
+
+void Camera::AdjustNearPlaneForPerspective( Matrix& perspective, const Vector4& clipPlane )
+{
+ Vector4 q;
+ float* v = perspective.AsFloat();
+
+ q.x = (Sign(clipPlane.x) + v[8]) / v[0];
+ q.y = (Sign(clipPlane.y) + v[9]) / v[5];
+ q.z = -1.0f;
+ q.w = (1.0f + v[10]) / v[14];
+
+ // Calculate the scaled plane vector
+ Vector4 c = clipPlane * (REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_A / q.Dot( clipPlane));
+
+ // Replace the third row of the projection v
+ v[2] = c.x;
+ v[6] = c.y;
+ v[10] = c.z + REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_D;
+ v[14] = c.w;
+}
+
+void Camera::SetReflectByPlane( const Vector4& plane )
+{
+ float* v = mReflectionMtx.AsFloat();
+ float _2ab = -2.0f * plane.x * plane.y;
+ float _2ac = -2.0f * plane.x * plane.z;
+ float _2bc = -2.0f * plane.y * plane.z;
+
+ v[0] = 1.0f - 2.0f * plane.x * plane.x;
+ v[1] = _2ab;
+ v[2] = _2ac;
+ v[3] = 0.0f;
+
+ v[4] = _2ab;
+ v[5] = 1.0f - 2.0f * plane.y * plane.y;
+ v[6] = _2bc;
+ v[7] = 0.0f;
+
+ v[8] = _2ac;
+ v[9] = _2bc;
+ v[10] = 1.0f - 2.0f * plane.z * plane.z;
+ v[11] = 0.0f;
+
+ v[12] = - 2 * plane.x * plane.w;
+ v[13] = - 2 * plane.y * plane.w;
+ v[14] = - 2 * plane.z * plane.w;
+ v[15] = 1.0f;
+
+ mUseReflection = true;
+ mReflectionPlane = plane;
+ mUpdateViewFlag = UPDATE_COUNT;
+}
+
+void Camera::RotateProjection(int rotationAngle)
+{
+ mProjectionRotation = rotationAngle;
+ mUpdateViewFlag = UPDATE_COUNT;
+}
+
const Matrix& Camera::GetProjectionMatrix( BufferIndex bufferIndex ) const
{
return mProjectionMatrix[ bufferIndex ];
return mInverseViewProjection[ bufferIndex ];
}
+const Matrix& Camera::GetFinalProjectionMatrix(BufferIndex bufferIndex) const
+{
+ return mFinalProjection[ bufferIndex ];
+}
+
const PropertyInputImpl* Camera::GetProjectionMatrix() const
{
return &mProjectionMatrix;
return &mViewMatrix;
}
-void Camera::Update( BufferIndex updateBufferIndex, const Node& owningNode )
+void Camera::Update( BufferIndex updateBufferIndex )
{
// if owning node has changes in world position we need to update camera for next 2 frames
- if( owningNode.IsLocalMatrixDirty() )
+ if( mNode->IsLocalMatrixDirty() )
{
mUpdateViewFlag = UPDATE_COUNT;
}
- if( owningNode.GetDirtyFlags() & VisibleFlag )
+ if( mNode->GetDirtyFlags() & NodePropertyFlags::VISIBLE )
{
// If the visibility changes, the projection matrix needs to be re-calculated.
// It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
}
// if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
- unsigned int viewUpdateCount = UpdateViewMatrix( updateBufferIndex, owningNode );
- unsigned int projectionUpdateCount = UpdateProjection( updateBufferIndex );
+ uint32_t viewUpdateCount = UpdateViewMatrix( updateBufferIndex );
+ uint32_t projectionUpdateCount = UpdateProjection( updateBufferIndex );
// if model or view matrix changed we need to either recalculate the inverse VP or copy previous
if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
return 0u != mUpdateViewFlag;
}
-unsigned int Camera::UpdateViewMatrix( BufferIndex updateBufferIndex, const Node& owningNode )
+uint32_t Camera::UpdateViewMatrix( BufferIndex updateBufferIndex )
{
- unsigned int retval( mUpdateViewFlag );
+ uint32_t retval( mUpdateViewFlag );
if( 0u != mUpdateViewFlag )
{
if( COPY_PREVIOUS_MATRIX == mUpdateViewFlag )
case Dali::Camera::FREE_LOOK:
{
Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
- viewMatrix = owningNode.GetWorldMatrix( updateBufferIndex );
+ viewMatrix = mNode->GetWorldMatrix( updateBufferIndex );
+
+ if (mUseReflection)
+ {
+ const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
+ Vector3 position{}, scale{};
+ Quaternion orientation{};
+ owningNodeMatrix.GetTransformComponents( position, orientation, scale );
+ mReflectionEye = position;
+ mUseReflectionClip = true;
+
+ Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
+ Matrix oldViewMatrix( viewMatrix );
+ Matrix::Multiply(viewMatrix, oldViewMatrix, mReflectionMtx);
+ }
+
viewMatrix.Invert();
mViewMatrix.SetDirty( updateBufferIndex );
break;
}
- // camera orientation constrained to look at a target
+
+ // camera orientation constrained to look at a target
case Dali::Camera::LOOK_AT_TARGET:
{
- const Matrix& owningNodeMatrix( owningNode.GetWorldMatrix( updateBufferIndex ) );
+ const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
Vector3 position, scale;
Quaternion orientation;
owningNodeMatrix.GetTransformComponents( position, orientation, scale );
Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
- LookAt( viewMatrix, position, mTargetPosition, orientation.Rotate( Vector3::YAXIS ) );
+
+ if (mUseReflection)
+ {
+ Vector3 up = orientation.Rotate( Vector3::YAXIS );
+ Vector4 position4 = Vector4(position);
+ Vector4 target4 = Vector4(mTargetPosition);
+ Vector4 up4 = Vector4(up);
+ Vector4 positionNew;
+ Vector4 targetNew;
+ Vector4 upNew;
+ Vector3 positionNew3;
+ Vector3 targetNewVector3;
+ Vector3 upNew3;
+
+ // eye
+ VectorReflectedByPlane(positionNew, position4, mReflectionPlane);
+ VectorReflectedByPlane(targetNew, target4, mReflectionPlane);
+ VectorReflectedByPlane(upNew, up4, mReflectionPlane);
+
+ positionNew3 = Vector3(positionNew);
+ targetNewVector3 = Vector3(targetNew);
+ upNew3 = Vector3(upNew);
+ LookAt(viewMatrix, positionNew3, targetNewVector3, upNew3 );
+
+ Matrix oldViewMatrix( viewMatrix );
+ Matrix tmp;
+ tmp.SetIdentityAndScale(Vector3(-1.0, 1.0,1.0));
+ Matrix::Multiply(viewMatrix, oldViewMatrix, tmp);
+
+ mReflectionEye = positionNew;
+ mUseReflectionClip = true;
+ }
+ else
+ {
+ LookAt( viewMatrix, position, mTargetPosition, orientation.Rotate( Vector3::YAXIS ) );
+ }
mViewMatrix.SetDirty( updateBufferIndex );
break;
}
if ( normalize )
{
- for ( unsigned int i = 0; i < 6; ++i )
+ for ( uint32_t i = 0; i < 6; ++i )
{
// Normalize planes to ensure correct bounding distance checking
Plane& plane = planes.mPlanes[ i ];
}
else
{
- for ( unsigned int i = 0; i < 6; ++i )
+ for ( uint32_t i = 0; i < 6; ++i )
{
planes.mSign[i] = Vector3( Sign(planes.mPlanes[ i ].mNormal.x), Sign(planes.mPlanes[ i ].mNormal.y), Sign(planes.mPlanes[ i ].mNormal.z) );
}
return true;
}
-unsigned int Camera::UpdateProjection( BufferIndex updateBufferIndex )
+uint32_t Camera::UpdateProjection( BufferIndex updateBufferIndex )
{
- unsigned int retval( mUpdateProjectionFlag );
+ uint32_t retval( mUpdateProjectionFlag );
// Early-exit if no update required
if ( 0u != mUpdateProjectionFlag )
{
mAspectRatio,
mNearClippingPlane,
mFarClippingPlane,
- mInvertYAxis,
- mStereoBias );
+ mInvertYAxis );
+
+ //need to apply custom clipping plane
+ if (mUseReflectionClip)
+ {
+ Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
+ Matrix viewInv = viewMatrix;
+ viewInv.Invert();
+ viewInv.Transpose();
+
+ Dali::Vector4 adjReflectPlane = mReflectionPlane;
+ float d = mReflectionPlane.Dot(mReflectionEye);
+ if (d < 0)
+ {
+ adjReflectPlane.w = -adjReflectPlane.w;
+ }
+
+ Vector4 customClipping = viewInv * adjReflectPlane;
+ AdjustNearPlaneForPerspective(projectionMatrix, customClipping);
+
+ // Invert Z
+ Matrix matZ;
+ matZ.SetIdentity();
+ float* vZ = matZ.AsFloat();
+ vZ[10] = -vZ[10];
+ Matrix::Multiply(projectionMatrix, projectionMatrix , matZ);
+ }
break;
}
case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
}
}
- mProjectionMatrix.SetDirty( updateBufferIndex );
+ mProjectionMatrix.SetDirty(updateBufferIndex);
+
+ Matrix& finalProjection = mFinalProjection[updateBufferIndex];
+ finalProjection.SetIdentity();
+
+ Quaternion rotationAngle;
+ switch(mProjectionRotation)
+ {
+ case 90:
+ {
+ rotationAngle = Quaternion(Dali::ANGLE_90, Vector3::ZAXIS);
+ break;
+ }
+ case 180:
+ {
+ rotationAngle = Quaternion(Dali::ANGLE_180, Vector3::ZAXIS);
+ break;
+ }
+ case 270:
+ {
+ rotationAngle = Quaternion(Dali::ANGLE_270, Vector3::ZAXIS);
+ break;
+ }
+ default:
+ rotationAngle = Quaternion(Dali::ANGLE_0, Vector3::ZAXIS);
+ break;
+ }
+
+ Matrix rotation;
+ rotation.SetIdentity();
+ rotation.SetTransformComponents(Vector3(1.0f, 1.0f, 1.0f), rotationAngle, Vector3(0.0f, 0.0f, 0.0f));
+
+ Matrix::Multiply(finalProjection, mProjectionMatrix.Get(updateBufferIndex), rotation);
}
--mUpdateProjectionFlag;
}