-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.cpp
90 lines (71 loc) · 2.99 KB
/
helper_functions.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
#include <helper_functions.h>
void error_callback(int error, const char* description) {
fputs(description, stderr);
}
glm::vec3 calcDir(float kat_x, float kat_y) {
glm::vec4 dir = glm::vec4(0, 0, 1, 0);
glm::mat4 M = glm::rotate(glm::mat4(1.0f), kat_y, glm::vec3(0, 1, 0));
M = glm::rotate(M, kat_x, glm::vec3(1, 0, 0));
dir = M * dir;
return glm::vec3(dir);
}
GLuint readTexture(const char* filename) {
GLuint tex;
glActiveTexture(GL_TEXTURE0);
//Wczytanie do pamiêci komputera
std::vector<unsigned char> image; //Alokuj wektor do wczytania obrazka
unsigned width, height; //Zmienne do których wczytamy wymiary obrazka
unsigned error = lodepng::decode(image, width, height, filename); //Wczytaj obrazek
//Import do pamiêci karty graficznej
glGenTextures(1, &tex); //Zainicjuj jeden uchwyt
glBindTexture(GL_TEXTURE_2D, tex); //Uaktywnij uchwyt
//Wczytaj obrazek do pamiêci KG skojarzonej z uchwytem
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)image.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return tex;
}
GLuint loadCubemap(std::vector<std::string> faces) {
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
std::vector<unsigned char> data;
unsigned width, height;
for (unsigned int i = 0; i < faces.size(); i++)
{
unsigned error = lodepng::decode(data, width, height, faces[i].c_str());
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)data.data());
data.clear();
}
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;
}
void Model::loadModel(std::string plik) {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(plik, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenSmoothNormals);
std::cout << importer.GetErrorString() << std::endl;
num_mesh = scene->mNumMeshes;
//std::cout << num_mesh << std::endl;
if (scene->HasMeshes()) {
aiMesh* mesh = scene->mMeshes[0];
num_verts = mesh->mNumVertices;
for (int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D vertex = mesh->mVertices[i];
verts.push_back(glm::vec4(vertex.x, vertex.y, vertex.z, 1));
aiVector3D normal = mesh->mNormals[i];
norms.push_back(glm::vec4(normal.x, normal.y, normal.z, 0));
aiVector3D texCoord = mesh->mTextureCoords[0][i];
texCoords.push_back(glm::vec2(texCoord.x, texCoord.y));
}
for (int i = 0; i < mesh->mNumFaces; i++) {
aiFace& face = mesh->mFaces[i];
for (int j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
}
}