/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004 \/)\/ * * Visual Computing Lab /\/| * * ISTI - Italian National Research Council | * * \ * * All rights reserved. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * ****************************************************************************/ /**************************************************************************** History $Log: not supported by cvs2svn $ ****************************************************************************/ #ifndef CAMERA_H #define CAMERA_H #include #include #include #include namespace vcg { template class View { public: void GetView(); void SetView(); Point3 Project(const Point3 &p) const; Point3 UnProject(const Point3 &p) const; Point3 ViewPoint(); //convert coordinates range 0-1 to range 0 viewport[2] Point3 ScreenToViewport(const Point3 &p) const; //viceversa Point3 ViewportToScreen(const Point3 &p) const; Matrix44 proj; Matrix44 model; Matrix44 matrix; Matrix44 inverse; int viewport[4]; }; template void View::GetView() { double m[16]; glGetDoublev(GL_PROJECTION_MATRIX, m); for(int i = 0; i < 16; i++) proj[i] = (T)m[i]; glGetDoublev(GL_MODELVIEW_MATRIX, m); for(int i = 0; i < 16; i++) model[i] = (T)m[i]; glGetIntegerv(GL_VIEWPORT, viewport); matrix = model * proj; inverse = matrix; Invert(inverse); } template void View::SetView() { } template Point3 View::ViewPoint() { return inverse * Point3(0, 0, 0); /*Matrix44d model(model_matrix); model.Invert(); Point3d view = model * Point3d(0, 0, 0); return Point3(view[0], view[1], view[2]); */ } template Point3 View::Project(const Point3 &p) const { Point3 r; r = p * matrix; r[0] = (r[0]+1)*(viewport[2]/(T)2.0)+viewport[0]; r[1] =(r[1]+1)*(viewport[3]/(T)2.0)+viewport[1]; r[1] = viewport[3]-r[1]; return r; /*double r[3]; gluProject(p[0], p[1], p[2], model_matrix, proj_matrix, viewport, &r[0], &r[1], &r[2]); return Point3((T)r[0], (T)r[1], (T)r[2]);*/ } template Point3 View::UnProject(const Point3 &p) const { Point3 s = p; s[0] = (p[0]- viewport[0])/ (viewport[2]/(T)2.0) - 1; s[1] = (p[1]- viewport[1])/ (viewport[3]/(T)2.0) - 1; s[1] = -s[1]; s[2] = p[2]; //s[1] = -s[1]; // pezza aggiunta per il tan2.... ????????????? s = s * inverse; return s; /*double r[3]; gluUnProject(p[0], p[1], p[2], model_matrix, proj_matrix, viewport, &r[0], &r[1], &r[2]); return Point3((T)r[0], (T)r[1], (T)r[2]);*/ } template Point3 View::ScreenToViewport(const Point3 &p) const { Point3 a; a[0] = (p[0]+1)*(viewport[2]/(T)2.0)+viewport[0]; a[1] =(p[1]+1)*(viewport[3]/(T)2.0)+viewport[1]; a[1] = viewport[3] - a[1]; a[2] = p[2]; return a; } //viceversa template Point3 View::ViewportToScreen(const Point3 &p) const { Point3 a; a[0] = (p[0]- viewport[0])/ (viewport[2]/(T)2.0) - 1; a[1] = (p[1]- viewport[1])/ (viewport[3]/(T)2.0) - 1; a[1] = -a[1]; a[2] = p[2]; //a[1] = -a[1]; // pezza aggiunta per il tan2.... ????????????? return a; } }//namespace #endif