GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_texture.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 *****************************************************************************/
22 
24 
25 
26 #include "glc_texture.h"
27 #include "../glc_exception.h"
28 #include "../glc_global.h"
29 
30 // Quazip library
31 #include "../quazip/quazip.h"
32 #include "../quazip/quazipfile.h"
33 
34 #include <QtDebug>
35 
36 // The default maximum texture size
37 QSize GLC_Texture::m_MaxTextureSize(676, 676);
38 
39 // The Minimum texture size
40 const QSize GLC_Texture::m_MinTextureSize(10, 10);
41 
42 QHash<GLuint, int> GLC_Texture::m_TextureIdUsage;
43 
45 // Constructor Destructor
47 
50 : m_pQGLContext(NULL)
51 , m_FileName()
52 , m_GlTextureID(0)
53 , m_textureImage()
54 , m_TextureSize()
55 , m_HasAlphaChannel(false)
56 {
57 
58 }
59 
60 // Constructor with fileName
61 GLC_Texture::GLC_Texture(const QString &Filename)
62 : m_pQGLContext(NULL)
63 , m_FileName(Filename)
64 , m_GlTextureID(0)
65 , m_textureImage(loadFromFile(m_FileName))
66 , m_TextureSize()
67 , m_HasAlphaChannel(m_textureImage.hasAlphaChannel())
68 {
69  if (m_textureImage.isNull())
70  {
71  QString ErrorMess("GLC_Texture::GLC_Texture open image : ");
72  ErrorMess.append(m_FileName).append(" Failed");
73  qDebug() << ErrorMess;
74  GLC_Exception e(ErrorMess);
75  throw(e);
76  }
77 }
78 // Constructor with QFile
79 GLC_Texture::GLC_Texture(const QFile &file)
80 : m_pQGLContext(NULL)
81 , m_FileName(file.fileName())
82 , m_GlTextureID(0)
83 , m_textureImage()
84 , m_TextureSize()
85 , m_HasAlphaChannel(m_textureImage.hasAlphaChannel())
86 {
87  m_textureImage.load(const_cast<QFile*>(&file), QFileInfo(m_FileName).suffix().toLocal8Bit());
88  if (m_textureImage.isNull())
89  {
90  QString ErrorMess("GLC_Texture::GLC_Texture open image : ");
91  ErrorMess.append(m_FileName).append(" Failed");
92  qDebug() << ErrorMess;
93  GLC_Exception e(ErrorMess);
94  throw(e);
95  }
96 }
97 
98 // Constructor with QImage
99 GLC_Texture::GLC_Texture(const QImage& image, const QString& fileName)
100 : m_pQGLContext(NULL)
101 , m_FileName(fileName)
102 , m_GlTextureID(0)
103 , m_textureImage(image)
104 , m_TextureSize()
105 , m_HasAlphaChannel(m_textureImage.hasAlphaChannel())
106 {
107  Q_ASSERT(!m_textureImage.isNull());
108 }
109 
111 : m_pQGLContext(TextureToCopy.m_pQGLContext)
112 , m_FileName(TextureToCopy.m_FileName)
113 , m_GlTextureID(TextureToCopy.m_GlTextureID)
114 , m_textureImage(TextureToCopy.m_textureImage)
115 , m_TextureSize(TextureToCopy.m_TextureSize)
116 , m_HasAlphaChannel(m_textureImage.hasAlphaChannel())
117 {
118  if (m_textureImage.isNull())
119  {
120  QString ErrorMess("GLC_Texture::GLC_Texture open image : ");
121  ErrorMess.append(m_FileName).append(" Failed");
122  qDebug() << ErrorMess;
123  GLC_Exception e(ErrorMess);
124  throw(e);
125  }
127 
128 }
129 
130 // Overload "=" operator
132 {
133  if (!(*this == texture))
134  {
136  m_pQGLContext= texture.m_pQGLContext;
137  m_FileName= texture.m_FileName;
138  m_GlTextureID= texture.m_GlTextureID;
141  m_TextureSize= texture.m_TextureSize;
142  m_HasAlphaChannel= m_textureImage.hasAlphaChannel();
143  }
144 
145  return *this;
146 }
147 
149 {
150  //qDebug() << "GLC_Texture::~GLC_Texture Texture ID : " << m_GlTextureID;
152 }
154 // Get Functions
156 
157 // Return true if texture are the same
158 bool GLC_Texture::operator==(const GLC_Texture& texture) const
159 {
160  bool result;
161  if (this == &texture)
162  {
163  result= true;
164  }
165  else
166  {
167  result= (m_FileName == texture.m_FileName) && (m_textureImage == texture.m_textureImage);
168  }
169  return result;
170 }
171 
173 // Set Functions
175 
176 // Set the maximum texture size
177 void GLC_Texture::setMaxTextureSize(const QSize& size)
178 {
179  if ((size.height() > m_MinTextureSize.height()) && (size.width() > m_MinTextureSize.width()))
180  {
182  }
183  else
184  {
185  qDebug() << "GLC_Texture::setMaxTextureSize m_MaxTextureSize set to m_MinTextureSize";
187  }
188 }
189 
191 // Private OpenGL functions
193 // Load the texture
194 void GLC_Texture::glLoadTexture(QGLContext* pContext)
195 {
196  if (m_GlTextureID == 0)
197  {
198  if (NULL == pContext)
199  {
200  m_pQGLContext= const_cast<QGLContext*>(QGLContext::currentContext());
201  }
202  else
203  {
204  m_pQGLContext= pContext;
205  }
206 
207  // Test image size
208  if ((m_textureImage.height() > m_MaxTextureSize.height())
209  || (m_textureImage.width() > m_MaxTextureSize.width()))
210  {
211  QImage rescaledImage;
212  if(m_textureImage.height() > m_textureImage.width())
213  {
214  rescaledImage= m_textureImage.scaledToHeight(m_MaxTextureSize.height(), Qt::SmoothTransformation);
215  }
216  else
217  {
218  rescaledImage= m_textureImage.scaledToWidth(m_MaxTextureSize.width(), Qt::SmoothTransformation);
219  }
220  m_textureImage= rescaledImage;
222  }
223  else
224  {
226  }
229 
230  //qDebug() << "GLC_Texture::glcBindTexture Texture ID = " << m_GlTextureID;
231  }
232 }
233 
234 // Bind texture in 2D mode
236 {
237  if (m_GlTextureID == 0)
238  {
239  glLoadTexture();
240  }
241  glBindTexture(GL_TEXTURE_2D, m_GlTextureID);
242 }
243 
244 QImage GLC_Texture::loadFromFile(const QString& fileName)
245 {
246  QImage resultImage;
247  if (glc::isArchiveString(fileName))
248  {
249 
250  // Load the image from a zip archive
251  QuaZip* p3dxmlArchive= new QuaZip(glc::archiveFileName(fileName));
252  // Trying to load archive
253  if(!p3dxmlArchive->open(QuaZip::mdUnzip))
254  {
255  delete p3dxmlArchive;
256  return QImage();
257  }
258  else
259  {
260  // Set the file Name Codec
261  p3dxmlArchive->setFileNameCodec("IBM866");
262  }
263  QString imageFileName= glc::archiveEntryFileName(fileName);
264 
265  // Create QuaZip File
266  QuaZipFile* p3dxmlFile= new QuaZipFile(p3dxmlArchive);
267 
268  // Get the file of the 3dxml
269  if (!p3dxmlArchive->setCurrentFile(imageFileName, QuaZip::csInsensitive))
270  {
271  delete p3dxmlFile;
272  delete p3dxmlArchive;
273  return QImage();
274  }
275 
276  // Open the file of the 3dxml
277  if(!p3dxmlFile->open(QIODevice::ReadOnly))
278  {
279  delete p3dxmlFile;
280  delete p3dxmlArchive;
281  return QImage();
282  }
283  resultImage.load(p3dxmlFile, QFileInfo(imageFileName).suffix().toLocal8Bit());
284  p3dxmlFile->close();
285  delete p3dxmlFile;
286  delete p3dxmlArchive;
287  }
288  else
289  {
290  resultImage.load(fileName);
291  }
292 
293  return resultImage;
294 }
295 
297 {
298  if ( 0 != m_GlTextureID)
299  {
300  Q_ASSERT(m_TextureIdUsage.contains(m_GlTextureID));
302  if (m_TextureIdUsage.value(m_GlTextureID) == 0)
303  {
304  m_pQGLContext->deleteTexture(m_GlTextureID);
305  m_TextureIdUsage.remove(m_GlTextureID);
306  m_GlTextureID= 0;
307  }
308  }
309 }
310 
312 {
313  if (0 != m_GlTextureID)
314  {
315  if (m_TextureIdUsage.contains(m_GlTextureID))
316  {
318  }
319  else
320  {
321  m_TextureIdUsage.insert(m_GlTextureID, 1);
322  }
323  }
324 }
325 
326 // Non-member stream operator
327 QDataStream &operator<<(QDataStream &stream, const GLC_Texture &texture)
328 {
329  stream << texture.fileName();
330 
331  return stream;
332 }
333 QDataStream &operator>>(QDataStream &stream, GLC_Texture &texture)
334 {
335  QString fileName;
336  stream >> fileName;
337  texture= GLC_Texture(fileName);
338 
339  return stream;
340 }
341 

©2005-2013 Laurent Ribon