-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilitis.cpp
125 lines (102 loc) · 4.12 KB
/
Utilitis.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
#include"Utilitis.h"
GLuint CreateTexture(GLsizei width, GLsizei height, GLint internalformat, GLenum fromat, GLint param)
{
GLuint texture;
// çàïðîñèì ó OpenGL ñâîáîäíûé èíäåêñ òåêñòóðû
glGenTextures(1, &texture);
// ñäåëàåì òåêñòóðó àêòèâíîé
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, param);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, param);
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, GL_RGBA, fromat, NULL);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
GLuint loadCubemap(const std::vector<std::string>& faces) {
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrChannels;
for (unsigned int i = 0; i < faces.size(); i++)
{
unsigned char* data = SOIL_load_image(faces[i].c_str(), &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data
);
SOIL_free_image_data(data);
}
else
{
std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
SOIL_free_image_data(data);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return textureID;
}
unsigned int loadTexture(char const* path, GLint mag_filter_param, GLint min_filter_param)
{
unsigned int textureID;
glGenTextures(1, &textureID);
int width, height, nrComponents;
unsigned char* data = SOIL_load_image(path, &width, &height, &nrComponents, 0);
if (data)
{
GLenum format;
if (nrComponents == 1)
format = GL_RED;
else if (nrComponents == 3)
format = GL_RGB;
else if (nrComponents == 4)
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
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, min_filter_param);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter_param);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(data);
glBindTexture(GL_TEXTURE_2D, 0);
}
else
{
std::cout << "Texture failed to load at path: " << path << std::endl;
SOIL_free_image_data(data);
}
return textureID;
}
std::shared_ptr<Mesh> genGrid(int size) {
std::vector<Vertex> verticies;
std::vector<unsigned int> indices;
std::vector<Texture> textures;
float deltaW = 1.f / size;
float deltaH = 1.f / size;
for (long long i = 0; i < 1ll * (size + 1) * (size + 1); ++i) {
glm::vec3 Position((i % (size + 1)) * deltaW, 0.f, (i / (size + 1)) * deltaH);
Position.x = Position.x * 2 - 1;
Position.z = Position.z * 2 - 1;
Vertex vert;
vert.Position = Position;
vert.TexCoords = glm::vec2(Position.x, Position.z);
verticies.push_back(std::move(vert));
if (i / (size + 1) < size && i % (size + 1) < size) {
indices.push_back(i);
indices.push_back(i + 1);
indices.push_back(i + (size + 1));
indices.push_back(i + size + 1);
indices.push_back(i + size + 2);
indices.push_back(i + 1);
}
}
return std::make_shared<Mesh>(verticies, indices, textures);
}