Stellarium 0.11.4
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., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
00018  */
00019 
00020 #ifndef _STELPAINTER_HPP_
00021 #define _STELPAINTER_HPP_
00022 #include "VecMath.hpp"
00023 #include "StelSphereGeometry.hpp"
00024 #include "StelProjectorType.hpp"
00025 #include "StelProjector.hpp"
00026 #include <QString>
00027 #include <QVarLengthArray>
00028 #include <QFontMetrics>
00029 
00030 #ifdef USE_OPENGL_ES2
00031  #define STELPAINTER_GL2 1
00032 #endif
00033 
00034 #ifdef STELPAINTER_GL2
00035 class QGLShaderProgram;
00036 #endif
00037 
00038 class QPainter;
00039 class QGLContext;
00040 
00041 class StelPainterLight
00042 {
00043 public:
00044     StelPainterLight(int alight=0) : light(alight), enabled(false) {}
00045 
00046     void setPosition(const Vec4f& v);
00047     Vec4f& getPosition() {return position;}
00048 
00049     void setDiffuse(const Vec4f& v);
00050     Vec4f& getDiffuse() {return diffuse;}
00051 
00052     void setSpecular(const Vec4f& v);
00053     Vec4f& getSpecular() {return specular;}
00054 
00055     void setAmbient(const Vec4f& v);
00056     Vec4f& getAmbient() {return ambient;}
00057 
00058     void setEnable(bool v);
00059     void enable();
00060     void disable();
00061     bool isEnabled() const {return enabled;}
00062 
00063 private:
00064     int light;
00065     Vec4f position;
00066     Vec4f diffuse;
00067     Vec4f specular;
00068     Vec4f ambient;
00069     bool enabled;
00070 };
00071 
00072 
00073 class StelPainterMaterial
00074 {
00075 public:
00076     StelPainterMaterial();
00077 
00078     void setSpecular(const Vec4f& v);
00079     Vec4f& getSpecular() {return specular;}
00080 
00081     void setAmbient(const Vec4f& v);
00082     Vec4f& getAmbient() {return ambient;}
00083 
00084     void setEmission(const Vec4f& v);
00085     Vec4f& getEmission() {return emission;}
00086 
00087     void setShininess(float v);
00088     float getShininess() {return shininess;}
00089 private:
00090     Vec4f specular;
00091     Vec4f ambient;
00092     Vec4f emission;
00093     float shininess;
00094 };
00095 
00102 class StelPainter
00103 {
00104 public:
00105     friend class VertexArrayProjector;
00106 
00108     enum SphericalPolygonDrawMode
00109     {
00110         SphericalPolygonDrawModeFill=0,         
00111         SphericalPolygonDrawModeBoundary=1,     
00112         SphericalPolygonDrawModeTextureFill=2   
00113     };
00114 
00116     enum ShadeModel
00117     {
00118         ShadeModelFlat=0x1D00,      
00119         ShadeModelSmooth=0x1D01 
00120     };
00121 
00123     enum DrawingMode
00124     {
00125         Points                      = 0x0000, 
00126         Lines                       = 0x0001, 
00127         LineLoop                    = 0x0002, 
00128         LineStrip                   = 0x0003, 
00129         Triangles                   = 0x0004, 
00130         TriangleStrip               = 0x0005, 
00131         TriangleFan                 = 0x0006  
00132     };
00133 
00134     explicit StelPainter(const StelProjectorP& prj);
00135     ~StelPainter();
00136 
00138     const StelProjectorP& getProjector() const {return prj;}
00139     void setProjector(const StelProjectorP& p);
00140 
00142     void drawViewportShape();
00143 
00153     void drawText(float x, float y, const QString& str, float angleDeg=0.f,
00154               float xshift=0.f, float yshift=0.f, bool noGravity=true);
00155     void drawText(const Vec3d& v, const QString& str, float angleDeg=0.f,
00156               float xshift=0.f, float yshift=0.f, bool noGravity=true);
00157 
00164     void drawSphericalRegion(const SphericalRegion* region, SphericalPolygonDrawMode drawMode=SphericalPolygonDrawModeFill, const SphericalCap* clippingCap=NULL, bool doSubDivise=true, double maxSqDistortion=5.);
00165 
00166     void drawGreatCircleArcs(const StelVertexArray& va, const SphericalCap* clippingCap=NULL);
00167 
00168     void drawSphericalTriangles(const StelVertexArray& va, bool textured, const SphericalCap* clippingCap=NULL, bool doSubDivide=true, double maxSqDistortion=5.);
00169 
00176     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);
00177 
00184     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);
00185 
00187     void drawCircle(float x, float y, float r);
00188 
00194     void drawSprite2dMode(float x, float y, float radius);
00195     void drawSprite2dMode(const Vec3d& v, float radius);
00196 
00203     void drawSprite2dMode(float x, float y, float radius, float rotation);
00204 
00208     void drawPoint2d(float x, float y);
00209 
00215     void drawLine2d(float x1, float y1, float x2, float y2);
00216 
00224     void drawRect2d(float x, float y, float width, float height, bool textured=true);
00225 
00227     void sSphere(float radius, float oneMinusOblateness, int slices, int stacks, int orientInside = 0, bool flipTexture = false);
00228 
00230     static StelVertexArray computeSphereNoLight(float radius, float oneMinusOblateness, int slices, int stacks, int orientInside = 0, bool flipTexture = false);
00231 
00233     void sCylinder(float radius, float height, int slices, int orientInside = 0);
00234 
00243     static void computeFanDisk(float radius, int innerFanSlices, int level, QVector<double>& vertexArr, QVector<float>& texCoordArr);
00244 
00246     void sRing(float rMin, float rMax, int slices, int stacks, int orientInside);
00247 
00249     void sSphereMap(float radius, int slices, int stacks, float textureFov = 2.f*M_PI, int orientInside = 0);
00250 
00252     void setFont(const QFont& font);
00253 
00255     void setColor(float r, float g, float b, float a=1.f);
00256 
00258     Vec4f getColor() const;
00259 
00261     StelPainterLight& getLight() {return light;}
00262 
00264     StelPainterMaterial& getMaterial() {return material;}
00265 
00267     QFontMetrics getFontMetrics() const;
00268 
00271     static void initSystemGLInfo(QGLContext* ctx);
00272 
00274     static void setQPainter(QPainter* qPainter);
00275 
00277     static void swapBuffer();
00278 
00280     static void makeMainGLContextCurrent();
00281 
00282     // The following methods try to reflect the API of the incoming QGLPainter class
00283 
00287     void setPointSize(qreal size);
00288 
00290     void setShadeModel(ShadeModel m);
00291 
00293     void enableTexture2d(bool b);
00294 
00295     // Thoses methods should eventually be replaced by a single setVertexArray
00297     void setVertexPointer(int size, int type, const void* pointer) {
00298         vertexArray.size = size; vertexArray.type = type; vertexArray.pointer = pointer;
00299     }
00300 
00302     void setTexCoordPointer(int size, int type, const void* pointer)
00303     {
00304         texCoordArray.size = size; texCoordArray.type = type; texCoordArray.pointer = pointer;
00305     }
00306 
00308     void setColorPointer(int size, int type, const void* pointer)
00309     {
00310         colorArray.size = size; colorArray.type = type; colorArray.pointer = pointer;
00311     }
00312 
00314     void setNormalPointer(int type, const void* pointer)
00315     {
00316         normalArray.size = 3; normalArray.type = type; normalArray.pointer = pointer;
00317     }
00318 
00320     void enableClientStates(bool vertex, bool texture=false, bool color=false, bool normal=false);
00321 
00324     void setArrays(const Vec3d* vertice, const Vec2f* texCoords=NULL, const Vec3f* colorArray=NULL, const Vec3f* normalArray=NULL);
00325 
00331     void drawFromArray(DrawingMode mode, int count, int offset=0, bool doProj=true, const unsigned int* indices=NULL);
00332 
00335     void drawStelVertexArray(const StelVertexArray& arr, bool checkDiscontinuity=true);
00336 
00337 private:
00338 
00339     friend class StelTextureMgr;
00340     friend class StelTexture;
00342     typedef struct
00343     {
00344         int size;               // The number of coordinates per vertex.
00345         int type;               // The data type of each coordinate (GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE).
00346         const void* pointer;    // Pointer to the first coordinate of the first vertex in the array.
00347         bool enabled;           // Define whether the array is enabled or not.
00348     } ArrayDesc;
00349 
00352     ArrayDesc projectArray(const ArrayDesc& array, int offset, int count, const unsigned int* indices=NULL);
00353 
00360     void projectSphericalTriangle(const SphericalCap* clippingCap, const Vec3d* vertices, QVarLengthArray<Vec3f, 4096>* outVertices,
00361             const Vec2f* texturePos=NULL, QVarLengthArray<Vec2f, 4096>* outTexturePos=NULL, double maxSqDistortion=5., int nbI=0,
00362             bool checkDisc1=true, bool checkDisc2=true, bool checkDisc3=true) const;
00363 
00364     void drawTextGravity180(float x, float y, const QString& str, float xshift = 0, float yshift = 0);
00365 
00366     // Used by the method below
00367     static QVector<Vec2f> smallCircleVertexArray;
00368     void drawSmallCircleVertexArray();
00369 
00371     StelProjectorP prj;
00372 
00373 #ifndef NDEBUG
00374 
00375     static class QMutex* globalMutex;
00376 #endif
00377 
00379     static QPainter* qPainter;
00380 
00382     static QGLContext* glContext;
00383 
00385     static bool isNoPowerOfTwoAllowed;
00386 
00387 #ifdef STELPAINTER_GL2
00388     Vec4f currentColor;
00389     bool texture2dEnabled;
00390     static QGLShaderProgram* basicShaderProgram;
00391     struct BasicShaderVars {
00392         int projectionMatrix;
00393         int color;
00394         int vertex;
00395     };
00396     static BasicShaderVars basicShaderVars;
00397     static QGLShaderProgram* colorShaderProgram;
00398     static QGLShaderProgram* texturesShaderProgram;
00399     struct TexturesShaderVars {
00400         int projectionMatrix;
00401         int texCoord;
00402         int vertex;
00403         int texColor;
00404         int texture;
00405     };
00406     static TexturesShaderVars texturesShaderVars;
00407     static QGLShaderProgram* texturesColorShaderProgram;
00408     struct TexturesColorShaderVars {
00409         int projectionMatrix;
00410         int texCoord;
00411         int vertex;
00412         int color;
00413         int texture;
00414     };
00415     static TexturesColorShaderVars texturesColorShaderVars;
00416 #endif
00417 
00419     ArrayDesc vertexArray;
00421     ArrayDesc texCoordArray;
00423     ArrayDesc normalArray;
00425     ArrayDesc colorArray;
00426 
00428     StelPainterLight light;
00429 
00431     StelPainterMaterial material;
00432 };
00433 
00434 #endif // _STELPAINTER_HPP_
00435 
Generated on Sat Aug 25 22:13:30 2012 for Stellarium by  doxygen 1.6.3