qtk/src/model.h

142 lines
4.1 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: Model classes for importing with Assimp ##
## From following tutorials on learnopengl.com ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#ifndef QTK_MODEL_H
#define QTK_MODEL_H
// QT
#include <QObject>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QOpenGLVertexArrayObject>
// Assimp
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
// QTK
#include <qtkapi.h>
2021-09-03 16:56:57 +00:00
#include <transform3D.h>
namespace Qtk {
struct QTKAPI ModelVertex {
QVector3D mPosition;
QVector3D mNormal;
QVector3D mTangent;
QVector3D mBitangent;
QVector2D mTextureCoord;
};
struct QTKAPI ModelTexture {
GLuint mID;
QOpenGLTexture * mTexture;
std::string mType;
std::string mPath;
};
class Model;
class QTKAPI ModelMesh : protected QOpenGLFunctions {
public:
friend Model;
typedef std::vector<ModelVertex> Vertices;
typedef std::vector<GLuint> Indices;
typedef std::vector<ModelTexture> Textures;
// Constructors, Destructors
ModelMesh(Vertices vertices, Indices indices, Textures textures,
const char * vertexShader=":/model-basic.vert",
const char * fragmentShader=":/model-basic.frag")
: mProgram(new QOpenGLShaderProgram),
2021-09-03 16:56:57 +00:00
mVAO(new QOpenGLVertexArrayObject),
mVBO(new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer)),
mEBO(new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer)),
mVertices(std::move(vertices)),
mIndices(std::move(indices)),
mTextures(std::move(textures))
{ initMesh(vertexShader, fragmentShader);}
~ModelMesh() {}
2021-09-03 16:56:57 +00:00
private:
void initMesh(const char * vert, const char * frag);
2021-09-03 16:56:57 +00:00
// ModelMesh Private Members
QOpenGLBuffer * mVBO, * mEBO;
QOpenGLVertexArrayObject * mVAO;
QOpenGLShaderProgram * mProgram;
2021-09-03 16:56:57 +00:00
public:
inline void draw() { draw(*mProgram);}
void draw(QOpenGLShaderProgram & shader);
2021-09-03 16:56:57 +00:00
// ModelMesh Public Members
Vertices mVertices;
Indices mIndices;
Textures mTextures;
Transform3D mTransform;
};
2021-09-03 16:56:57 +00:00
class QTKAPI Model : public QObject {
Q_OBJECT
2021-09-03 16:56:57 +00:00
public:
inline Model(const char * name, const char * path,
const char * vertexShader=":/model-basic.vert",
const char * fragmentShader=":/model-basic.frag")
: mName(name), mVertexShader(vertexShader),
2021-09-03 16:56:57 +00:00
mFragmentShader(fragmentShader)
{ loadModel(path);}
inline ~Model() { mManager.remove(mName);}
2021-09-03 16:56:57 +00:00
void draw();
void draw(QOpenGLShaderProgram & shader);
2021-09-03 16:56:57 +00:00
void flipTexture(const std::string & fileName,
bool flipX=false, bool flipY=true);
2021-09-03 16:56:57 +00:00
template <typename T>
void setUniform(const char * location, T value)
{
for (auto & mesh : mMeshes) {
mesh.mProgram->bind();
2021-09-03 16:56:57 +00:00
mesh.mProgram->setUniformValue(location, value);
2021-09-03 16:56:57 +00:00
mesh.mProgram->release();
}
2021-09-03 16:56:57 +00:00
}
Transform3D mTransform;
2021-09-03 16:56:57 +00:00
static Model * getInstance(const char * name);
2021-09-03 16:56:57 +00:00
typedef QHash<QString, Model *> ModelManager;
private:
static ModelManager mManager;
void loadModel(const std::string & path);
void processNode(aiNode * node, const aiScene * scene);
ModelMesh processMesh(aiMesh * mesh, const aiScene * scene);
ModelMesh::Textures loadMaterialTextures(aiMaterial * mat, aiTextureType type,
const std::string & typeName);
void sortModels();
2021-09-03 16:56:57 +00:00
// Model Private Members
2021-09-03 16:56:57 +00:00
ModelMesh::Textures mTexturesLoaded;
std::vector<ModelMesh> mMeshes;
std::string mDirectory;
const char * mVertexShader, * mFragmentShader, * mName;
};
}
2021-09-03 16:56:57 +00:00
#endif // QTK_MODEL_H