forked from Milez213/CityTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HUDShader.h
257 lines (193 loc) · 7 KB
/
HUDShader.h
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifndef HUD_SHADER_H
#define HUD_SHADER_H
#include "glm/glm.hpp"
#include "glm/gtc/type_ptr.hpp"
#include <GL/glfw.h>
#include "Shader.h"
#include "defines.h"
#include "LoadTexture.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using glm::vec3;
using glm::mat4;
using glm::value_ptr;
using namespace std;
class HUDShader : public Shader {
public:
HUDShader() : Shader("shaders/hud_vert.glsl", "shaders/hud_frag.glsl") {
if (m_installed) {
// matrices
h_uModelMatrix = safe_glGetUniformLocation(m_shaderProg, "uModelMatrix");
h_uProjMatrix = safe_glGetUniformLocation(m_shaderProg, "uProjMatrix");
h_uViewMatrix = safe_glGetUniformLocation(m_shaderProg, "uViewMatrix");
h_uColor = safe_glGetUniformLocation(m_shaderProg, "uColor");
// attributes
h_aPosition = safe_glGetAttribLocation(m_shaderProg, "aPosition");
// TODO - add texture coordinates handle in shader
h_aTexture = safe_glGetAttribLocation(m_shaderProg, "aTexture");
loadCoordinates("textures/hudMap.crd");
GLfloat verticies[12] = {
0.0, 0.0, 0.0,
0.0, 1.0, 0.0,
1.0, 0.0, 0.0,
1.0, 1.0, 0.0
};
/*GLfloat verticies[12] = {
-1.0, -1.0, 0.0,
-1.0, 1.0, 0.0,
1.0, -1.0, 0.0,
1.0, 1.0, 0.0
};*/
GLfloat text[8] = {
0.0, 1.0,
0.0, 0.0,
1.0, 1.0,
1.0, 0.0
};
GLushort faces[6] = {
0, 3, 1,
0, 3, 2
};
glGenBuffers(1, &planeCoord);
glBindBuffer(GL_ARRAY_BUFFER, planeCoord);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW);
glGenBuffers(1, &textCoord);
glBindBuffer(GL_ARRAY_BUFFER, textCoord);
glBufferData(GL_ARRAY_BUFFER, sizeof(text), text, GL_STATIC_DRAW);
glGenBuffers(1, &elements);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elements);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(faces), faces, GL_STATIC_DRAW);
currentColor = vec4(1.0, 0.0, 1.0, 0.0);
texture = 0;
LoadTextureAlpha("textures/hudAtlas.bmp", texture);
// There's also LoadTextureAlpha which loads loads 32-bit RGBA BMP's
/*
// too slow
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glfwLoadTexture2D("textures/hudAtlas.tga", 0);
already done in LoadTexture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
*/
} else {
fprintf(stderr, "HUDShader(): Couldn't install shaders\n");
exit(1);
}
}
void makeActive() {
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
setTexture(0);
safe_glEnableVertexAttribArray(h_aPosition);
glBindBuffer(GL_ARRAY_BUFFER, planeCoord);
safe_glVertexAttribPointer(h_aPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
}
void draw(string name) {
int index = -1;
for (int i = 0; i < numCoords && index == -1; i++) {
if (name == names[i])
index = i;
}
if (index == -1) {
printf("No Coordinates Mapped\n");
index = 0;
}
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
setTexture(texture);
glUniform4f(h_uColor, currentColor.x, currentColor.y, currentColor.z, currentColor.w);
safe_glEnableVertexAttribArray(h_aPosition);
glBindBuffer(GL_ARRAY_BUFFER, planeCoord);
safe_glVertexAttribPointer(h_aPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
safe_glEnableVertexAttribArray(h_aTexture);
glBindBuffer(GL_ARRAY_BUFFER, textCoords[index]);
safe_glVertexAttribPointer(h_aTexture, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elements);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
}
void usePlayerColor() {
currentColor.w = 1.0;
}
void useTextureColor() {
currentColor.w = 0.0;
}
void deactivate() {
safe_glDisableVertexAttribArray(h_aPosition);
safe_glDisableVertexAttribArray(h_aTexture);
glDisable(GL_TEXTURE_2D);
}
void useTexture() {
safe_glEnableVertexAttribArray(h_aTexture);
glBindBuffer(GL_ARRAY_BUFFER, textCoords[0]);
safe_glVertexAttribPointer(h_aTexture, 2, GL_FLOAT, GL_FALSE, 0, 0);
}
void setTexture(GLuint textureName) {
GLint location = safe_glGetUniformLocation(m_shaderProg, "uTexUnit");
safe_glUniform1i(location, textureName);
}
void setColor(vec3 color) {
currentColor = vec4(color.x, color.y, color.z, currentColor.w);
}
/*
void setNormTexture(GLuint textureName) {
GLint location = safe_glGetUniformLocation(m_shaderProg, "uNormTexUnit");
safe_glUniform1i(location, textureName);
}*/
void setModelMatrix(mat4 mat) {
safe_glUniformMatrix4fv(h_uModelMatrix, value_ptr(mat));
}
void setViewMatrix(mat4 mat) {
safe_glUniformMatrix4fv(h_uViewMatrix, value_ptr(mat));
}
void setProjectionMatrix(mat4 mat) {
safe_glUniformMatrix4fv(h_uProjMatrix, value_ptr(mat));
}
private:
void loadCoordinates(const char *filename)
{
ifstream in(filename, ios::in);
if (!in) { cerr << "Cannot open " << filename << endl; exit(1); }
string line;
getline(in, line);
istringstream n(line);
n >> numCoords;
names = new string[numCoords];
textCoords = new GLuint[numCoords];
for (int i = 0; i < numCoords && getline(in, line); i++) {
istringstream s(line);
s >> names[i];
GLfloat coord[8];
for (int j = 0; j < 8; j++) {
s >> coord[j];
printf("%0.3f ", coord[j]);
}
glGenBuffers(1, &textCoords[i]);
glBindBuffer(GL_ARRAY_BUFFER, textCoords[i]);
glBufferData(GL_ARRAY_BUFFER, sizeof(coord), coord, GL_STATIC_DRAW);
}
}
GLuint planeCoord;
GLuint elements;
GLuint textCoord;
GLuint texture;
// vertex
GLint h_aPosition;
GLint h_aTexture;
// matrices
GLint h_uModelMatrix;
GLint h_uProjMatrix;
GLint h_uViewMatrix;
GLint h_uColor;
vec4 currentColor;
int numCoords;
GLuint *textCoords;
string *names;
};
#endif