00001 /**************************************************************************** 00002 * VCGLib o o * 00003 * Visual and Computer Graphics Library o o * 00004 * _ O _ * 00005 * Copyright(C) 2004 \/)\/ * 00006 * Visual Computing Lab /\/| * 00007 * ISTI - Italian National Research Council | * 00008 * \ * 00009 * All rights reserved. * 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 * This program is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * 00020 * for more details. * 00021 * * 00022 ****************************************************************************/ 00023 /**************************************************************************** 00024 History 00025 00026 $Log: not supported by cvs2svn $ 00027 Revision 1.15 2007/01/18 01:24:32 cignoni 00028 Added cast for mac compiling 00029 00030 Revision 1.14 2005/10/15 15:23:32 ponchio 00031 Fixed viewport<->window coordinate change for the z. (z = 2*z -1 now) 00032 Accordingly to gluUnproject documentation. 00033 00034 Revision 1.13 2005/02/11 11:53:18 tommyfranken 00035 changed pointf to point<t> in ViewLineFromWindow 00036 00037 Revision 1.12 2005/02/10 20:09:11 tarini 00038 dispelled the mega-evil of GL_TRANSPOSE_*_MATRIX_ARB 00039 00040 Revision 1.11 2005/01/19 10:29:45 cignoni 00041 Removed the inclusion of a glext.h 00042 00043 Revision 1.10 2005/01/18 16:47:42 ricciodimare 00044 *** empty log message *** 00045 00046 Revision 1.9 2004/09/28 10:22:00 ponchio 00047 Added #include <GL/gl.h> 00048 00049 Revision 1.8 2004/05/26 15:15:46 cignoni 00050 Removed inclusion of gl extension stuff 00051 00052 Revision 1.7 2004/05/14 03:15:40 ponchio 00053 Added ViewLineFromModel 00054 00055 Revision 1.6 2004/05/12 20:55:18 ponchio 00056 *** empty log message *** 00057 00058 Revision 1.5 2004/05/07 12:46:08 cignoni 00059 Restructured and adapted in a better way to opengl 00060 00061 Revision 1.4 2004/04/07 10:54:11 cignoni 00062 Commented out unused parameter names and other minor warning related issues 00063 00064 Revision 1.3 2004/03/31 15:07:37 ponchio 00065 CAMERA_H -> VCG_CAMERA_H 00066 00067 Revision 1.2 2004/03/25 14:55:25 ponchio 00068 Adding copyright. 00069 00070 00071 ****************************************************************************/ 00072 00073 #ifndef __VCGLIB_WRAP_GUI_VIEW_H 00074 #define __VCGLIB_WRAP_GUI_VIEW_H 00075 00076 /********************** 00077 WARNING 00078 Everything here assumes the opengl window coord system. 00079 the 0,0 is bottom left 00080 y is upward! 00081 **********************/ 00082 00083 #include <vcg/space/point3.h> 00084 #include <vcg/space/plane3.h> 00085 #include <vcg/space/line3.h> 00086 #include <vcg/math/matrix44.h> 00087 #include <wrap/gl/math.h> 00088 00089 #ifdef WIN32 00090 #include <windows.h> 00091 #endif 00092 00093 #if defined(__APPLE__) 00094 #include <OpenGL/gl.h> 00095 #else 00096 #include <GL/gl.h> 00097 #endif 00098 00099 namespace vcg { 00109 template <class T> class View { 00110 public: 00111 void GetView(); 00112 void SetView(const float *_proj, const float *_modelview, const int *_viewport); 00113 Point3<T> Project(const Point3<T> &p) const; 00114 Point3<T> UnProject(const Point3<T> &p) const; 00115 Point3<T> ViewPoint() const; 00116 00118 Plane3<T> ViewPlaneFromModel(const Point3<T> &p); 00119 00121 Line3<T> ViewLineFromModel(const Point3<T> &p); 00122 00124 Line3<T> ViewLineFromWindow(const Point3<T> &p); 00125 00127 Point3<T> NormDevCoordToWindowCoord(const Point3<T> &p) const; 00128 00130 Point3<T> WindowCoordToNormDevCoord(const Point3<T> &p) const; 00131 00132 Matrix44<T> proj; 00133 Matrix44<T> model; 00134 Matrix44<T> matrix; 00135 Matrix44<T> inverse; 00136 int viewport[4]; 00137 }; 00138 00139 template <class T> void View<T>::GetView() { 00140 glGetv(GL_PROJECTION_MATRIX,proj); 00141 glGetv(GL_MODELVIEW_MATRIX,model); 00142 glGetIntegerv(GL_VIEWPORT, (GLint*)viewport); 00143 00144 matrix = proj*model; 00145 inverse = matrix; 00146 Invert(inverse); 00147 } 00148 00149 template <class T> void View<T>::SetView(const float *_proj = NULL, 00150 const float *_modelview = NULL, 00151 const int *_viewport = NULL) { 00152 for(int i = 0; i < 4; i++) { 00153 for(int k =0; k < 4; k++) { 00154 proj[i][k] = _proj[4*i+k]; 00155 model[i][k] = _modelview[4*i+k]; 00156 } 00157 viewport[i] = _viewport[i]; 00158 } 00159 matrix = proj*model; 00160 inverse = matrix; 00161 Invert(inverse); 00162 } 00163 00164 template <class T> Point3<T> View<T>::ViewPoint() const { 00165 Matrix44<T> mi=model; 00166 Invert(mi); 00167 return mi* Point3<T>(0, 0, 0); 00168 } 00169 // Note that p it is assumed to be in model coordinate. 00170 template <class T> Plane3<T> View<T>::ViewPlaneFromModel(const Point3<T> &p) 00171 { 00172 Point3<T> vp=ViewPoint(); 00173 Plane3<T> pl; // plane perpedicular to view direction and passing through manip center 00174 pl.n=(vp-p); 00175 pl.d=pl.n*p; 00176 return pl; 00177 } 00178 00179 // Note that p it is assumed to be in model coordinate. 00180 template <class T> Line3<T> View<T>::ViewLineFromModel(const Point3<T> &p) 00181 { 00182 Point3<T> vp=ViewPoint(); 00183 Line3<T> line; 00184 line.SetOrigin(vp); 00185 line.SetDirection(p - vp); 00186 return line; 00187 } 00188 00189 // Note that p it is assumed to be in window coordinate. 00190 template <class T> Line3<T> View<T>::ViewLineFromWindow(const Point3<T> &p) 00191 { 00192 Line3<T> ln; // plane perpedicular to view direction and passing through manip center 00193 Point3<T> vp=ViewPoint(); 00194 Point3<T> pp=UnProject(p); 00195 ln.SetOrigin(vp); 00196 ln.SetDirection(pp-vp); 00197 return ln; 00198 } 00199 00200 template <class T> Point3<T> View<T>::Project(const Point3<T> &p) const { 00201 Point3<T> r; 00202 r = matrix * p; 00203 return NormDevCoordToWindowCoord(r); 00204 } 00205 00206 template <class T> Point3<T> View<T>::UnProject(const Point3<T> &p) const { 00207 Point3<T> s = WindowCoordToNormDevCoord(p); 00208 s = inverse * s ; 00209 return s; 00210 } 00211 00212 // Come spiegato nelle glspec 00213 // dopo la perspective division le coordinate sono dette normalized device coords ( NDC ). 00214 // Per passare alle window coords si deve fare la viewport transformation. 00215 // Le coordinate di viewport stanno tra -1 e 1 00216 00217 template <class T> Point3<T> View<T>::NormDevCoordToWindowCoord(const Point3<T> &p) const { 00218 Point3<T> a; 00219 a[0] = (p[0]+1)*(viewport[2]/(T)2.0)+viewport[0]; 00220 a[1] = (p[1]+1)*(viewport[3]/(T)2.0)+viewport[1]; 00221 //a[1] = viewport[3] - a[1]; 00222 a[2] = (p[2]+1)/2; 00223 return a; 00224 } 00225 00226 00227 template <class T> Point3<T> View<T>::WindowCoordToNormDevCoord(const Point3<T> &p) const { 00228 Point3<T> a; 00229 a[0] = (p[0]- viewport[0])/ (viewport[2]/(T)2.0) - 1; 00230 a[1] = (p[1]- viewport[1])/ (viewport[3]/(T)2.0) - 1; 00231 //a[1] = -a[1]; 00232 a[2] = 2*p[2] - 1; 00233 return a; 00234 } 00235 00236 00237 }//namespace 00238 00239 #endif