Work on texture class

+ Set up TextureManager class to handle static texture map
This commit is contained in:
Shaun Reed 2022-07-31 21:59:30 -04:00
parent ebb597dae6
commit d7fe2c0fc2
5 changed files with 43 additions and 16 deletions

View File

@ -8,9 +8,9 @@
#include <QApplication>
#include <QLabel>
#include <QSurfaceFormat>
#include <mainwindow.h>
#include <QSurfaceFormat>
int main(int argc, char *argv[])

View File

@ -24,10 +24,9 @@ public:
* Should be given in qrc format: ':/path/to/asset.obj'
* @return Absoulte system path to a qtk asset
*/
static std::string getPath(const std::string & path) {
// Only construct qtk resource path if in qrc format; else return it as-is
return path[0] == ':' ? resourcesDir + path.substr(2) : path;
}
static std::string getPath(const std::string & path)
// Only construct qtk resource path if in qrc format; else return it as-is
{ return path[0] == ':' ? resourcesDir + path.substr(2) : path; }
static std::string resourcesDir;
} RM;

View File

@ -515,6 +515,9 @@ void Scene::init()
mMeshes.back()->mProgram.bind();
mMeshes.back()->setTexture(":/crate.png");
// Test assignment of Texture
mMeshes.back()->mTexture = Texture(":/crate.png");
mMeshes.back()->setUniform("uTexture", 0);
mMeshes.back()->mVAO.bind();

View File

@ -11,6 +11,8 @@
#include <texture.h>
std::unordered_map<std::string, Texture *> TM::textureMap;
QImage * OpenGLTextureFactory::initImage(const char * image, bool flipX, bool flipY)
{
// Qt6 limits loaded images to 256MB by default

View File

@ -38,23 +38,47 @@ private:
OpenGLTextureFactory() = default;
};
class Texture;
typedef class TextureManager {
public:
static inline Texture * getInstance(std::string path)
{ return textureMap.count(path) > 0 ? textureMap[path] : Q_NULLPTR; }
static inline bool addInstance(const std::string & path, Texture * texture)
{ return textureMap.emplace(path, texture).second ? true : false; }
private:
static std::unordered_map<std::string, Texture *> textureMap;
} TM;
class Texture {
// explicit Texture(QOpenGLTexture * texture) : mOpenGLTexture(texture) { }
public:
Texture() = default;
Texture(const Texture & value) = delete;
// Texture(const Texture & value) {
// mOpenGLTexture = OpenGLTextureFactory::initTexture2D(value.mPath);
// mPath = value.mPath;
// }
Texture(const Texture & value) {
if (TM::getInstance(value.mPath)) {
mOpenGLTexture = TM::getInstance(value.mPath)->mOpenGLTexture;
}
else {
mOpenGLTexture = OpenGLTextureFactory::initTexture2D(value.mPath);
TM::addInstance(value.mPath, this);
}
mPath = value.mPath;
}
explicit Texture(const char * path, bool flipX=false, bool flipY=false) :
mOpenGLTexture(OpenGLTextureFactory::initTexture2D(path, flipX, flipY)),
mPath(path) { }
// explicit Texture(QOpenGLTexture * texture) : mOpenGLTexture(texture) { }
~Texture() { mOpenGLTexture->destroy();}
mPath(path) {
if (TM::getInstance(path) == Q_NULLPTR) {
mOpenGLTexture = OpenGLTextureFactory::initTexture2D(path, flipX, flipY);
TM::addInstance(path, this);
}
else {
mOpenGLTexture = TM::getInstance(path)->mOpenGLTexture;
}
}
// ~Texture() { if (mOpenGLTexture) mOpenGLTexture->destroy();}
inline QOpenGLTexture & getOpenGLTexture() { return *mOpenGLTexture;}
void setTexture(const char * path, bool flipX=false, bool flipY=false)
inline void setTexture(const char * path, bool flipX=false, bool flipY=false)
{ mOpenGLTexture = OpenGLTextureFactory::initTexture2D(path, flipX, flipY);}
inline void setTexture(QOpenGLTexture * texture) { mOpenGLTexture = texture;}
@ -64,5 +88,4 @@ public:
const char * mPath;
};
#endif // QTOPENGL_TEXTURE_H