-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGui.cpp
209 lines (159 loc) · 6.09 KB
/
Gui.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
// +------------------------------------------------------------+
// | University Racer |
// | Projekt do PGR a GMU, FIT VUT v Brne, 2011 |
// +------------------------------------------------------------+
// | Autori: Tomáš Kimer, [email protected] |
// | Tomáš Sychra, [email protected] |
// | David Šabata, [email protected] |
// +------------------------------------------------------------+
#include "Gui.h"
using namespace std;
#define PROGRAM_NAME "gui"
#define UNUSED_SHADER_ATTR -1
#define TEX_COLS 16
#define TEX_ROWS 16
#define TEX_WIDTH 512
#define TEX_HEIGHT 512
#define LTR_BOX_WIDTH ((float)TEX_WIDTH/TEX_COLS)
#define LTR_BOX_HEIGHT ((float)TEX_HEIGHT/TEX_ROWS)
Gui::Gui(int screenWidth, int screenHeight) :
screenWidth(screenWidth), screenHeight(screenHeight)
{
// nacist shader
ShaderManager::loadProgram(PROGRAM_NAME);
// nastavit parametry 'materialu'
GLuint guiTexture = ShaderManager::loadTexture(ShaderManager::getTexturesPath() + "font.bmp");
materialParams.textures.push_back(guiTexture);
}
void Gui::updateData()
{
vertices.clear();
texcoords.clear();
indices.clear();
unsigned int indexI = 0;
// rozmery ramecku pismena v souradnicich zarizeni (vcetne paddingu)
float letterWidth = (LTR_BOX_WIDTH / screenWidth) * 2;
float letterHeight = (LTR_BOX_HEIGHT / screenHeight) * 2;
for (vector<pair<string, POSITION>>::iterator it = strings.begin(); it != strings.end(); it++)
{
pair<string, POSITION> p = (*it);
string str = p.first;
POSITION position = p.second;
// rozmery bounding boxu v rozmerech zarizeni
float boxWidth = str.size() * letterWidth;
float boxHeight = letterHeight;
// levy horni roh bounding boxu
glm::vec2 topleft(-1, 1);
switch (position.left)
{
case LEFT: // prilepene k levemu okraji
topleft.x = -1;
break;
case CENTER: // vycentrovane
topleft.x = 0 - boxWidth / 2;
break;
case RIGHT: // prilepene k pravemu okraji
topleft.x = 1 - boxWidth;
break;
default: // procentualni pozice zleva
topleft.x = ((float)position.left - 50) / 50;
}
switch (position.top)
{
case TOP: // prilepene k hornimu okraji
topleft.y = 1;
break;
case CENTER: // vycentrovane
topleft.y = 0 - boxHeight / 2;
break;
case BOTTOM: // prilepene k dolnimu okraji
topleft.y = -1 + boxHeight;
break;
default: // procentualni pozice zleva
topleft.y = ((float)position.top - 50) / 50;
}
for (unsigned int i = 0; i < str.size(); i++)
{
LTRTEXCOORDS tex = getCharTexCoords(str[i]);
//tex.topleft.x += 0.01;
//tex.topright.x -= 0.01;
//tex.btmleft.x += 0.01;
//tex.btmright -= 0.01;
vertices.push_back(topleft.x + i * letterWidth);
vertices.push_back(topleft.y);
texcoords.push_back(tex.topleft.x);
texcoords.push_back(tex.topleft.y);
vertices.push_back(topleft.x + (i + 1) * letterWidth);
vertices.push_back(topleft.y);
texcoords.push_back(tex.topright.x);
texcoords.push_back(tex.topright.y);
vertices.push_back(topleft.x + (i + 1) * letterWidth);
vertices.push_back(topleft.y - letterHeight);
texcoords.push_back(tex.btmright.x);
texcoords.push_back(tex.btmright.y);
vertices.push_back(topleft.x + i * letterWidth);
vertices.push_back(topleft.y - letterHeight);
texcoords.push_back(tex.btmleft.x);
texcoords.push_back(tex.btmleft.y);
indices.push_back(indexI);
indices.push_back(indexI + 1);
indices.push_back(indexI + 2);
indices.push_back(indexI + 3);
indexI += 4;
}
}
}
Gui::LTRTEXCOORDS Gui::getCharTexCoords(char c)
{
// pozice znaku v texture
unsigned int index = c;
LTRTEXCOORDS tex;
tex.topleft.x = tex.btmleft.x = (index % TEX_COLS) * (1.f / TEX_COLS);
tex.topright.x = tex.btmright.x = tex.topleft.x + (1.f / TEX_COLS);
tex.topleft.y = tex.topright.y = 1 - (index / TEX_COLS) * (1.f / TEX_ROWS);
tex.btmleft.y = tex.btmright.y = tex.topleft.y - (1.f / TEX_ROWS);
return tex;
}
void Gui::draw()
{
// nastaveni kresleni
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
ShaderManager::PROGRAMBINDING activeBinding = ShaderManager::useProgram(PROGRAM_NAME);
ShaderManager::useMaterialParams(materialParams);
if (activeBinding.matParams.ambient != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.matParams.ambient);
if (activeBinding.matParams.diffuse != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.matParams.diffuse);
if (activeBinding.matParams.specular != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.matParams.specular);
if (activeBinding.matParams.shininess != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.matParams.shininess);
if (activeBinding.normalAttrib != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.normalAttrib);
if (activeBinding.tangentAttrib != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.tangentAttrib);
if (activeBinding.texposAttrib != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.texposAttrib);
if (activeBinding.iEnabledLightsUniform != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.iEnabledLightsUniform);
if (activeBinding.vLightsUniform != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.vLightsUniform);
if (activeBinding.mMVInverseTranspose != UNUSED_SHADER_ATTR)
glDisableVertexAttribArray(activeBinding.mMVInverseTranspose);
// vrcholy
glEnableVertexAttribArray(activeBinding.positionAttrib);
glVertexAttribPointer(activeBinding.positionAttrib, 2, GL_FLOAT, GL_FALSE, 0, (void*)&(vertices.at(0)));
// texturovaci souradnice
if (activeBinding.texposAttrib != UNUSED_SHADER_ATTR) {
glEnableVertexAttribArray(activeBinding.texposAttrib);
glVertexAttribPointer(activeBinding.texposAttrib, 2, GL_FLOAT, GL_FALSE, 0, (void*)&(texcoords.at(0)));
}
// kresleni ctvercu
glDrawElements(GL_QUADS, indices.size(), GL_UNSIGNED_INT, (void*)&(indices.at(0)));
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glDisable(GL_BLEND);
}