| 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_Hcxx . . $ `vcg/wrap/gui/view.h:77:9`! |
| 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>cxx . . $ `<file>/vcg/vcg/space/point3.h`/ |
| 87 | #include <vcg/space/plane3.h>cxx . . $ `<file>/vcg/vcg/space/plane3.h`/ |
| 88 | #include <vcg/space/line3.h>cxx . . $ `<file>/vcg/vcg/space/line3.h`/ |
| 89 | #include <vcg/math/matrix44.h>cxx . . $ `<file>/vcg/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 vcgcxx . . $ 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 Viewcxx . . $ vcg/View# { |
| 109 | public: |
| 110 | void GetViewcxx . . $ vcg/View#GetView(49f6e7a06ebc5aa8).(); |
| 111 | void SetViewcxx . . $ vcg/View#SetView(49f6e7a06ebc5aa8).(); |
| 112 | Point3cxx . . $ vcg/Point3#<T> Projectcxx . . $ vcg/View#Project(7198c4a0a81b097f).(const Point3cxx . . $ vcg/Point3#<T> &p) const; |
| 113 | Point3cxx . . $ vcg/Point3#<T> UnProjectcxx . . $ vcg/View#UnProject(7198c4a0a81b097f).(const Point3cxx . . $ vcg/Point3#<T> &p) const; |
| 114 | Point3cxx . . $ vcg/Point3#<T> ViewPointcxx . . $ vcg/View#ViewPoint(d14b145c9ae9c214).() const; |
| 115 | |
| 116 | /// Return the plane perpendicular to the view axis and passing through point P. |
| 117 | Plane3cxx . . $ vcg/Plane3#<T> ViewPlaneFromModelcxx . . $ vcg/View#ViewPlaneFromModel(b58d12aa12d607b2).(const Point3cxx . . $ vcg/Point3#<T> &p); |
| 118 | |
| 119 | /// Return the line of view passing through point P. |
| 120 | Line3cxx . . $ vcg/Line3#<T> ViewLineFromModelcxx . . $ vcg/View#ViewLineFromModel(49e77427d541d16d).(const Point3cxx . . $ vcg/Point3#<T> &p); |
| 121 | |
| 122 | /// Return the line passing through the point p and the observer. |
| 123 | Line3cxx . . $ vcg/Line3#<T> ViewLineFromWindowcxx . . $ vcg/View#ViewLineFromWindow(49e77427d541d16d).(const Point3cxx . . $ vcg/Point3#<T> &p); |
| 124 | |
| 125 | /// Convert coordinates from the range -1..1 of Normalized Device Coords to range 0 viewport[2] |
| 126 | Point3cxx . . $ vcg/Point3#<T> NormDevCoordToWindowCoordcxx . . $ vcg/View#NormDevCoordToWindowCoord(7198c4a0a81b097f).(const Point3cxx . . $ vcg/Point3#<T> &p) const; |
| 127 | |
| 128 | /// Convert coordinates from 0--viewport[2] to the range -1..1 of Normalized Device Coords to |
| 129 | Point3cxx . . $ vcg/Point3#<T> WindowCoordToNormDevCoordcxx . . $ vcg/View#WindowCoordToNormDevCoord(7198c4a0a81b097f).(const Point3cxx . . $ vcg/Point3#<T> &p) const; |
| 130 | |
| 131 | Matrix44cxx . . $ vcg/Matrix44#<T> projcxx . . $ vcg/View#proj.; |
| 132 | Matrix44cxx . . $ vcg/Matrix44#<T> modelcxx . . $ vcg/View#model.; |
| 133 | Matrix44cxx . . $ vcg/Matrix44#<T> matrixcxx . . $ vcg/View#matrix.; |
| 134 | Matrix44cxx . . $ vcg/Matrix44#<T> inversecxx . . $ vcg/View#inverse.; |
| 135 | int viewportcxx . . $ vcg/View#viewport.[4]; |
| 136 | }; |
| 137 | |
| 138 | template <class T> void Viewcxx . . $ vcg/View#<T>::GetViewcxx . . $ vcg/View#GetView(49f6e7a06ebc5aa8).() { |
| 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_MATRIXcxx . . $ `<external>/18396878532286971346/glew.h:486:9`!, m); |
| 156 | projcxx . . $ vcg/View#proj..Importcxx . . $ vcg/Matrix44#Import(95feadcb3d20b31e).(Matrix44dcxx . . $ vcg/Matrix44#Matrix44(88376428bb603c08).(m)); |
| 157 | Transposecxx . . $ vcg/Transpose(24a89560d95dde74).(projcxx . . $ vcg/View#proj.); |
| 158 | glGetDoublev(GL_MODELVIEW_MATRIXcxx . . $ `<external>/11565930711847777359/gl.h:504:9`!, m); |
| 159 | modelcxx . . $ vcg/View#model..Importcxx . . $ vcg/Matrix44#Import(95feadcb3d20b31e).(Matrix44dcxx . . $ vcg/Matrix44#Matrix44(88376428bb603c08).(m)); |
| 160 | Transposecxx . . $ vcg/Transpose(24a89560d95dde74).(modelcxx . . $ vcg/View#model.); |
| 161 | |
| 162 | glGetIntegerv(GL_VIEWPORTcxx . . $ `<external>/18396878532286971346/glew.h:19034:9`!, (GLint*)viewportcxx . . $ vcg/View#viewport.); |
| 163 | |
| 164 | matrixcxx . . $ vcg/View#matrix. = projcxx . . $ vcg/View#proj.*cxx . . $ vcg/`operator*`(28da454bb9273425).modelcxx . . $ vcg/View#model.; |
| 165 | inversecxx . . $ vcg/View#inverse. = matrixcxx . . $ vcg/View#matrix.; |
| 166 | Invertcxx . . $ vcg/Invert(24a89560d95dde74).(inversecxx . . $ vcg/View#inverse.); |
| 167 | } |
| 168 | |
| 169 | template <class T> void Viewcxx . . $ vcg/View#<T>::SetViewcxx . . $ vcg/View#SetView(49f6e7a06ebc5aa8).() { |
| 170 | |
| 171 | } |
| 172 | |
| 173 | template <class T> Point3cxx . . $ vcg/Point3#<T> Viewcxx . . $ vcg/View#<T>::ViewPointcxx . . $ vcg/View#ViewPoint(d14b145c9ae9c214).() const { |
| 174 | Matrix44cxx . . $ vcg/Matrix44#<T> mi=modelcxx . . $ vcg/View#model.; |
| 175 | Invertcxx . . $ vcg/Invert(df0fec4f85dc0cb1).(mi); |
| 176 | return mi*cxx . . $ vcg/`operator*`(28da454bb9273425). Point3cxx . . $ vcg/Point3#<T>(0, 0, 0); |
| 177 | } |
| 178 | // Note that p it is assumed to be in model coordinate. |
| 179 | template <class T> Plane3cxx . . $ vcg/Plane3#<T> Viewcxx . . $ vcg/View#<T>::ViewPlaneFromModelcxx . . $ vcg/View#ViewPlaneFromModel(b58d12aa12d607b2).(const Point3cxx . . $ vcg/Point3#<T> &p) |
| 180 | { |
| 181 | Point3cxx . . $ vcg/Point3#<T> vp=ViewPointcxx . . $ vcg/View#ViewPoint(d14b145c9ae9c214).(); |
| 182 | Plane3cxx . . $ vcg/Plane3#<T> pl; // plane perpedicular to view direction and passing through manip center |
| 183 | pl.n=(vp-cxx . . $ operator-(a1904e9708fcf93e).p); |
| 184 | pl.d=pl.n*cxx . . $ vcg/`operator*`(a9bd0f68cbe04cf9).p; |
| 185 | return pl; |
| 186 | } |
| 187 | |
| 188 | // Note that p it is assumed to be in model coordinate. |
| 189 | template <class T> Line3cxx . . $ vcg/Line3#<T> Viewcxx . . $ vcg/View#<T>::ViewLineFromModelcxx . . $ vcg/View#ViewLineFromModel(49e77427d541d16d).(const Point3cxx . . $ vcg/Point3#<T> &p) |
| 190 | { |
| 191 | Point3cxx . . $ vcg/Point3#<T> vp=ViewPointcxx . . $ vcg/View#ViewPoint(d14b145c9ae9c214).(); |
| 192 | Line3cxx . . $ vcg/Line3#<T> line; |
| 193 | line.SetOrigincxx . . $ vcg/Line3#SetOrigin(e282c5ee7bda69b7).(vp); |
| 194 | line.SetDirectioncxx . . $ vcg/Line3#SetDirection(e282c5ee7bda69b7).(p -cxx . . $ operator-(d2dbe4d702b1483a). vp); |
| 195 | return line; |
| 196 | } |
| 197 | |
| 198 | // Note that p it is assumed to be in window coordinate. |
| 199 | template <class T> Line3cxx . . $ vcg/Line3#<T> Viewcxx . . $ vcg/View#<T>::ViewLineFromWindowcxx . . $ vcg/View#ViewLineFromWindow(49e77427d541d16d).(const Point3cxx . . $ vcg/Point3#<T> &p) |
| 200 | { |
| 201 | Line3cxx . . $ vcg/Line3#<T> ln; // plane perpedicular to view direction and passing through manip center |
| 202 | Point3cxx . . $ vcg/Point3#<T> vp=ViewPointcxx . . $ vcg/View#ViewPoint(d14b145c9ae9c214).(); |
| 203 | Point3cxx . . $ vcg/Point3#<T> pp=UnProjectcxx . . $ vcg/View#UnProject(7198c4a0a81b097f).(p); |
| 204 | ln.SetOrigincxx . . $ vcg/Line3#SetOrigin(e282c5ee7bda69b7).(vp); |
| 205 | ln.SetDirectioncxx . . $ vcg/Line3#SetDirection(e282c5ee7bda69b7).(pp-cxx . . $ operator-(75ef8c2db3144c1d).vp); |
| 206 | return ln; |
| 207 | } |
| 208 | |
| 209 | template <class T> Point3cxx . . $ vcg/Point3#<T> Viewcxx . . $ vcg/View#<T>::Projectcxx . . $ vcg/View#Project(7198c4a0a81b097f).(const Point3cxx . . $ vcg/Point3#<T> &p) const { |
| 210 | Point3cxx . . $ vcg/Point3#<T> r; |
| 211 | r = matrixcxx . . $ vcg/View#matrix. *cxx . . $ vcg/`operator*`(a9bd0f68cbe04cf9). p; |
| 212 | return NormDevCoordToWindowCoordcxx . . $ vcg/View#NormDevCoordToWindowCoord(7198c4a0a81b097f).(r); |
| 213 | } |
| 214 | |
| 215 | template <class T> Point3cxx . . $ vcg/Point3#<T> Viewcxx . . $ vcg/View#<T>::UnProjectcxx . . $ vcg/View#UnProject(7198c4a0a81b097f).(const Point3cxx . . $ vcg/Point3#<T> &p) const { |
| 216 | Point3cxx . . $ vcg/Point3#<T> s = WindowCoordToNormDevCoordcxx . . $ vcg/View#WindowCoordToNormDevCoord(7198c4a0a81b097f).(p); |
| 217 | s = inversecxx . . $ vcg/View#inverse. *cxx . . $ vcg/`operator*`(a9bd0f68cbe04cf9). 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> Point3cxx . . $ vcg/Point3#<T> Viewcxx . . $ vcg/View#<T>::NormDevCoordToWindowCoordcxx . . $ vcg/View#NormDevCoordToWindowCoord(7198c4a0a81b097f).(const Point3cxx . . $ vcg/Point3#<T> &p) const { |
| 227 | Point3cxx . . $ vcg/Point3#<T> a; |
| 228 | a[0] = (p[0]+cxx . . $ operator+(64c93ae51f48ffc6).1)*cxx . . $ vcg/`operator*`(28da454bb9273425).(viewportcxx . . $ vcg/View#viewport.[2]/cxx . . $ `operator/`(fcbf24ccaa3898bd).(T)2.0)+cxx . . $ operator+(7b486e2244b631b2).viewportcxx . . $ vcg/View#viewport.[0]; |
| 229 | a[1] = (p[1]+cxx . . $ operator+(9a1d87909e89eb2e).1)*cxx . . $ vcg/`operator*`(28da454bb9273425).(viewportcxx . . $ vcg/View#viewport.[3]/cxx . . $ `operator/`(d8d44cadf7d035de).(T)2.0)+cxx . . $ operator+(a5ec08826f979ef7).viewportcxx . . $ vcg/View#viewport.[1]; |
| 230 | //a[1] = viewport[3] - a[1]; |
| 231 | a[2] = (p[2]+cxx . . $ operator+(7670cc885d9ae89b).1)/cxx . . $ `operator/`(f0db7c72027f9443).2; |
| 232 | return a; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | template <class T> Point3cxx . . $ vcg/Point3#<T> Viewcxx . . $ vcg/View#<T>::WindowCoordToNormDevCoordcxx . . $ vcg/View#WindowCoordToNormDevCoord(7198c4a0a81b097f).(const Point3cxx . . $ vcg/Point3#<T> &p) const { |
| 237 | Point3cxx . . $ vcg/Point3#<T> a; |
| 238 | a[0] = (p[0]-cxx . . $ operator-(f43f6720c4732e21). viewportcxx . . $ vcg/View#viewport.[0])/cxx . . $ `operator/`(5c6f5a5f7c4fbe05). (viewportcxx . . $ vcg/View#viewport.[2]/cxx . . $ `operator/`(699a39822ea3af83).(T)2.0) -cxx . . $ operator-(f879e562987fa1f7). 1; |
| 239 | a[1] = (p[1]-cxx . . $ operator-(75ef8c2db3144c1d). viewportcxx . . $ vcg/View#viewport.[1])/cxx . . $ `operator/`(5c6f5a5f7c4fbe05). (viewportcxx . . $ vcg/View#viewport.[3]/cxx . . $ `operator/`(f0db7c72027f9443).(T)2.0) -cxx . . $ operator-(d2dbe4d702b1483a). 1; |
| 240 | //a[1] = -a[1]; |
| 241 | a[2] = 2*cxx . . $ vcg/`operator*`(a9bd0f68cbe04cf9).p[2] -cxx . . $ operator-(4b981d54470072b9). 1; |
| 242 | return a; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | }//namespace |
| 247 | |
| 248 | #endif |