~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to src/core/STexture.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2009-03-13 20:07:22 UTC
  • mfrom: (1.1.8 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090313200722-l66s4zy2s3e8up0s
Tags: 0.10.2-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
 */
19
 
 
20
 
#ifndef _STEXTURE_HPP_
21
 
#define _STEXTURE_HPP_
22
 
 
23
 
#include "GLee.h"
24
 
#include "fixx11h.h"
25
 
#include "vecmath.h"
26
 
#include "STextureTypes.hpp"
27
 
 
28
 
#include <QObject>
29
 
#include <QImage>
30
 
 
31
 
class QMutex;
32
 
class QSemaphore;
33
 
class QFile;
34
 
 
35
 
//! @class STexture 
36
 
//! Base texture class
37
 
class STexture : public QObject
38
 
{
39
 
        Q_OBJECT;
40
 
        
41
 
        friend class StelTextureMgr;
42
 
        friend class ImageLoadThread;
43
 
 
44
 
public:
45
 
        //! Destructor
46
 
        virtual ~STexture();
47
 
        
48
 
        //! Bind the texture so that it can be used for openGL drawing (calls glBindTexture).
49
 
        //! If the texture is lazyly loaded, this starts the loading and return false immediately.
50
 
        //! @return true if the binding successfully occured, false if the texture is not yet loaded.
51
 
        bool bind();
52
 
        
53
 
        //! Return whether the texture can be binded, i.e. it is fully loaded
54
 
        bool canBind() const {return id!=0;}
55
 
        
56
 
        //! Get the average texture luminance.
57
 
        //! @param lum 0 is black, 1 is white
58
 
        //! @return true if the returned luminance is known, false if not. In this later case, the value of num is undefined.
59
 
        bool getAverageLuminance(float& lum);
60
 
 
61
 
        //! Return the width and heigth of the texture in pixels
62
 
        bool getDimensions(int &width, int &height);
63
 
 
64
 
        //! Return the position of the 4 corners of the texture in texture coordinates
65
 
        const Vec2d* getCoordinates() const {return texCoordinates;}
66
 
 
67
 
        //! Get the error message which caused the texture loading to fail
68
 
        //! @return the human friendly error message or empty string if no errors occured
69
 
        const QString& getErrorMessage() const {return errorMessage;}
70
 
        
71
 
        //! Return the full path to the image file
72
 
        const QString& getFullPath() const {return fullPath;}
73
 
        
74
 
        //! Return whether the image is currently being loaded
75
 
        bool isLoading() const {return isLoadingImage && !canBind();}
76
 
        
77
 
signals:
78
 
        //! Emitted when the texture is ready to be bind(), i.e. when downloaded, imageLoading and      glLoading is over
79
 
        //! or when an error occured and the texture will never be available
80
 
        //! In case of error, you can query what the problem was by calling getErrorMessage()
81
 
        //! @param error is equal to true if an error occured while loading the texture
82
 
        void loadingProcessFinished(bool error);
83
 
        
84
 
private slots:
85
 
        //! Called when the download for the texture file terminated
86
 
        void downloadFinished();
87
 
        
88
 
        //! Called when the file loading thread has terminated
89
 
        void fileLoadFinished();
90
 
        
91
 
private:
92
 
        //! Private constructor
93
 
        STexture();
94
 
        
95
 
        //! Load the texture already in the RAM to the openGL memory
96
 
        //! This function uses openGL routines and must be called in the main thread
97
 
        //! @return false if an error occured
98
 
        bool glLoad();
99
 
        
100
 
        //! Loading of the image data.
101
 
        //! This method is thread safe
102
 
        //! @return false if an error occured
103
 
        bool imageLoad();
104
 
        
105
 
        //! This method should be called if the texture loading failed for any reasons
106
 
        //! @param errorMessage the human friendly error message
107
 
        void reportError(const QString& errorMessage);
108
 
        
109
 
        //! Define the range mode used to rescale the texture when loading
110
 
        STextureTypes::DynamicRangeMode dynamicRangeMode;
111
 
        
112
 
        //! Used to download remote files if needed
113
 
        class QNetworkReply* httpReply;
114
 
        
115
 
        //! Used to load in thread
116
 
        class ImageLoadThread* loadThread;
117
 
                        
118
 
        //! Define if the texture was already downloaded if it was a remote one
119
 
        bool downloaded;
120
 
        //! Define whether the image is already loading
121
 
        bool isLoadingImage;
122
 
        
123
 
        //! The URL where to download the file
124
 
        QString fullPath;
125
 
        
126
 
        //! The data that was laoded from http
127
 
        QByteArray downloadedData;
128
 
        QImage qImage;
129
 
        
130
 
        //! Used ony when creating temporary file
131
 
        QString fileExtension;
132
 
        
133
 
        //! True when something when wrong in the loading process
134
 
        bool errorOccured;
135
 
        
136
 
        //! Human friendly error message if loading failed
137
 
        QString errorMessage;
138
 
        
139
 
        //! OpenGL id
140
 
        GLuint id;
141
 
        bool mipmapsMode;
142
 
        GLint wrapMode;
143
 
        GLint minFilter;
144
 
        GLint magFilter;
145
 
                        
146
 
        ///////////////////////////////////////////////////////////////////////////
147
 
        // Attributes protected by the Mutex
148
 
        //! Mutex used to protect all the attributes below 
149
 
        QMutex* mutex;
150
 
 
151
 
        //! Cached average luminance
152
 
        float avgLuminance;
153
 
        
154
 
        GLsizei width;  //! Texture image width
155
 
        GLsizei height; //! Texture image height
156
 
        GLenum format;
157
 
        GLint internalFormat;
158
 
        GLubyte* texels;
159
 
        GLenum type;
160
 
        
161
 
        //! Position of the 4 corners of the texture in texture coordinates
162
 
        Vec2d texCoordinates[4];
163
 
        
164
 
        //! Fix a limit to the number of maximum simultaneous loading threads
165
 
        static QSemaphore* maxLoadThreadSemaphore;
166
 
};
167
 
 
168
 
 
169
 
#endif // _STEXTURE_HPP_