add project function to camera

This commit is contained in:
lvlonggame 2015-02-01 21:14:37 +08:00
parent 5c424194b9
commit 4c65332550
2 changed files with 19 additions and 0 deletions

View File

@ -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);

View File

@ -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.
*/