Stellarium 0.13.0
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 
25 #include <QObject>
26 #include <QImage>
27 #include <QOpenGLFunctions>
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) :
50  generateMipmaps(qgenerateMipmaps),
51  filtering(afiltering),
52  wrapMode(awrapMode) {;}
56  GLint filtering;
58  GLint wrapMode;
59  };
60 
62  virtual ~StelTexture();
63 
67  bool bind(int slot = 0);
68 
70  bool loadFromMemory(const char *data, int width, int height, GLint format, GLint type, GLint internalFormat = 0);
71 
73  bool canBind() const {return id!=0;}
74 
76  bool getDimensions(int &width, int &height);
77 
80  const QString& getErrorMessage() const {return errorMessage;}
81 
84  const QString& getFullPath() const {return fullPath;}
85 
87  bool isLoading() const {return (loader || networkReply) && !canBind();}
88 
89 signals:
94  void loadingProcessFinished(bool error);
95 
96 private slots:
97  void onNetworkReply();
98 
99 private:
100  friend class StelTextureMgr;
101 
104  struct GLData
105  {
106  QByteArray data;
107  int width;
108  int height;
109  GLint format;
110  GLint type;
111  };
113  static GLData imageToGLData(const QImage &image);
114  static GLData loadFromPath(const QString &path);
115  static GLData loadFromData(const QByteArray& data);
116 
118  StelTexture();
119 
121  static QByteArray convertToGLFormat(const QImage& image, GLint* format, GLint* type);
122 
125  void reportError(const QString& errorMessage);
126 
128  void loadData(const char *data, int width, int height, GLint format, GLint type, GLint internalFormat = 0);
129 
133  bool glLoad(const QImage& image);
135  bool glLoad(const GLData& data);
136 
137  StelTextureParams loadParams;
138 
140  QNetworkReply *networkReply;
141 
143  QFuture<GLData>* loader;
144 
145 
147  QString fullPath;
148 
150  bool errorOccured;
151 
153  QString errorMessage;
154 
156  GLuint id;
157 
159  float avgLuminance;
160 
161  GLsizei width;
162  GLsizei height;
163 };
164 
165 
166 #endif // _STELTEXTURE_HPP_
bool loadFromMemory(const char *data, int width, int height, GLint format, GLint type, GLint internalFormat=0)
Load image data from in memory data.
bool isLoading() const
Return whether the image is currently being loaded.
Definition: StelTexture.hpp:87
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:84
bool canBind() const
Return whether the texture can be binded, i.e. it is fully loaded.
Definition: StelTexture.hpp:73
GLint wrapMode
Define the wrapping mode to use. Must be one of GL_CLAMP_TO_EDGE, or GL_REPEAT.
Definition: StelTexture.hpp:58
const QString & getErrorMessage() const
Get the error message which caused the texture loading to fail.
Definition: StelTexture.hpp:80
Definition: StelTexture.hpp:32
Base texture class.
Definition: StelTexture.hpp:41
Manage textures loading.
Definition: StelTextureMgr.hpp:33
bool generateMipmaps
Define if mipmaps must be created.
Definition: StelTexture.hpp:54
GLint filtering
Define the scaling filter to use. Must be one of GL_NEAREST or GL_LINEAR.
Definition: StelTexture.hpp:56
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.