2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
18 #include <dali/internal/event/common/projection.h>
21 #include <dali/public-api/math/rect.h>
22 #include <dali/public-api/math/matrix.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/integration-api/debug.h>
32 bool Unproject( const Vector4& windowPos,
33 const Matrix& inverseMvp,
38 objectPos.x = windowPos.x;
39 objectPos.y = windowPos.y;
40 objectPos.z = windowPos.z;
43 objectPos.x = objectPos.x / viewportWidth;
44 objectPos.y = objectPos.y / viewportHeight;
46 objectPos.x = objectPos.x * 2.0f - 1.0f;
47 objectPos.y = objectPos.y * 2.0f - 1.0f;
48 objectPos.z = objectPos.z * 2.0f - 1.0f;
50 objectPos = inverseMvp * objectPos;
52 // In the case where objectPos.w is exactly zero, the unproject fails
53 if ( EqualsZero( objectPos.w ) )
58 objectPos.x /= objectPos.w;
59 objectPos.y /= objectPos.w;
60 objectPos.z /= objectPos.w;
65 bool UnprojectFull( const Vector4& windowPos,
66 const Matrix& modelView,
67 const Matrix& projection,
72 Matrix invertedMvp( false ); // Don't initialize.
73 Matrix::Multiply( invertedMvp, modelView, projection );
75 if (invertedMvp.Invert())
77 return Unproject( windowPos, invertedMvp, viewportWidth, viewportHeight, objectPos );
83 bool XyPlaneIntersect( const Vector4& pointA, const Vector4& pointB, Vector4& intersect )
85 const Vector4* near = NULL;
86 const Vector4* far = NULL;
88 if ( true || (pointA.z > 0.0f && pointB.z < 0.0f ))
93 else if ( pointB.z > 0.0f && pointA.z < 0.0f )
100 return false; // ray does not cross xy plane
103 float dist = near->z / (near->z - far->z);
105 intersect.x = near->x + (far->x - near->x) * dist;
106 intersect.y = near->y + (far->y - near->y) * dist;
112 } // namespace Internal