From 4c6533255026b8e7263b2dca20dc87e00e13bcf7 Mon Sep 17 00:00:00 2001 From: lvlonggame Date: Sun, 1 Feb 2015 21:14:37 +0800 Subject: [PATCH 1/3] add project function to camera --- cocos/2d/CCCamera.cpp | 17 +++++++++++++++++ cocos/2d/CCCamera.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/cocos/2d/CCCamera.cpp b/cocos/2d/CCCamera.cpp index 89baefd956..e4d1513761 100644 --- a/cocos/2d/CCCamera.cpp +++ b/cocos/2d/CCCamera.cpp @@ -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); diff --git a/cocos/2d/CCCamera.h b/cocos/2d/CCCamera.h index bd4e73fb15..35188abe70 100644 --- a/cocos/2d/CCCamera.h +++ b/cocos/2d/CCCamera.h @@ -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. */ From 6d92b7adc9753a6cd7a24764ee78958bc10477f3 Mon Sep 17 00:00:00 2001 From: lvlong Date: Tue, 3 Feb 2015 11:59:43 +0800 Subject: [PATCH 2/3] modify Camera::project --- cocos/2d/CCCamera.cpp | 16 ++++++---------- cocos/2d/CCCamera.h | 19 +++++++++++++++---- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/cocos/2d/CCCamera.cpp b/cocos/2d/CCCamera.cpp index e4d1513761..0c071f5a33 100644 --- a/cocos/2d/CCCamera.cpp +++ b/cocos/2d/CCCamera.cpp @@ -234,21 +234,17 @@ 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 +void Camera::project(const Size& viewport, Vec3* src, Vec2* dst) 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. + getViewProjectionMatrix().transformVector(Vec4(src->x, src->y, src->z, 1.0f), &clipPos); + 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; + + dst->x = (ndcX + 1.0f) * 0.5f * viewport.width; + dst->y = (1.0f - (ndcY + 1.0f) * 0.5f) * viewport.height; } void Camera::unproject(const Size& viewport, Vec3* src, Vec3* dst) const diff --git a/cocos/2d/CCCamera.h b/cocos/2d/CCCamera.h index 35188abe70..cf20bbc86e 100644 --- a/cocos/2d/CCCamera.h +++ b/cocos/2d/CCCamera.h @@ -131,11 +131,22 @@ 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. - */ + * convert the specified point of viewport from world-space coordinates into the screen-space coordinates. + * + * @param viewport The viewport size to use. + * @param src The world-space position. + * @param dst The screen-space position. + */ + void project(const Size& viewport, Vec3* src, Vec2* dst) const; + + /** + * Convert the specified point of viewport from screen-space coordinate into the world-space coordinate. + * + * @param viewport The viewport size to use. + * @param src The screen-space position. + * @param dst The world-space position. + */ void unproject(const Size& viewport, Vec3* src, Vec3* dst) const; /** From a0c7723edbf7db5149f1722a3dc05ad5676bb24b Mon Sep 17 00:00:00 2001 From: lvlong Date: Tue, 3 Feb 2015 14:21:12 +0800 Subject: [PATCH 3/3] add GP_ASSERT --- cocos/2d/CCCamera.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cocos/2d/CCCamera.cpp b/cocos/2d/CCCamera.cpp index 0c071f5a33..e610139df4 100644 --- a/cocos/2d/CCCamera.cpp +++ b/cocos/2d/CCCamera.cpp @@ -236,6 +236,9 @@ bool Camera::initOrthographic(float zoomX, float zoomY, float nearPlane, float f void Camera::project(const Size& viewport, Vec3* src, Vec2* dst) const { + GP_ASSERT(src); + GP_ASSERT(dst); + Vec4 clipPos; getViewProjectionMatrix().transformVector(Vec4(src->x, src->y, src->z, 1.0f), &clipPos); @@ -249,7 +252,9 @@ void Camera::project(const Size& viewport, Vec3* src, Vec2* dst) const void Camera::unproject(const Size& viewport, Vec3* src, Vec3* dst) const { - assert(dst); + GP_ASSERT(src); + GP_ASSERT(dst); + Vec4 screen(src->x / viewport.width, ((viewport.height - src->y)) / viewport.height, src->z, 1.0f); screen.x = screen.x * 2.0f - 1.0f; screen.y = screen.y * 2.0f - 1.0f;