-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_window.cpp
390 lines (355 loc) · 13.9 KB
/
opt_window.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "opt_window.h"
//#include "opt_field.h"
OPT_WINDOW::OPT_WINDOW()
{
}
OPT_WINDOW::OPT_WINDOW(Shader *win_shader, Shader *text, std::map<char, Character> *Chars, WINDOW_TYPE TYPE)
{
W_TYPE = TYPE;
shader = win_shader;
text_shader = text;
Characters = Chars;
projection = glm::mat4(1.0f);//glm::ortho(0.0f, (float)SCREEN_WIDTH, 0.0f, (float)SCREEN_HEIGHT);
model = glm::mat4(1.0f);
init_window_body();
}
void OPT_WINDOW::init_window_body()
{
// Defines The Window Size
float z = 0.0f;
float transparency = 0.75f;
float win_body[210] = {
-0.48f, -0.98f, z, 0.1f, 0.1f, 0.1f, transparency, // Main Window Body
0.48f, -0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
0.48f, 0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
0.48f, 0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
-0.48f, 0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
-0.48f, -0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
-0.5f, -1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f, // Left Side
-0.48f,-0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
-0.48f, 0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.48f, 0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
-0.48f, 0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f, // Top Side
0.48f, 0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
-0.48f, 0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.48f,-0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f, // Right Side
0.5f, -1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.5f, 1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.48f, 0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.48f,-0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f, // Bottom Side
0.5f, -1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.48f,-0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.48f,-0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
-0.48f,-0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f
};
unsigned int VBO;
// Use my button Shaders
shader->use();
unsigned int modelLoc = glGetUniformLocation(shader->ID, "model");
//unsigned int modelLoc = glGetUniformLocation(Button_Body->ID, "model");
//unsigned int viewLoc = glGetUniformLocation(Button_Body->ID, "view");
// pass them to the shaders (3 different ways)
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
shader->setMat4("model", model);
//glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
// note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
shader->setMat4("projection", projection);
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(win_body), win_body, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
//glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(7 * sizeof(float)));
//glEnableVertexAttribArray(2);
// render
// ------
/*
//glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
glBindVertexArray(this->VAO_BACK);
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);*/
//glm::vec4 test = projection * model * glm::vec4(-0.5f, 1.0f, 1.0, 1.0f);
//printf("test.x: %f test.y: %f test.z: %f test.w: %f\n", test.x, test.y, test.z, test.w);
}
void OPT_WINDOW::init_menu_window_body()
{
// Defines The Window Size
float z = 0.0f;
float transparency = 0.75f;
float win_body[210] = {
-0.73f, -0.98f, z, 0.1f, 0.1f, 0.1f, transparency, // Main Window Body
0.73f, -0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
0.73f, 0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
0.73f, 0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
-0.73f, 0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
-0.73f, -0.98f, z, 0.1f, 0.1f, 0.1f, transparency,
-0.75f, -1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f, // Left Side
-0.73f,-0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
-0.73f, 0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.73f, 0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.75f, 1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.75f, -1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
-0.73f, 0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f, // Top Side
0.73f, 0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
0.75f, 1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f,
0.75f, 1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.75f, 1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
-0.73f, 0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.73f,-0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f, // Right Side
0.75f, -1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f,
0.75f, 1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.75f, 1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.73f, 0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.73f,-0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.75f, -1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f, // Bottom Side
0.75f, -1.0f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.73f,-0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
0.73f,-0.98f, z, 0.5f, 0.5f, 0.5f, 1.0f,
-0.73f,-0.98f, z, 1.0f, 1.0f, 1.0f, 1.0f,
-0.75f, -1.0f, z, 1.0f, 1.0f, 1.0f, 1.0f
};
unsigned int VBO;
// Use my button Shaders
shader->use();
unsigned int modelLoc = glGetUniformLocation(shader->ID, "model");
//unsigned int modelLoc = glGetUniformLocation(Button_Body->ID, "model");
//unsigned int viewLoc = glGetUniformLocation(Button_Body->ID, "view");
// pass them to the shaders (3 different ways)
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
shader->setMat4("model", model);
//glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
// note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
shader->setMat4("projection", projection);
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(win_body), win_body, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
//glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(7 * sizeof(float)));
//glEnableVertexAttribArray(2);
// render
// ------
/*
//glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
glBindVertexArray(this->VAO_BACK);
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);*/
//glm::vec4 test = projection * model * glm::vec4(-0.5f, 1.0f, 1.0, 1.0f);
//printf("test.x: %f test.y: %f test.z: %f test.w: %f\n", test.x, test.y, test.z, test.w);
}
void OPT_WINDOW::draw_window_body()
{
// Use my button Shaders
shader->use();
unsigned int modelLoc = glGetUniformLocation(shader->ID, "model");
//unsigned int viewLoc = glGetUniformLocation(Button_Body->ID, "view");
// pass them to the shaders (3 different ways)
/* I really really want to fix this so I can pass my shader parameters by pointer */
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
//glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
// note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
shader->setMat4("model", model);
shader->setMat4("projection", projection);
//glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, 30);
}
void OPT_WINDOW::draw_window()
{
draw_window_body();
for(int i = 0;i < fields.size();i++){
fields[i].draw_field();
//draw_window_body();
//printf("%s\n", fields[i].label.c_str());
fields[i].draw_text(0.0f, 0.98f - 0.05- fields[i].y_off*fields[i].FIELD_ID, 0.0f, glm::vec3(0.5, 0.5, 0.5));
}
//printf("fields.size(): %d\n", fields.size());
}
void OPT_WINDOW::init_fields()
{
if(W_TYPE == ESC_WIN){
for(int i = 0; i < 4; i++){
OPT_FIELD field(shader, text_shader, 0.0, 0.0, i, Characters, ESC_WIN);
field.model = model;
field.projection = projection;
field.init_field();
fields.push_back(field);
}
fields[0].label = "NEW";
fields[0].TYPE = NEW;
fields[1].label = "SAVE";
fields[1].TYPE = SAVE;
fields[2].label = "LOAD";
fields[2].TYPE = LOAD;
fields[3].label = "EXIT";
fields[3].TYPE = EXIT;
} else if(W_TYPE == BUTTON_WIN){
for(int i = 0; i < 8; i++){
OPT_FIELD field(shader, text_shader, 0.0f, 0.0f, i, Characters, BUTTON_WIN);
field.model = model;
field.projection = projection;
switch(i){
case 0:
field.label = "Field" + std::to_string(i);
field.TYPE = LABEL;
break;
case 1:
field.label = "Sample Select";
field.TYPE = SAMPLE;
break;
case 2:
field.label = "Color";
field.TYPE = COLOR;
break;
case 3:
field.label = "Remove";
field.TYPE = REMOVE;
break;
default:
field.label = "Field" + std::to_string(i);
field.TYPE = NONE;
break;
}
field.init_field();
fields.push_back(field);
}
} else {
printf("you can't handle muh menu\n");
}
}
void OPT_WINDOW::set_field_values()
{
for(int i = 0; i < fields.size(); i++){
fields[i].model = model;
fields[i].projection = projection;
fields[i].init_field();
}
}
void OPT_WINDOW::calc_bound_box()
{
glm::vec4 TL = projection * model * TL_Corner;
glm::vec4 BR = projection * model * BR_Corner;
S_COORD_TL.x = (TL.x + 1.0f) * (SCREEN_WIDTH / 2.0f);
S_COORD_TL.y = (-TL.y + 1.0f) * (SCREEN_HEIGHT / 2.0f);
S_COORD_BR.x = (BR.x + 1.0f) * (SCREEN_WIDTH / 2.0f);
S_COORD_BR.y = (-BR.y + 1.0f) * (SCREEN_HEIGHT / 2.0f);
}
/*void OPT_WINDOW::handleEvent( SDL_Event* e, int &x, int &y )
{
//If mouse event happened
if( e->type == SDL_MOUSEMOTION || e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP )
{
//Get mouse position
//int x, y;
SDL_GetMouseState( &x, &y );
//if(press < 0.0f){
// press = 0.0f;
// init_button();
// gRenderQuad = true;
//}
SDL_LockMutex(counterLock);
if(timeRemain < 0.01f && playing > 0.0f){
playing = 0.0f;
init_button();
gRenderQuad = true;
}
SDL_UnlockMutex(counterLock);
//Check if mouse is in button
bool inside = true;
//Mouse is left of the button
if( x < field.x )
{
inside = false;
}
//Mouse is right of the button
else if( x > B_Right.x )
{
inside = false;
}
//Mouse above the button
else if( y < T_Left.y )
{
inside = false;
}
//Mouse below the button
else if( y > B_Right.y )
{
inside = false;
}
//Mouse is outside button
if( !inside )
{
ring = 0.0f;
if(prev_visit){
gRenderQuad = true;
prev_visit = false;
init_button();
}
}
//Mouse is inside button
else
{
// light facets
ring = 0.25f;
// Avoid excessive re-rendering
if(!prev_visit){
gRenderQuad = true;
init_button();
}
//Set mouse over sprite
switch( e->type )
{
case SDL_MOUSEMOTION:
//mCurrentSprite = BUTTON_SPRITE_MOUSE_OVER_MOTION;
break;
case SDL_MOUSEBUTTONDOWN:
if(e->button.button == SDL_BUTTON_LEFT){
SDL_LockMutex(counterLock);
if(sample != NULL && timeRemain < 0.01f){
countID myID;
myID.ID = &ID;
myID.timeRemain = &timeRemain;
ButtonID->push_back(myID);
playing = 0.3f;
timeRemain = sampleDuration;
init_button();
Mix_PlayChannel( -1, sample, 0 );
}
SDL_UnlockMutex(counterLock);
press = -0.05f;
init_button();
gRenderQuad = true;
}
break;
case SDL_MOUSEBUTTONUP:
//mCurrentSprite = BUTTON_SPRITE_MOUSE_UP;
break;
}
prev_visit = true;
}
}
}*/