Stellarium 0.13.0
StelPainter.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2008 Fabien Chereau
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _STELPAINTER_HPP_
21 #define _STELPAINTER_HPP_
22 #include "VecMath.hpp"
23 #include "StelSphereGeometry.hpp"
24 #include "StelProjectorType.hpp"
25 #include "StelProjector.hpp"
26 #include <QString>
27 #include <QVarLengthArray>
28 #include <QFontMetrics>
29 #include <QOpenGLFunctions>
30 
31 class QOpenGLShaderProgram;
32 
34 {
35 public:
36  StelPainterLight(int alight=0) : light(alight), enabled(false) {}
37 
38  void setPosition(const Vec4f& v);
39  Vec4f& getPosition() {return position;}
40 
41  void setDiffuse(const Vec4f& v);
42  Vec4f& getDiffuse() {return diffuse;}
43 
44  void setSpecular(const Vec4f& v);
45  Vec4f& getSpecular() {return specular;}
46 
47  void setAmbient(const Vec4f& v);
48  Vec4f& getAmbient() {return ambient;}
49 
50  void setEnable(bool v);
51  void enable();
52  void disable();
53  bool isEnabled() const {return enabled;}
54 
55 private:
56  int light;
57  Vec4f position;
58  Vec4f diffuse;
59  Vec4f specular;
60  Vec4f ambient;
61  bool enabled;
62 };
63 
64 
71 class StelPainter: protected QOpenGLFunctions
72 {
73 public:
74  friend class VertexArrayProjector;
75 
78  {
82  };
83 
86  {
87  Points = 0x0000,
88  Lines = 0x0001,
89  LineLoop = 0x0002,
90  LineStrip = 0x0003,
91  Triangles = 0x0004,
92  TriangleStrip = 0x0005,
93  TriangleFan = 0x0006
94  };
95 
96  explicit StelPainter(const StelProjectorP& prj);
97  ~StelPainter();
98 
100  const StelProjectorP& getProjector() const {return prj;}
101  void setProjector(const StelProjectorP& p);
102 
104  void drawViewportShape();
105 
116  void drawText(float x, float y, const QString& str, float angleDeg=0.f,
117  float xshift=0.f, float yshift=0.f, const bool noGravity=true);
118  void drawText(const Vec3d& v, const QString& str, const float angleDeg=0.f,
119  const float xshift=0.f, const float yshift=0.f, const bool noGravity=true);
120 
127  void drawSphericalRegion(const SphericalRegion* region, SphericalPolygonDrawMode drawMode=SphericalPolygonDrawModeFill, const SphericalCap* clippingCap=NULL, bool doSubDivise=true, double maxSqDistortion=5.);
128 
129  void drawGreatCircleArcs(const StelVertexArray& va, const SphericalCap* clippingCap=NULL);
130 
131  void drawSphericalTriangles(const StelVertexArray& va, const bool textured, const SphericalCap* clippingCap=NULL, const bool doSubDivide=true, const double maxSqDistortion=5.);
132 
139  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);
140 
147  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);
148 
150  void drawCircle(const float x, const float y, float r);
151 
158  void drawSprite2dMode(const float x, const float y, float radius);
159  void drawSprite2dMode(const Vec3d& v, const float radius);
160 
162  void drawSprite2dModeNoDeviceScale(const float x, const float y, const float radius);
163 
170  void drawSprite2dMode(float x, float y, float radius, float rotation);
171 
175  void drawPoint2d(const float x, const float y);
176 
182  void drawLine2d(const float x1, const float y1, const float x2, const float y2);
183 
191  void drawRect2d(const float x, const float y, const float width, const float height, const bool textured=true);
192 
208  void sSphere(const float radius, const float oneMinusOblateness, const int slices, const int stacks, const int orientInside = 0, const bool flipTexture = false,
209  const float topAngle=0.0f, const float bottomAngle=M_PI);
210 
212  static StelVertexArray computeSphereNoLight(const float radius, const float oneMinusOblateness, const int slices, const int stacks,
213  const int orientInside = 0, const bool flipTexture = false);
214 
216  void sCylinder(const float radius, const float height, const int slices, const int orientInside = 0);
217 
226  static void computeFanDisk(float radius, int innerFanSlices, int level, QVector<double>& vertexArr, QVector<float>& texCoordArr);
227 
229  void sRing(const float rMin, const float rMax, int slices, const int stacks, const int orientInside);
230 
232  void sSphereMap(const float radius, const int slices, const int stacks, const float textureFov = 2.f*M_PI, const int orientInside = 0);
233 
235  void setFont(const QFont& font);
236 
238  void setColor(float r, float g, float b, float a=1.f);
239 
241  Vec4f getColor() const;
242 
244  StelPainterLight& getLight() {return light;}
245 
247  QFontMetrics getFontMetrics() const;
248 
251  static void initGLShaders();
252 
255  static void deinitGLShaders();
256 
258  void enableTexture2d(const bool b);
259 
260  // Thoses methods should eventually be replaced by a single setVertexArray
262  void setVertexPointer(const int size, const int type, const void* pointer) {
263  vertexArray.size = size; vertexArray.type = type; vertexArray.pointer = pointer;
264  }
265 
267  void setTexCoordPointer(const int size, const int type, const void* pointer)
268  {
269  texCoordArray.size = size; texCoordArray.type = type; texCoordArray.pointer = pointer;
270  }
271 
273  void setColorPointer(const int size, const int type, const void* pointer)
274  {
275  colorArray.size = size; colorArray.type = type; colorArray.pointer = pointer;
276  }
277 
279  void setNormalPointer(const int type, const void* pointer)
280  {
281  normalArray.size = 3; normalArray.type = type; normalArray.pointer = pointer;
282  }
283 
285  void enableClientStates(const bool vertex, const bool texture=false, const bool color=false, const bool normal=false);
286 
289  void setArrays(const Vec3d* vertices, const Vec2f* texCoords=NULL, const Vec3f* colorArray=NULL, const Vec3f* normalArray=NULL);
290  void setArrays(const Vec3f* vertices, const Vec2f* texCoords=NULL, const Vec3f* colorArray=NULL, const Vec3f* normalArray=NULL);
291 
297  void drawFromArray(const DrawingMode mode, const int count, const int offset=0, const bool doProj=true, const unsigned short *indices=NULL);
298 
301  void drawStelVertexArray(const StelVertexArray& arr, const bool checkDiscontinuity=true);
302 
305  static bool linkProg(class QOpenGLShaderProgram* prog, const QString& name);
306 
308  void usePlanetShader(bool use);
309 
310 private:
311 
312  friend class StelTextureMgr;
313  friend class StelTexture;
314 
317  class GLState : protected QOpenGLFunctions
318  {
319  public:
320  GLState();
321  ~GLState();
322  private:
323  bool blend;
324  int blendSrcRGB, blendDstRGB, blendSrcAlpha, blendDstAlpha;
325  };
326 
328  typedef struct
329  {
330  int size; // The number of coordinates per vertex.
331  int type; // The data type of each coordinate (GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE).
332  const void* pointer; // Pointer to the first coordinate of the first vertex in the array.
333  bool enabled; // Define whether the array is enabled or not.
334  } ArrayDesc;
335 
338  ArrayDesc projectArray(const ArrayDesc& array, int offset, int count, const unsigned short *indices=NULL);
339 
341  void convertArrayToFloat(StelPainter::ArrayDesc& array, int offset, int count, const unsigned short *indices=NULL);
342 
349  void projectSphericalTriangle(const SphericalCap* clippingCap, const Vec3d* vertices, QVarLengthArray<Vec3f, 4096>* outVertices,
350  const Vec2f* texturePos=NULL, QVarLengthArray<Vec2f, 4096>* outTexturePos=NULL, const double maxSqDistortion=5., const int nbI=0,
351  const bool checkDisc1=true, const bool checkDisc2=true, const bool checkDisc3=true) const;
352 
353  void drawTextGravity180(float x, float y, const QString& str, const float xshift = 0, const float yshift = 0);
354 
355  // Used by the method below
356  static QVector<Vec2f> smallCircleVertexArray;
357  void drawSmallCircleVertexArray();
358 
360  StelProjectorP prj;
361 
362 #ifndef NDEBUG
363  static class QMutex* globalMutex;
365 #endif
366 
368  QFont currentFont;
369 
371  bool planetShader;
372 
373  Vec4f currentColor;
374  bool texture2dEnabled;
375 
376  static QOpenGLShaderProgram* basicShaderProgram;
377  struct BasicShaderVars {
378  int projectionMatrix;
379  int color;
380  int vertex;
381  };
382  static BasicShaderVars basicShaderVars;
383 
384  static QOpenGLShaderProgram* colorShaderProgram;
385  static BasicShaderVars colorShaderVars;
386 
387  static QOpenGLShaderProgram* texturesShaderProgram;
388  struct TexturesShaderVars {
389  int projectionMatrix;
390  int texCoord;
391  int vertex;
392  int texColor;
393  int texture;
394  };
395  static TexturesShaderVars texturesShaderVars;
396  static QOpenGLShaderProgram* texturesColorShaderProgram;
397  struct TexturesColorShaderVars {
398  int projectionMatrix;
399  int texCoord;
400  int vertex;
401  int color;
402  int texture;
403  };
404  static TexturesColorShaderVars texturesColorShaderVars;
405 
406 
408  ArrayDesc vertexArray;
410  ArrayDesc texCoordArray;
412  ArrayDesc normalArray;
414  ArrayDesc colorArray;
415 
417  StelPainterLight light;
418 };
419 
420 #endif // _STELPAINTER_HPP_
421 
void setArrays(const Vec3d *vertices, const Vec2f *texCoords=NULL, const Vec3f *colorArray=NULL, const Vec3f *normalArray=NULL)
convenience method that enable and set all the given arrays.
void drawText(float x, float y, const QString &str, float angleDeg=0.f, float xshift=0.f, float yshift=0.f, const bool noGravity=true)
Draw the string at the given position and angle with the given font.
void drawLine2d(const float x1, const float y1, const float x2, const float y2)
Draw a line between the 2 points.
void setTexCoordPointer(const int size, const int type, const void *pointer)
use instead of glTexCoordPointer
Definition: StelPainter.hpp:267
void drawRect2d(const float x, const float y, const float width, const float height, const bool textured=true)
Draw a rectangle using the current texture at the given projected 2d position.
void drawSprite2dMode(const float x, const float y, float radius)
Draw a square using the current texture at the given projected 2d position.
static void deinitGLShaders()
Delete the OpenGL shaders objects.
void setVertexPointer(const int size, const int type, const void *pointer)
use instead of glVertexPointer
Definition: StelPainter.hpp:262
void usePlanetShader(bool use)
Sets whether the special planet shader should be used.
Vec4f getColor() const
Get the color currently used for drawing.
static StelVertexArray computeSphereNoLight(const float radius, const float oneMinusOblateness, const int slices, const int stacks, const int orientInside=0, const bool flipTexture=false)
Generate a StelVertexArray for a sphere.
void sCylinder(const float radius, const float height, const int slices, const int orientInside=0)
Re-implementation of gluCylinder : glu is overridden for non-standard projection. ...
A SphericalCap is defined by a direction and an aperture.
Definition: StelSphereGeometry.hpp:273
StelPainterLight & getLight()
Get the light.
Definition: StelPainter.hpp:244
void drawFromArray(const DrawingMode mode, const int count, const int offset=0, const bool doProj=true, const unsigned short *indices=NULL)
Draws primitives using vertices from the arrays specified by setVertexArray().
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)
Draw a small circle arc between points start and stop with rotation point in rotCenter.
void drawCircle(const float x, const float y, float r)
Draw a simple circle, 2d viewport coordinates in pixel.
void setColor(float r, float g, float b, float a=1.f)
Set the color to use for subsequent drawing.
Definition: StelVertexArray.hpp:28
Draw the interior of the polygon filled with the current texture.
Definition: StelPainter.hpp:81
GL_TRIANGLE_FAN.
Definition: StelPainter.hpp:93
static void initGLShaders()
Create the OpenGL shaders programs used by the StelPainter.
void drawSphericalRegion(const SphericalRegion *region, SphericalPolygonDrawMode drawMode=SphericalPolygonDrawModeFill, const SphericalCap *clippingCap=NULL, bool doSubDivise=true, double maxSqDistortion=5.)
Draw the given SphericalRegion.
const StelProjectorP & getProjector() const
Return the instance of projector associated to this painter.
Definition: StelPainter.hpp:100
void setColorPointer(const int size, const int type, const void *pointer)
use instead of glColorPointer
Definition: StelPainter.hpp:273
void sRing(const float rMin, const float rMax, int slices, const int stacks, const int orientInside)
Draw a ring with a radial texturing.
QSharedPointer< StelProjector > StelProjectorP
Shared pointer on a StelProjector instance (implement reference counting)
Definition: StelProjectorType.hpp:34
void setNormalPointer(const int type, const void *pointer)
use instead of glNormalPointer
Definition: StelPainter.hpp:279
GL_TRIANGLES.
Definition: StelPainter.hpp:91
void sSphereMap(const float radius, const int slices, const int stacks, const float textureFov=2.f *M_PI, const int orientInside=0)
Draw a fisheye texture in a sphere.
static void computeFanDisk(float radius, int innerFanSlices, int level, QVector< double > &vertexArr, QVector< float > &texCoordArr)
Draw a disk with a special texturing mode having texture center at center of disk.
Draw the interior of the polygon.
Definition: StelPainter.hpp:79
void drawPoint2d(const float x, const float y)
Draw a GL_POINT at the given position.
Provides functions for performing openGL drawing operations.
Definition: StelPainter.hpp:71
void enableTexture2d(const bool b)
Set whether texturing is enabled.
SphericalPolygonDrawMode
Define the drawing mode when drawing polygons.
Definition: StelPainter.hpp:77
GL_LINES.
Definition: StelPainter.hpp:88
GL_LINE_LOOP.
Definition: StelPainter.hpp:89
GL_TRIANGLE_STRIP.
Definition: StelPainter.hpp:92
Abstract class defining a region of the sphere.
Definition: StelSphereGeometry.hpp:135
Base texture class.
Definition: StelTexture.hpp:41
Manage textures loading.
Definition: StelTextureMgr.hpp:33
void setFont(const QFont &font)
Set the font to use for subsequent text drawing.
GL_POINTS.
Definition: StelPainter.hpp:87
void enableClientStates(const bool vertex, const bool texture=false, const bool color=false, const bool normal=false)
use instead of glEnableClient
Define the StelProjectorP type.
Draw the boundary of the polygon.
Definition: StelPainter.hpp:80
void drawSprite2dModeNoDeviceScale(const float x, const float y, const float radius)
Same as drawSprite2dMode but don't scale according to display device scaling.
QFontMetrics getFontMetrics() const
Get the font metrics for the current font.
static bool linkProg(class QOpenGLShaderProgram *prog, const QString &name)
Link an opengl program and show a message in case of error or warnings.
Define all SphericalGeometry primitives as well as the SphericalRegionP type.
void drawStelVertexArray(const StelVertexArray &arr, const bool checkDiscontinuity=true)
Draws the primitives defined in the StelVertexArray.
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)
Draw a great circle arc between points start and stop.
DrawingMode
Define the drawing mode when drawing vertex.
Definition: StelPainter.hpp:85
Definition: StelPainter.hpp:33
GL_LINE_STRIP.
Definition: StelPainter.hpp:90
void drawViewportShape()
Fill with black around the viewport.