Work on texture class
+ Set up TextureManager class to handle static texture map
This commit is contained in:
parent
ebb597dae6
commit
d7fe2c0fc2
|
@ -8,9 +8,9 @@
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QSurfaceFormat>
|
||||||
|
|
||||||
#include <mainwindow.h>
|
#include <mainwindow.h>
|
||||||
#include <QSurfaceFormat>
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
|
|
@ -24,10 +24,9 @@ public:
|
||||||
* Should be given in qrc format: ':/path/to/asset.obj'
|
* Should be given in qrc format: ':/path/to/asset.obj'
|
||||||
* @return Absoulte system path to a qtk asset
|
* @return Absoulte system path to a qtk asset
|
||||||
*/
|
*/
|
||||||
static std::string getPath(const std::string & path) {
|
static std::string getPath(const std::string & path)
|
||||||
// Only construct qtk resource path if in qrc format; else return it as-is
|
// Only construct qtk resource path if in qrc format; else return it as-is
|
||||||
return path[0] == ':' ? resourcesDir + path.substr(2) : path;
|
{ return path[0] == ':' ? resourcesDir + path.substr(2) : path; }
|
||||||
}
|
|
||||||
|
|
||||||
static std::string resourcesDir;
|
static std::string resourcesDir;
|
||||||
} RM;
|
} RM;
|
||||||
|
|
|
@ -515,6 +515,9 @@ void Scene::init()
|
||||||
mMeshes.back()->mProgram.bind();
|
mMeshes.back()->mProgram.bind();
|
||||||
|
|
||||||
mMeshes.back()->setTexture(":/crate.png");
|
mMeshes.back()->setTexture(":/crate.png");
|
||||||
|
|
||||||
|
// Test assignment of Texture
|
||||||
|
mMeshes.back()->mTexture = Texture(":/crate.png");
|
||||||
mMeshes.back()->setUniform("uTexture", 0);
|
mMeshes.back()->setUniform("uTexture", 0);
|
||||||
|
|
||||||
mMeshes.back()->mVAO.bind();
|
mMeshes.back()->mVAO.bind();
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
#include <texture.h>
|
#include <texture.h>
|
||||||
|
|
||||||
|
std::unordered_map<std::string, Texture *> TM::textureMap;
|
||||||
|
|
||||||
QImage * OpenGLTextureFactory::initImage(const char * image, bool flipX, bool flipY)
|
QImage * OpenGLTextureFactory::initImage(const char * image, bool flipX, bool flipY)
|
||||||
{
|
{
|
||||||
// Qt6 limits loaded images to 256MB by default
|
// Qt6 limits loaded images to 256MB by default
|
||||||
|
|
|
@ -38,23 +38,47 @@ private:
|
||||||
OpenGLTextureFactory() = default;
|
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 {
|
class Texture {
|
||||||
|
// explicit Texture(QOpenGLTexture * texture) : mOpenGLTexture(texture) { }
|
||||||
public:
|
public:
|
||||||
Texture() = default;
|
Texture() = default;
|
||||||
Texture(const Texture & value) = delete;
|
Texture(const Texture & value) {
|
||||||
// Texture(const Texture & value) {
|
if (TM::getInstance(value.mPath)) {
|
||||||
// mOpenGLTexture = OpenGLTextureFactory::initTexture2D(value.mPath);
|
mOpenGLTexture = TM::getInstance(value.mPath)->mOpenGLTexture;
|
||||||
// mPath = value.mPath;
|
}
|
||||||
// }
|
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) :
|
explicit Texture(const char * path, bool flipX=false, bool flipY=false) :
|
||||||
mOpenGLTexture(OpenGLTextureFactory::initTexture2D(path, flipX, flipY)),
|
mPath(path) {
|
||||||
mPath(path) { }
|
if (TM::getInstance(path) == Q_NULLPTR) {
|
||||||
// explicit Texture(QOpenGLTexture * texture) : mOpenGLTexture(texture) { }
|
mOpenGLTexture = OpenGLTextureFactory::initTexture2D(path, flipX, flipY);
|
||||||
~Texture() { mOpenGLTexture->destroy();}
|
TM::addInstance(path, this);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mOpenGLTexture = TM::getInstance(path)->mOpenGLTexture;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ~Texture() { if (mOpenGLTexture) mOpenGLTexture->destroy();}
|
||||||
|
|
||||||
inline QOpenGLTexture & getOpenGLTexture() { return *mOpenGLTexture;}
|
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);}
|
{ mOpenGLTexture = OpenGLTextureFactory::initTexture2D(path, flipX, flipY);}
|
||||||
inline void setTexture(QOpenGLTexture * texture) { mOpenGLTexture = texture;}
|
inline void setTexture(QOpenGLTexture * texture) { mOpenGLTexture = texture;}
|
||||||
|
|
||||||
|
@ -64,5 +88,4 @@ public:
|
||||||
const char * mPath;
|
const char * mPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif // QTOPENGL_TEXTURE_H
|
#endif // QTOPENGL_TEXTURE_H
|
||||||
|
|
Loading…
Reference in New Issue