-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurvas.cpp
448 lines (371 loc) · 13 KB
/
curvas.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#define _USE_MATH_DEFINES
#include <math.h>
#include <filesystem>
#include <GLES3/gl3.h>
#include <GLFW/glfw3.h>
#include <emscripten/emscripten.h>
// Estrutura para ponto 2D
struct Pontos {
float x, y;
Pontos(float x = 0, float y = 0) : x(x), y(y) {}
};
// Estrutura para transformação
struct Transformacao {
char tipo; // 't' para translação, 'r' para rotação, 's' para escala
float param[3]; // [x, y] para translação e escala, [angulo, px, py] para rotação
};
// Variáveis globais
std::vector<std::vector<Pontos>> CURVAs;
std::vector<Transformacao> transforms;
bool mostrarPoligono = false;
int atualTransform = -1;
std::vector<std::vector<Pontos>> originalCURVAs;
// Cores
const float COR_EIXO_X[] = { 0.0f, 1.0f, 0.0f };
const float COR_EIXO_Y[] = { 0.0f, 0.0f, 1.0f };
const float COR_CURVA[] = { 1.0f, 0.0f, 0.0f };
const float COR_ORIGINAL[] = { 0.5f, 0.5f, 0.5f };
const float COR_PONTOS_CONTROLE[] = { 1.0f, 1.0f, 0.0f };
// Shader de vértice atualizado com suporte a tamanho de ponto
const char* vertexShaderSource = R"(#version 300 es
layout(location = 0) in vec2 aPos;
uniform mat4 uProjection;
uniform vec3 uColor;
out vec3 fragColor;
void main() {
gl_Position = uProjection * vec4(aPos, 0.0, 1.0);
gl_PointSize = 5.0; // Tamanho fixo do ponto
fragColor = uColor;
}
)";
const char* fragmentShaderSource = R"(#version 300 es
precision mediump float;
in vec3 fragColor;
out vec4 FragColor;
void main() {
FragColor = vec4(fragColor, 1.0);
}
)";
// Objetos OpenGL
GLuint shaderProgram;
GLuint VBO;
GLfloat projectionMatrix[16] = {1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
// Declarações antecipadas
void aplicarTransformacao(std::vector<std::vector<Pontos>>& CURVAs, const Transformacao& transform);
bool loadObjFile(const std::string& directory, const std::string& filename);
void remodelar(GLFWwindow* window, int w, int h);
void teclado(GLFWwindow* window, int key, int scancode, int action, int mods);
void desenharPontosControle(const std::vector<Pontos>& controlePontos); // Adicione esta linha
// Função para calcular ponto na curva de Bézier
Pontos calcularPontosBezier(const std::vector<Pontos>& controlePontos, float t) {
std::vector<Pontos> pontos = controlePontos;
int n = pontos.size();
for (int j = 1; j < n; j++) {
for (int i = 0; i < n - j; i++) {
pontos[i].x = (1 - t) * pontos[i].x + t * pontos[i + 1].x;
pontos[i].y = (1 - t) * pontos[i].y + t * pontos[i + 1].y;
}
}
return pontos[0];
}
// Inicialização do shader
GLuint compileShader(GLenum type, const char* source) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char infoLog[512];
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
printf("Erro de compilação do shader: %s\n", infoLog);
}
return shader;
}
void initGL() {
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGenBuffers(1, &VBO);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
// Funções de desenho
void drawLines(const std::vector<Pontos>& points, const float color[3], GLenum mode) {
if (points.empty()) return;
glUseProgram(shaderProgram);
GLint colorLoc = glGetUniformLocation(shaderProgram, "uColor");
GLint projLoc = glGetUniformLocation(shaderProgram, "uProjection");
glUniform3fv(colorLoc, 1, color);
glUniformMatrix4fv(projLoc, 1, GL_FALSE, projectionMatrix);
std::vector<float> vertices;
for (const auto& p : points) {
vertices.push_back(p.x);
vertices.push_back(p.y);
}
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(mode, 0, points.size());
}
void desenharEixos() {
std::vector<Pontos> xAxis = {
Pontos(-1000.0f, 0.0f),
Pontos(1000.0f, 0.0f)
};
std::vector<Pontos> yAxis = {
Pontos(0.0f, -1000.0f),
Pontos(0.0f, 1000.0f)
};
drawLines(xAxis, COR_EIXO_X, GL_LINES);
drawLines(yAxis, COR_EIXO_Y, GL_LINES);
}
void desenharCurvaBezier(const std::vector<Pontos>& controlePontos, const float COR[3]) {
std::vector<Pontos> curvePoints;
for (float t = 0; t <= 1.0f; t += 0.01f) {
curvePoints.push_back(calcularPontosBezier(controlePontos, t));
}
drawLines(curvePoints, COR, GL_LINE_STRIP);
if (mostrarPoligono) {
drawLines(controlePontos, COR_PONTOS_CONTROLE, GL_LINE_STRIP);
drawLines(controlePontos, COR_PONTOS_CONTROLE, GL_POINTS);
}
}
void aplicarTransformacao(std::vector<std::vector<Pontos>>& CURVAs, const Transformacao& transform) {
for (auto& CURVA : CURVAs) {
for (auto& ponto : CURVA) {
switch (transform.tipo) {
case 't': // Translação
ponto.x += transform.param[0];
ponto.y += transform.param[1];
break;
case 'r': { // Rotação em torno de um ponto
float angulo = transform.param[0];
float px = transform.param[1];
float py = transform.param[2];
float rad = angulo * M_PI / 180.0f;
float dx = ponto.x - px;
float dy = ponto.y - py;
ponto.x = px + (dx * cos(rad) - dy * sin(rad));
ponto.y = py + (dx * sin(rad) + dy * cos(rad));
break;
}
case 's': { // Escala
float sx = transform.param[0];
float sy = transform.param[1];
ponto.x *= sx;
ponto.y *= sy;
break;
}
}
}
}
}
void remodelar(GLFWwindow* window, int w, int h) {
glViewport(0, 0, w, h);
float proporcao = (float)w / h;
float left, right, bottom, top;
if (w <= h) {
left = -100.0f;
right = 100.0f;
bottom = -100.0f / proporcao;
top = 100.0f / proporcao;
} else {
left = -100.0f * proporcao;
right = 100.0f * proporcao;
bottom = -100.0f;
top = 100.0f;
}
// Atualizar matriz de projeção
projectionMatrix[0] = 2.0f / (right - left);
projectionMatrix[5] = 2.0f / (top - bottom);
projectionMatrix[10] = -1.0f;
projectionMatrix[12] = -(right + left) / (right - left);
projectionMatrix[13] = -(top + bottom) / (top - bottom);
}
void teclado(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
switch (key) {
case GLFW_KEY_P:
mostrarPoligono = !mostrarPoligono;
break;
case GLFW_KEY_SPACE:
if (atualTransform < (int)transforms.size() - 1) {
atualTransform++;
CURVAs = originalCURVAs;
for (int i = 0; i <= atualTransform; i++) {
aplicarTransformacao(CURVAs, transforms[i]);
}
} else {
atualTransform = -1;
CURVAs = originalCURVAs;
}
break;
case GLFW_KEY_Q:
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GLFW_TRUE);
break;
}
}
}
bool loadObjFile(const std::string& directory, const std::string& filename) {
std::filesystem::path filePath = std::filesystem::path(directory) / filename;
std::ifstream file(filePath);
if (!file.is_open()) {
return false;
}
CURVAs.clear();
transforms.clear();
std::vector<Pontos> atualCURVA;
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
std::istringstream iss(line);
char tipo;
iss >> tipo;
if (tipo == 'v') { // Vértice
float x, y;
if (iss >> x >> y) {
atualCURVA.push_back(Pontos(x, y));
}
}
else if (tipo == 'c') { // Nova curva
if (!atualCURVA.empty()) {
CURVAs.push_back(atualCURVA);
atualCURVA.clear();
}
}
else if (tipo == 't' || tipo == 'r' || tipo == 's') { // Transformação
Transformacao transform;
transform.tipo = tipo;
if (tipo == 't') {
float x, y;
if (iss >> x >> y) {
transform.param[0] = x;
transform.param[1] = y;
transform.param[2] = 0;
transforms.push_back(transform);
}
}
else if (tipo == 'r') {
float angulo, px, py;
if (iss >> angulo >> px >> py) {
transform.param[0] = angulo;
transform.param[1] = px;
transform.param[2] = py;
transforms.push_back(transform);
}
}
else if (tipo == 's') {
float sx, sy;
if (iss >> sx >> sy) {
transform.param[0] = sx;
transform.param[1] = sy;
transform.param[2] = 0;
transforms.push_back(transform);
}
}
}
}
// Adicionar última curva se houver
if (!atualCURVA.empty()) {
CURVAs.push_back(atualCURVA);
}
// Fazer cópia das curvas originais
originalCURVAs = CURVAs;
file.close();
return true;
}
// Função para pontos de controle
void desenharPontosControle(const std::vector<Pontos>& controlePontos) {
std::vector<float> vertices;
for (const auto& p : controlePontos) {
vertices.push_back(p.x);
vertices.push_back(p.y);
}
glUseProgram(shaderProgram);
GLint colorLoc = glGetUniformLocation(shaderProgram, "uColor");
GLint projLoc = glGetUniformLocation(shaderProgram, "uProjection");
glUniform3fv(colorLoc, 1, COR_PONTOS_CONTROLE);
glUniformMatrix4fv(projLoc, 1, GL_FALSE, projectionMatrix);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, controlePontos.size());
}
void display(GLFWwindow* window) {
glClear(GL_COLOR_BUFFER_BIT);
desenharEixos();
if (atualTransform >= 0) {
for (const auto& CURVA : originalCURVAs) {
desenharCurvaBezier(CURVA, COR_ORIGINAL);
if (mostrarPoligono) {
desenharPontosControle(CURVA);
}
}
}
for (const auto& CURVA : CURVAs) {
desenharCurvaBezier(CURVA, COR_CURVA);
if (mostrarPoligono) {
desenharPontosControle(CURVA);
}
}
glfwSwapBuffers(window);
}
// Função de loop principal
void main_loop(void* arg) {
GLFWwindow* window = static_cast<GLFWwindow*>(arg);
display(window);
}
int main(int argc, char** argv) {
if (!glfwInit()) {
fprintf(stderr, "Falha ao inicializar GLFW\n");
return -1;
}
// Configurar GLFW para OpenGL ES 3.0
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
// Obter monitor primário
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
// Criar janela em fullscreen
GLFWwindow* window = glfwCreateWindow(mode->width, mode->height,
"Visualizador de Curvas de Bézier", monitor, nullptr);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Inicializar OpenGL
initGL();
// Configurar OpenGL
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Carregar arquivo .obj
if (!loadObjFile(".", "arquivo.obj")) {
printf("Erro ao carregar arquivo.obj\n");
return 1;
}
// Definir callbacks
glfwSetFramebufferSizeCallback(window, remodelar);
glfwSetKeyCallback(window, teclado);
// Redimensionamento inicial
remodelar(window, 640, 480);
// Iniciar loop principal
emscripten_set_main_loop_arg(main_loop, window, 0, 1);
return 0;
}