-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaux.cpp
More file actions
85 lines (69 loc) · 2.53 KB
/
Copy pathaux.cpp
File metadata and controls
85 lines (69 loc) · 2.53 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include "./includes/stb_image.h"
GLuint CompileShader(GLenum type, const char* source) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char info[512];
glGetShaderInfoLog(shader, 512, NULL, info);
std::cerr << "Error compiling shader: " << info << std::endl;
}
return shader;
}
std::string ReadFile(const char* path) {
std::ifstream file(path);
std::stringstream buf;
buf << file.rdbuf();
return buf.str();
}
GLuint CreateShaderProgram(const char* vertexPath, const char* fragmentPath) {
std::string vCode = ReadFile(vertexPath);
std::string fCode = ReadFile(fragmentPath);
GLuint vShader = CompileShader(GL_VERTEX_SHADER, vCode.c_str());
GLuint fShader = CompileShader(GL_FRAGMENT_SHADER, fCode.c_str());
GLuint program = glCreateProgram();
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
char info[512];
glGetProgramInfoLog(program, 512, NULL, info);
std::cerr << "Error linking program: " << info << std::endl;
}
glDeleteShader(vShader);
glDeleteShader(fShader);
return program;
}
int cargaTextura(const char* nombre) {
GLuint textura;
glGenTextures(1, &textura);
glBindTexture(GL_TEXTURE_2D, textura);
// Configuración de parámetros de la textura
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Carga la imagen
int width, height, nrChannels;
unsigned char *data = stbi_load(nombre, &width, &height, &nrChannels, 0);
if (data) {
// Determina el formato de la imagen (por ejemplo, GL_RGB o GL_RGBA)
GLenum format = (nrChannels == 4) ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "Error al cargar la textura" << std::endl;
}
stbi_image_free(data);
return textura;
}