2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/common/projection.h>
22 #include <dali/public-api/math/rect.h>
23 #include <dali/public-api/math/matrix.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/integration-api/debug.h>
33 bool Unproject( const Vector4& windowPos,
34 const Matrix& inverseMvp,
39 objectPos.x = windowPos.x;
40 objectPos.y = windowPos.y;
41 objectPos.z = windowPos.z;
44 objectPos.x = objectPos.x / viewportWidth;
45 objectPos.y = objectPos.y / viewportHeight;
47 objectPos.x = objectPos.x * 2.0f - 1.0f;
48 objectPos.y = objectPos.y * 2.0f - 1.0f;
49 objectPos.z = objectPos.z * 2.0f - 1.0f;
51 objectPos = inverseMvp * objectPos;
53 // In the case where objectPos.w is exactly zero, the unproject fails
54 if ( EqualsZero( objectPos.w ) )
59 objectPos.x /= objectPos.w;
60 objectPos.y /= objectPos.w;
61 objectPos.z /= objectPos.w;
66 bool UnprojectFull( const Vector4& windowPos,
67 const Matrix& modelView,
68 const Matrix& projection,
73 Matrix invertedMvp( false ); // Don't initialize.
74 Matrix::Multiply( invertedMvp, modelView, projection );
76 if (invertedMvp.Invert())
78 return Unproject( windowPos, invertedMvp, viewportWidth, viewportHeight, objectPos );
84 bool XyPlaneIntersect( const Vector4& pointA, const Vector4& pointB, Vector4& intersect )
86 const Vector4* near = NULL;
87 const Vector4* far = NULL;
89 if ( pointA.z > 0.0f && pointB.z < 0.0f )
94 else if ( pointB.z > 0.0f && pointA.z < 0.0f )
101 return false; // ray does not cross xy plane
104 float dist = near->z / (near->z - far->z);
106 intersect.x = near->x + (far->x - near->x) * dist;
107 intersect.y = near->y + (far->y - near->y) * dist;
113 } // namespace Internal