-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
197 lines (182 loc) · 6.69 KB
/
Copy pathMesh.cpp
File metadata and controls
197 lines (182 loc) · 6.69 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
186
187
188
189
190
191
192
193
194
195
196
197
#include "Mesh.h"
#include "glm/glm/gtx/transform.hpp"
#include "glm/glm/gtx/Normal.hpp"
glm::vec3 Normalize(glm::vec3 vec)
{
glm::vec3 temp;
float vLen = sqrt((vec.x * vec.x) + (vec.y * vec.y) + (vec.z * vec.z));
if (vLen == 0)
return vec;
temp.x = vec.x / vLen;
temp.y = vec.y / vLen;
temp.z = vec.z / vLen;
return temp;
}
Mesh::Mesh()
{
MMatrix = glm::mat4(1.0f);
}
Mesh::Mesh(std::vector<Vertex> vertices,
std::vector<unsigned int> indices, bool stable,
Material material)
{
MMatrix = glm::mat4(1.0f);
this->vertices = vertices;
this->indices = indices;
this->textures = textures;
this->material = material;
if (stable == 1)
{
normals();
setupMesh();
}
}
//rysowanie
void Mesh::Draw(Shader *shader)
{
//je¿eli jest wiêcej ni¿ jedna tekstura na³o¿ona
// shader->run();
for (unsigned int i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i); // je¿eli tekstury s¹
glBindTexture(GL_TEXTURE_2D, textures[i].id);
}
glActiveTexture(GL_TEXTURE0);
//GLint tmp = glGetUniformLocation(shader->Program, "objectColor");
glUniformMatrix4fv(glGetUniformLocation(shader->Program, "model"), 1, GL_FALSE, glm::value_ptr(MMatrix));
//glUniform3f(tmp, material.Color.x, material.Color.y, material.Color.z);
// draw mesh
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
//wrzucenie danych o punktach, normalnych, kordach tekstury, kolorach
void Mesh::setupMesh()
{
// generates table of verex and assings in VAO
glGenVertexArrays(1, &VAO);
// generate buffer object
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
//binds an array of vertex
glBindVertexArray(VAO);
//to samo co wy¿ej tyle ¿e bufer
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//rysowanie statyczne
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
//thanks to that we are sending an array of verticies to graphic card
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int),
&indices[0], GL_STATIC_DRAW);
// vertex positions to buffer
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Position));
// vertex normals to buffer
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
// vertex texture coords to buffer
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Color));
glBindVertexArray(0);
}
//obliczanie normalnych dla punktów
void Mesh::normals()
{
glm::vec3 temp, tmp = {0,0,0};
int div = 0;
for (unsigned int i = 0; i < vertices.size(); i++)
{
div = 0;
temp = { 0,0,0 };
for (unsigned int j = 0; j < indices.size(); j++)
{
if (i == indices[j])
{
if (j % 3 == 0)
tmp = glm::triangleNormal(vertices[indices[j]].Position, vertices[indices[j +2]].Position, vertices[indices[j + 1]].Position);
// tmp = CalcNormal(vertices[indices[j]].Position, vertices[indices[j + 1]].Position, vertices[indices[j + 2]].Position);
if (j % 3 == 1)
tmp = glm::triangleNormal(vertices[indices[j]].Position, vertices[indices[j - 1]].Position, vertices[indices[j + 1]].Position);
//tmp = CalcNormal(vertices[indices[j]].Position, vertices[indices[j - 1]].Position, vertices[indices[j + 1]].Position);
if (j % 3 == 2)
//tmp = CalcNormal(vertices[indices[j]].Position, vertices[indices[j - 1]].Position, vertices[indices[j -2 ]].Position);
tmp = glm::triangleNormal(vertices[indices[j]].Position, vertices[indices[j - 1]].Position, vertices[indices[j -2]].Position);
temp += tmp;
div += 1;
}
}
vertices[i].Normal = { temp.x ,temp.y , temp.z };
}
}
void Mesh::applyTransform()
{
for (unsigned int i = 0; i < vertices.size(); i++)
{
vertices[i].Position = MMatrix * glm::vec4(vertices[i].Position, 1.0f);
}
MMatrix = {
1.0f,0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f
};
}
//pamiêtaj skala rotacja przemieszczenie
void rotate_mesh(Mesh* mesh, glm::vec3 rot, glm::vec3 center)
{
float a = sinf(degtorad * rot.x),
b = sinf(degtorad * rot.y),
c = sinf(degtorad * rot.z),
d = cosf(degtorad * rot.x),
e = cosf(degtorad * rot.y),
h = cosf(degtorad * rot.z);
//to jest rotacja ry, rz, rx co oznacza ¿e przy rotacji z sie knoci
float temp[9] =
{
h * e,
-a * b - e * c * d,
b * d - e * c * d,
c ,
h * d,
a * h,
-b * h,
b * c * d - e * a,
e * d + b * c * a
};
glm::vec3 tem = { 0.0f, 0.0f, 0.0f };
//rotowanie ka¿dego wierzcho³ka
for (unsigned int i = 0; i < mesh->vertices.size(); i++)
{
tem.x = (mesh->vertices[i].Position.x - center.x) * temp[0] + (mesh->vertices[i].Position.y - center.y) * temp[1] + (mesh->vertices[i].Position.z - center.z) * temp[2];
tem.y = (mesh->vertices[i].Position.x - center.x) * temp[3] + (mesh->vertices[i].Position.y - center.y) * temp[4] + (mesh->vertices[i].Position.z - center.z) * temp[5];
tem.z = (mesh->vertices[i].Position.x - center.x) * temp[6] + (mesh->vertices[i].Position.y - center.y) * temp[7] + (mesh->vertices[i].Position.z - center.z) * temp[8];
tem.x = tem.x + center.x;
tem.y = tem.y + center.y;
tem.z = tem.z + center.z,
mesh->vertices[i].Position = tem;
}
//funkcja dzia³a w miarê dobrz ale nie rotuj o z
}
void move_mesh(Mesh* mesh, glm::vec3 pos)
{
mesh->MMatrix[3] += glm::vec4(pos, 0.0f);
}
void scale_mesh(Mesh* mesh, glm::vec3 scale, glm::vec3 pos)
{
mesh->MMatrix = glm::scale(mesh->MMatrix, scale);
}
void rotate_mesh(Mesh* mesh, float angle, glm::vec3 rot, glm::vec3 center)
{
mesh->MMatrix = glm::rotate(mesh->MMatrix, angle * degtorad, rot);
}
void m_rotate_mesh(Mesh* mesh, glm::vec3 rot, glm::vec3 center)
{
mesh->MMatrix = glm::rotate(mesh->MMatrix, rot.x * degtorad, {1,0,0});
mesh->MMatrix = glm::rotate(mesh->MMatrix, rot.y * degtorad, { 0,1,0 });
}
void Mesh::setMaterial(Material mat)
{
material = mat;
}