FileInterface.h 2.02 KB
Newer Older
Anakin's avatar
Anakin committed
1 2 3 4 5 6 7 8
#pragma once
#include <fstream>
#include <QVector>
#include <QVector2D>
#include <QVector3D>
#include <QMatrix4x4>
#include <QQuaternion>
#include <QOpenGLFunctions>
9 10
#include <QObject>
#include <..\Header\MainWindow.h>
Anakin's avatar
Anakin committed
11 12 13 14 15 16 17 18


struct BoundingBox {
	QQuaternion rotation;
	QVector3D center;
	QVector3D extents;
};

19 20 21 22 23 24
struct VertexData
{
	QVector3D position;
	QVector2D texCoord;
};

Anakin's avatar
Anakin committed
25 26 27 28 29 30 31 32 33 34
struct Segment {
	std::uint32_t textureIndex = 0;
	QVector<VertexData> vertices;
	QVector<GLuint> indices;
};

struct Model {
	std::string name = "";
	std::string parent = "";
	QMatrix4x4 m4x4Translation;
Anakin's avatar
Anakin committed
35
	QQuaternion quadRotation;
Anakin's avatar
Anakin committed
36 37 38
	std::vector<Segment*> segmList;
};

39
class FileInterface : public QObject
Anakin's avatar
Anakin committed
40
{
41 42
	Q_OBJECT

Anakin's avatar
Anakin committed
43
public:
44 45 46
	explicit FileInterface(const char* path, QObject *parent)
		: QObject(parent)
		, m_models(new QVector<Model*>)
Anakin's avatar
Anakin committed
47 48 49 50 51 52
		, m_textureNames(new QVector<std::string>)
	{
		//open file
		m_file.open(path, std::ios::in | std::ios::binary);

		if (!m_file.is_open())
53 54 55 56 57 58 59
			throw std::invalid_argument(std::string("ERROR: file not found: ") += path);

		MainWindow* tmp = dynamic_cast<MainWindow*>(parent->parent()->parent());
		if(tmp != NULL)
			connect(this, SIGNAL(sendMessage(QString, int)), tmp, SLOT(showMessage(QString, int)));


Anakin's avatar
Anakin committed
60 61 62 63 64 65
	};

	virtual ~FileInterface()
	{
		// close file
		m_file.close();
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

		//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;
Anakin's avatar
Anakin committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
	};

protected:
	QVector<Model*>* m_models;
	std::fstream m_file;
	QVector<std::string>* m_textureNames;
	BoundingBox m_sceneBbox;

	virtual void import() = 0;

public:
	virtual QVector<Model*>* getModels() const { return m_models; };
	virtual QVector<std::string>* getTextureNames() const { return m_textureNames; };
	virtual BoundingBox getBoundingBox() const { return m_sceneBbox; };
99 100 101

signals:
	void sendMessage(QString msg, int severity);
Anakin's avatar
Anakin committed
102
};