From d0c1352cf3e6ec2f2d5cc2923ba18ecf3ee5eb89 Mon Sep 17 00:00:00 2001 From: ganovelli Date: Wed, 3 Nov 2004 09:32:50 +0000 Subject: [PATCH] SetPerspective and SetFrustum added (same parameters as in opengl) --- vcg/math/camera.h | 50 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/vcg/math/camera.h b/vcg/math/camera.h index e710d08a..4d301094 100644 --- a/vcg/math/camera.h +++ b/vcg/math/camera.h @@ -48,8 +48,8 @@ creation #define __VCGLIB_CAMERA // VCG -#include -#include +#include +#include #include namespace vcg{ @@ -62,11 +62,12 @@ public: Camera(): f(0.f),s(vcg::Point2(0.0,0.0)), c(vcg::Point2(0.0,0.0)), - viewport(vcg::Point2(0.0,0.0)), + viewport(vcg::Point2(0,0)), _flags(0),viewportM(1) {} S f; // Focal Distance (cioe' la distanza del piano immagine dal centro di proiezione + S farend; // farend end of frustum (it doesn influence the projection ) vcg::Point2 s; // Scale factor of the image (nei casi in cui a senso) vcg::Point2 c; // pin-hole position vcg::Point2 viewport; // Size viewport (in pixels) @@ -83,14 +84,20 @@ public: bool IsOrtho(){ return (_flags & ORTHO_BIT);} void SetOrtho(bool v ){ if(v) _flags |= ORTHO_BIT; else _flags &= ~ORTHO_BIT; }; char & UberFlags() {return _flags;} -public: + + /// set the camera specifying the perspecive view + inline void SetPerspective(S angle, S ratio, S near, S farend,vcg::Point2 viewport=vcg::Point2(500,-1) ); + + /// set the camera specifying the frustum view + inline void SetFrustum(S dx, S sx, S bt, S tp, S near, S farend,vcg::Point2 viewport=vcg::Point2(500,-1)); + /// project a point from space 3d (in the reference system of the camera) to the camera's plane /// the result is in absolute coordinates inline vcg::Point2 Project(const vcg::Point3 & p); }; +/// project a point in the camera plane template - vcg::Point2 Camera::Project(const vcg::Point3 & p){ S tx = p.X(); S ty = p.Y(); @@ -117,6 +124,37 @@ vcg::Point2 Camera::Project(const vcg::Point3 & p){ return q; } + /// set the camera specifying the perspecive view + template + void Camera::SetPerspective(S angle, S ratio, S nr, S _far,vcg::Point2 vp){ + S halfsize[2]; + halfsize[1] = tan(math::ToRad(angle/2)) * nr; + halfsize[0] = halfsize[1]*ratio; + SetFrustum(-halfsize[0],halfsize[0],-halfsize[1],halfsize[1],nr,_far,vp); + } + + /// set the camera specifying the frustum view + template + void Camera::SetFrustum(S sx, S dx, S bt, S tp, S nr, S _far,vcg::Point2 vp){ + S vpt[2]; + vpt[0] = dx-sx; + vpt[1] = tp-bt; + + viewport[0] = vp[0]; + if(vp[1] != -1) + viewport[1] = vp[1];// the user specified the viewport + else + viewport[1] = viewport[0];// default viewport + + s[0] = vpt[0]/(S) viewport[0]; + s[1] = vpt[1]/(S) viewport[1]; + + c[0] = -sx/vpt[0] * viewport[0]; + c[1] = -bt/vpt[1] * viewport[1]; + + f =nr; + farend = _far; + } } @@ -773,7 +811,7 @@ vcg::Point2 Camera::Project(const vcg::Point3 & p){ // //// Prende in ingresso il bounding box dell'oggetto da inquadrare e setta projection e modelmatrix //// in modo da matchare il piu' possibile quelle della camera. Ovviamente (?) si ignora le distorsioni radiali. -//// Nota che bb viene utilizzato solo per settare i near e far plane in maniera sensata. +//// Nota che bb viene utilizzato solo per settare i near e farend plane in maniera sensata. //void SetGL(const Box3 &bb,scalar subx0=0, scalar subx1=1,scalar suby0=0,scalar suby1=1) //{ // scalar _,__;