• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Stellarium / stellarium / 3996069357

pending completion
3996069357

push

github

Ruslan Kabatsayev
Shorten some lines

5 of 5 new or added lines in 1 file covered. (100.0%)

14663 of 124076 relevant lines covered (11.82%)

22035.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/core/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 <QOpenGLFunctions>
24
#include <QObject>
25
#include <QImage>
26

27
class QFile;
28
class StelTextureMgr;
29
class QNetworkReply;
30
template <class T> class QFuture;
31

32
#ifndef GL_CLAMP_TO_EDGE
33
#define GL_CLAMP_TO_EDGE 0x812F
34
#endif
35

36
//! @class StelTexture
37
//! Base texture class. For creating an instance, use StelTextureMgr::createTexture() and StelTextureMgr::createTextureThread()
38
//! @sa StelTextureSP
39
class StelTexture: public QObject, public QEnableSharedFromThis<StelTexture>
40
{
41
        Q_OBJECT
42

43
public:
44
        //! Contains the parameters defining how a texture is created.
45
        struct StelTextureParams
46
        {
47
                StelTextureParams(bool qgenerateMipmaps=false, GLint afiltering=GL_LINEAR, GLint awrapMode=GL_CLAMP_TO_EDGE, bool qfilterMipmaps=false, int decimateBy=1) :
×
48
                                generateMipmaps(qgenerateMipmaps),
×
49
                                filterMipmaps(qfilterMipmaps),
×
50
                                filtering(afiltering),
×
51
                                wrapMode(awrapMode),
×
52
                                decimation(decimateBy){;}
×
53
                //! Define if mipmaps must be created.
54
                bool generateMipmaps;
55
                //! If true, mipmapped textures are filtered with GL_LINEAR_MIPMAP_LINEAR instead of GL_LINEAR_MIPMAP_NEAREST (i.e. enabling "trilinear" filtering)
56
                bool filterMipmaps;
57
                //! Define the scaling filter to use. Must be one of GL_NEAREST or GL_LINEAR
58
                GLint filtering;
59
                //! Define the wrapping mode to use. Must be one of GL_CLAMP_TO_EDGE, or GL_REPEAT.
60
                GLint wrapMode;
61
                //! Allow a reduction of the size of the texture image (useful for very limited hardware)
62
                //! The image size will be divided by this factor (e.g. 2, 3, 4, ...)
63
                int decimation;
64
        };
65

66
        //! Destructor
67
        virtual ~StelTexture();
68

69
        //! Bind the texture so that it can be used for openGL drawing (calls glBindTexture).
70
        //! If the texture is lazyly loaded, this starts the loading and return false immediately.
71
        //! @return true if the binding successfully occurred, false if the texture is not yet loaded.
72
        bool bind(uint slot=0);
73

74
        //! Releases the currently bound texture without testing if it is currently bound,
75
        //! i.e. it simply calls glBindTexture(GL_TEXTURE_2D, 0)
76
        inline void release() const { gl->glBindTexture(GL_TEXTURE_2D, 0 ); }
×
77

78
        //! Waits until the texture data is ready for usage (i.e. bind will return true after this).
79
        //! Do not use this for potentially network loaded textures.
80
        void waitForLoaded();
81

82
        //! Return whether the texture can be binded, i.e. it is fully loaded
83
        bool canBind() const {return id!=0;}
×
84

85
        //! Return the width and height of the texture in pixels
86
        bool getDimensions(int &width, int &height);
87

88
        //! Returns whether the texture has an alpha channel (GL_RGBA or GL_LUMINANCE_ALPHA format)
89
        //! This only returns valid information after the texture is fully loaded.
90
        bool hasAlphaChannel() const { return alphaChannel ; }
×
91

92
        //! Get the error message which caused the texture loading to fail
93
        //! @return the human friendly error message or empty string if no errors occurred
94
        const QString& getErrorMessage() const {return errorMessage;}
×
95

96
        //! Returns true if a loading error occurred
97
        bool hasError() const { return errorOccured; }
98

99
        //! Return the full path to the image file.
100
        //! If the texture was downloaded from a remote location, this function return the full URL.
101
        const QString& getFullPath() const {return fullPath;}
×
102

103
        //! Return whether the image is currently being loaded
104
        bool isLoading() const {return (loader || networkReply) && !canBind();}
×
105

106
        //! Return texture memory size
107
        unsigned int getGlSize() const {return glSize;}
×
108

109
signals:
110
        //! Emitted when the texture is ready to be bind(), i.e. when downloaded, imageLoading and        glLoading is over
111
        //! or when an error occurred and the texture will never be available
112
        //! In case of error, you can query what the problem was by calling getErrorMessage()
113
        //! @param error is equal to true if an error occurred while loading the texture
114
        void loadingProcessFinished(bool error);
115

116
private slots:
117
        void onNetworkReply();
118

119
private:
120
        friend class StelTextureMgr;
121

122
        //! structure returned by the loader threads, containing all the
123
        //! data and information to create the OpenGL texture.
124
        struct GLData
125
        {
126
                GLData() : width(0), height(0), format(0), type(0) {}
×
127
                QString loaderError; //! can contain an error message if data is null
128
                QByteArray data;
129
                int width;
130
                int height;
131
                GLint format;
132
                GLint type;
133
        };
134
        //! Those static methods can be called by QtConcurrent::run
135
        //! @param decimateBy: On limited platforms we must be able to reduce texture sizes. Divide texture size in both dimensions by this number.
136
        static GLData imageToGLData(const QImage    &image, const int decimateBy);
137
        static GLData loadFromPath( const QString    &path, const int decimateBy);
138
        static GLData loadFromData( const QByteArray &data, const int decimateBy);
139

140
        //! Private constructor
141
        StelTexture(StelTextureMgr* mgr);
142

143
        //! Wrap an existing GL texture with this object
144
        void wrapGLTexture(GLuint texId);
145

146
        //! Convert a QImage into OpenGL compatible format.
147
        //! The texture will be at most as large as GL_MAX_TEXTURE_SIZE
148
        //! @param decimate divide image size by this factor: reduce texture sizes to conserve texture memory (required on e.g. Raspberry Pi 3).
149
        static QByteArray convertToGLFormat(QImage image, GLint& format, GLint& type, int decimate, int& width, int& height);
150

151
        //! This method should be called if the texture loading failed for any reasons
152
        //! @param errorMessage the human friendly error message
153
        void reportError(const QString& errorMessage);
154

155
        //! Load the texture already in the RAM to the openGL memory
156
        //! This function uses openGL routines and must be called in the main thread
157
        //! @return false if an error occurred
158
        bool glLoad(const QImage& image);
159
        //! Same as glLoad(QImage), but with an image already in OpenGl format
160
        bool glLoad(const GLData& data);
161

162
        //! Starts the loading process if it has not already started.
163
        //! Returns true if the data was loaded, false if not yet ready.
164
        bool load();
165

166
        template <typename T, typename...Params, typename...Args>
167
        void startAsyncLoader(T (*functionPointer)(Params...), Args&&...args);
168

169
        //! The parent texture manager
170
        StelTextureMgr* textureMgr;
171

172
        QOpenGLFunctions* gl;
173
        StelTextureParams loadParams;
174

175
        //! Used to handle the connection for remote textures.
176
        QNetworkReply *networkReply;
177

178
        //! The loader object
179
        QFuture<GLData>* loader;
180

181
        //! The URL where to download the file
182
        QString fullPath;
183

184
        //! True when something when wrong in the loading process
185
        bool errorOccured;
186

187
        //! True if this texture contains an alpha channel
188
        bool alphaChannel;
189

190
        //! Human friendly error message if loading failed
191
        QString errorMessage;
192

193
        //! OpenGL id
194
        GLuint id;
195

196
        GLsizei width;        //! Texture image width
197
        GLsizei height;        //! Texture image height
198

199
        //! Size in GL memory
200
        unsigned int glSize;
201
};
202

203

204
#endif // STELTEXTURE_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc