Work on Texture class

This commit is contained in:
Shaun Reed 2022-07-17 17:20:39 -04:00
parent ab60994203
commit ebb597dae6
6 changed files with 49 additions and 42 deletions

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

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

View File

@ -407,12 +407,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>
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

View File

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

View File

@ -11,10 +11,9 @@
#include <QOpenGLTexture>
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