Home · All Namespaces · All Classes · Functions · Coding Style · 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         double fov;                    
00055         bool gravityLabels;            
00056         StelProjectorMaskType maskType;    
00057         double zNear, zFar;            
00058         Vec2d viewportCenter;          
00059         double 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 double getMaxFov() const = 0;   
00082     virtual bool forward(Vec3d &v) const = 0;
00084     virtual bool backward(Vec3d &v) const = 0;
00086     virtual double deltaZoom(double fov) const = 0;
00087 
00089     virtual double fovToViewScalingFactor(double fov) const = 0;
00091     virtual double viewScalingFactorToFov(double vsf) const = 0;
00092     
00096     bool getFlagGravityLabels() const { return gravityLabels; }
00097 
00099     const Vec4i& getViewport(void) const {return viewportXywh;}
00100 
00102     Vec2d getViewportCenter(void) const
00103     {
00104         return Vec2d(viewportCenter[0]-viewportXywh[0],viewportCenter[1]-viewportXywh[1]);
00105     }
00106     
00108     int getViewportPosX(void) const {return viewportXywh[0];}
00110     int getViewportPosY(void) const {return viewportXywh[1];}
00112     int getViewportWidth(void) const {return viewportXywh[2];}
00114     int getViewportHeight(void) const {return viewportXywh[3];}
00115     
00120     StelGeom::ConvexPolygon getViewportConvexPolygon(double marginX=0., double marginY=0.) const;
00121 
00125     StelGeom::ConvexS unprojectViewport() const;
00126 
00128     StelGeom::HalfSpace getBoundingHalfSpace() const;
00129     
00131     double getPixelPerRadAtCenter(void) const {return pixelPerRad;}
00132     
00134     double getFov() const {return 360./M_PI*viewScalingFactorToFov(0.5*viewportFovDiameter/pixelPerRad);}
00135     
00137     bool needGlFrontFaceCW(void) const {return (flipHorz*flipVert < 0.0);}
00138     
00140     // Full projection methods
00143     bool checkInViewport(const Vec3d& pos) const
00144     {
00145         return (pos[1]>=viewportXywh[1] && pos[0]>=viewportXywh[0] &&
00146             pos[1]<=(viewportXywh[1] + viewportXywh[3]) && pos[0]<=(viewportXywh[0] + viewportXywh[2]));
00147     }
00148 
00151     Vec3d viewPortIntersect(const Vec3d& p1, const Vec3d& p2) const
00152     {
00153         Vec3d v1=p1;
00154         Vec3d v2=p2;
00155         Vec3d v;
00156         for (int i=0;i<8;++i)
00157         {
00158             v=(v1+v2)*0.5;
00159             if (!checkInViewport(v))
00160                 v2=v;
00161             else
00162                 v1=v;
00163         }
00164         return v;
00165     }
00166         
00171     bool project(const Vec3d& v, Vec3d& win) const
00172     {
00173         // really important speedup:
00174         win[0] = modelViewMatrix.r[0]*v[0] + modelViewMatrix.r[4]*v[1]
00175                 + modelViewMatrix.r[8]*v[2] + modelViewMatrix.r[12];
00176         win[1] = modelViewMatrix.r[1]*v[0] + modelViewMatrix.r[5]*v[1]
00177                 + modelViewMatrix.r[9]*v[2] + modelViewMatrix.r[13];
00178         win[2] = modelViewMatrix.r[2]*v[0] + modelViewMatrix.r[6]*v[1]
00179                 + modelViewMatrix.r[10]*v[2] + modelViewMatrix.r[14];
00180         const bool rval = forward(win);
00181         // very important: even when the projected point comes from an
00182         // invisible region of the sky (rval=false), we must finish
00183         // reprojecting, so that OpenGl can successfully eliminate
00184         // polygons by culling.
00185         win[0] = viewportCenter[0] + flipHorz * pixelPerRad * win[0];
00186         win[1] = viewportCenter[1] + flipVert * pixelPerRad * win[1];
00187         win[2] = (win[2] - zNear) * oneOverZNearMinusZFar;
00188         return rval;
00189     }
00190 
00195     bool projectCheck(const Vec3d& v, Vec3d& win) const {return (project(v, win) && checkInViewport(win));}
00196 
00201     bool unProject(const Vec3d& win, Vec3d& v) const {return unProject(win[0], win[1], v);}
00202     bool unProject(double x, double y, Vec3d& v) const;
00203 
00210     bool projectLineCheck(const Vec3d& v1, Vec3d& win1, const Vec3d& v2, Vec3d& win2) const
00211         {return project(v1, win1) && project(v2, win2) && (checkInViewport(win1) || checkInViewport(win2));}
00212 
00214     const Mat4d& getModelViewMatrix() const {return modelViewMatrix;}
00215     
00218     static const QString maskTypeToString(StelProjectorMaskType type);
00220     static StelProjectorMaskType stringToMaskType(const QString &s);
00221     
00223     StelProjectorMaskType getMaskType(void) const {return maskType;}
00224     
00225 protected:
00227     StelProjector(const Mat4d& modelViewMat) : modelViewMatrix(modelViewMat) {;}
00228 
00229 private:
00231     void init(const StelProjectorParams& param);
00232     
00233     StelProjectorMaskType maskType;    // The current projector mask
00234     double zNear, oneOverZNearMinusZFar;  // Near and far clipping planes
00235     Vec4i viewportXywh;     // Viewport parameters
00236     Vec2d viewportCenter;          // Viewport center in screen pixel
00237     double viewportFovDiameter;    // diameter of the FOV disk in pixel
00238     double pixelPerRad;            // pixel per rad at the center of the viewport disk
00239     double flipHorz,flipVert;      // Whether to flip in horizontal or vertical directions
00240     bool gravityLabels;            // should label text align with the horizon?
00241     Mat4d modelViewMatrix;         // openGL MODELVIEW Matrix
00242 };
00243 
00244 #endif // _STELPROJECTOR_HPP_

Generated on Mon Feb 2 17:23:47 2009 for Stellarium by  doxygen 1.5.5