-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeshdata.h
174 lines (152 loc) · 6.13 KB
/
meshdata.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifndef MESHDATA_H
#define MESHDATA_H
#include <memory>
#include "innpch.h"
#include "gltypes.h"
#include "texture.h"
#include <variant>
#include <QJsonObject>
#include <QJsonArray>
/** Information struct about how a mesh should look.
* Defined by a shader, optional textures, and all parameters
* that should be sent to the shader.
* @brief Information struct about how a mesh should look.
*/
struct Material
{
std::shared_ptr<Shader> mShader{nullptr};
std::map<std::string, ShaderParamType> mParameters;
std::vector<std::pair<uint, GLenum>> mTextures;
Material(std::shared_ptr<Shader> shader = std::shared_ptr<Shader>{},
std::map<std::string, ShaderParamType> parameters = std::map<std::string, ShaderParamType>{},
std::vector<std::pair<uint, GLenum>> textures = std::vector<std::pair<uint, GLenum>>{})
: mShader{shader}, mParameters{parameters}, mTextures{textures}
{}
Material(const Material& material) = default;
/**
* @brief Sets up the current shader for the material and adds the shader parameters to the mesh widget UI.
*/
void loadShaderWithParameters(std::shared_ptr<Shader> shader)
{
mParameters.clear();
if(shader)
{
mShader = shader;
mParameters = mShader->params;
}
else
{
mShader = nullptr;
}
}
QJsonObject toJSON()
{
QJsonObject returnObject;
returnObject.insert("Shader", QJsonValue(mShader ? mShader->mName.c_str() : "None"));
QJsonArray parameterArray;
int i = 0;
for(auto& value : mParameters)
{
QJsonObject obj;
if(std::holds_alternative<bool>(value.second))
{
obj.insert(value.first.c_str(), QJsonValue(std::get<bool>(value.second)));
}
else if (std::holds_alternative<int>(value.second))
{
obj.insert(value.first.c_str(), QJsonValue(std::get<int>(value.second)));
}
else if (std::holds_alternative<float>(value.second))
{
obj.insert(value.first.c_str(), QJsonValue(static_cast<double>(std::get<float>(value.second))));
}
else if (std::holds_alternative<gsl::vec2>(value.second))
{
QJsonArray array;
array.insert(0, QJsonValue(static_cast<double>(std::get<gsl::vec2>(value.second).x)));
array.insert(1, QJsonValue(static_cast<double>(std::get<gsl::vec2>(value.second).y)));
obj.insert(value.first.c_str(), array);
}
else if (std::holds_alternative<gsl::vec3>(value.second))
{
QJsonArray array;
array.insert(0, QJsonValue(static_cast<double>(std::get<gsl::vec3>(value.second).x)));
array.insert(1, QJsonValue(static_cast<double>(std::get<gsl::vec3>(value.second).y)));
array.insert(2, QJsonValue(static_cast<double>(std::get<gsl::vec3>(value.second).z)));
obj.insert(value.first.c_str(), array);
}
else if(std::holds_alternative<gsl::vec4>(value.second))
{
QJsonArray array;
array.insert(0, QJsonValue(static_cast<double>(std::get<gsl::vec4>(value.second).x)));
array.insert(1, QJsonValue(static_cast<double>(std::get<gsl::vec4>(value.second).y)));
array.insert(2, QJsonValue(static_cast<double>(std::get<gsl::vec4>(value.second).z)));
array.insert(3, QJsonValue(static_cast<double>(std::get<gsl::vec4>(value.second).w)));
obj.insert(value.first.c_str(), array);
}
parameterArray.insert(i, obj);
i++;
}
returnObject.insert("Parameters", parameterArray);
QJsonArray textureArray;
i = 0;
for(auto& texture : mTextures)
{
QJsonObject obj;
obj.insert("ID", QJsonValue(static_cast<int>(texture.first)));
obj.insert("Type", QJsonValue(static_cast<int>(texture.second)));
textureArray.insert(i, obj);
i++;
}
returnObject.insert("Textures", textureArray);
return returnObject;
}
Material& operator= (const Material& other) = default;
Material& operator= (Material&& other)
{
mShader = std::move(other.mShader);
mParameters = std::move(other.mParameters);
mTextures = std::move(other.mTextures);
return *this;
}
};
/** Information struct that describes information about a mesh.
* Includes a index to the buffer describing the mesh and the 2 next LODs,
* vertex counts, triangle index counts and mesh bounds.
* @brief Information struct that describes information about a mesh.
*/
struct MeshData
{
GLenum mRenderType{};
std::array<unsigned, 3> mVAOs{};
std::array<unsigned, 3> mVerticesCounts{};
std::array<unsigned, 3> mIndicesCounts{};
std::string mName{};
/** AABB describing the extents of the mesh in local space.
* @brief AABB describing the extents of the mesh in local space.
*/
struct Bounds
{
gsl::vec3 centre;
float radius;
} bounds;
MeshData(const std::string& name = "", GLenum renderType = GL_TRIANGLES)
: mRenderType(renderType), mName(name) {}
QJsonObject toJSON()
{
QJsonObject returnObject;
returnObject.insert("RenderType", QJsonValue(static_cast<int>(mRenderType)));
returnObject.insert("Name", QJsonValue(mName.size() ? mName.c_str() : "None"));
// Not sure if we need bounds in JSON
// QJsonObject boundsObj;
// QJsonArray centreArr;
// centreArr.insert(0, QJsonValue(static_cast<double>(bounds.centre.x)));
// centreArr.insert(1, QJsonValue(static_cast<double>(bounds.centre.y)));
// centreArr.insert(2, QJsonValue(static_cast<double>(bounds.centre.z)));
// boundsObj.insert("Centre", centreArr);
// boundsObj.insert("Radius", QJsonValue(static_cast<double>(bounds.radius)));
// returnObject.insert("Bounds", boundsObj);
return returnObject;
}
};
#endif // MESHDATA_H