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

core/StelPainter.hpp

00001 /*
00002  * Stellarium
00003  * Copyright (C) 2008 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 _STELPAINTER_HPP_
00021 #define _STELPAINTER_HPP_
00022 
00023 #include "VecMath.hpp"
00024 #include "StelSphereGeometry.hpp"
00025 #include "StelProjectorType.hpp"
00026 #include "StelProjector.hpp"
00027 #include <QString>
00028 #include <QVarLengthArray>
00029 #include <QFontMetrics>
00030 
00031 #ifdef USE_OPENGL_ES2
00032  #define STELPAINTER_GL2 1
00033 #endif
00034 
00035 #ifdef STELPAINTER_GL2
00036 class QGLShaderProgram;
00037 #endif
00038 
00039 class QPainter;
00040 class QGLContext;
00041 
00042 class StelPainterLight
00043 {
00044 public:
00045     StelPainterLight(int alight=0) : light(alight), enabled(false) {}
00046 
00047     void setPosition(const Vec4f& v);
00048     Vec4f& getPosition() {return position;}
00049 
00050     void setDiffuse(const Vec4f& v);
00051     Vec4f& getDiffuse() {return diffuse;}
00052 
00053     void setSpecular(const Vec4f& v);
00054     Vec4f& getSpecular() {return specular;}
00055 
00056     void setAmbient(const Vec4f& v);
00057     Vec4f& getAmbient() {return ambient;}
00058 
00059     void setEnable(bool v);
00060     void enable();
00061     void disable();
00062     bool isEnabled() const {return enabled;}
00063 
00064 private:
00065     int light;
00066     Vec4f position;
00067     Vec4f diffuse;
00068     Vec4f specular;
00069     Vec4f ambient;
00070     bool enabled;
00071 };
00072 
00073 
00074 class StelPainterMaterial
00075 {
00076 public:
00077     StelPainterMaterial();
00078 
00079     void setSpecular(const Vec4f& v);
00080     Vec4f& getSpecular() {return specular;}
00081 
00082     void setAmbient(const Vec4f& v);
00083     Vec4f& getAmbient() {return ambient;}
00084 
00085     void setEmission(const Vec4f& v);
00086     Vec4f& getEmission() {return emission;}
00087 
00088     void setShininess(float v);
00089     float getShininess() {return shininess;}
00090 private:
00091     Vec4f specular;
00092     Vec4f ambient;
00093     Vec4f emission;
00094     float shininess;
00095 };
00096 
00103 class StelPainter
00104 {
00105 public:
00106     friend class VertexArrayProjector;
00107 
00109     enum SphericalPolygonDrawMode
00110     {
00111         SphericalPolygonDrawModeFill=0,         
00112         SphericalPolygonDrawModeBoundary=1,     
00113         SphericalPolygonDrawModeTextureFill=2   
00114     };
00115 
00117     enum ShadeModel
00118     {
00119         ShadeModelFlat=0x1D00,      
00120         ShadeModelSmooth=0x1D01 
00121     };
00122 
00124     enum DrawingMode
00125     {
00126         Points                      = 0x0000, 
00127         Lines                       = 0x0001, 
00128         LineLoop                    = 0x0002, 
00129         LineStrip                   = 0x0003, 
00130         Triangles                   = 0x0004, 
00131         TriangleStrip               = 0x0005, 
00132         TriangleFan                 = 0x0006  
00133     };
00134 
00135     explicit StelPainter(const StelProjectorP& prj);
00136     ~StelPainter();
00137 
00139     const StelProjectorP& getProjector() const {return prj;}
00140     void setProjector(const StelProjectorP& p);
00141 
00143     void drawViewportShape();
00144 
00154     void drawText(float x, float y, const QString& str, float angleDeg=0.f,
00155               float xshift=0.f, float yshift=0.f, bool noGravity=true);
00156     void drawText(const Vec3d& v, const QString& str, float angleDeg=0.f,
00157               float xshift=0.f, float yshift=0.f, bool noGravity=true);
00158 
00165     void drawSphericalRegion(const SphericalRegion* region, SphericalPolygonDrawMode drawMode=SphericalPolygonDrawModeFill, const SphericalCap* clippingCap=NULL, bool doSubDivise=true, double maxSqDistortion=5.);
00166 
00167     void drawGreatCircleArcs(const StelVertexArray& va, const SphericalCap* clippingCap=NULL);
00168 
00169     void drawSphericalTriangles(const StelVertexArray& va, bool textured, const SphericalCap* clippingCap=NULL, bool doSubDivide=true, double maxSqDistortion=5.);
00170 
00177     void drawSmallCircleArc(const Vec3d& start, const Vec3d& stop, const Vec3d& rotCenter, void (*viewportEdgeIntersectCallback)(const Vec3d& screenPos, const Vec3d& direction, void* userData)=NULL, void* userData=NULL);
00178 
00185     void drawGreatCircleArc(const Vec3d& start, const Vec3d& stop, const SphericalCap* clippingCap=NULL, void (*viewportEdgeIntersectCallback)(const Vec3d& screenPos, const Vec3d& direction, void* userData)=NULL, void* userData=NULL);
00186 
00188     void drawCircle(float x, float y, float r);
00189 
00195     void drawSprite2dMode(float x, float y, float radius);
00196     void drawSprite2dMode(const Vec3d& v, float radius);
00197 
00204     void drawSprite2dMode(float x, float y, float radius, float rotation);
00205 
00209     void drawPoint2d(float x, float y);
00210 
00216     void drawLine2d(float x1, float y1, float x2, float y2);
00217 
00225     void drawRect2d(float x, float y, float width, float height, bool textured=true);
00226 
00228     void sSphere(float radius, float oneMinusOblateness, int slices, int stacks, int orientInside = 0, bool flipTexture = false);
00229 
00231     static StelVertexArray computeSphereNoLight(float radius, float oneMinusOblateness, int slices, int stacks, int orientInside = 0, bool flipTexture = false);
00232 
00234     void sCylinder(float radius, float height, int slices, int orientInside = 0);
00235 
00244     static void computeFanDisk(float radius, int innerFanSlices, int level, QVector<double>& vertexArr, QVector<float>& texCoordArr);
00245 
00247     void sRing(float rMin, float rMax, int slices, int stacks, int orientInside);
00248 
00250     void sSphereMap(float radius, int slices, int stacks, float textureFov = 2.f*M_PI, int orientInside = 0);
00251 
00253     void setFont(const QFont& font);
00254 
00256     void setColor(float r, float g, float b, float a=1.f);
00257 
00259     Vec4f getColor() const;
00260 
00262     StelPainterLight& getLight() {return light;}
00263 
00265     StelPainterMaterial& getMaterial() {return material;}
00266 
00268     QFontMetrics getFontMetrics() const;
00269 
00272     static void initSystemGLInfo(QGLContext* ctx);
00273 
00275     static void setQPainter(QPainter* qPainter);
00276 
00278     static void swapBuffer();
00279 
00281     static void makeMainGLContextCurrent();
00282 
00283     // The following methods try to reflect the API of the incoming QGLPainter class
00284 
00288     void setPointSize(qreal size);
00289 
00291     void setShadeModel(ShadeModel m);
00292 
00294     void enableTexture2d(bool b);
00295 
00296     // Thoses methods should eventually be replaced by a single setVertexArray
00298     void setVertexPointer(int size, int type, const void* pointer) {
00299         vertexArray.size = size; vertexArray.type = type; vertexArray.pointer = pointer;
00300     }
00301 
00303     void setTexCoordPointer(int size, int type, const void* pointer)
00304     {
00305         texCoordArray.size = size; texCoordArray.type = type; texCoordArray.pointer = pointer;
00306     }
00307 
00309     void setColorPointer(int size, int type, const void* pointer)
00310     {
00311         colorArray.size = size; colorArray.type = type; colorArray.pointer = pointer;
00312     }
00313 
00315     void setNormalPointer(int type, const void* pointer)
00316     {
00317         normalArray.size = 3; normalArray.type = type; normalArray.pointer = pointer;
00318     }
00319 
00321     void enableClientStates(bool vertex, bool texture=false, bool color=false, bool normal=false);
00322 
00325     void setArrays(const Vec3d* vertice, const Vec2f* texCoords=NULL, const Vec3f* colorArray=NULL, const Vec3f* normalArray=NULL);
00326 
00332     void drawFromArray(DrawingMode mode, int count, int offset=0, bool doProj=true, const unsigned int* indices=NULL);
00333 
00336     void drawStelVertexArray(const StelVertexArray& arr, bool checkDiscontinuity=true);
00337 
00338 private:
00339 
00340     friend class StelTextureMgr;
00341     friend class StelTexture;
00343     typedef struct
00344     {
00345         int size;               // The number of coordinates per vertex.
00346         int type;               // The data type of each coordinate (GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE).
00347         const void* pointer;    // Pointer to the first coordinate of the first vertex in the array.
00348         bool enabled;           // Define whether the array is enabled or not.
00349     } ArrayDesc;
00350 
00353     ArrayDesc projectArray(const ArrayDesc& array, int offset, int count, const unsigned int* indices=NULL);
00354 
00361     void projectSphericalTriangle(const SphericalCap* clippingCap, const Vec3d* vertices, QVarLengthArray<Vec3f, 4096>* outVertices,
00362             const Vec2f* texturePos=NULL, QVarLengthArray<Vec2f, 4096>* outTexturePos=NULL, double maxSqDistortion=5., int nbI=0,
00363             bool checkDisc1=true, bool checkDisc2=true, bool checkDisc3=true) const;
00364 
00365     void drawTextGravity180(float x, float y, const QString& str, float xshift = 0, float yshift = 0);
00366 
00367     // Used by the method below
00368     static QVector<Vec2f> smallCircleVertexArray;
00369     void drawSmallCircleVertexArray();
00370 
00372     StelProjectorP prj;
00373 
00374 #ifndef NDEBUG
00375 
00376     static class QMutex* globalMutex;
00377 #endif
00378 
00380     static QPainter* qPainter;
00381 
00383     static QGLContext* glContext;
00384 
00386     static bool isNoPowerOfTwoAllowed;
00387 
00388 #ifdef STELPAINTER_GL2
00389     Vec4f currentColor;
00390     bool texture2dEnabled;
00391     static QGLShaderProgram* basicShaderProgram;
00392     struct BasicShaderVars {
00393         int projectionMatrix;
00394         int color;
00395         int vertex;
00396     };
00397     static BasicShaderVars basicShaderVars;
00398     static QGLShaderProgram* colorShaderProgram;
00399     static QGLShaderProgram* texturesShaderProgram;
00400     struct TexturesShaderVars {
00401         int projectionMatrix;
00402         int texCoord;
00403         int vertex;
00404         int texColor;
00405         int texture;
00406     };
00407     static TexturesShaderVars texturesShaderVars;
00408     static QGLShaderProgram* texturesColorShaderProgram;
00409     struct TexturesColorShaderVars {
00410         int projectionMatrix;
00411         int texCoord;
00412         int vertex;
00413         int color;
00414         int texture;
00415     };
00416     static TexturesColorShaderVars texturesColorShaderVars;
00417 #endif
00418 
00420     ArrayDesc vertexArray;
00422     ArrayDesc texCoordArray;
00424     ArrayDesc normalArray;
00426     ArrayDesc colorArray;
00427 
00429     StelPainterLight light;
00430 
00432     StelPainterMaterial material;
00433 };
00434 
00435 #endif // _STELPAINTER_HPP_
00436