Code_TYMPAN  4.4.0
Industrial site acoustic simulation
OGLTextureManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
18 #include <cstddef>
19 #include <memory>
20 #include <qcolor.h>
21 #include <qdebug.h>
22 #include <qimage.h>
23 #include <qnamespace.h>
24 #include <qobject.h>
25 #include <qoffscreensurface.h>
26 #include <qopenglcontext.h>
27 #include <qopengltexture.h>
28 #include <qopenglversionfunctions.h>
29 #include <qsurface.h>
30 #include <qsurfaceformat.h>
31 
32 QMap<QOpenGLContext*, OGLTextureManager*> OGLTextureManager::instances;
33 
35 {
36  if (!instances.contains(context))
37  {
38  QObject::connect(context, &QOpenGLContext::destroyed, [=]() { instances.remove(context); });
39  instances[context] = new OGLTextureManager(context);
40  }
41 
42  return instances[context];
43 }
44 
45 OGLTextureManager::OGLTextureManager(QOpenGLContext* sourceContext)
46  : _fallbackTexture(QOpenGLTexture::Target2D), _whiteTexture(QOpenGLTexture::Target2D)
47 {
49 }
50 
52 {
53  QImage fallbackImage(QSize(404, 404), QImage::Format_RGBA8888);
54  fallbackImage.fill(QColor(255, 0, 255));
55 
56  // Pink fallback texture
57  _fallbackTexture.setMinificationFilter(QOpenGLTexture::Nearest);
58  _fallbackTexture.setMagnificationFilter(QOpenGLTexture::Nearest);
59  _fallbackTexture.setWrapMode(QOpenGLTexture::Repeat);
60  _fallbackTexture.setData(fallbackImage, QOpenGLTexture::GenerateMipMaps);
61 
62  QImage whiteImage(QSize(1, 1), QImage::Format_RGBA8888);
63  whiteImage.fill(Qt::white);
64 
65  // White texture
66  _whiteTexture.setMinificationFilter(QOpenGLTexture::Nearest);
67  _whiteTexture.setMagnificationFilter(QOpenGLTexture::Nearest);
68  _whiteTexture.setWrapMode(QOpenGLTexture::Repeat);
69  _whiteTexture.setData(whiteImage, QOpenGLTexture::GenerateMipMaps);
70 }
71 
73 {
74  return &_whiteTexture;
75 }
76 
78 {
79  return &_fallbackTexture;
80 }
81 
82 QOpenGLTexture* OGLTextureManager::getTexture(const std::shared_ptr<QImage>& image)
83 {
84  if (image->isNull())
85  {
86  return &_fallbackTexture;
87  }
88  std::weak_ptr<QImage> key(image);
89  auto textureIterator = imageTextures.find(key);
90  if (textureIterator == imageTextures.end())
91  {
92  auto textureIt =
93  imageTextures.emplace(key, std::make_unique<QOpenGLTexture>(QOpenGLTexture::Target2D)).first;
94 
95  QOpenGLTexture* glTexture = textureIt->second.get();
96  glTexture->setMinificationFilter(QOpenGLTexture::Nearest);
97  glTexture->setMagnificationFilter(QOpenGLTexture::Nearest);
98  glTexture->setWrapMode(QOpenGLTexture::Repeat);
99  glTexture->setData(*image, QOpenGLTexture::GenerateMipMaps);
100  return glTexture;
101  }
102  return &*textureIterator->second;
103 }
Tables pour le dialogue de la gestion des preferences (fichier header)
Class allowing to handle textures lifetime.
static QMap< QOpenGLContext *, OGLTextureManager * > instances
QOpenGLTexture * fallbackTexture()
OGLTextureManager(QOpenGLContext *context)
QOpenGLTexture _fallbackTexture
QOpenGLTexture _whiteTexture
static OGLTextureManager * instance(QOpenGLContext *context)
std::map< std::weak_ptr< QImage >, std::unique_ptr< QOpenGLTexture >, std::owner_less<> > imageTextures
QOpenGLTexture * getTexture(const std::shared_ptr< QImage > &image)
QOpenGLTexture * whiteTexture()