Stellarium 0.12.0
Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · Plugins · Renderer · File Structure

StelRenderer.hpp

00001 /*
00002  * Stellarium
00003  * Copyright (C) 2012 Ferdinand Majerech
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
00018  */
00019 
00020 #ifndef _STELRENDERER_HPP_
00021 #define _STELRENDERER_HPP_
00022 
00023 #include <QImage>
00024 #include <QPainter>
00025 #include <QSize>
00026 
00027 #include "StelApp.hpp"
00028 #include "StelCore.hpp"
00029 #include "StelGLSLShader.hpp"
00030 #include "StelIndexBuffer.hpp"
00031 #include "StelRendererStatistics.hpp"
00032 #include "StelVertexAttribute.hpp"
00033 #include "StelVertexBuffer.hpp"
00034 #include "StelViewportEffect.hpp"
00035 #include "StelTextureNew.hpp"
00036 #include "StelTextureParams.hpp"
00037 
00042 enum BlendMode
00043 {
00045     BlendMode_None,
00048     BlendMode_Add,
00050     BlendMode_Alpha,
00052     BlendMode_Multiply
00053 };
00054 
00058 enum CullFace 
00059 {
00061     CullFace_None,
00063     CullFace_Front,
00065     CullFace_Back
00066 };
00067 
00069 enum DepthTest
00070 {
00072     DepthTest_Disabled,
00074     DepthTest_ReadOnly,
00076     DepthTest_ReadWrite
00077 };
00078 
00080 enum StencilTest
00081 {
00083     StencilTest_Disabled,
00085     StencilTest_Write_1,
00089     StencilTest_DrawIf_1
00090 };
00091 
00097 enum TextureDataFormat
00098 {
00100     TextureDataFormat_RGBA_F32
00101 };
00102 
00107 class StelRenderClient
00108 {
00109 public:
00114     virtual bool drawPartial() = 0;
00115 
00121     virtual QPainter* getPainter() = 0;
00122 
00126     virtual StelViewportEffect* getViewportEffect() = 0;
00127 };
00128 
00146 struct TextParams
00147 {
00148 friend class StelQGLRenderer;
00160     TextParams(const float x, const float y, const QString& string)
00161         : position_(x, y, 0.0f)
00162         , string_(string)
00163         , angleDegrees_(0.0f)
00164         , xShift_(0.0f)
00165         , yShift_(0.0f)
00166         , noGravity_(true)
00167         , projector_(NULL)
00168         , doNotProject_(true)
00169     {}
00170 
00183     template<class F>
00184     TextParams(Vector3<F>& position3D, StelProjectorP projector, const QString& string)
00185         : position_(position3D[0], position3D[1], position3D[2])
00186         , string_(string)
00187         , angleDegrees_(0.0f)
00188         , xShift_(0.0f)
00189         , yShift_(0.0f)
00190         , noGravity_(true)
00191         , projector_(projector)
00192         , doNotProject_(false)
00193     {
00194     }
00195 
00197     TextParams& angleDegrees(const float angle)
00198     {
00199         angleDegrees_ = angle;
00200         return *this;
00201     }
00202 
00204     TextParams& shift(const float x, const float y)
00205     {
00206         xShift_ = x;
00207         yShift_ = y;
00208         return *this;
00209     }
00210 
00212     TextParams& useGravity()
00213     {
00214         noGravity_ = false;
00215         return *this;
00216     }
00217 
00219     TextParams& projector(StelProjectorP projector)
00220     {
00221         projector_ = projector;
00222         return *this;
00223     }
00224 
00225 private:
00227     Vec3f position_;
00229     QString string_;
00231     float angleDegrees_;
00233     float xShift_;
00235     float yShift_;
00237     bool  noGravity_;
00239     StelProjectorP projector_;
00241     bool doNotProject_;
00242 };
00243 
00248 class StelRenderer
00249 {
00250 // For destroyTextureBackend() and bindTextureBackend()
00251 friend class StelTextureNew;
00252 public:
00254     virtual ~StelRenderer(){};
00255 
00261     virtual bool init() = 0;
00262     
00264     virtual QImage screenshot() = 0;
00265     
00267     virtual void viewportHasBeenResized(const QSize size) = 0;
00268 
00270     virtual class StelIndexBuffer* createIndexBuffer(const IndexType type) = 0;
00271     
00282     template<class V>
00283     StelVertexBuffer<V>* createVertexBuffer(const PrimitiveType primitiveType)
00284     {
00285         return new StelVertexBuffer<V>
00286             (createVertexBufferBackend(primitiveType, V::attributes()), primitiveType);
00287     }
00288     
00310     template<class V>
00311     void drawVertexBuffer(StelVertexBuffer<V>* vertexBuffer, 
00312                           class StelIndexBuffer* indexBuffer = NULL,
00313                           StelProjectorP projector = StelProjectorP(NULL),
00314                           bool dontProject = false)
00315     {
00316         drawVertexBufferBackend(vertexBuffer->backend, indexBuffer, &(*projector), dontProject);
00317     }
00318 
00343     template<class V>
00344     void drawVertexBuffer(StelVertexBuffer<V>* vertexBuffer, 
00345                           class StelIndexBuffer* indexBuffer,
00346                           StelProjector* projector,
00347                           bool dontProject = false)
00348     {
00349         drawVertexBufferBackend(vertexBuffer->backend, indexBuffer, projector, dontProject);
00350     }
00351 
00358     virtual void drawLine(const float startX, const float startY, 
00359                           const float endX, const float endY);
00360 
00374     virtual void drawRect(const float x, const float y, 
00375                           const float width, const float height, 
00376                           const float angle = 0.0f) = 0;
00377     
00390     virtual void drawTexturedRect(const float x, const float y, 
00391                                   const float width, const float height, 
00392                                   const float angle = 0.0f) = 0;
00393 
00414     virtual void drawText(const TextParams& params) = 0;
00415 
00417     virtual void setFont(const QFont& font) = 0;
00418 
00428     virtual void renderFrame(StelRenderClient& renderClient) = 0;
00429 
00460     StelTextureNew* createTexture
00461         (const QString& filename, const TextureParams& params = TextureParams(), 
00462          const TextureLoadingMode loadingMode = TextureLoadingMode_Normal)
00463     {
00464         //This function tests preconditions and calls implementation.
00465         Q_ASSERT_X(!filename.endsWith(".pvr"), Q_FUNC_INFO,
00466                    "createTexture() can't load a PVR texture directly, as PVR "
00467                    "support may not be implemented by all Renderer backends. Request "
00468                    "a non-PVR texture, and if a PVR version exists and the backend "
00469                    "supports it, it will be loaded.");
00470         Q_ASSERT_X(!filename.isEmpty(), Q_FUNC_INFO,
00471                    "Trying to load a texture with an empty filename or URL");
00472         Q_ASSERT_X(!(filename.startsWith("http://") && loadingMode == TextureLoadingMode_Normal),
00473                    Q_FUNC_INFO,
00474                    "When loading a texture from network, texture loading mode must be "
00475                    "Asynchronous or LazyAsynchronous");
00476 
00477         return new StelTextureNew(this, createTextureBackend(filename, params, loadingMode));
00478     }
00479 
00499     StelTextureNew* createTexture
00500         (QImage& image, const TextureParams& params = TextureParams())
00501     {
00502         Q_ASSERT_X(!image.isNull(), Q_FUNC_INFO, "Trying to create a texture from a null image");
00503         return new StelTextureNew(this, createTextureBackend(image, params));
00504     }
00505 
00536     StelTextureNew* createTexture
00537         (const void* const data, const QSize size, const TextureDataFormat format, 
00538          const TextureParams& params = TextureParams())
00539     {
00540         Q_ASSERT_X(NULL != data, Q_FUNC_INFO, "Trying to load a texture from a NULL pointer to data");
00541         if(!areFloatTexturesSupported())
00542         {
00543             Q_ASSERT_X(format != TextureDataFormat_RGBA_F32, Q_FUNC_INFO,
00544                        "Trying to load a floating-point texture even though "
00545                        "float textures are not supported");
00546         }
00547         return new StelTextureNew(this, createTextureBackend(data, size, format, params));
00548     }
00549 
00551     virtual bool areFloatTexturesSupported() const = 0;
00552 
00561     StelTextureNew* getViewportTexture()
00562     {
00563         return new StelTextureNew(this, getViewportTextureBackend());
00564     }
00565 
00571     virtual StelGLSLShader* createGLSLShader()
00572     {
00573         Q_ASSERT_X(false, Q_FUNC_INFO, 
00574                    "Trying to create a GLSL shader with a renderer backend that doesn't "
00575                    "support GLSL");
00576         // Avoids compiler warnings.
00577         return NULL;
00578     }
00579 
00581     virtual bool isGLSLSupported() const = 0;
00582 
00584     virtual QSize getViewportSize() const = 0;
00585 
00597     virtual void setGlobalColor(const Vec4f& color) = 0;
00598 
00602     void setGlobalColor(const float r, const float g, const float b, const float a = 1.0f)
00603     {
00604         setGlobalColor(Vec4f(r, g, b, a));
00605     }
00606 
00612     virtual void setBlendMode(const BlendMode blendMode) = 0;
00613 
00629     virtual void setCulledFaces(const CullFace cullFace) = 0;
00630 
00632     virtual void clearDepthBuffer() = 0;
00633 
00641     virtual void setDepthTest(const DepthTest test) = 0;
00642 
00644     virtual void clearStencilBuffer() = 0;
00645 
00653     virtual void setStencilTest(const StencilTest test) = 0;
00654 
00659     virtual void swapBuffers() = 0;
00660 
00667     virtual StelRendererStatistics& getStatistics() = 0;
00668 
00669 protected:
00678     virtual StelVertexBufferBackend* createVertexBufferBackend
00679         (const PrimitiveType primitiveType, const QVector<StelVertexAttribute>& attributes) = 0;
00680 
00684     virtual void drawVertexBufferBackend(StelVertexBufferBackend* vertexBuffer, 
00685                                          class StelIndexBuffer* indexBuffer,
00686                                          StelProjector* projector,
00687                                          const bool dontProject) = 0;
00688 
00695     virtual class StelTextureBackend* createTextureBackend
00696         (const QString& filename, const TextureParams& params, 
00697          const TextureLoadingMode loadingMode) = 0;
00698 
00699 
00706     virtual class StelTextureBackend* createTextureBackend
00707         (QImage& image, const TextureParams& params) = 0;
00708 
00715     virtual class StelTextureBackend* createTextureBackend
00716         (const void* data, const QSize size, const TextureDataFormat format, 
00717          const TextureParams& params) = 0;
00718 
00722     virtual class StelTextureBackend* getViewportTextureBackend() = 0;
00723 
00728     virtual void destroyTextureBackend(class StelTextureBackend* backend) = 0;
00729 
00736     virtual void bindTextureBackend(class StelTextureBackend* textureBackend, const int textureUnit) = 0;
00737 };
00738 
00739 #endif // _STELRENDERER_HPP_
Generated on Thu Jan 31 14:05:40 2013 for Stellarium by  doxygen 1.6.3