Stellarium 0.13.3
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
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 
94 signals:
99  void loadingProcessFinished(bool error);
100 
101 private slots:
102  void onNetworkReply();
103 
104 private:
105  friend class StelTextureMgr;
106 
109  struct GLData
110  {
111  GLData() : data(NULL), width(0), height(0), format(0), type(0) {}
112  QByteArray data;
113  int width;
114  int height;
115  GLint format;
116  GLint type;
117  };
119  static GLData imageToGLData(const QImage &image);
120  static GLData loadFromPath(const QString &path);
121  static GLData loadFromData(const QByteArray& data);
122 
124  StelTexture();
125 
127  static QByteArray convertToGLFormat(const QImage& image, GLint* format, GLint* type);
128 
131  void reportError(const QString& errorMessage);
132 
136  bool glLoad(const QImage& image);
138  bool glLoad(const GLData& data);
139 
140  StelTextureParams loadParams;
141 
143  QNetworkReply *networkReply;
144 
146  QFuture<GLData>* loader;
147 
148 
150  QString fullPath;
151 
153  bool errorOccured;
154 
156  bool alphaChannel;
157 
159  QString errorMessage;
160 
162  GLuint id;
163 
165  float avgLuminance;
166 
167  GLsizei width;
168  GLsizei height;
169 };
170 
171 
172 #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
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.