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 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
00284
00288 void setPointSize(qreal size);
00289
00291 void setShadeModel(ShadeModel m);
00292
00294 void enableTexture2d(bool b);
00295
00296
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;
00346 int type;
00347 const void* pointer;
00348 bool enabled;
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
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
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