Stellarium 0.14.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 
23 #include "StelOpenGL.hpp"
24 #include "VecMath.hpp"
25 #include "StelSphereGeometry.hpp"
26 #include "StelProjectorType.hpp"
27 #include "StelProjector.hpp"
28 #include <QString>
29 #include <QVarLengthArray>
30 #include <QFontMetrics>
31 
32 class QOpenGLShaderProgram;
33 
41 {
42 public:
43  friend class VertexArrayProjector;
44 
47  {
52  };
53 
56  {
57  Points = 0x0000,
58  Lines = 0x0001,
59  LineLoop = 0x0002,
60  LineStrip = 0x0003,
61  Triangles = 0x0004,
62  TriangleStrip = 0x0005,
63  TriangleFan = 0x0006
64  };
65 
66  explicit StelPainter(const StelProjectorP& prj);
67  ~StelPainter();
68 
70  const StelProjectorP& getProjector() const {return prj;}
71  void setProjector(const StelProjectorP& p);
72 
74  void drawViewportShape(void);
75  void drawViewportShape(const GLfloat innerRadius);
76 
87  void drawText(float x, float y, const QString& str, float angleDeg=0.f,
88  float xshift=0.f, float yshift=0.f, bool noGravity=true);
89  void drawText(const Vec3d& v, const QString& str, float angleDeg=0.f,
90  float xshift=0.f, float yshift=0.f, bool noGravity=true);
91 
98  void drawSphericalRegion(const SphericalRegion* region, SphericalPolygonDrawMode drawMode=SphericalPolygonDrawModeFill, const SphericalCap* clippingCap=NULL, bool doSubDivise=true, double maxSqDistortion=5.);
99 
100  void drawGreatCircleArcs(const StelVertexArray& va, const SphericalCap* clippingCap=NULL);
101 
102  void drawSphericalTriangles(const StelVertexArray& va, bool textured, bool colored, const SphericalCap* clippingCap=NULL, bool doSubDivide=true, double maxSqDistortion=5.);
103 
111  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);
112 
119  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);
120 
124  void drawPath(const QVector<Vec3d> &points, const QVector<Vec4f> &colors);
125 
127  void drawCircle(float x, float y, float r);
128 
135  void drawSprite2dMode(float x, float y, float radius);
136  void drawSprite2dMode(const Vec3d& v, float radius);
137 
139  void drawSprite2dModeNoDeviceScale(float x, float y, float radius);
140 
147  void drawSprite2dMode(float x, float y, float radius, float rotation);
148 
152  void drawPoint2d(float x, float y);
153 
159  void drawLine2d(float x1, float y1, float x2, float y2);
160 
168  void drawRect2d(float x, float y, float width, float height, bool textured=true);
169 
185  void sSphere(float radius, float oneMinusOblateness, int slices, int stacks, int orientInside = 0, bool flipTexture = false,
186  float topAngle=0.0f, float bottomAngle=M_PI);
187 
199  static StelVertexArray computeSphereNoLight(float radius, float oneMinusOblateness, int slices, int stacks,
200  int orientInside = 0, bool flipTexture = false,
201  float topAngle=0.0f, float bottomAngle=M_PI);
202 
204  void sCylinder(float radius, float height, int slices, int orientInside = 0);
205 
214  static void computeFanDisk(float radius, int innerFanSlices, int level, QVector<double>& vertexArr, QVector<float>& texCoordArr);
215 
217  void sSphereMap(float radius, int slices, int stacks, float textureFov = 2.f*M_PI, int orientInside = 0);
218 
220  void setFont(const QFont& font);
221 
223  void setColor(float r, float g, float b, float a=1.f);
224 
226  Vec4f getColor() const;
227 
229  QFontMetrics getFontMetrics() const;
230 
233  static void initGLShaders();
234 
237  static void deinitGLShaders();
238 
240  void enableTexture2d(bool b);
241 
242  // Thoses methods should eventually be replaced by a single setVertexArray
244  void setVertexPointer(int size, int type, const void* pointer) {
245  vertexArray.size = size; vertexArray.type = type; vertexArray.pointer = pointer;
246  }
247 
249  void setTexCoordPointer(int size, int type, const void* pointer)
250  {
251  texCoordArray.size = size; texCoordArray.type = type; texCoordArray.pointer = pointer;
252  }
253 
255  void setColorPointer(int size, int type, const void* pointer)
256  {
257  colorArray.size = size; colorArray.type = type; colorArray.pointer = pointer;
258  }
259 
261  void setNormalPointer(int type, const void* pointer)
262  {
263  normalArray.size = 3; normalArray.type = type; normalArray.pointer = pointer;
264  }
265 
267  void enableClientStates(bool vertex, bool texture=false, bool color=false, bool normal=false);
268 
271  void setArrays(const Vec3d* vertices, const Vec2f* texCoords=NULL, const Vec3f* colorArray=NULL, const Vec3f* normalArray=NULL);
272  void setArrays(const Vec3f* vertices, const Vec2f* texCoords=NULL, const Vec3f* colorArray=NULL, const Vec3f* normalArray=NULL);
273 
279  void drawFromArray(DrawingMode mode, int count, int offset=0, bool doProj=true, const unsigned short *indices=NULL);
280 
283  void drawStelVertexArray(const StelVertexArray& arr, bool checkDiscontinuity=true);
284 
287  static bool linkProg(class QOpenGLShaderProgram* prog, const QString& name);
288 
289 private:
290 
291  friend class StelTextureMgr;
292  friend class StelTexture;
293 
296  class GLState
297  {
298  public:
299  GLState();
300  ~GLState();
301  private:
302  bool blend;
303  int blendSrcRGB, blendDstRGB, blendSrcAlpha, blendDstAlpha;
304  };
305 
307  typedef struct ArrayDesc
308  {
309  ArrayDesc() : size(0), type(0), pointer(NULL), enabled(false) {}
310  int size; // The number of coordinates per vertex.
311  int type; // The data type of each coordinate (GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE).
312  const void* pointer; // Pointer to the first coordinate of the first vertex in the array.
313  bool enabled; // Define whether the array is enabled or not.
314  } ArrayDesc;
315 
318  ArrayDesc projectArray(const ArrayDesc& array, int offset, int count, const unsigned short *indices=NULL);
319 
327  void projectSphericalTriangle(const SphericalCap* clippingCap, const Vec3d* vertices, QVarLengthArray<Vec3f, 4096>* outVertices,
328  const Vec2f* texturePos=NULL, QVarLengthArray<Vec2f, 4096>* outTexturePos=NULL,
329  const Vec3f* colors=NULL, QVarLengthArray<Vec3f, 4096>* outColors=NULL,
330  double maxSqDistortion=5., int nbI=0,
331  bool checkDisc1=true, bool checkDisc2=true, bool checkDisc3=true) const;
332 
333  void drawTextGravity180(float x, float y, const QString& str, float xshift = 0, float yshift = 0);
334 
335  // Used by the method below
336  static QVector<Vec2f> smallCircleVertexArray;
337  static QVector<Vec4f> smallCircleColorArray;
338  void drawSmallCircleVertexArray();
339 
341  StelProjectorP prj;
342 
343 #ifndef NDEBUG
344  static class QMutex* globalMutex;
346 #endif
347 
349  QFont currentFont;
350 
351  Vec4f currentColor;
352  bool texture2dEnabled;
353 
354  static QOpenGLShaderProgram* basicShaderProgram;
355  struct BasicShaderVars {
356  int projectionMatrix;
357  int color;
358  int vertex;
359  };
360  static BasicShaderVars basicShaderVars;
361 
362  static QOpenGLShaderProgram* colorShaderProgram;
363  static BasicShaderVars colorShaderVars;
364 
365  static QOpenGLShaderProgram* texturesShaderProgram;
366  struct TexturesShaderVars {
367  int projectionMatrix;
368  int texCoord;
369  int vertex;
370  int texColor;
371  int texture;
372  };
373  static TexturesShaderVars texturesShaderVars;
374  static QOpenGLShaderProgram* texturesColorShaderProgram;
375  struct TexturesColorShaderVars {
376  int projectionMatrix;
377  int texCoord;
378  int vertex;
379  int color;
380  int texture;
381  };
382  static TexturesColorShaderVars texturesColorShaderVars;
383 
384 
386  ArrayDesc vertexArray;
388  ArrayDesc texCoordArray;
390  ArrayDesc normalArray;
392  ArrayDesc colorArray;
393 };
394 
395 #endif // _STELPAINTER_HPP_
396 
A templatized 4d vector compatible with openGL.
Definition: VecMath.hpp:34
void drawCircle(float x, float y, float r)
Draw a simple circle, 2d viewport coordinates in pixel.
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 setTexCoordPointer(int size, int type, const void *pointer)
use instead of glTexCoordPointer
void sCylinder(float radius, float height, int slices, int orientInside=0)
Re-implementation of gluCylinder : glu is overridden for non-standard projection. ...
static void deinitGLShaders()
Delete the OpenGL shaders objects.
Vec4f getColor() const
Get the color currently used for drawing.
Draw the interior of the polygon filled with the current texture multiplied by vertex colors...
Definition: StelPainter.hpp:51
A templatized 3d vector compatible with openGL.
Definition: VecMath.hpp:33
void drawSprite2dMode(float x, float y, float radius)
Draw a square using the current texture at the given projected 2d position.
A SphericalCap is defined by a direction and an aperture.
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.
A templatized 2d vector compatible with openGL.
Definition: VecMath.hpp:32
void enableClientStates(bool vertex, bool texture=false, bool color=false, bool normal=false)
use instead of glEnableClient
void setColor(float r, float g, float b, float a=1.f)
Set the color to use for subsequent drawing.
void drawPath(const QVector< Vec3d > &points, const QVector< Vec4f > &colors)
Draw a curve defined by a list of points.
Draw the interior of the polygon filled with the current texture.
Definition: StelPainter.hpp:50
GL_TRIANGLE_FAN.
Definition: StelPainter.hpp:63
void setColorPointer(int size, int type, const void *pointer)
use instead of glColorPointer
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:70
void drawPoint2d(float x, float y)
Draw a GL_POINT at the given position.
void drawLine2d(float x1, float y1, float x2, float y2)
Draw a line between the 2 points.
QSharedPointer< StelProjector > StelProjectorP
Shared pointer on a StelProjector instance (implement reference counting)
static StelVertexArray computeSphereNoLight(float radius, float oneMinusOblateness, int slices, int stacks, int orientInside=0, bool flipTexture=false, float topAngle=0.0f, float bottomAngle=M_PI)
Generate a StelVertexArray for 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:48
Provides functions for performing openGL drawing operations.
Definition: StelPainter.hpp:40
void enableTexture2d(bool b)
Set whether texturing is enabled.
void drawText(float x, float y, const QString &str, float angleDeg=0.f, float xshift=0.f, float yshift=0.f, bool noGravity=true)
Draw the string at the given position and angle with the given font.
void drawStelVertexArray(const StelVertexArray &arr, bool checkDiscontinuity=true)
Draws the primitives defined in the StelVertexArray.
void drawFromArray(DrawingMode mode, int count, int offset=0, bool doProj=true, const unsigned short *indices=NULL)
Draws primitives using vertices from the arrays specified by setVertexArray().
SphericalPolygonDrawMode
Define the drawing mode when drawing polygons.
Definition: StelPainter.hpp:46
void setNormalPointer(int type, const void *pointer)
use instead of glNormalPointer
GL_LINE_LOOP.
Definition: StelPainter.hpp:59
GL_TRIANGLE_STRIP.
Definition: StelPainter.hpp:62
void setVertexPointer(int size, int type, const void *pointer)
use instead of glVertexPointer
Abstract class defining a region of the sphere.
Base texture class.
Definition: StelTexture.hpp:41
void drawViewportShape(void)
Fill with black around the viewport.
Manage textures loading.
void setFont(const QFont &font)
Set the font to use for subsequent text drawing.
void drawSprite2dModeNoDeviceScale(float x, float y, float radius)
Same as drawSprite2dMode but don't scale according to display device scaling.
Define the StelProjectorP type.
void drawRect2d(float x, float y, float width, float height, bool textured=true)
Draw a rectangle using the current texture at the given projected 2d position.
Draw the boundary of the polygon.
Definition: StelPainter.hpp:49
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 sSphereMap(float radius, int slices, int stacks, float textureFov=2.f *M_PI, int orientInside=0)
Draw a fisheye texture in a sphere.
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:55
GL_LINE_STRIP.
Definition: StelPainter.hpp:60