-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
185 lines (128 loc) · 6.13 KB
/
Copy pathMesh.cpp
File metadata and controls
185 lines (128 loc) · 6.13 KB
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
175
176
177
178
179
180
181
182
183
184
185
//
// Created by roudn on 18/06/2022.
//
#include "Mesh.hpp"
void Mesh::setTriData(std::vector<Vertex>& vertices, std::vector<uint32_t>& indices) {
std::vector<Vertex> _vertices = {
{ { 0.0f, -1.0f, 0.0f },{ 0.0f, 0.0f, 1.0 },{ 1.0f, 0.0f, 0.0 },{ 0.0, 1.0 } },
{ { 1.0f, 1.0f, 0.0f },{ 0.0f, 0.0f, 1.0 },{ 0.0f, 1.0f, 0.0 },{ 0.0, 0.0 } },
{ { -1.0f, 1.0f, 0.0f },{ 0.0f, 0.0f, 1.0 },{ 0.0f, 0.0f, 1.0 },{ 1.0, 0.0 } },
};
std::vector<uint32_t> _indices = {
0, 1, 2,
};
vertices.clear(); indices.clear();
vertices = _vertices;
indices = _indices;
}
void Mesh::setQuadData(std::vector<Vertex>& vertices, std::vector<uint32_t>& indices) {
std::vector<Vertex> _vertices = {
{ { -1.0f, -1.0f, 0.0f },{ 0.0f, 0.0f, 1.0 },{ 1.0f, 0.0f, 0.0 },{ 0.0, 1.0 } },
{ { -1.0f, 1.0f, 0.0f },{ 0.0f, 0.0f, 1.0 },{ 0.0f, 1.0f, 0.0 },{ 0.0, 0.0 } },
{ { 1.0f, 1.0f, 0.0f },{ 0.0f, 0.0f, 1.0 },{ 0.0f, 0.0f, 1.0 },{ 1.0, 0.0 } },
{ { 1.0f, -1.0f, 0.0f },{ 0.0f, 0.0f, 1.0 },{ 1.0f, 0.0f, 1.0 },{ 1.0, 1.0 } }
};
std::vector<uint32_t> _indices = {
0, 1, 2,
0, 2, 3
};
vertices.clear(); indices.clear();
vertices = _vertices;
indices = _indices;
}
void Mesh::setCubeData(std::vector<Vertex>& vertices, std::vector<uint32_t>& indices) {
std::vector<Vertex> _vertices = {
//front
{ { -1.0f, -1.0f, 1.0f },{ 0.0f, 0.0f, 1.0 },{ 1.0f, 0.0f, 0.0 },{ 0.0, 1.0 } },
{ { -1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, 1.0 },{ 0.0f, 1.0f, 0.0 },{ 0.0, 0.0 } },
{ { 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, 1.0 },{ 0.0f, 0.0f, 1.0 },{ 1.0, 0.0 } },
{ { 1.0f, -1.0f, 1.0f },{ 0.0f, 0.0f, 1.0 },{ 1.0f, 0.0f, 1.0 },{ 1.0, 1.0 } },
// back
{ { 1.0, -1.0, -1.0 },{ 0.0f, 0.0f, -1.0 },{ 1.0f, 0.0f, 1.0 },{ 0.0, 1.0 } }, //4
{ { 1.0f, 1.0, -1.0 },{ 0.0f, 0.0f, -1.0 },{ 0.0f, 1.0f, 1.0 },{ 0.0, 0.0 } }, //5
{ { -1.0, 1.0, -1.0 },{ 0.0f, 0.0f, -1.0 },{ 0.0f, 1.0f, 1.0 },{ 1.0, 0.0 } }, //6
{ { -1.0, -1.0, -1.0 },{ 0.0f, 0.0f, -1.0 },{ 1.0f, 0.0f, 1.0 },{ 1.0, 1.0 } }, //7
//left
{ { -1.0, -1.0, -1.0 },{ -1.0f, 0.0f, 0.0 },{ 0.0f, 0.0f, 1.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0 },{ 0.0, 0.0 } }, //21
{ { 1.0, -1.0, 1.0 },{ 0.0f, -1.0f, 0.0 },{ 0.0f, 0.0f, 1.0 },{ 1.0, 0.0 } }, //22
{ { 1.0, -1.0, -1.0 },{ 0.0f, -1.0f, 0.0 },{ 0.0f, 0.0f, 1.0 },{ 1.0, 1.0 } }, //23
};
std::vector<uint32_t> _indices = {
0, 1, 2,
2, 3, 0,
4, 5, 6,
4, 6, 7,
8, 9, 10,
8, 10, 11,
12, 13, 14,
12, 14, 15,
16, 17, 18,
16, 18, 19,
20, 21, 22,
20, 22, 23
};
vertices.clear(); indices.clear();
vertices = _vertices;
indices = _indices;
}
void Mesh::setSphereData(std::vector<Vertex>& vertices, std::vector<uint32_t>& indices) {
std::vector<Vertex> _vertices;
std::vector<uint32_t> _indices;
float latitudeBands = 20.0f;
float longitudeBands = 20.0f;
float radius = 1.0f;
for (float latNumber = 0; latNumber <= latitudeBands; latNumber++) {
float theta = latNumber * 3.14 / latitudeBands;
float sinTheta = sin(theta);
float cosTheta = cos(theta);
for (float longNumber = 0; longNumber <= longitudeBands; longNumber++) {
float phi = longNumber * 2 * 3.147 / longitudeBands;
float sinPhi = sin(phi);
float cosPhi = cos(phi);
Vertex vs;
vs.texCoords.x = (longNumber / longitudeBands); // u
vs.texCoords.y = (latNumber / latitudeBands); // v
vs.normal.x = cosPhi * sinTheta; // normal x
vs.normal.y = cosTheta; // normal y
vs.normal.z = sinPhi * sinTheta; // normal z
vs.color.r = vs.normal.x;
vs.color.g = vs.normal.y;
vs.color.b = vs.normal.z;
vs.pos.x = radius * vs.normal.x; // x
vs.pos.y = radius * vs.normal.y; // y
vs.pos.z = radius * vs.normal.z; // z
_vertices.push_back(vs);
}
}
for (uint32_t latNumber = 0; latNumber < latitudeBands; latNumber++) {
for (uint32_t longNumber = 0; longNumber < longitudeBands; longNumber++) {
uint32_t first = (latNumber * (longitudeBands + 1)) + longNumber;
uint32_t second = first + longitudeBands + 1;
_indices.push_back(first);
_indices.push_back(second);
_indices.push_back(first + 1);
_indices.push_back(second);
_indices.push_back(second + 1);
_indices.push_back(first + 1);
}
}
vertices.clear(); indices.clear();
vertices = _vertices;
indices = _indices;
}