-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.cpp
More file actions
78 lines (61 loc) · 2.22 KB
/
Util.cpp
File metadata and controls
78 lines (61 loc) · 2.22 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
#include "Util.h"
#define STB_IMAGE_IMPLEMENTATION
#include <SDL_image.h>
#include "stb_image.h"
GLuint Util::LoadTexture(const char* filePath) {
int w, h, n;
unsigned char* image = stbi_load(filePath, &w, &h, &n, STBI_rgb_alpha);
if (image == NULL) {
std::cout << "Unable to load image. Make sure the path is correct\n";
assert(false);
}
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
stbi_image_free(image);
return textureID;
}
void Util::DrawText(ShaderProgram* program, GLuint fontTexture, std::string text, float size,
float spacing, glm::vec3 position) {
float width = 1.0f / 16.0f;
float height = 1.0 / 16.0f;
std::vector<float> vertices;
std::vector<float> texCoords;
for (int i = 0; i < text.size(); i++) {
int index = (int)text[i];
float u = (float)(index % 16) / 16.0f;
float v = (float)(index / 16) / 16.0f;
float offset = (size + spacing) * i;
texCoords.insert(texCoords.end(), {
u, v,
u, v + height,
u + width, v,
u + width, v + height,
u + width, v,
u, v + height,
});
vertices.insert(vertices.end(), {
offset + (-0.5f * size), 0.5f * size,
offset + (-0.5f * size), -0.5f * size,
offset + (0.5f * size), 0.5f * size,
offset + (0.5f * size), -0.5f * size,
offset + (0.5f * size), 0.5f * size,
offset + (-0.5f * size), -0.5f * size,
});
}
glm::mat4 modelMatrix = glm::mat4(1.0f);
modelMatrix = glm::translate(modelMatrix, position);
program->SetModelMatrix(modelMatrix);
glUseProgram(program->programID);
glVertexAttribPointer(program->positionAttribute, 2, GL_FLOAT, false, 0, vertices.data());
glEnableVertexAttribArray(program->positionAttribute);
glVertexAttribPointer(program->texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords.data());
glEnableVertexAttribArray(program->texCoordAttribute);
glBindTexture(GL_TEXTURE_2D, fontTexture);
glDrawArrays(GL_TRIANGLES, 0, (int)(text.size() * 6));
glDisableVertexAttribArray(program->positionAttribute);
glDisableVertexAttribArray(program->texCoordAttribute);
}