Stellarium 0.15.2
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, protected QOpenGLFunctions
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 
71  bool bind(int slot=0);
72 
74  bool canBind() const {return id!=0;}
75 
77  bool getDimensions(int &width, int &height);
78 
81  bool hasAlphaChannel() const { return alphaChannel ; }
82 
85  const QString& getErrorMessage() const {return errorMessage;}
86 
89  const QString& getFullPath() const {return fullPath;}
90 
92  bool isLoading() const {return (loader || networkReply) && !canBind();}
93 
95  unsigned int getGlSize() const {return glSize;}
96 
97 signals:
102  void loadingProcessFinished(bool error);
103 
104 private slots:
105  void onNetworkReply();
106 
107 private:
108  friend class StelTextureMgr;
109 
112  struct GLData
113  {
114  GLData() : data(NULL), width(0), height(0), format(0), type(0) {}
115  QByteArray data;
116  int width;
117  int height;
118  GLint format;
119  GLint type;
120  };
122  static GLData imageToGLData(const QImage &image);
123  static GLData loadFromPath(const QString &path);
124  static GLData loadFromData(const QByteArray& data);
125 
128 
130  static QByteArray convertToGLFormat(const QImage& image, GLint* format, GLint* type);
131 
134  void reportError(const QString& errorMessage);
135 
139  bool glLoad(const QImage& image);
141  bool glLoad(const GLData& data);
142 
144  StelTextureMgr* textureMgr;
145 
146  StelTextureParams loadParams;
147 
149  QNetworkReply *networkReply;
150 
152  QFuture<GLData>* loader;
153 
154 
156  QString fullPath;
157 
159  bool errorOccured;
160 
162  bool alphaChannel;
163 
165  QString errorMessage;
166 
168  GLuint id;
169 
171  float avgLuminance;
172 
173  GLsizei width;
174  GLsizei height;
175 
177  unsigned int glSize;
178 };
179 
180 
181 #endif // _STELTEXTURE_HPP_
bool isLoading() const
Return whether the image is currently being loaded.
Definition: StelTexture.hpp:92
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:89
bool hasAlphaChannel() const
Returns whether the texture has an alpha channel (GL_RGBA or GL_LUMINANCE_ALPHA format) This only ret...
Definition: StelTexture.hpp:81
bool canBind() const
Return whether the texture can be binded, i.e. it is fully loaded.
Definition: StelTexture.hpp:74
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:85
bool filterMipmaps
If true, mipmapped textures are filtered with GL_LINEAR_MIPMAP_LINEAR instead of GL_LINEAR_MIPMAP_NEA...
Definition: StelTexture.hpp:57
unsigned int getGlSize() const
Return texture memory size.
Definition: StelTexture.hpp:95
Base texture class.
Definition: StelTexture.hpp:41
Manage textures loading.
bool generateMipmaps
Define if mipmaps must be created.
Definition: StelTexture.hpp:55
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.