view.h
Go to the documentation of this file.
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   View();
00112   void GetView();
00113   void SetView(const float *_proj, const float *_modelview, const int *_viewport);
00114   Point3<T> Project(const Point3<T> &p) const;
00115   Point3<T> UnProject(const Point3<T> &p) const;
00116   Point3<T> ViewPoint() const;
00117 
00119   Plane3<T> ViewPlaneFromModel(const Point3<T> &p);
00120 
00122   Line3<T> ViewLineFromModel(const Point3<T> &p);
00123 
00125   Line3<T> ViewLineFromWindow(const Point3<T> &p);
00126 
00128   Point3<T> NormDevCoordToWindowCoord(const Point3<T> &p) const;
00129 
00131   Point3<T> WindowCoordToNormDevCoord(const Point3<T> &p) const;
00132 
00133   Matrix44<T> proj;
00134   Matrix44<T> model;
00135   Matrix44<T> matrix;
00136   Matrix44<T> inverse;
00137   int viewport[4];
00138   bool isOrtho;
00139 };
00140 
00141 template <class T>   View<T>::View() {
00142   isOrtho=false;
00143 }
00144 
00145 template <class T> void View<T>::GetView() {
00146     glGetv(GL_PROJECTION_MATRIX,proj);
00147     glGetv(GL_MODELVIEW_MATRIX,model);
00148     glGetIntegerv(GL_VIEWPORT, (GLint*)viewport);
00149 
00150     if(proj[3][3]==0) isOrtho = false;
00151     else isOrtho = true;
00152 
00153     matrix = proj*model;
00154     inverse = vcg::Inverse(matrix);
00155 }
00156 
00157 template <class T> void View<T>::SetView(const float *_proj,
00158                                          const float *_modelview,
00159                                          const int *_viewport) {
00160   for(int i = 0; i < 4; i++) {
00161     for(int k =0; k < 4; k++) {
00162       proj[i][k] = _proj[4*i+k];
00163       model[i][k] = _modelview[4*i+k];
00164     }
00165     viewport[i] = _viewport[i];
00166   }
00167   matrix = proj*model;
00168   inverse = matrix;
00169   Invert(inverse);
00170 }
00171 
00172 template <class T> Point3<T> View<T>::ViewPoint() const {
00173   if(isOrtho) return vcg::Inverse(model)* Point3<T>(0, 0, 3);
00174   return vcg::Inverse(model)* Point3<T>(0, 0, 0);
00175 }
00176 // Note that p it is assumed to be in model coordinate.
00177 template <class T> Plane3<T> View<T>::ViewPlaneFromModel(const Point3<T> &p)
00178 {
00179   //compute normal, pointing away from view.
00180   Matrix44<T> imodel = vcg::Inverse(model);
00181   Point3<T> vp=ViewPoint();
00182   vcg::Point3f n = imodel * vcg::Point3<T>(0.0f, 0, -1.0f) - vp;
00183 
00184   Plane3<T> pl;
00185   pl.Init(p, n);
00186   return pl;
00187 }
00188 
00189 // Note that p it is assumed to be in model coordinate.
00190 template <class T> Line3<T> View<T>::ViewLineFromModel(const Point3<T> &p)
00191 {
00192   Line3<T> line;
00193   Point3<T> vp=ViewPoint();
00194   if(isOrtho){
00195     line.SetOrigin(p);
00196     line.SetDirection(- vp );
00197   }  else {
00198     line.SetOrigin(vp);
00199     line.SetDirection(p - vp);
00200   }
00201   return line;
00202 }
00203 
00204 // Note that p it is assumed to be in window coordinate.
00205 template <class T> Line3<T> View<T>::ViewLineFromWindow(const Point3<T> &p)
00206 {
00207   Line3<T> line;  // plane perpedicular to view direction and passing through manip center
00208   Point3<T> vp=ViewPoint();
00209   Point3<T> pp=UnProject(p);
00210 
00211   if(isOrtho){
00212     line.SetOrigin(pp);
00213     line.SetDirection(- vp );
00214   }  else {
00215     line.SetOrigin(vp);
00216     line.SetDirection(pp-vp);
00217   }
00218   return line;
00219 }
00220 
00221 template <class T> Point3<T> View<T>::Project(const Point3<T> &p) const {
00222   Point3<T> r;
00223   r = matrix * p;
00224   return NormDevCoordToWindowCoord(r);
00225  }
00226 
00227 template <class T> Point3<T> View<T>::UnProject(const Point3<T> &p) const {
00228   Point3<T> s = WindowCoordToNormDevCoord(p);
00229   s =  inverse * s ;
00230   return s;
00231 }
00232 
00233 // Come spiegato nelle glspec
00234 // dopo la perspective division le coordinate sono dette normalized device coords ( NDC ).
00235 // Per passare alle window coords si deve fare la viewport transformation.
00236 // Le coordinate di viewport stanno tra -1 e 1
00237 
00238 template <class T> Point3<T> View<T>::NormDevCoordToWindowCoord(const Point3<T> &p) const {
00239   Point3<T> a;
00240   a[0] = (p[0]+1)*(viewport[2]/(T)2.0)+viewport[0];
00241     a[1] = (p[1]+1)*(viewport[3]/(T)2.0)+viewport[1];
00242   //a[1] = viewport[3] - a[1];
00243   a[2] = (p[2]+1)/2;
00244   return a;
00245 }
00246 
00247 
00248 template <class T> Point3<T> View<T>::WindowCoordToNormDevCoord(const Point3<T> &p) const {
00249   Point3<T> a;
00250   a[0] = (p[0]- viewport[0])/ (viewport[2]/(T)2.0) - 1;
00251     a[1] = (p[1]- viewport[1])/ (viewport[3]/(T)2.0) - 1;
00252   //a[1] = -a[1];
00253     a[2] = 2*p[2] - 1;
00254   return a;
00255 }
00256 
00257 
00258 }//namespace
00259 
00260 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:38:39