diff --git a/src/meshrenderer.cpp b/src/meshrenderer.cpp index 7d2270b..fbd5f92 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 d51618f..1d9ec49 100644 --- a/src/meshrenderer.h +++ b/src/meshrenderer.h @@ -56,11 +56,6 @@ public: 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; diff --git a/src/model.cpp b/src/model.cpp index eb0dd41..d8fb37c 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 e344f62..07738cb 100644 --- a/src/object.h +++ b/src/object.h @@ -14,6 +14,7 @@ #include #include +#include class Object : public QObject { @@ -32,15 +33,12 @@ public: ~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 Texture & getTexture() { return mTexture;} inline const Vertices & getVertices() { return mShape.mVertices;} virtual inline void setColors(const Colors & value) { mShape.mColors = value;} @@ -48,8 +46,10 @@ public: 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 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;} - // To set mTexture use the accessor and QOpenGLTexture API QOpenGLBuffer mVBO, mNBO; QOpenGLVertexArrayObject mVAO; @@ -57,10 +57,9 @@ public: Transform3D mTransform; Shape mShape; + Texture mTexture; const char * mName; -private: - QOpenGLTexture * mTexture; }; #endif // QTK_OBJECT_H diff --git a/src/scene.cpp b/src/scene.cpp index 2a99731..a83974a 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -514,7 +514,7 @@ void Scene::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(); @@ -540,7 +540,8 @@ void Scene::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/texture.h b/src/texture.h index 6e27522..d2f2f82 100644 --- a/src/texture.h +++ b/src/texture.h @@ -11,10 +11,9 @@ #include - class OpenGLTextureFactory { public: - ~OpenGLTextureFactory() {} + ~OpenGLTextureFactory() = default; // QImage static QImage * initImage(const char * image, @@ -36,7 +35,34 @@ public: private: // Private ctor to prevent creating instances of this class - OpenGLTextureFactory() {} + 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