SWBF2-Classic-Msh-Viewer/QtMeshViewer/Header/FileInterface.h

103 lines
2.0 KiB
C++

#pragma once
#include <fstream>
#include <QVector>
#include <QVector2D>
#include <QVector3D>
#include <QStringList>
#include <QMatrix4x4>
#include <QQuaternion>
#include <QOpenGLFunctions>
#include <QObject>
#include <..\Header\MainWindow.h>
struct BoundingBox {
QQuaternion rotation;
QVector3D center;
QVector3D extents;
};
struct VertexData
{
QVector3D position;
QVector2D texCoord;
};
struct Segment {
std::uint32_t textureIndex = 0;
QVector<VertexData> vertices;
QVector<GLuint> indices;
};
struct Model {
std::string name = "";
std::string parent = "";
QMatrix4x4 m4x4Translation;
QQuaternion quadRotation;
std::vector<Segment*> segmList;
};
class FileInterface : public QObject
{
Q_OBJECT
public:
explicit FileInterface(QString path, QObject *parent)
: QObject(parent)
, m_models(new QVector<Model*>)
, m_textureNames(new QStringList)
{
//open file
m_file.open(path.toStdString().c_str(), std::ios::in | std::ios::binary);
if (!m_file.is_open())
throw std::invalid_argument(std::string("ERROR: file not found: ") += path.toStdString());
MainWindow* tmp = dynamic_cast<MainWindow*>(parent->parent()->parent());
if(tmp != NULL)
connect(this, SIGNAL(sendMessage(QString, int)), tmp, SLOT(printMessage(QString, int)));
};
virtual ~FileInterface()
{
// close file
m_file.close();
//clean up
m_textureNames->clear();
delete m_textureNames;
for (Model* modelIt : *m_models)
{
for (Segment* segIt : modelIt->segmList)
{
segIt->indices.clear();
segIt->vertices.clear();
delete segIt;
}
modelIt->segmList.clear();
delete modelIt;
}
m_models->clear();
delete m_models;
};
protected:
QVector<Model*>* m_models;
std::fstream m_file;
QStringList* m_textureNames;
BoundingBox m_sceneBbox;
virtual void import() = 0;
public:
virtual QVector<Model*>* getModels() const { return m_models; };
virtual QStringList* getTextureNames() const { return m_textureNames; };
virtual BoundingBox getBoundingBox() const { return m_sceneBbox; };
signals:
void sendMessage(QString msg, int severity);
};