| 1 | /**************************************************************************** |
| 2 | * VCGLib o o * |
| 3 | * Visual and Computer Graphics Library o o * |
| 4 | * _ O _ * |
| 5 | * Copyright(C) 2004 \/)\/ * |
| 6 | * Visual Computing Lab /\/| * |
| 7 | * ISTI - Italian National Research Council | * |
| 8 | * \ * |
| 9 | * All rights reserved. * |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published by * |
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
| 14 | * (at your option) any later version. * |
| 15 | * * |
| 16 | * This program is distributed in the hope that it will be useful, * |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 19 | * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * |
| 20 | * for more details. * |
| 21 | * * |
| 22 | ****************************************************************************/ |
| 23 | /**************************************************************************** |
| 24 | History |
| 25 | |
| 26 | $Log: view.h,v $ |
| 27 | Revision 1.16 2007/02/22 09:17:29 cignoni |
| 28 | Added guards for opengl including in mac compiling |
| 29 | |
| 30 | Revision 1.15 2007/01/18 01:24:32 cignoni |
| 31 | Added cast for mac compiling |
| 32 | |
| 33 | Revision 1.14 2005/10/15 15:23:32 ponchio |
| 34 | Fixed viewport<->window coordinate change for the z. (z = 2*z -1 now) |
| 35 | Accordingly to gluUnproject documentation. |
| 36 | |
| 37 | Revision 1.13 2005/02/11 11:53:18 tommyfranken |
| 38 | changed pointf to point<t> in ViewLineFromWindow |
| 39 | |
| 40 | Revision 1.12 2005/02/10 20:09:11 tarini |
| 41 | dispelled the mega-evil of GL_TRANSPOSE_*_MATRIX_ARB |
| 42 | |
| 43 | Revision 1.11 2005/01/19 10:29:45 cignoni |
| 44 | Removed the inclusion of a glext.h |
| 45 | |
| 46 | Revision 1.10 2005/01/18 16:47:42 ricciodimare |
| 47 | *** empty log message *** |
| 48 | |
| 49 | Revision 1.9 2004/09/28 10:22:00 ponchio |
| 50 | Added #include <GL/gl.h> |
| 51 | |
| 52 | Revision 1.8 2004/05/26 15:15:46 cignoni |
| 53 | Removed inclusion of gl extension stuff |
| 54 | |
| 55 | Revision 1.7 2004/05/14 03:15:40 ponchio |
| 56 | Added ViewLineFromModel |
| 57 | |
| 58 | Revision 1.6 2004/05/12 20:55:18 ponchio |
| 59 | *** empty log message *** |
| 60 | |
| 61 | Revision 1.5 2004/05/07 12:46:08 cignoni |
| 62 | Restructured and adapted in a better way to opengl |
| 63 | |
| 64 | Revision 1.4 2004/04/07 10:54:11 cignoni |
| 65 | Commented out unused parameter names and other minor warning related issues |
| 66 | |
| 67 | Revision 1.3 2004/03/31 15:07:37 ponchio |
| 68 | CAMERA_H -> VCG_CAMERA_H |
| 69 | |
| 70 | Revision 1.2 2004/03/25 14:55:25 ponchio |
| 71 | Adding copyright. |
| 72 | |
| 73 | |
| 74 | ****************************************************************************/ |
| 75 | |
| 76 | #ifndef VCG_CAMERA_H |
| 77 | #define VCG_CAMERA_H |
| 78 | |
| 79 | /********************** |
| 80 | WARNING |
| 81 | Everything here assumes the opengl window coord system. |
| 82 | the 0,0 is bottom left |
| 83 | y is upward! |
| 84 | **********************/ |
| 85 | |
| 86 | #include <vcg/space/point3.h> |
| 87 | #include <vcg/space/plane3.h> |
| 88 | #include <vcg/space/line3.h> |
| 89 | #include <vcg/math/matrix44.h> |
| 90 | |
| 91 | #ifdef WIN32 |
| 92 | #include <windows.h> |
| 93 | #endif |
| 94 | |
| 95 | #if defined(__APPLE__) |
| 96 | #include <OpenGL/gl.h> |
| 97 | #else |
| 98 | #include <GL/gl.h> |
| 99 | #endif |
| 100 | |
| 101 | namespace vcg { |
| 102 | /** |
| 103 | This class represent the viewing parameters under opengl. |
| 104 | Mainly it stores the projection and modelview matrix and the viewport |
| 105 | and it is used to simply project back and forth points, computing line of sight, planes etc. |
| 106 | */ |
| 107 | |
| 108 | template <class T> class View { |
| 109 | public: |
| 110 | void GetView(); |
| 111 | void SetView(); |
| 112 | Point3<T> Project(const Point3<T> &p) const; |
| 113 | Point3<T> UnProject(const Point3<T> &p) const; |
| 114 | Point3<T> ViewPoint() const; |
| 115 | |
| 116 | /// Return the plane perpendicular to the view axis and passing through point P. |
| 117 | Plane3<T> ViewPlaneFromModel(const Point3<T> &p); |
| 118 | |
| 119 | /// Return the line of view passing through point P. |
| 120 | Line3<T> ViewLineFromModel(const Point3<T> &p); |
| 121 | |
| 122 | /// Return the line passing through the point p and the observer. |
| 123 | Line3<T> ViewLineFromWindow(const Point3<T> &p); |
| 124 | |
| 125 | /// Convert coordinates from the range -1..1 of Normalized Device Coords to range 0 viewport[2] |
| 126 | Point3<T> NormDevCoordToWindowCoord(const Point3<T> &p) const; |
| 127 | |
| 128 | /// Convert coordinates from 0--viewport[2] to the range -1..1 of Normalized Device Coords to |
| 129 | Point3<T> WindowCoordToNormDevCoord(const Point3<T> &p) const; |
| 130 | |
| 131 | Matrix44<T> proj; |
| 132 | Matrix44<T> model; |
| 133 | Matrix44<T> matrix; |
| 134 | Matrix44<T> inverse; |
| 135 | int viewport[4]; |
| 136 | }; |
| 137 | |
| 138 | template <class T> void View<T>::GetView() { |
| 139 | double m[16]; |
| 140 | |
| 141 | // Don't use GL_TRANSPOSE_*_MATRIX_ARB |
| 142 | // they are veeeery evil! |
| 143 | // On many OpenGL implementation they just don't work, causing endless hours of debugging! |
| 144 | |
| 145 | /* |
| 146 | glGetDoublev(GL_TRANSPOSE_PROJECTION_MATRIX_ARB, m); |
| 147 | proj.Import(Matrix44d(m)); |
| 148 | glGetDoublev(GL_TRANSPOSE_MODELVIEW_MATRIX_ARB, m); |
| 149 | model.Import(Matrix44d(m)); |
| 150 | */ |
| 151 | |
| 152 | // Use standard routines, and transpose manually. |
| 153 | // some 10^-20 seconds slower, but a lot safer |
| 154 | |
| 155 | glGetDoublev(GL_PROJECTION_MATRIX, m); |
| 156 | proj.Import(Matrix44d(m)); |
| 157 | Transpose(proj); |
| 158 | glGetDoublev(GL_MODELVIEW_MATRIX, m); |
| 159 | model.Import(Matrix44d(m)); |
| 160 | Transpose(model); |
| 161 | |
| 162 | glGetIntegerv(GL_VIEWPORT, (GLint*)viewport); |
| 163 | |
| 164 | matrix = proj*model; |
| 165 | inverse = matrix; |
| 166 | Invert(inverse); |
| 167 | } |
| 168 | |
| 169 | template <class T> void View<T>::SetView() { |
| 170 | |
| 171 | } |
| 172 | |
| 173 | template <class T> Point3<T> View<T>::ViewPoint() const { |
| 174 | Matrix44<T> mi=model; |
| 175 | Invert(mi); |
| 176 | return mi* Point3<T>(0, 0, 0); |
| 177 | } |
| 178 | // Note that p it is assumed to be in model coordinate. |
| 179 | template <class T> Plane3<T> View<T>::ViewPlaneFromModel(const Point3<T> &p) |
| 180 | { |
| 181 | Point3<T> vp=ViewPoint(); |
| 182 | Plane3<T> pl; // plane perpedicular to view direction and passing through manip center |
| 183 | pl.n=(vp-p); |
| 184 | pl.d=pl.n*p; |
| 185 | return pl; |
| 186 | } |
| 187 | |
| 188 | // Note that p it is assumed to be in model coordinate. |
| 189 | template <class T> Line3<T> View<T>::ViewLineFromModel(const Point3<T> &p) |
| 190 | { |
| 191 | Point3<T> vp=ViewPoint(); |
| 192 | Line3<T> line; |
| 193 | line.SetOrigin(vp); |
| 194 | line.SetDirection(p - vp); |
| 195 | return line; |
| 196 | } |
| 197 | |
| 198 | // Note that p it is assumed to be in window coordinate. |
| 199 | template <class T> Line3<T> View<T>::ViewLineFromWindow(const Point3<T> &p) |
| 200 | { |
| 201 | Line3<T> ln; // plane perpedicular to view direction and passing through manip center |
| 202 | Point3<T> vp=ViewPoint(); |
| 203 | Point3<T> pp=UnProject(p); |
| 204 | ln.SetOrigin(vp); |
| 205 | ln.SetDirection(pp-vp); |
| 206 | return ln; |
| 207 | } |
| 208 | |
| 209 | template <class T> Point3<T> View<T>::Project(const Point3<T> &p) const { |
| 210 | Point3<T> r; |
| 211 | r = matrix * p; |
| 212 | return NormDevCoordToWindowCoord(r); |
| 213 | } |
| 214 | |
| 215 | template <class T> Point3<T> View<T>::UnProject(const Point3<T> &p) const { |
| 216 | Point3<T> s = WindowCoordToNormDevCoord(p); |
| 217 | s = inverse * s ; |
| 218 | return s; |
| 219 | } |
| 220 | |
| 221 | // Come spiegato nelle glspec |
| 222 | // dopo la perspective division le coordinate sono dette normalized device coords ( NDC ). |
| 223 | // Per passare alle window coords si deve fare la viewport transformation. |
| 224 | // Le coordinate di viewport stanno tra -1 e 1 |
| 225 | |
| 226 | template <class T> Point3<T> View<T>::NormDevCoordToWindowCoord(const Point3<T> &p) const { |
| 227 | Point3<T> a; |
| 228 | a[0] = (p[0]+1)*(viewport[2]/(T)2.0)+viewport[0]; |
| 229 | a[1] = (p[1]+1)*(viewport[3]/(T)2.0)+viewport[1]; |
| 230 | //a[1] = viewport[3] - a[1]; |
| 231 | a[2] = (p[2]+1)/2; |
| 232 | return a; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | template <class T> Point3<T> View<T>::WindowCoordToNormDevCoord(const Point3<T> &p) const { |
| 237 | Point3<T> a; |
| 238 | a[0] = (p[0]- viewport[0])/ (viewport[2]/(T)2.0) - 1; |
| 239 | a[1] = (p[1]- viewport[1])/ (viewport[3]/(T)2.0) - 1; |
| 240 | //a[1] = -a[1]; |
| 241 | a[2] = 2*p[2] - 1; |
| 242 | return a; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | }//namespace |
| 247 | |
| 248 | #endif |