-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeshutility.cpp
160 lines (133 loc) · 4.84 KB
/
meshutility.cpp
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
#include "meshutility.h"
#include <QFile>
#include <QTextStream>
#include <QRegExp>
#include <QStringList>
#include "mesh.h"
Mesh *MeshUtility::loadObjModel(string filename)
{
vector<SimpleVertex> vertexBuffer;
IndexBuffer indexBuffer;
vector<vec3> normals;
vector<vec2> uvs;
QFile file(filename.c_str());
if (!file.open(QFile::ReadOnly | QFile::Text)) {
assert(false);
return NULL;
}
QTextStream f(&file);
QString line;
QRegExp spaces("\\s+");
do {
line = f.readLine().trimmed();
QStringList parts = line.split(spaces);
if (parts.isEmpty()) continue;
if (parts[0] == "v" && parts.count() >= 4) {
float x = parts[1].toFloat();
float y = parts[2].toFloat();
float z = parts[3].toFloat();
SimpleVertex v;
v.position = {x, y, z};
vertexBuffer.push_back(v);
}
else if (parts[0] == "vn" && parts.count() >= 4) {
float x = parts[1].toFloat();
float y = parts[2].toFloat();
float z = parts[3].toFloat();
normals.push_back({x, y, z});
}
else if (parts[0] == "vt" && parts.count() >= 3) {
float u = parts[1].toFloat();
float v = parts[2].toFloat();
uvs.push_back({u, v});
}
else if (parts[0] == "f" && parts.count() >= 4) {
for (int i = 1; i <= 3; i++) {
QStringList subparts = parts[i].split('/');
int vertex = subparts.count() > 0 ? subparts[0].toInt() - 1 : -1;
int uv = subparts.count() > 1 ? subparts[1].toInt() - 1 : -1;
int normal = subparts.count() > 2 ? subparts[2].toInt() - 1 : -1;
indexBuffer.push_back(vertex);
SimpleVertex& v = vertexBuffer[vertex];
if (normal != -1) {
v.normal += normals[normal];
}
if (uv != -1) {
v.uv = uvs[uv];
}
}
}
} while (!line.isNull());
for (SimpleVertex& v : vertexBuffer) {
v.normal.normalize();
}
Mesh* mesh = new Mesh;
VertexBufferDesc desc;
desc.bufferData = vertexBuffer.data();
desc.bufferSize = vertexBuffer.size() * sizeof(SimpleVertex);
desc.vertexElementSizes = {3, 3, 2};
desc.stride = sizeof(SimpleVertex);
mesh->setVertexBuffer(desc, PrimitiveType::Triangles);
mesh->setIndexBuffer(indexBuffer);
return mesh;
}
Mesh *MeshUtility::createPlane(int tessellationLevel, float size)
{
assert(tessellationLevel > 0);
assert(size > 0);
vector<SimpleVertex> vertexBuffer;
IndexBuffer indexBuffer;
vec3 normal = {0, 1, 0};
vec3 upperleft = {-size, 0, -size};
vec3 lowerleft = {-size, 0, size};
vec3 lowerright = {size, 0, size};
vec3 dy = (upperleft - lowerleft) / tessellationLevel;
float width = (lowerright - lowerleft).length();
float height = (upperleft - lowerleft).length();
// build vertices
for (int row = 0; row <= tessellationLevel; row++) {
vector<vec3> result;
tessellate(lowerleft + dy * row, lowerright + dy * row, tessellationLevel, result);
for (const vec3& point : result) {
SimpleVertex v;
v.position = point;
v.normal = normal;
v.uv.setX(vec3::dotProduct(point - lowerleft, (lowerright - lowerleft).normalized()) / width);
v.uv.setY(vec3::dotProduct(point - lowerleft, (upperleft - lowerleft).normalized()) / height);
vertexBuffer.push_back(v);
}
}
// build faces
for (int l = 0; l < tessellationLevel; l++) {
for (int s = 0; s < tessellationLevel; s++) {
uint16_t i1 = l * (tessellationLevel+1) + s;
uint16_t i2 = i1 + 1;
uint16_t i3 = i1 + tessellationLevel+1;
uint16_t i4 = i2 + tessellationLevel+1;
indexBuffer.push_back(i1);
indexBuffer.push_back(i2);
indexBuffer.push_back(i3);
indexBuffer.push_back(i4);
indexBuffer.push_back(i3);
indexBuffer.push_back(i2);
}
}
Mesh* mesh = new Mesh;
VertexBufferDesc desc;
desc.bufferData = vertexBuffer.data();
desc.bufferSize = vertexBuffer.size() * sizeof(SimpleVertex);
desc.vertexElementSizes = {3, 3, 2};
desc.stride = sizeof(SimpleVertex);
mesh->setVertexBuffer(desc, PrimitiveType::Triangles);
mesh->setIndexBuffer(indexBuffer);
return mesh;
}
void MeshUtility::tessellate(vec3 p1, vec3 p2, int level, vector<vec3> &result)
{
assert(level > 0);
auto delta = (p2 - p1) / level;
for (int i = 0; i <= level; i++)
{
result.push_back(p1 + delta * i);
}
}