glc_texture.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00026
00027
00028 #include "glc_texture.h"
00029 #include "../glc_exception.h"
00030 #include "../glc_global.h"
00031
00032
00033 #include "../quazip/quazip.h"
00034 #include "../quazip/quazipfile.h"
00035
00036 #include <QtDebug>
00037
00038
00039 QSize GLC_Texture::m_MaxTextureSize(676, 676);
00040
00041
00042 const QSize GLC_Texture::m_MinTextureSize(10, 10);
00044
00046
00048 GLC_Texture::GLC_Texture(const QGLContext *pContext)
00049 : m_pQGLContext(const_cast<QGLContext*>(pContext))
00050 , m_FileName()
00051 , m_GlTextureID(0)
00052 , m_textureImage()
00053 , m_TextureSize()
00054 , m_HasAlphaChannel(false)
00055 {
00056
00057 }
00058
00059
00060 GLC_Texture::GLC_Texture(const QGLContext *pContext, const QString &Filename)
00061 : m_pQGLContext(const_cast<QGLContext*>(pContext))
00062 , m_FileName(Filename)
00063 , m_GlTextureID(0)
00064 , m_textureImage(loadFromFile(m_FileName))
00065 , m_TextureSize()
00066 , m_HasAlphaChannel(m_textureImage.hasAlphaChannel())
00067 {
00068 if (m_textureImage.isNull())
00069 {
00070 QString ErrorMess("GLC_Texture::GLC_Texture open image : ");
00071 ErrorMess.append(m_FileName).append(" Failed");
00072 qDebug() << ErrorMess;
00073 GLC_Exception e(ErrorMess);
00074 throw(e);
00075 }
00076 }
00077
00078 GLC_Texture::GLC_Texture(const QGLContext *pContext, const QFile &file)
00079 : m_pQGLContext(const_cast<QGLContext*>(pContext))
00080 , m_FileName(file.fileName())
00081 , m_GlTextureID(0)
00082 , m_textureImage()
00083 , m_TextureSize()
00084 , m_HasAlphaChannel(m_textureImage.hasAlphaChannel())
00085 {
00086 m_textureImage.load(const_cast<QFile*>(&file), QFileInfo(m_FileName).suffix().toLocal8Bit());
00087 if (m_textureImage.isNull())
00088 {
00089 QString ErrorMess("GLC_Texture::GLC_Texture open image : ");
00090 ErrorMess.append(m_FileName).append(" Failed");
00091 qDebug() << ErrorMess;
00092 GLC_Exception e(ErrorMess);
00093 throw(e);
00094 }
00095 }
00096
00097
00098 GLC_Texture::GLC_Texture(const QGLContext* pContext, const QImage& image, const QString& fileName)
00099 : m_pQGLContext(const_cast<QGLContext*>(pContext))
00100 , m_FileName(fileName)
00101 , m_GlTextureID(0)
00102 , m_textureImage(image)
00103 , m_TextureSize()
00104 , m_HasAlphaChannel(m_textureImage.hasAlphaChannel())
00105 {
00106 Q_ASSERT(!m_textureImage.isNull());
00107 }
00108
00109 GLC_Texture::GLC_Texture(const GLC_Texture &TextureToCopy)
00110 : m_pQGLContext(TextureToCopy.m_pQGLContext)
00111 , m_FileName(TextureToCopy.m_FileName)
00112 , m_GlTextureID(0)
00113 , m_textureImage(TextureToCopy.m_textureImage)
00114 , m_TextureSize(TextureToCopy.m_TextureSize)
00115 , m_HasAlphaChannel(m_textureImage.hasAlphaChannel())
00116 {
00117 if (m_textureImage.isNull())
00118 {
00119 QString ErrorMess("GLC_Texture::GLC_Texture open image : ");
00120 ErrorMess.append(m_FileName).append(" Failed");
00121 qDebug() << ErrorMess;
00122 GLC_Exception e(ErrorMess);
00123 throw(e);
00124 }
00125 }
00126
00127
00128 GLC_Texture& GLC_Texture::operator=(const GLC_Texture& texture)
00129 {
00130 if (!(*this == texture))
00131 {
00132 if (m_GlTextureID != 0)
00133 {
00134 m_pQGLContext->deleteTexture(m_GlTextureID);
00135 m_GlTextureID= 0;
00136 }
00137
00138 m_pQGLContext= texture.m_pQGLContext;
00139 m_FileName= texture.m_FileName;
00140 m_GlTextureID= 0;
00141 m_textureImage= texture.m_textureImage;
00142 m_TextureSize= texture.m_TextureSize;
00143 m_HasAlphaChannel= m_textureImage.hasAlphaChannel();
00144 }
00145
00146 return *this;
00147 }
00148
00149 GLC_Texture::~GLC_Texture()
00150 {
00151
00152 if (m_GlTextureID != 0)
00153 {
00154 m_pQGLContext->deleteTexture(m_GlTextureID);
00155 m_GlTextureID= 0;
00156 }
00157 }
00159
00161
00162
00163 bool GLC_Texture::operator==(const GLC_Texture& texture) const
00164 {
00165 bool result;
00166 if (this == &texture)
00167 {
00168 result= true;
00169 }
00170 else
00171 {
00172 result= (m_FileName == texture.m_FileName) && (m_textureImage == texture.m_textureImage);
00173 }
00174 return result;
00175 }
00176
00178
00180
00181
00182 void GLC_Texture::setMaxTextureSize(const QSize& size)
00183 {
00184 if ((size.height() > m_MinTextureSize.height()) && (size.width() > m_MinTextureSize.width()))
00185 {
00186 m_MaxTextureSize= size;
00187 }
00188 else
00189 {
00190 qDebug() << "GLC_Texture::setMaxTextureSize m_MaxTextureSize set to m_MinTextureSize";
00191 m_MaxTextureSize= m_MinTextureSize;
00192 }
00193 }
00194
00196
00198
00199 void GLC_Texture::glLoadTexture(void)
00200 {
00201 if (m_GlTextureID == 0)
00202 {
00203 if (NULL == m_pQGLContext)
00204 {
00205 m_pQGLContext= const_cast<QGLContext*>(QGLContext::currentContext());
00206 }
00207
00208 if ((m_textureImage.height() > m_MaxTextureSize.height())
00209 || (m_textureImage.width() > m_MaxTextureSize.width()))
00210 {
00211 QImage rescaledImage;
00212 if(m_textureImage.height() > m_textureImage.width())
00213 {
00214 rescaledImage= m_textureImage.scaledToHeight(m_MaxTextureSize.height(), Qt::SmoothTransformation);
00215 }
00216 else
00217 {
00218 rescaledImage= m_textureImage.scaledToWidth(m_MaxTextureSize.width(), Qt::SmoothTransformation);
00219 }
00220 m_textureImage= rescaledImage;
00221 m_TextureSize= m_textureImage.size();
00222 m_GlTextureID= m_pQGLContext->bindTexture(m_textureImage);
00223 }
00224 else
00225 {
00226 m_TextureSize= m_textureImage.size();
00227 m_GlTextureID= m_pQGLContext->bindTexture(m_textureImage);
00228
00229 }
00230
00231 }
00232 }
00233
00234
00235 void GLC_Texture::glcBindTexture(void)
00236 {
00237 if (m_GlTextureID == 0)
00238 {
00239 glLoadTexture();
00240 }
00241 glBindTexture(GL_TEXTURE_2D, m_GlTextureID);
00242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00243 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00244 }
00245
00246 QImage GLC_Texture::loadFromFile(const QString& fileName)
00247 {
00248 QImage resultImage;
00249 if (glc::isArchiveString(fileName))
00250 {
00251
00252
00253 QuaZip* p3dxmlArchive= new QuaZip(glc::archiveFileName(fileName));
00254
00255 if(!p3dxmlArchive->open(QuaZip::mdUnzip))
00256 {
00257 delete p3dxmlArchive;
00258 return QImage();
00259 }
00260 else
00261 {
00262
00263 p3dxmlArchive->setFileNameCodec("IBM866");
00264 }
00265 QString imageFileName= glc::archiveEntryFileName(fileName);
00266
00267
00268 QuaZipFile* p3dxmlFile= new QuaZipFile(p3dxmlArchive);
00269
00270
00271 if (!p3dxmlArchive->setCurrentFile(imageFileName, QuaZip::csInsensitive))
00272 {
00273 delete p3dxmlFile;
00274 delete p3dxmlArchive;
00275 return QImage();
00276 }
00277
00278
00279 if(!p3dxmlFile->open(QIODevice::ReadOnly))
00280 {
00281 delete p3dxmlFile;
00282 delete p3dxmlArchive;
00283 return QImage();
00284 }
00285 resultImage.load(p3dxmlFile, QFileInfo(imageFileName).suffix().toLocal8Bit());
00286 p3dxmlFile->close();
00287 delete p3dxmlFile;
00288 delete p3dxmlArchive;
00289 }
00290 else
00291 {
00292 resultImage.load(fileName);
00293 }
00294
00295 return resultImage;
00296 }
00297
00298
00299 QDataStream &operator<<(QDataStream &stream, const GLC_Texture &texture)
00300 {
00301 stream << texture.fileName();
00302
00303 return stream;
00304 }
00305 QDataStream &operator>>(QDataStream &stream, GLC_Texture &texture)
00306 {
00307 QString fileName;
00308 stream >> fileName;
00309 texture= GLC_Texture(texture.context(), fileName);
00310
00311 return stream;
00312 }
00313