STexture.cpp   StelTexture.cpp 
skipping to change at line 21 skipping to change at line 21
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA.
*/ */
#include <cstdlib> #include <cstdlib>
#include "STexture.hpp" #include "StelTexture.hpp"
#include "StelTextureMgr.hpp" #include "StelTextureMgr.hpp"
#include "StelFileMgr.hpp" #include "StelFileMgr.hpp"
#include "StelApp.hpp" #include "StelApp.hpp"
#include <QThread> #include <QThread>
#include <QMutexLocker> #include <QMutexLocker>
#include <QSemaphore> #include <QSemaphore>
#include <QImageReader> #include <QImageReader>
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
skipping to change at line 48 skipping to change at line 48
#include <QGLWidget> #include <QGLWidget>
#include <QNetworkReply> #include <QNetworkReply>
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
#include <OpenGL/glu.h> /* Header File For The GLU Library */ #include <OpenGL/glu.h> /* Header File For The GLU Library */
#else #else
#include <GL/glu.h> /* Header File For The GLU Library */ #include <GL/glu.h> /* Header File For The GLU Library */
#endif #endif
// Initialize statics // Initialize statics
QSemaphore* STexture::maxLoadThreadSemaphore = new QSemaphore(5); QSemaphore* StelTexture::maxLoadThreadSemaphore = new QSemaphore(5);
/************************************************************************* /*************************************************************************
Class used to load an image and set the texture parameters in a thread Class used to load an image and set the texture parameters in a thread
*************************************************************************/ *************************************************************************/
class ImageLoadThread : public QThread class ImageLoadThread : public QThread
{ {
public: public:
ImageLoadThread(STexture* tex) : QThread((QObject*)tex), tex ture(tex) {;} ImageLoadThread(StelTexture* tex) : QThread((QObject*)tex), texture(tex) {;}
virtual void run(); virtual void run();
private: private:
STexture* texture; StelTexture* texture;
}; };
void ImageLoadThread::run() void ImageLoadThread::run()
{ {
STexture::maxLoadThreadSemaphore->acquire(1); StelTexture::maxLoadThreadSemaphore->acquire(1);
texture->imageLoad(); texture->imageLoad();
STexture::maxLoadThreadSemaphore->release(1); StelTexture::maxLoadThreadSemaphore->release(1);
} }
/************************************************************************* /*************************************************************************
Constructor Constructor
*************************************************************************/ *************************************************************************/
STexture::STexture() : httpReply(NULL), loadThread(NULL), downloaded(false) , isLoadingImage(false), StelTexture::StelTexture() : httpReply(NULL), loadThread(NULL), downloaded( false), isLoadingImage(false),
errorOccured(false), id(0), avgLuminance( -1.f), texels(NULL), type(GL_UNSIGNED_BYTE) errorOccured(false), id(0), avgLuminance( -1.f), texels(NULL), type(GL_UNSIGNED_BYTE)
{ {
mutex = new QMutex(); mutex = new QMutex();
texCoordinates[0].set(1., 0.); texCoordinates[0].set(1., 0.);
texCoordinates[1].set(0., 0.); texCoordinates[1].set(0., 0.);
texCoordinates[2].set(1., 1.); texCoordinates[2].set(1., 1.);
texCoordinates[3].set(0., 1.); texCoordinates[3].set(0., 1.);
width = -1; width = -1;
height = -1; height = -1;
} }
STexture::~STexture() StelTexture::~StelTexture()
{ {
if (httpReply || (loadThread && loadThread->isRunning())) if (httpReply || (loadThread && loadThread->isRunning()))
{ {
reportError("Aborted (texture deleted)"); reportError("Aborted (texture deleted)");
} }
if (httpReply) if (httpReply)
{ {
// HTTP is still doing something for this texture. We abort it. // HTTP is still doing something for this texture. We abort it.
httpReply->abort(); httpReply->abort();
skipping to change at line 115 skipping to change at line 115
loadThread->wait(500); loadThread->wait(500);
} }
if (texels) if (texels)
TexMalloc::free(texels); TexMalloc::free(texels);
texels = NULL; texels = NULL;
if (id!=0) if (id!=0)
{ {
if (glIsTexture(id)==GL_FALSE) if (glIsTexture(id)==GL_FALSE)
{ {
qDebug() << "WARNING: in STexture::~STexture() tried to delete invalid texture with ID=" << id << " Current GL ERROR status is " << glGetError(); qDebug() << "WARNING: in StelTexture::~StelTexture() tried to delete invalid texture with ID=" << id << " Current GL ERROR stat us is " << glGetError();
} }
else else
{ {
glDeleteTextures(1, &id); glDeleteTextures(1, &id);
} }
id = 0; id = 0;
} }
delete mutex; delete mutex;
mutex = NULL; mutex = NULL;
} }
/************************************************************************* /*************************************************************************
This method should be called if the texture loading failed for any reasons This method should be called if the texture loading failed for any reasons
*************************************************************************/ *************************************************************************/
void STexture::reportError(const QString& aerrorMessage) void StelTexture::reportError(const QString& aerrorMessage)
{ {
errorOccured = true; errorOccured = true;
errorMessage = aerrorMessage; errorMessage = aerrorMessage;
// Report failure of texture loading // Report failure of texture loading
emit(loadingProcessFinished(true)); emit(loadingProcessFinished(true));
} }
/************************************************************************* /*************************************************************************
Bind the texture so that it can be used for openGL drawing (calls glBindTe xture) Bind the texture so that it can be used for openGL drawing (calls glBindTe xture)
*************************************************************************/ *************************************************************************/
bool STexture::bind() bool StelTexture::bind()
{ {
if (id!=0) if (id!=0)
{ {
// The texture is already fully loaded, just bind and return true; // The texture is already fully loaded, just bind and return true;
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
return true; return true;
} }
if (errorOccured) if (errorOccured)
return false; return false;
// The texture is not yet fully loaded // The texture is not yet fully loaded
if (downloaded==false && httpReply==NULL && fullPath.startsWith("htt p://")) if (downloaded==false && httpReply==NULL && fullPath.startsWith("htt p://"))
{ {
// We need to start download // We need to start download
httpReply = StelApp::getInstance().getNetworkAccessManager() QNetworkRequest req = QNetworkRequest(QUrl(fullPath));
->get(QNetworkRequest(QUrl(fullPath))); // Define that preference should be given to cached files (n
o etag checks)
req.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
QNetworkRequest::PreferCache);
httpReply = StelApp::getInstance().getNetworkAccessManager()
->get(req);
connect(httpReply, SIGNAL(finished()), this, SLOT(downloadFi nished())); connect(httpReply, SIGNAL(finished()), this, SLOT(downloadFi nished()));
return false; return false;
} }
// From this point we assume that fullPath is valid // From this point we assume that fullPath is valid
// Start loading the image in a thread and return imediately // Start loading the image in a thread and return imediately
if (!isLoadingImage && downloaded==true) if (!isLoadingImage && downloaded==true)
{ {
isLoadingImage = true; isLoadingImage = true;
loadThread = new ImageLoadThread(this); loadThread = new ImageLoadThread(this);
connect(loadThread, SIGNAL(finished()), this, SLOT(fileLoadF inished())); connect(loadThread, SIGNAL(finished()), this, SLOT(fileLoadF inished()));
loadThread->start(QThread::LowestPriority); loadThread->start(QThread::LowestPriority);
} }
return false; return false;
} }
/************************************************************************* /*************************************************************************
Called when the download for the texture file terminated Called when the download for the texture file terminated
*************************************************************************/ *************************************************************************/
void STexture::downloadFinished() void StelTexture::downloadFinished()
{ {
downloadedData = httpReply->readAll(); downloadedData = httpReply->readAll();
downloaded=true; downloaded=true;
if (httpReply->error()!=QNetworkReply::NoError || errorOccured) if (httpReply->error()!=QNetworkReply::NoError || errorOccured)
{ {
if (httpReply->error()!=QNetworkReply::OperationCanceledErro r) if (httpReply->error()!=QNetworkReply::OperationCanceledErro r)
qWarning() << "Texture download failed for " + fullP ath+ ": " + httpReply->errorString(); qWarning() << "Texture download failed for " + fullP ath+ ": " + httpReply->errorString();
errorOccured = true; errorOccured = true;
} }
httpReply->deleteLater(); httpReply->deleteLater();
httpReply=NULL; httpReply=NULL;
// Call bind to activate data loading // Call bind to activate data loading
//bind(); //bind();
} }
/************************************************************************* /*************************************************************************
Called when the file loading thread has terminated Called when the file loading thread has terminated
*************************************************************************/ *************************************************************************/
void STexture::fileLoadFinished() void StelTexture::fileLoadFinished()
{ {
glLoad(); glLoad();
} }
/************************************************************************* /*************************************************************************
Return the average texture luminance, 0 is black, 1 is white Return the average texture luminance, 0 is black, 1 is white
*************************************************************************/ *************************************************************************/
bool STexture::getAverageLuminance(float& lum) bool StelTexture::getAverageLuminance(float& lum)
{ {
if (id==0) if (id==0)
return false; return false;
QMutexLocker lock(mutex); QMutexLocker lock(mutex);
if (avgLuminance<0) if (avgLuminance<0)
{ {
int size = width*height; int size = width*height;
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
GLfloat* p = (GLfloat*)calloc(size, sizeof(GLfloat)); GLfloat* p = (GLfloat*)calloc(size, sizeof(GLfloat));
assert(p); Q_ASSERT(p);
glGetTexImage(GL_TEXTURE_2D, 0, GL_LUMINANCE, GL_FLOAT, p); glGetTexImage(GL_TEXTURE_2D, 0, GL_LUMINANCE, GL_FLOAT, p);
float sum = 0.f; float sum = 0.f;
for (int i=0;i<size;++i) for (int i=0;i<size;++i)
{ {
sum += p[i]; sum += p[i];
} }
free(p); free(p);
avgLuminance = sum/size; avgLuminance = sum/size;
} }
lum = avgLuminance; lum = avgLuminance;
return true; return true;
} }
/************************************************************************* /*************************************************************************
Return the width and heigth of the texture in pixels Return the width and heigth of the texture in pixels
*************************************************************************/ *************************************************************************/
bool STexture::getDimensions(int &awidth, int &aheight) bool StelTexture::getDimensions(int &awidth, int &aheight)
{ {
QMutexLocker lock(mutex); QMutexLocker lock(mutex);
if (width<0 || height<0) if (width<0 || height<0)
{ {
// Try to get the size from the file without loading data // Try to get the size from the file without loading data
QImageReader im(fullPath); QImageReader im(fullPath);
if (!im.canRead()) if (!im.canRead())
{ {
return false; return false;
} }
QSize size = im.size(); QSize size = im.size();
width = size.width(); width = size.width();
height = size.height(); height = size.height();
} }
awidth = width; awidth = width;
aheight = height; aheight = height;
return true; return true;
} }
// This class let's us sleep in milleseconds
class MySleep : public QThread
{
public:
static void msleep(unsigned long msecs)
{
QThread::msleep(msecs);
}
};
/************************************************************************* /*************************************************************************
Load the image data Load the image data
*************************************************************************/ *************************************************************************/
bool STexture::imageLoad() bool StelTexture::imageLoad()
{ {
bool res=true; bool res=true;
if (downloadedData.isEmpty()) if (downloadedData.isEmpty())
{ {
// Load the data from the file // Load the data from the file
QMutexLocker lock(mutex); QMutexLocker lock(mutex);
res = StelApp::getInstance().getTextureManager().loadImage(t his); res = StelApp::getInstance().getTextureManager().loadImage(t his);
} }
else else
{ {
skipping to change at line 322 skipping to change at line 315
} }
// Release the memory // Release the memory
downloadedData = QByteArray(); downloadedData = QByteArray();
} }
return res; return res;
} }
/************************************************************************* /*************************************************************************
Actually load the texture already in the RAM to openGL memory Actually load the texture already in the RAM to openGL memory
*************************************************************************/ *************************************************************************/
bool STexture::glLoad() bool StelTexture::glLoad()
{ {
if (qImage.isNull() && !texels) if (qImage.isNull() && !texels)
{ {
errorOccured = true; errorOccured = true;
reportError("Unknown error"); reportError("Unknown error");
return false; return false;
} }
// generate texture // generate texture
glGenTextures (1, &id); glGenTextures (1, &id);
 End of changes. 20 change blocks. 
30 lines changed or deleted 25 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/