mirror of https://github.com/axmolengine/axmol.git
add project function to camera
This commit is contained in:
parent
5c424194b9
commit
4c65332550
|
@ -234,6 +234,23 @@ bool Camera::initOrthographic(float zoomX, float zoomY, float nearPlane, float f
|
|||
return true;
|
||||
}
|
||||
|
||||
void Camera::project(const Rect& viewport, const Vec3& position, Vec2* out) const
|
||||
{
|
||||
|
||||
// Transform the point to clip-space.
|
||||
Vec4 clipPos;
|
||||
getViewProjectionMatrix().transformVector(Vec4(position.x, position.y, position.z, 1.0f), &clipPos);
|
||||
|
||||
// Compute normalized device coordinates.
|
||||
GP_ASSERT(clipPos.w != 0.0f);
|
||||
float ndcX = clipPos.x / clipPos.w;
|
||||
float ndcY = clipPos.y / clipPos.w;
|
||||
|
||||
// Compute screen coordinates by applying our viewport transformation.
|
||||
out->x = viewport.origin.x + (ndcX + 1.0f) * 0.5f * viewport.size.width;
|
||||
out->y = viewport.origin.y + (1.0f - (ndcY + 1.0f) * 0.5f) * viewport.size.height;
|
||||
}
|
||||
|
||||
void Camera::unproject(const Size& viewport, Vec3* src, Vec3* dst) const
|
||||
{
|
||||
assert(dst);
|
||||
|
|
|
@ -131,6 +131,8 @@ public:
|
|||
/**get view projection matrix*/
|
||||
const Mat4& getViewProjectionMatrix() const;
|
||||
|
||||
void project(const Rect& viewport, const Vec3& position, Vec2* out) const;
|
||||
|
||||
/**
|
||||
* Convert the specified point of viewport from screenspace coordinate into the worldspace coordinate.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue