Stellarium 0.12.4
StelRenderer.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2012 Ferdinand Majerech
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 _STELRENDERER_HPP_
21 #define _STELRENDERER_HPP_
22 
23 #include <QImage>
24 #include <QPainter>
25 #include <QSize>
26 
27 #include "StelApp.hpp"
28 #include "StelCore.hpp"
29 #include "StelGLSLShader.hpp"
30 #include "StelIndexBuffer.hpp"
31 #include "StelRendererStatistics.hpp"
32 #include "StelVertexAttribute.hpp"
33 #include "StelVertexBuffer.hpp"
34 #include "StelViewportEffect.hpp"
35 #include "StelTextureNew.hpp"
36 #include "StelTextureParams.hpp"
37 
42 enum BlendMode
43 {
45  BlendMode_None,
48  BlendMode_Add,
50  BlendMode_Alpha,
52  BlendMode_Multiply
53 };
54 
58 enum CullFace
59 {
61  CullFace_None,
63  CullFace_Front,
65  CullFace_Back
66 };
67 
69 enum DepthTest
70 {
72  DepthTest_Disabled,
74  DepthTest_ReadOnly,
76  DepthTest_ReadWrite
77 };
78 
80 enum StencilTest
81 {
83  StencilTest_Disabled,
85  StencilTest_Write_1,
89  StencilTest_DrawIf_1
90 };
91 
97 enum TextureDataFormat
98 {
100  TextureDataFormat_RGBA_F32
101 };
102 
108 {
109 public:
114  virtual bool drawPartial() = 0;
115 
121  virtual QPainter* getPainter() = 0;
122 
126  virtual StelViewportEffect* getViewportEffect() = 0;
127 };
128 
147 {
148 friend class StelQGLRenderer;
160  TextParams(const float x, const float y, const QString& string)
161  : position_(x, y, 0.0f)
162  , string_(string)
163  , angleDegrees_(0.0f)
164  , xShift_(0.0f)
165  , yShift_(0.0f)
166  , noGravity_(true)
167  , projector_(NULL)
168  , doNotProject_(true)
169  {}
170 
183  template<class F>
184  TextParams(Vector3<F>& position3D, StelProjectorP projector, const QString& string)
185  : position_(position3D[0], position3D[1], position3D[2])
186  , string_(string)
187  , angleDegrees_(0.0f)
188  , xShift_(0.0f)
189  , yShift_(0.0f)
190  , noGravity_(true)
191  , projector_(projector)
192  , doNotProject_(false)
193  {
194  }
195 
197  TextParams& angleDegrees(const float angle)
198  {
199  angleDegrees_ = angle;
200  return *this;
201  }
202 
204  TextParams& shift(const float x, const float y)
205  {
206  xShift_ = x;
207  yShift_ = y;
208  return *this;
209  }
210 
213  {
214  noGravity_ = false;
215  return *this;
216  }
217 
220  {
221  projector_ = projector;
222  return *this;
223  }
224 
225 private:
227  Vec3f position_;
229  QString string_;
231  float angleDegrees_;
233  float xShift_;
235  float yShift_;
237  bool noGravity_;
239  StelProjectorP projector_;
241  bool doNotProject_;
242 };
243 
249 {
250 // For destroyTextureBackend() and bindTextureBackend()
251 friend class StelTextureNew;
252 public:
254  virtual ~StelRenderer(){};
255 
261  virtual bool init() = 0;
262 
264  virtual QImage screenshot() = 0;
265 
267  virtual void viewportHasBeenResized(const QSize size) = 0;
268 
270  virtual class StelIndexBuffer* createIndexBuffer(const IndexType type) = 0;
271 
282  template<class V>
283  StelVertexBuffer<V>* createVertexBuffer(const PrimitiveType primitiveType)
284  {
285  return new StelVertexBuffer<V>
286  (createVertexBufferBackend(primitiveType, V::attributes()), primitiveType);
287  }
288 
310  template<class V>
312  class StelIndexBuffer* indexBuffer = NULL,
313  StelProjectorP projector = StelProjectorP(NULL),
314  bool dontProject = false)
315  {
316  drawVertexBufferBackend(vertexBuffer->backend, indexBuffer, &(*projector), dontProject);
317  }
318 
343  template<class V>
345  class StelIndexBuffer* indexBuffer,
346  StelProjector* projector,
347  bool dontProject = false)
348  {
349  drawVertexBufferBackend(vertexBuffer->backend, indexBuffer, projector, dontProject);
350  }
351 
358  virtual void drawLine(const float startX, const float startY,
359  const float endX, const float endY);
360 
374  virtual void drawRect(const float x, const float y,
375  const float width, const float height,
376  const float angle = 0.0f) = 0;
377 
390  virtual void drawTexturedRect(const float x, const float y,
391  const float width, const float height,
392  const float angle = 0.0f) = 0;
393 
414  virtual void drawText(const TextParams& params) = 0;
415 
417  virtual void setFont(const QFont& font) = 0;
418 
428  virtual void renderFrame(StelRenderClient& renderClient) = 0;
429 
461  (const QString& filename, const TextureParams& params = TextureParams(),
462  const TextureLoadingMode loadingMode = TextureLoadingMode_Normal)
463  {
464  //This function tests preconditions and calls implementation.
465  Q_ASSERT_X(!filename.endsWith(".pvr"), Q_FUNC_INFO,
466  "createTexture() can't load a PVR texture directly, as PVR "
467  "support may not be implemented by all Renderer backends. Request "
468  "a non-PVR texture, and if a PVR version exists and the backend "
469  "supports it, it will be loaded.");
470  Q_ASSERT_X(!filename.isEmpty(), Q_FUNC_INFO,
471  "Trying to load a texture with an empty filename or URL");
472  Q_ASSERT_X(!(filename.startsWith("http://") && loadingMode == TextureLoadingMode_Normal),
473  Q_FUNC_INFO,
474  "When loading a texture from network, texture loading mode must be "
475  "Asynchronous or LazyAsynchronous");
476 
477  return new StelTextureNew(this, createTextureBackend(filename, params, loadingMode));
478  }
479 
500  (QImage& image, const TextureParams& params = TextureParams())
501  {
502  Q_ASSERT_X(!image.isNull(), Q_FUNC_INFO, "Trying to create a texture from a null image");
503  return new StelTextureNew(this, createTextureBackend(image, params));
504  }
505 
537  (const void* const data, const QSize size, const TextureDataFormat format,
538  const TextureParams& params = TextureParams())
539  {
540  Q_ASSERT_X(NULL != data, Q_FUNC_INFO, "Trying to load a texture from a NULL pointer to data");
542  {
543  Q_ASSERT_X(format != TextureDataFormat_RGBA_F32, Q_FUNC_INFO,
544  "Trying to load a floating-point texture even though "
545  "float textures are not supported");
546  }
547  return new StelTextureNew(this, createTextureBackend(data, size, format, params));
548  }
549 
551  virtual bool areFloatTexturesSupported() const = 0;
552 
562  {
563  return new StelTextureNew(this, getViewportTextureBackend());
564  }
565 
572  {
573  Q_ASSERT_X(false, Q_FUNC_INFO,
574  "Trying to create a GLSL shader with a renderer backend that doesn't "
575  "support GLSL");
576  // Avoids compiler warnings.
577  return NULL;
578  }
579 
581  virtual bool isGLSLSupported() const = 0;
582 
584  virtual QSize getViewportSize() const = 0;
585 
597  virtual void setGlobalColor(const Vec4f& color) = 0;
598 
602  void setGlobalColor(const float r, const float g, const float b, const float a = 1.0f)
603  {
604  setGlobalColor(Vec4f(r, g, b, a));
605  }
606 
612  virtual void setBlendMode(const BlendMode blendMode) = 0;
613 
629  virtual void setCulledFaces(const CullFace cullFace) = 0;
630 
632  virtual void clearDepthBuffer() = 0;
633 
641  virtual void setDepthTest(const DepthTest test) = 0;
642 
644  virtual void clearStencilBuffer() = 0;
645 
653  virtual void setStencilTest(const StencilTest test) = 0;
654 
659  virtual void swapBuffers() = 0;
660 
667  virtual StelRendererStatistics& getStatistics() = 0;
668 
669 protected:
679  (const PrimitiveType primitiveType, const QVector<StelVertexAttribute>& attributes) = 0;
680 
684  virtual void drawVertexBufferBackend(StelVertexBufferBackend* vertexBuffer,
685  class StelIndexBuffer* indexBuffer,
686  StelProjector* projector,
687  const bool dontProject) = 0;
688 
696  (const QString& filename, const TextureParams& params,
697  const TextureLoadingMode loadingMode) = 0;
698 
699 
707  (QImage& image, const TextureParams& params) = 0;
708 
716  (const void* data, const QSize size, const TextureDataFormat format,
717  const TextureParams& params) = 0;
718 
722  virtual class StelTextureBackend* getViewportTextureBackend() = 0;
723 
728  virtual void destroyTextureBackend(class StelTextureBackend* backend) = 0;
729 
736  virtual void bindTextureBackend(class StelTextureBackend* textureBackend, const int textureUnit) = 0;
737 };
738 
739 #endif // _STELRENDERER_HPP_