qtk/src/texture.h

242 lines
8.8 KiB
C
Raw Normal View History

2021-09-03 16:56:57 +00:00
/*##############################################################################
## Author: Shaun Reed ##
2022-03-06 16:54:05 +00:00
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
2021-09-03 16:56:57 +00:00
## About: Texture class to help with texture and image initializations ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#ifndef QTOPENGL_TEXTURE_H
#define QTOPENGL_TEXTURE_H
2022-11-25 19:36:12 +00:00
#include <QOpenGLShaderProgram>
2021-09-03 16:56:57 +00:00
#include <QOpenGLTexture>
2022-11-24 22:26:53 +00:00
#include <utility>
2021-09-03 16:56:57 +00:00
#include <qtkapi.h>
namespace Qtk {
2022-11-25 19:36:12 +00:00
/**
* Binds shader programs until the end of scope.
* Does nothing if the shader program was already bound.
*
* See MeshRenderer::setUniform() for example.
*/
class QTKAPI ShaderBindScope {
public:
/*************************************************************************
* Constructors / Destructors
************************************************************************/
explicit ShaderBindScope(
QOpenGLShaderProgram * program, bool was_locked) :
mWasBound(was_locked) {
mProgram = program;
if(!mWasBound) {
mProgram->bind();
}
}
~ShaderBindScope() {
if(!mWasBound) {
mProgram->release();
}
}
private:
/*************************************************************************
* Private Members
************************************************************************/
QOpenGLShaderProgram * mProgram;
bool mWasBound;
};
/**
* Factories for initializing various OpenGL textures
*/
2022-11-24 22:26:53 +00:00
class QTKAPI OpenGLTextureFactory {
public:
2022-11-25 19:36:12 +00:00
/*************************************************************************
* Constructors / Destructors
************************************************************************/
2022-11-24 22:26:53 +00:00
~OpenGLTextureFactory() = default;
2022-11-25 19:36:12 +00:00
/*************************************************************************
* Texture Factories
************************************************************************/
/**
* QImage factory
*
* @param image Path to image we want to load.
* Can be absolute or Qt resource path.
* @param flipX If true the image will be flipped on X axis.
* @param flipY If true the image will be flipped on Y axis.
* @return Pointer to an initialized QImage object.
*/
2022-11-24 22:26:53 +00:00
static QImage * initImage(
const char * image, bool flipX = false, bool flipY = false);
2022-11-25 19:36:12 +00:00
/**
* QOpenGLTexture factory
*
* @param texture Path to texture we want to load.
* Can be absolute or Qt resource path.
* @param flipX If true the image will be flipped on X axis.
* @param flipY If true the image will be flipped on Y axis.
* @return Pointer to an initialized QOpenGLTexture object.
*/
static QOpenGLTexture * initTexture(
2022-11-24 22:26:53 +00:00
const char * texture, bool flipX = false, bool flipY = false);
2022-11-25 19:36:12 +00:00
/**
* Cube map factory for initializing all sides of a CubeMap.
* All of these parameters can be absolute or Qt resource paths.
*
* @param right Path to image for the right side of the CubeMap.
* @param top Path to image for the top side of the CubeMap.
* @param front Path to image for the front side of the CubeMap.
* @param left Path to image for the left side of the CubeMap.
* @param bottom Path to image for the bottom side of the CubeMap.
* @param back Path to image for the back side of the CubeMap.
* @return Pointer to an initialized QOpenGLTexture object.
*/
2022-11-24 22:26:53 +00:00
static QOpenGLTexture * initCubeMap(
const QImage & right, const QImage & top, const QImage & front,
const QImage & left, const QImage & bottom, const QImage & back);
2022-11-25 19:36:12 +00:00
/**
* CubeMap factory for tiling the same image on all sides.
*
* @param tile Path to the image we want to make into a CubeMap.
* Can be absolute or Qt resource path.
* @return Pointer to an initialized QOpenGLTexture object.
*/
2022-11-24 22:26:53 +00:00
static QOpenGLTexture * initCubeMap(const char * tile);
2022-11-25 19:36:12 +00:00
/**
* Cube map factory for initializing all sides of a CubeMap.
* All of these parameters can be absolute or Qt resource paths.
*
* @param right Path to image for the right side of the CubeMap.
* @param top Path to image for the top side of the CubeMap.
* @param front Path to image for the front side of the CubeMap.
* @param left Path to image for the left side of the CubeMap.
* @param bottom Path to image for the bottom side of the CubeMap.
* @param back Path to image for the back side of the CubeMap.
* @return Pointer to an initialized QOpenGLTexture object.
*/
2022-11-24 22:26:53 +00:00
static QOpenGLTexture * initCubeMap(
const char * right, const char * top, const char * front,
const char * left, const char * bottom, const char * back);
private:
// Private ctor to prevent creating instances of this class
OpenGLTextureFactory() = default;
};
2022-11-25 19:36:12 +00:00
/**
* Texture object component class
*
* TODO: Struct for (re)storing texture state
* A struct to store flipX, flipY and other initial state needed to copy
*/
2022-11-24 22:26:53 +00:00
class Texture {
public:
2022-11-25 19:36:12 +00:00
/*************************************************************************
* Typedefs
************************************************************************/
friend class Skybox;
/*************************************************************************
* Constructors / Destructors
************************************************************************/
2022-11-24 22:26:53 +00:00
Texture() = default;
2022-11-25 19:36:12 +00:00
2022-11-24 22:26:53 +00:00
Texture(const Texture & value) {
2022-11-25 19:36:12 +00:00
mOpenGLTexture = OpenGLTextureFactory::initTexture(value.mPath);
2022-11-24 22:26:53 +00:00
mPath = value.mPath;
}
2022-11-25 19:36:12 +00:00
2022-11-24 22:26:53 +00:00
explicit Texture(
const char * path, bool flipX = false, bool flipY = false) :
2022-11-25 19:36:12 +00:00
mOpenGLTexture(OpenGLTextureFactory::initTexture(path, flipX, flipY)),
2022-11-24 22:26:53 +00:00
mPath(path) {}
2022-11-25 19:36:12 +00:00
2022-11-24 22:26:53 +00:00
explicit Texture(QOpenGLTexture * texture) : mOpenGLTexture(texture) {}
~Texture() { mOpenGLTexture->destroy(); }
2022-11-25 19:36:12 +00:00
/*************************************************************************
* Accessors
************************************************************************/
2022-11-24 22:26:53 +00:00
[[nodiscard]] inline QOpenGLTexture & getOpenGLTexture() const {
return *mOpenGLTexture;
}
[[nodiscard]] inline std::string getPath() const { return mPath; }
2022-11-25 19:36:12 +00:00
/*************************************************************************
* Setters
************************************************************************/
2022-11-24 22:26:53 +00:00
void setTexture(
const std::string & path, bool flipX = false, bool flipY = false) {
mOpenGLTexture =
2022-11-25 19:36:12 +00:00
OpenGLTextureFactory::initTexture(path.data(), flipX, flipY);
2022-11-24 22:26:53 +00:00
mPath = path.data();
}
void setTexture(
const char * path, bool flipX = false, bool flipY = false) {
2022-11-25 19:36:12 +00:00
mOpenGLTexture = OpenGLTextureFactory::initTexture(path, flipX, flipY);
2022-11-24 22:26:53 +00:00
mPath = path;
}
virtual inline void setCubeMap(const char * path) {
mOpenGLTexture = OpenGLTextureFactory::initCubeMap(path);
mPath = path;
}
virtual inline void setCubeMap(
const char * right, const char * top, const char * front,
const char * left, const char * bottom, const char * back) {
mOpenGLTexture = OpenGLTextureFactory::initCubeMap(
right, top, front, left, bottom, back);
}
virtual inline void setCubeMap(
const QImage & right, const QImage & top, const QImage & front,
const QImage & left, const QImage & bottom, const QImage & back) {
mOpenGLTexture = OpenGLTextureFactory::initCubeMap(
right, top, front, left, bottom, back);
}
2022-11-25 19:36:12 +00:00
/*************************************************************************
* Public Methods
************************************************************************/
2022-11-24 22:26:53 +00:00
[[nodiscard]] inline bool hasTexture() const {
return mOpenGLTexture != Q_NULLPTR;
}
private:
2022-11-25 19:36:12 +00:00
/*************************************************************************
* Private Members
************************************************************************/
inline void setTexture(QOpenGLTexture * texture) {
mOpenGLTexture = texture;
}
2022-11-24 22:26:53 +00:00
QOpenGLTexture * mOpenGLTexture = Q_NULLPTR;
2022-11-25 19:36:12 +00:00
/* Path to this texture on disk or Qt resource. */
2022-11-24 22:26:53 +00:00
const char * mPath {};
};
2021-09-03 16:56:57 +00:00
2022-11-24 22:26:53 +00:00
} // namespace Qtk
#endif // QTOPENGL_TEXTURE_H