00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
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(void);
00144
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, bool doSubDivide=true);
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
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 void sCylinder(float radius, float height, int slices, int orientInside = 0);
00232
00239 void sFanDisk(float radius, int innerFanSlices, int level);
00240
00242 void sRing(float rMin, float rMax, int slices, int stacks, int orientInside);
00243
00245 void sSphereMap(float radius, int slices, int stacks, float textureFov = 2.f*M_PI, int orientInside = 0);
00246
00248 void setFont(const QFont& font);
00249
00251 void setColor(float r, float g, float b, float a=1.f);
00252
00254 Vec4f getColor() const;
00255
00257 StelPainterLight& getLight() {return light;}
00258
00260 StelPainterMaterial& getMaterial() {return material;}
00261
00263 QFontMetrics getFontMetrics() const;
00264
00267 static void initSystemGLInfo();
00268
00270 static void setQPainter(QPainter* qPainter);
00271
00272
00273
00277 void setPointSize(qreal size);
00278
00280 void setShadeModel(ShadeModel m);
00281
00283 void enableTexture2d(bool b);
00284
00285
00287 void setVertexPointer(int size, int type, const void* pointer) {
00288 vertexArray.size = size; vertexArray.type = type; vertexArray.pointer = pointer;
00289 }
00290
00292 void setTexCoordPointer(int size, int type, const void* pointer)
00293 {
00294 texCoordArray.size = size; texCoordArray.type = type; texCoordArray.pointer = pointer;
00295 }
00296
00298 void setColorPointer(int size, int type, const void* pointer)
00299 {
00300 colorArray.size = size; colorArray.type = type; colorArray.pointer = pointer;
00301 }
00302
00304 void setNormalPointer(int type, const void* pointer)
00305 {
00306 normalArray.size = 3; normalArray.type = type; normalArray.pointer = pointer;
00307 }
00308
00310 void enableClientStates(bool vertex, bool texture=false, bool color=false, bool normal=false);
00311
00314 void setArrays(const Vec3d* vertice, const Vec2f* texCoords=NULL, const Vec3f* colorArray=NULL, const Vec3f* normalArray=NULL);
00315
00321 void drawFromArray(DrawingMode mode, int count, int offset=0, bool doProj=true, const unsigned int* indices=NULL);
00322
00323 private:
00325 typedef struct
00326 {
00327 int size;
00328 int type;
00329 const void* pointer;
00330 bool enabled;
00331 } ArrayDesc;
00332
00335 ArrayDesc projectArray(const ArrayDesc& array, int offset, int count, const unsigned int* indices=NULL);
00336
00343 void projectSphericalTriangle(const SphericalCap* clippingCap, const Vec3d* vertices, QVarLengthArray<Vec3f, 4096>* outVertices,
00344 const Vec2f* texturePos=NULL, QVarLengthArray<Vec2f, 4096>* outTexturePos=NULL,int nbI=0,
00345 bool checkDisc1=true, bool checkDisc2=true, bool checkDisc3=true) const;
00346
00347 void drawTextGravity180(float x, float y, const QString& str, float xshift = 0, float yshift = 0) const;
00348
00349
00350 static QVector<Vec2f> smallCircleVertexArray;
00351 void drawSmallCircleVertexArray();
00352
00354 StelProjectorP prj;
00355
00356 #ifndef NDEBUG
00358 static class QMutex* globalMutex;
00359 #endif
00360
00362 static QPainter* qPainter;
00363
00364 #ifdef STELPAINTER_GL2
00365 Vec4f currentColor;
00366 bool texture2dEnabled;
00367 static QGLShaderProgram* basicShaderProgram;
00368 struct BasicShaderVars {
00369 int projectionMatrix;
00370 int color;
00371 int vertex;
00372 };
00373 static BasicShaderVars basicShaderVars;
00374 static QGLShaderProgram* colorShaderProgram;
00375 static QGLShaderProgram* texturesShaderProgram;
00376 struct TexturesShaderVars {
00377 int projectionMatrix;
00378 int texCoord;
00379 int vertex;
00380 int texColor;
00381 int texture;
00382 };
00383 static TexturesShaderVars texturesShaderVars;
00384 static QGLShaderProgram* texturesColorShaderProgram;
00385 struct TexturesColorShaderVars {
00386 int projectionMatrix;
00387 int texCoord;
00388 int vertex;
00389 int color;
00390 int texture;
00391 };
00392 static TexturesColorShaderVars texturesColorShaderVars;
00393 #endif
00394
00396 ArrayDesc vertexArray;
00398 ArrayDesc texCoordArray;
00400 ArrayDesc normalArray;
00402 ArrayDesc colorArray;
00403
00405 StelPainterLight light;
00406
00408 StelPainterMaterial material;
00409 };
00410
00411 #endif // _STELPAINTER_HPP_
00412