Work on Texture class

This commit is contained in:
Shaun Reed 2022-07-17 17:20:39 -04:00
parent 99b4f9355b
commit 697cdd0e75
6 changed files with 58 additions and 51 deletions

View File

@ -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();

View File

@ -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

View File

@ -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<QString, MeshRenderer *> MeshManager;

View File

@ -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);
}

View File

@ -14,6 +14,7 @@
#include <QOpenGLVertexArrayObject>
#include <mesh.h>
#include <texture.h>
#include <qtkapi.h>
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

View File

@ -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