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 
00104 class StelPainter
00105 {
00106 public:
00107     friend class VertexArrayProjector;
00108 
00110     enum SphericalPolygonDrawMode
00111     {
00112         SphericalPolygonDrawModeFill=0,         
00113         SphericalPolygonDrawModeBoundary=1,     
00114         SphericalPolygonDrawModeTextureFill=2   
00115     };
00116 
00118     enum ShadeModel
00119     {
00120         ShadeModelFlat=0x1D00,      
00121         ShadeModelSmooth=0x1D01 
00122     };
00123 
00125     enum DrawingMode
00126     {
00127         Points                      = 0x0000, 
00128         Lines                       = 0x0001, 
00129         LineLoop                    = 0x0002, 
00130         LineStrip                   = 0x0003, 
00131         Triangles                   = 0x0004, 
00132         TriangleStrip               = 0x0005, 
00133         TriangleFan                 = 0x0006  
00134     };
00135 
00136     explicit StelPainter(const StelProjectorP& prj);
00137     ~StelPainter();
00138 
00140     const StelProjectorP& getProjector() const {return prj;}
00141     void setProjector(const StelProjectorP& p);
00142 
00144     void drawViewportShape(void);
00145 
00155     void drawText(float x, float y, const QString& str, float angleDeg=0.f,
00156               float xshift=0.f, float yshift=0.f, bool noGravity=true) const;
00157     void drawText(const Vec3d& v, const QString& str, float angleDeg=0.f,
00158               float xshift=0.f, float yshift=0.f, bool noGravity=true) const;
00159 
00166     void drawSphericalRegion(const SphericalRegion* region, SphericalPolygonDrawMode drawMode=SphericalPolygonDrawModeFill, const SphericalCap* clippingCap=NULL, bool doSubDivise=true);
00167 
00168     void drawGreatCircleArcs(const StelVertexArray& va, const SphericalCap* clippingCap=NULL);
00169 
00170     void drawSphericalTriangles(const StelVertexArray& va, bool textured, const SphericalCap* clippingCap=NULL, bool doSubDivide=true);
00171 
00178     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);
00179 
00186     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);
00187 
00189     void drawCircle(float x, float y, float r);
00190 
00196     void drawSprite2dMode(float x, float y, float radius);
00197     void drawSprite2dMode(const Vec3d& v, float radius);
00198 
00205     void drawSprite2dMode(float x, float y, float radius, float rotation);
00206 
00210     void drawPoint2d(float x, float y);
00211 
00217     void drawLine2d(float x1, float y1, float x2, float y2);
00218 
00226     void drawRect2d(float x, float y, float width, float height, bool textured=true);
00227 
00229     void sSphere(float radius, float oneMinusOblateness, int slices, int stacks, int orientInside = 0, bool flipTexture = false);
00230 
00232     static StelVertexArray computeSphereNoLight(float radius, float oneMinusOblateness, int slices, int stacks, int orientInside = 0, bool flipTexture = false);
00233 
00235     void sCylinder(float radius, float height, int slices, int orientInside = 0);
00236 
00245     static void computeFanDisk(float radius, int innerFanSlices, int level, QVector<double>& vertexArr, QVector<float>& texCoordArr);
00246 
00248     void sRing(float rMin, float rMax, int slices, int stacks, int orientInside);
00249 
00251     void sSphereMap(float radius, int slices, int stacks, float textureFov = 2.f*M_PI, int orientInside = 0);
00252 
00254     void setFont(const QFont& font);
00255 
00257     void setColor(float r, float g, float b, float a=1.f);
00258 
00260     Vec4f getColor() const;
00261 
00263     StelPainterLight& getLight() {return light;}
00264 
00266     StelPainterMaterial& getMaterial() {return material;}
00267 
00269     QFontMetrics getFontMetrics() const;
00270 
00273     static void initSystemGLInfo(QGLContext* ctx);
00274 
00276     static void setQPainter(QPainter* qPainter);
00277 
00279     static void swapBuffer();
00280 
00282     static void makeMainGLContextCurrent();
00283 
00284     // The following methods try to reflect the API of the incoming QGLPainter class
00285 
00289     void setPointSize(qreal size);
00290 
00292     void setShadeModel(ShadeModel m);
00293 
00295     void enableTexture2d(bool b);
00296 
00297     // Thoses methods should eventually be replaced by a single setVertexArray
00299     void setVertexPointer(int size, int type, const void* pointer) {
00300         vertexArray.size = size; vertexArray.type = type; vertexArray.pointer = pointer;
00301     }
00302 
00304     void setTexCoordPointer(int size, int type, const void* pointer)
00305     {
00306         texCoordArray.size = size; texCoordArray.type = type; texCoordArray.pointer = pointer;
00307     }
00308 
00310     void setColorPointer(int size, int type, const void* pointer)
00311     {
00312         colorArray.size = size; colorArray.type = type; colorArray.pointer = pointer;
00313     }
00314 
00316     void setNormalPointer(int type, const void* pointer)
00317     {
00318         normalArray.size = 3; normalArray.type = type; normalArray.pointer = pointer;
00319     }
00320 
00322     void enableClientStates(bool vertex, bool texture=false, bool color=false, bool normal=false);
00323 
00326     void setArrays(const Vec3d* vertice, const Vec2f* texCoords=NULL, const Vec3f* colorArray=NULL, const Vec3f* normalArray=NULL);
00327 
00333     void drawFromArray(DrawingMode mode, int count, int offset=0, bool doProj=true, const unsigned int* indices=NULL);
00334 
00336     void drawStelVertexArray(const StelVertexArray& arr);
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,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) const;
00366 
00367     // Used by the method below
00368     static QVector<Vec2f> smallCircleVertexArray;
00369     void drawSmallCircleVertexArray();
00370 
00372     StelProjectorP prj;
00373 
00374 #ifndef NDEBUG
00376     static class QMutex* globalMutex;
00377 #endif
00378 
00380     static QPainter* qPainter;
00381 
00383     static QGLContext* glContext;
00384 
00385 #ifdef STELPAINTER_GL2
00386     Vec4f currentColor;
00387     bool texture2dEnabled;
00388     static QGLShaderProgram* basicShaderProgram;
00389     struct BasicShaderVars {
00390         int projectionMatrix;
00391         int color;
00392         int vertex;
00393     };
00394     static BasicShaderVars basicShaderVars;
00395     static QGLShaderProgram* colorShaderProgram;
00396     static QGLShaderProgram* texturesShaderProgram;
00397     struct TexturesShaderVars {
00398         int projectionMatrix;
00399         int texCoord;
00400         int vertex;
00401         int texColor;
00402         int texture;
00403     };
00404     static TexturesShaderVars texturesShaderVars;
00405     static QGLShaderProgram* texturesColorShaderProgram;
00406     struct TexturesColorShaderVars {
00407         int projectionMatrix;
00408         int texCoord;
00409         int vertex;
00410         int color;
00411         int texture;
00412     };
00413     static TexturesColorShaderVars texturesColorShaderVars;
00414 #endif
00415 
00417     ArrayDesc vertexArray;
00419     ArrayDesc texCoordArray;
00421     ArrayDesc normalArray;
00423     ArrayDesc colorArray;
00424 
00426     StelPainterLight light;
00427 
00429     StelPainterMaterial material;
00430 };
00431 
00432 #endif // _STELPAINTER_HPP_
00433 

Generated on Wed Jun 2 13:11:13 2010 for Stellarium by  doxygen 1.5.5