FileInterface.h 2.83 KB
Newer Older
Anakin's avatar
Anakin committed
1
#pragma once
2
#include <QOpenGlTexture>
Anakin's avatar
Anakin committed
3
#include <QFile>
Anakin's avatar
Anakin committed
4
5
6
7
#include <QVector>
#include <QVector2D>
#include <QMatrix4x4>
#include <QQuaternion>
8
#include <QRegExp>
Anakin's avatar
Anakin committed
9
10
11
12
13
14
15

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

16
17
18
19
struct VertexData
{
	QVector3D position;
	QVector2D texCoord;
20
	QVector3D vertexNormal;
21
22
};

Anakin's avatar
Anakin committed
23
struct Segment {
Anakin's avatar
Anakin committed
24
	quint32 textureIndex = 0;
Anakin's avatar
Anakin committed
25
26
27
28
29
	QVector<VertexData> vertices;
	QVector<GLuint> indices;
};

struct Model {
Anakin's avatar
Anakin committed
30
31
	QString name = "";
	QString parent = "";
Anakin's avatar
Anakin committed
32
	QMatrix4x4 m4x4Translation;
Anakin's avatar
Anakin committed
33
	QQuaternion quadRotation;
Anakin's avatar
Anakin committed
34
	QVector<Segment*> segmList;
Anakin's avatar
Anakin committed
35
36
};

37
38
struct Material {
	QString name;
39
40
41
42
43
44
	QString tx0d;
	QString tx1d;
	QString tx2d;
	QString tx3d;
	QOpenGLTexture* texture0 = Q_NULLPTR;
	QOpenGLTexture* texture1 = Q_NULLPTR;
Anakin's avatar
Anakin committed
45
	QVector4D specularColor = { 0.1f, 0.1f, 0.1f, 1.0 };
Anakin's avatar
Anakin committed
46
47
	QVector4D diffuseColor = { 1.0, 0.0, 0.0, 1.0 };
	QVector4D ambientColor = { 1.0, 1.0, 1.0, 1.0 };
Anakin's avatar
Anakin committed
48
	float shininess = 1;
Anakin's avatar
Anakin committed
49
	bool flags[8] = { false };
50
	bool transparent = false;
Anakin's avatar
Anakin committed
51
52
	quint8 rendertype = 0;
	quint8 dataValues[2] = { 0 };
53
54
};

55
class FileInterface
Anakin's avatar
Anakin committed
56
{
57

Anakin's avatar
Anakin committed
58
public:
59
60
	explicit FileInterface(QString path)
		: m_models(new QVector<Model*>)
61
		, m_materials(new QVector<Material>)
Anakin's avatar
Anakin committed
62
63
	{
		//open file
Anakin's avatar
Anakin committed
64
		m_file.setFileName(path);
Anakin's avatar
Anakin committed
65

Anakin's avatar
Anakin committed
66
		if (!m_file.open(QIODevice::ReadOnly))
Anakin's avatar
Anakin committed
67
			throw std::invalid_argument(std::string("ERROR: file not found: ") += path.toStdString());
68

69
		m_filepath = path.left(path.lastIndexOf(QRegExp("/|\\\\")));
70

Anakin's avatar
Anakin committed
71
72
73
74
75
76
	};

	virtual ~FileInterface()
	{
		// close file
		m_file.close();
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

		//clean up
		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
93
94
95
96
	};

protected:
	QVector<Model*>* m_models;
Anakin's avatar
Anakin committed
97
	QFile m_file;
98
	QVector<Material>* m_materials;
Anakin's avatar
Anakin committed
99
	BoundingBox m_sceneBbox;
100
	QString m_filepath;
Anakin's avatar
Anakin committed
101
102
103
104
105

	virtual void import() = 0;

public:
	virtual QVector<Model*>* getModels() const { return m_models; };
106
	virtual QVector<Material>* getMaterials() const { return m_materials; };
Anakin's avatar
Anakin committed
107
	virtual BoundingBox getBoundingBox() const { return m_sceneBbox; };
108

Anakin's avatar
Anakin committed
109
110
	static Material* getDefaultMaterial() {
		Material* defMaterial = new Material;
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

		QImage img(1, 1, QImage::Format_RGB32);
		img.fill(Qt::red);

		QOpenGLTexture* new_texture = new QOpenGLTexture(img.mirrored());

		// Set nearest filtering mode for texture minification
		new_texture->setMinificationFilter(QOpenGLTexture::Nearest);

		// Set bilinear filtering mode for texture magnification
		new_texture->setMagnificationFilter(QOpenGLTexture::Linear);

		// Wrap texture coordinates by repeating
		// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
		new_texture->setWrapMode(QOpenGLTexture::Repeat);

127
		defMaterial->texture0 = new_texture;
Anakin's avatar
Anakin committed
128
		defMaterial->name = "Default Material";
129
130
131
132

		return defMaterial;
	};

Anakin's avatar
Anakin committed
133
};