From 697cdd0e75daefcee3340043556a5d53d7956505 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Sun, 17 Jul 2022 17:20:39 -0400 Subject: [PATCH] Work on Texture class --- app/examplescene.cpp | 5 +++-- src/meshrenderer.cpp | 23 ++++------------------- src/meshrenderer.h | 11 +++-------- src/model.cpp | 13 +++++++------ src/object.h | 20 +++++++++----------- src/texture.h | 37 ++++++++++++++++++++++++++++++++----- 6 files changed, 58 insertions(+), 51 deletions(-) diff --git a/app/examplescene.cpp b/app/examplescene.cpp index c341886..785587e 100644 --- a/app/examplescene.cpp +++ b/app/examplescene.cpp @@ -516,7 +516,7 @@ void ExampleScene::init() mMeshes.back()->init(); mMeshes.back()->mProgram.bind(); - mMeshes.back()->setTexture(OpenGLTextureFactory::initTexture2D(":/crate.png")); + mMeshes.back()->setTexture(":/crate.png"); mMeshes.back()->setUniform("uTexture", 0); mMeshes.back()->mVAO.bind(); @@ -542,7 +542,8 @@ void ExampleScene::init() mMeshes.back()->init(); mMeshes.back()->mProgram.bind(); - mMeshes.back()->setTexture(OpenGLTextureFactory::initTexture2D(":/crate.png")); +// mMeshes.back()->setTexture(OpenGLTextureFactory::initTexture2D(":/crate.png")); + mMeshes.back()->mTexture.setTexture(":/crate.png"); mMeshes.back()->setUniform("uTexture", 0); mMeshes.back()->mVAO.bind(); diff --git a/src/meshrenderer.cpp b/src/meshrenderer.cpp index dd9a728..722e1a2 100644 --- a/src/meshrenderer.cpp +++ b/src/meshrenderer.cpp @@ -29,9 +29,6 @@ MeshRenderer::MeshRenderer(const char * name, const ShapeBase & shape) MeshRenderer::~MeshRenderer() { - if (mHasTexture) { - mTexture->destroy(); - } sInstances.remove(mName); } @@ -96,8 +93,8 @@ void MeshRenderer::draw() mProgram.bind(); mVAO.bind(); - if(mHasTexture) { - mTexture->bind(); + if(mTexture.hasTexture()) { + mTexture.getOpenGLTexture().bind(); } // TODO: Automate uniforms some other way @@ -112,8 +109,8 @@ void MeshRenderer::draw() GL_UNSIGNED_INT, mShape.mIndices.data()); } - if(mHasTexture) { - mTexture->release(); + if(mTexture.hasTexture()) { + mTexture.getOpenGLTexture().release(); } mVAO.release(); @@ -148,18 +145,6 @@ void MeshRenderer::setColor(const QVector3D & color) } } -void MeshRenderer::setTexture(const char * path) -{ - mTexture = new QOpenGLTexture(*OpenGLTextureFactory::initImage(path)); - mHasTexture = true; -} - -void MeshRenderer::setTexture(QOpenGLTexture * texture) -{ - mTexture = texture; - mHasTexture = true; -} - /******************************************************************************* * Inherited Virtual Member Functions diff --git a/src/meshrenderer.h b/src/meshrenderer.h index ae84420..d0ebaa6 100644 --- a/src/meshrenderer.h +++ b/src/meshrenderer.h @@ -58,15 +58,10 @@ namespace Qtk { void setUniformMVP(const char * model="uModel", const char * view="uView", const char * projection="uProjection"); - // Sets the texture to the image at the given path - // + Sets mHasTexture to enable texture binding in draw() - void setTexture(const char * path); - void setTexture(QOpenGLTexture * texture); - // These functions modify data stored in a VBO - // + After calling them, the VBO will need to be reallocated - void setShape(const Shape & value) override; - void setColor(const QVector3D & color); + // + After calling them, the VBO will need to be reallocated + void setShape(const Shape & value) override; + void setColor(const QVector3D & color); // Static QHash of all mesh objects within the scene typedef QHash MeshManager; diff --git a/src/model.cpp b/src/model.cpp index 385b0d6..9caf82a 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -409,12 +409,13 @@ ModelMesh::Textures Model::loadMaterialTextures( void Model::sortModels() { auto cameraPos = Scene::Camera().transform(); - auto cameraDistance = [&cameraPos](const ModelMesh &a, const ModelMesh &b) - { - // Sort by the first vertex position, since all transforms will be the same - return (cameraPos.getTranslation().distanceToPoint(a.mVertices[0].mPosition)) - < (cameraPos.getTranslation().distanceToPoint(b.mVertices[0].mPosition)); - }; + auto cameraDistance = + [&cameraPos](const ModelMesh &a, const ModelMesh &b) + { + // Sort by the first vertex position in the model + return (cameraPos.getTranslation().distanceToPoint(a.mVertices[0].mPosition)) + < (cameraPos.getTranslation().distanceToPoint(b.mVertices[0].mPosition)); + }; std::sort(mMeshes.begin(), mMeshes.end(), cameraDistance); } diff --git a/src/object.h b/src/object.h index 599784e..f686ebf 100644 --- a/src/object.h +++ b/src/object.h @@ -14,6 +14,7 @@ #include #include +#include #include namespace Qtk { @@ -33,24 +34,23 @@ namespace Qtk { ~Object() {} - // Look to MeshRenderer for wrapped texture functionality - // + Object only exposes a QOpenGLTexture object with no wrapped features - inline QOpenGLTexture & texture() const { return *mTexture;} - // These custom types are wrapped for Qtk inline const Colors & getColors() { return mShape.mColors;} inline const Indices & getIndexData() { return mShape.mIndices;} inline const Normals & getNormals() { return mShape.mNormals;} inline const Shape & getShape() { return mShape;} inline const TexCoords & getTexCoords() { return mShape.mTexCoords;} - inline const Vertices & getVertices() { return mShape.mVertices;} + inline Texture & getTexture() { return mTexture;} + inline const Vertices & getVertices() { return mShape.mVertices;} virtual inline void setColors(const Colors & value) { mShape.mColors = value;} virtual inline void setIndices(const Indices & value) { mShape.mIndices = value;} virtual inline void setNormals(const Normals & value) { mShape.mNormals = value;} virtual inline void setShape(const Shape & value) { mShape = value;} virtual inline void setTexCoords(const TexCoords & value) { mShape.mTexCoords = value;} - virtual inline void setVertices(const Vertices & value) { mShape.mVertices = value;} - // To set mTexture use the accessor and QOpenGLTexture API + virtual inline void setTexture(const char * path, bool flipX=false, bool flipY=false) + { mTexture.setTexture(OpenGLTextureFactory::initTexture2D(path, flipX, flipY));} + virtual inline void setTexture(QOpenGLTexture * value) { mTexture.setTexture(value);} + virtual inline void setVertices(const Vertices & value) { mShape.mVertices = value;} QOpenGLBuffer mVBO, mNBO; QOpenGLVertexArrayObject mVAO; @@ -58,11 +58,9 @@ namespace Qtk { Transform3D mTransform; Shape mShape; + Texture mTexture; const char * mName; - private: - QOpenGLTexture * mTexture; - }; -} + };} #endif // QTK_OBJECT_H diff --git a/src/texture.h b/src/texture.h index 012fc0a..6fef1dc 100644 --- a/src/texture.h +++ b/src/texture.h @@ -16,7 +16,7 @@ namespace Qtk { class QTKAPI OpenGLTextureFactory { public: - ~OpenGLTextureFactory() {} + ~OpenGLTextureFactory() = default; // QImage static QImage * initImage(const char * image, @@ -36,10 +36,37 @@ namespace Qtk { const char * front, const char * left, const char * bottom, const char * back); - private: - // Private ctor to prevent creating instances of this class - OpenGLTextureFactory() {} - }; +private: + // Private ctor to prevent creating instances of this class + OpenGLTextureFactory() = default; +}; + +class Texture { +public: + Texture() = default; + Texture(const Texture & value) = delete; +// Texture(const Texture & value) { +// mOpenGLTexture = OpenGLTextureFactory::initTexture2D(value.mPath); +// 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();} + + inline QOpenGLTexture & getOpenGLTexture() { return *mOpenGLTexture;} + + void setTexture(const char * path, bool flipX=false, bool flipY=false) + { mOpenGLTexture = OpenGLTextureFactory::initTexture2D(path, flipX, flipY);} + inline void setTexture(QOpenGLTexture * texture) { mOpenGLTexture = texture;} + + inline bool hasTexture() const { return mOpenGLTexture != Q_NULLPTR;} + + QOpenGLTexture * mOpenGLTexture = Q_NULLPTR; + const char * mPath; +}; + } #endif // QTOPENGL_TEXTURE_H