Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · Plugins · File Structure

core/StelProjector.hpp

00001 /*
00002  * Stellarium
00003  * Copyright (C) 2003 Fabien Chereau
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019 
00020 #ifndef _STELPROJECTOR_HPP_
00021 #define _STELPROJECTOR_HPP_
00022 
00023 #include "StelProjectorType.hpp"
00024 #include "VecMath.hpp"
00025 #include "StelSphereGeometry.hpp"
00026 
00034 class StelProjector
00035 {
00036 public:
00037     friend class StelPainter;
00038     friend class StelCore;
00039 
00042     enum StelProjectorMaskType
00043     {
00044         MaskNone,   
00045         MaskDisk    
00046     };
00047 
00050     struct StelProjectorParams
00051     {
00052         StelProjectorParams() : viewportXywh(0), fov(60.), gravityLabels(false), maskType(MaskNone), viewportCenter(0., 0.), flipHorz(false), flipVert(false) {;}
00053         Vector4<int> viewportXywh;     
00054         float fov;                    
00055         bool gravityLabels;            
00056         StelProjectorMaskType maskType;    
00057         float zNear, zFar;            
00058         Vec2f viewportCenter;          
00059         float viewportFovDiameter;    
00060         bool flipHorz, flipVert;       
00061     };
00062 
00064     virtual ~StelProjector() {;}
00065 
00067     // Methods which must be reimplemented by all instance of StelProjector
00069     virtual QString getNameI18() const = 0;
00071     virtual QString getDescriptionI18() const {return "No description";}
00073     QString getHtmlSummary() const;
00075     virtual float getMaxFov() const = 0;
00082     virtual bool forward(Vec3d &v) const = 0;
00084     virtual bool backward(Vec3d &v) const = 0;
00086     virtual float deltaZoom(float fov) const = 0;
00087 
00091     bool intersectViewportDiscontinuity(const Vec3d& p1, const Vec3d& p2) const
00092     {
00093         if (hasDiscontinuity()==false)
00094             return false;
00095         return intersectViewportDiscontinuityInternal(modelViewMatrix*p1, modelViewMatrix*p2);
00096     }
00097 
00099     virtual float fovToViewScalingFactor(float fov) const = 0;
00101     virtual float viewScalingFactorToFov(float vsf) const = 0;
00102 
00106     bool getFlagGravityLabels() const { return gravityLabels; }
00107 
00109     const Vec4i& getViewport() const {return viewportXywh;}
00110 
00112     Vec2f getViewportCenter() const
00113     {
00114         return Vec2f(viewportCenter[0]-viewportXywh[0],viewportCenter[1]-viewportXywh[1]);
00115     }
00116 
00118     int getViewportPosX() const {return viewportXywh[0];}
00120     int getViewportPosY() const {return viewportXywh[1];}
00122     int getViewportWidth() const {return viewportXywh[2];}
00124     int getViewportHeight() const {return viewportXywh[3];}
00125 
00131     SphericalRegionP getViewportConvexPolygon(float marginX=0., float marginY=0.) const;
00132 
00134     SphericalCap getBoundingSphericalCap() const;
00135 
00137     float getPixelPerRadAtCenter() const {return pixelPerRad;}
00138 
00140     float getFov() const {return 360.f/M_PI*viewScalingFactorToFov(0.5f*viewportFovDiameter/pixelPerRad);}
00141 
00143     bool needGlFrontFaceCW() const {return (flipHorz*flipVert < 0.f);}
00144 
00146     // Full projection methods
00149     bool checkInViewport(const Vec3d& pos) const
00150     {
00151         return (pos[1]>=viewportXywh[1] && pos[0]>=viewportXywh[0] &&
00152             pos[1]<=(viewportXywh[1] + viewportXywh[3]) && pos[0]<=(viewportXywh[0] + viewportXywh[2]));
00153     }
00154 
00157     Vec3d viewPortIntersect(const Vec3d& p1, const Vec3d& p2) const
00158     {
00159         Vec3d v1=p1;
00160         Vec3d v2=p2;
00161         Vec3d v;
00162         for (int i=0;i<8;++i)
00163         {
00164             v=(v1+v2)*0.5;
00165             if (!checkInViewport(v))
00166                 v2=v;
00167             else
00168                 v1=v;
00169         }
00170         return v;
00171     }
00172 
00177     inline bool project(const Vec3d& v, Vec3d& win) const
00178     {
00179         win = v;
00180         return projectInPlace(win);
00181     }
00182 
00183     virtual void project(int n, const Vec3d* in, Vec3f* out)
00184     {
00185         Vec3d v;
00186         for (int i = 0; i < n; ++i)
00187         {
00188             v = in[i];
00189             v.transfo4d(modelViewMatrix);
00190             forward(v);
00191             out[i][0] = viewportCenter[0] + flipHorz * pixelPerRad * v[0];
00192             out[i][1] = viewportCenter[1] + flipVert * pixelPerRad * v[1];
00193             out[i][2] = (v[2] - zNear) * oneOverZNearMinusZFar;
00194         }
00195     }
00196 
00200     inline bool projectInPlace(Vec3d& v) const
00201     {
00202         v.transfo4d(modelViewMatrix);
00203         const bool rval = forward(v);
00204         // very important: even when the projected point comes from an
00205         // invisible region of the sky (rval=false), we must finish
00206         // reprojecting, so that OpenGl can successfully eliminate
00207         // polygons by culling.
00208         v[0] = viewportCenter[0] + flipHorz * pixelPerRad * v[0];
00209         v[1] = viewportCenter[1] + flipVert * pixelPerRad * v[1];
00210         v[2] = (v[2] - zNear) * oneOverZNearMinusZFar;
00211         return rval;
00212     }
00213     
00218     bool projectCheck(const Vec3d& v, Vec3d& win) const {return (project(v, win) && checkInViewport(win));}
00219 
00224     bool unProject(const Vec3d& win, Vec3d& v) const {return unProject(win[0], win[1], v);}
00225     bool unProject(double x, double y, Vec3d& v) const;
00226 
00233     bool projectLineCheck(const Vec3d& v1, Vec3d& win1, const Vec3d& v2, Vec3d& win2) const
00234         {return project(v1, win1) && project(v2, win2) && (checkInViewport(win1) || checkInViewport(win2));}
00235 
00237     const Mat4d& getModelViewMatrix() const {return modelViewMatrix;}
00238 
00240     Mat4f getProjectionMatrix() const {return Mat4f(2.f/viewportXywh[2], 0, 0, 0, 0, 2.f/viewportXywh[3], 0, 0, 0, 0, -1., 0., -(2.f*viewportXywh[0] + viewportXywh[2])/viewportXywh[2], -(2.f*viewportXywh[1] + viewportXywh[3])/viewportXywh[3], 0, 1);}
00241 
00244     static const QString maskTypeToString(StelProjectorMaskType type);
00246     static StelProjectorMaskType stringToMaskType(const QString &s);
00247 
00249     StelProjectorMaskType getMaskType(void) const {return maskType;}
00250 
00251 protected:
00253     StelProjector(const Mat4d& modelViewMat) : modelViewMatrix(modelViewMat),
00254         modelViewMatrixf(modelViewMat[0], modelViewMat[1], modelViewMat[2], modelViewMat[3],
00255                          modelViewMat[4], modelViewMat[5], modelViewMat[6], modelViewMat[7],
00256                          modelViewMat[8], modelViewMat[9], modelViewMat[10], modelViewMat[11],
00257                          modelViewMat[12], modelViewMat[13], modelViewMat[14], modelViewMat[15]) {;}
00259     virtual bool hasDiscontinuity() const =0;
00263     virtual bool intersectViewportDiscontinuityInternal(const Vec3d& p1, const Vec3d& p2) const = 0;
00264 
00265     Mat4d modelViewMatrix;         // openGL MODELVIEW Matrix
00266     Mat4f modelViewMatrixf;         // openGL MODELVIEW Matrix
00267     float flipHorz,flipVert;      // Whether to flip in horizontal or vertical directions
00268     float pixelPerRad;            // pixel per rad at the center of the viewport disk
00269     StelProjectorMaskType maskType;    // The current projector mask
00270     float zNear, oneOverZNearMinusZFar;  // Near and far clipping planes
00271     Vec4i viewportXywh;     // Viewport parameters
00272     Vec2f viewportCenter;          // Viewport center in screen pixel
00273     float viewportFovDiameter;    // diameter of the FOV disk in pixel
00274     bool gravityLabels;            // should label text align with the horizon?
00275 
00276 private:
00278     void init(const StelProjectorParams& param);
00279 };
00280 
00281 #endif // _STELPROJECTOR_HPP_

Generated on Mon Mar 22 09:55:38 2010 for Stellarium by  doxygen 1.5.5