Stellarium  0.16.1
StelTexture.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2006 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 _STELTEXTURE_HPP_
21 #define _STELTEXTURE_HPP_
22 
23 #include "StelTextureTypes.hpp"
24 #include "StelOpenGL.hpp"
25 
26 #include <QObject>
27 #include <QImage>
28 
29 class QFile;
30 class StelTextureMgr;
31 class QNetworkReply;
32 template <class T> class QFuture;
33 
34 #ifndef GL_CLAMP_TO_EDGE
35 #define GL_CLAMP_TO_EDGE 0x812F
36 #endif
37 
41 class StelTexture: public QObject, public QEnableSharedFromThis<StelTexture>
42 {
43  Q_OBJECT
44 
45 public:
48  {
49  StelTextureParams(bool qgenerateMipmaps=false, GLint afiltering=GL_LINEAR, GLint awrapMode=GL_CLAMP_TO_EDGE, bool qfilterMipmaps=false) :
50  generateMipmaps(qgenerateMipmaps),
51  filterMipmaps(qfilterMipmaps),
52  filtering(afiltering),
53  wrapMode(awrapMode){;}
59  GLint filtering;
61  GLint wrapMode;
62  };
63 
65  virtual ~StelTexture();
66 
70  bool bind(int slot=0);
71 
74  inline void release() const { gl->glBindTexture(GL_TEXTURE_2D, 0 ); }
75 
78  void waitForLoaded();
79 
81  bool canBind() const {return id!=0;}
82 
84  bool getDimensions(int &width, int &height);
85 
88  bool hasAlphaChannel() const { return alphaChannel ; }
89 
92  const QString& getErrorMessage() const {return errorMessage;}
93 
95  bool hasError() const { return errorOccured; }
96 
99  const QString& getFullPath() const {return fullPath;}
100 
102  bool isLoading() const {return (loader || networkReply) && !canBind();}
103 
105  unsigned int getGlSize() const {return glSize;}
106 
107 signals:
112  void loadingProcessFinished(bool error);
113 
114 private slots:
115  void onNetworkReply();
116 
117 private:
118  friend class StelTextureMgr;
119 
122  struct GLData
123  {
124  GLData() : width(0), height(0), format(0), type(0) {}
125  QString loaderError;
126  QByteArray data;
127  int width;
128  int height;
129  GLint format;
130  GLint type;
131  };
133  static GLData imageToGLData(const QImage &image);
134  static GLData loadFromPath(const QString &path);
135  static GLData loadFromData(const QByteArray& data);
136 
139 
141  void wrapGLTexture(GLuint texId);
142 
144  static QByteArray convertToGLFormat(const QImage& image, GLint* format, GLint* type);
145 
148  void reportError(const QString& errorMessage);
149 
153  bool glLoad(const QImage& image);
155  bool glLoad(const GLData& data);
156 
159  bool load();
160 
161  template <typename T, typename Param1, typename Arg1>
162  void startAsyncLoader(T (*functionPointer)(Param1), const Arg1 &arg1);
163 
165  StelTextureMgr* textureMgr;
166 
167  QOpenGLFunctions* gl;
168  StelTextureParams loadParams;
169 
171  QNetworkReply *networkReply;
172 
174  QFuture<GLData>* loader;
175 
177  QString fullPath;
178 
180  bool errorOccured;
181 
183  bool alphaChannel;
184 
186  QString errorMessage;
187 
189  GLuint id;
190 
191  GLsizei width;
192  GLsizei height;
193 
195  unsigned int glSize;
196 };
197 
198 
199 #endif // _STELTEXTURE_HPP_
bool isLoading() const
Return whether the image is currently being loaded.
Contains the parameters defining how a texture is created.
Definition: StelTexture.hpp:47
bool bind(int slot=0)
Bind the texture so that it can be used for openGL drawing (calls glBindTexture). ...
Define the StelTextureSP type.
const QString & getFullPath() const
Return the full path to the image file.
Definition: StelTexture.hpp:99
bool hasAlphaChannel() const
Returns whether the texture has an alpha channel (GL_RGBA or GL_LUMINANCE_ALPHA format) This only ret...
Definition: StelTexture.hpp:88
bool canBind() const
Return whether the texture can be binded, i.e. it is fully loaded.
Definition: StelTexture.hpp:81
GLint wrapMode
Define the wrapping mode to use. Must be one of GL_CLAMP_TO_EDGE, or GL_REPEAT.
Definition: StelTexture.hpp:61
const QString & getErrorMessage() const
Get the error message which caused the texture loading to fail.
Definition: StelTexture.hpp:92
void release() const
Releases the currently bound texture without testing if it is currently bound, i.e.
Definition: StelTexture.hpp:74
bool filterMipmaps
If true, mipmapped textures are filtered with GL_LINEAR_MIPMAP_LINEAR instead of GL_LINEAR_MIPMAP_NEA...
Definition: StelTexture.hpp:57
bool hasError() const
Returns true if a loading error occurred.
Definition: StelTexture.hpp:95
unsigned int getGlSize() const
Return texture memory size.
Base texture class.
Definition: StelTexture.hpp:41
Manage textures loading.
bool generateMipmaps
Define if mipmaps must be created.
Definition: StelTexture.hpp:55
void waitForLoaded()
Waits until the texture data is ready for usage (i.e.
GLint filtering
Define the scaling filter to use. Must be one of GL_NEAREST or GL_LINEAR.
Definition: StelTexture.hpp:59
virtual ~StelTexture()
Destructor.
void loadingProcessFinished(bool error)
Emitted when the texture is ready to be bind(), i.e.
bool getDimensions(int &width, int &height)
Return the width and heigth of the texture in pixels.