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
391
392
|
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "pwyazh.h"
#include "pwyazh_GL.h"
#include "common.h"
#include <unistd.h>
#define WIDTH 1024
#define HEIGHT 768
static V3F camera_pos;
static F32 dt;
static Input input;
static B32 first_mouse = 1;
void
key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
switch (action)
{
case GLFW_PRESS: {
switch (key)
{
case GLFW_KEY_D: {
input.move_right.state = KeyState_PRESS;
} break;
case GLFW_KEY_W: {
input.move_forward.state = KeyState_PRESS;
} break;
case GLFW_KEY_A: {
input.move_left.state = KeyState_PRESS;
} break;
case GLFW_KEY_S: {
input.move_backward.state = KeyState_PRESS;
} break;
case GLFW_KEY_E: {
input.move_up.state = KeyState_PRESS;
} break;
case GLFW_KEY_Q: {
input.move_down.state = KeyState_PRESS;
} break;
}
} break;
case GLFW_RELEASE: {
switch (key)
{
case GLFW_KEY_D: {
input.move_right.state = KeyState_RELEASE;
} break;
case GLFW_KEY_W: {
input.move_forward.state = KeyState_RELEASE;
} break;
case GLFW_KEY_A: {
input.move_left.state = KeyState_RELEASE;
} break;
case GLFW_KEY_S: {
input.move_backward.state = KeyState_RELEASE;
} break;
case GLFW_KEY_E: {
input.move_up.state = KeyState_RELEASE;
} break;
case GLFW_KEY_Q: {
input.move_down.state = KeyState_RELEASE;
} break;
}
} break;
}
}
void
cursor_pos_callback(GLFWwindow* window, double xpos, double ypos)
{
if (first_mouse)
{
input.last_mouse_pos = v2f(xpos, ypos);
first_mouse = 0;
}
input.mouse_offset = v2f(input.mouse_offset.x+((F32)xpos-input.last_mouse_pos.x),
input.mouse_offset.y+((F32)ypos-input.last_mouse_pos.y));
input.last_mouse_pos = v2f((F32)xpos, (F32)ypos);
}
int
main(void)
{
GLFWwindow *window;
if (glfwInit() == GLFW_FALSE)
{
fprintf(stderr, "[ERROR] Failed to initialize glfw.\n");
return(1);
}
glfwWindowHint(GLFW_RESIZABLE, 0);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(WIDTH, HEIGHT, "Advanced data (uniform buffer)", 0, 0);
if (!window)
{
fprintf(stderr, "[ERROR] Failed to create window.\n");
glfwTerminate();
return(1);
}
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, cursor_pos_callback);
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "[ERROR] Failed to initialize glew.\n");
glfwTerminate();
return(1);
}
U32 skybox_shader = create_shader_program("shaders/skybox.vs", "shaders/skybox.fs");
U32 cube_shader = create_shader_program("shaders/cube.vs", "shaders/cube.fs");
U32 color_cube_shader = create_shader_program("shaders/color_cube.vs",
"shaders/color_cube.fs");
U32 skybox_matrices = glGetUniformBlockIndex(skybox_shader, "Matrices");
U32 cube_matrices = glGetUniformBlockIndex(cube_shader, "Matrices");
U32 color_cube_matrices = glGetUniformBlockIndex(color_cube_shader, "Matrices");
glUniformBlockBinding(skybox_shader, skybox_matrices, 0);
glUniformBlockBinding(cube_shader, cube_matrices, 0);
glUniformBlockBinding(color_cube_shader, color_cube_matrices, 0);
U32 matrices_ubo;
glGenBuffers(1, &matrices_ubo);
glBindBuffer(GL_UNIFORM_BUFFER, matrices_ubo);
glBufferData(GL_UNIFORM_BUFFER, 2*sizeof(MAT4), 0, GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferRange(GL_UNIFORM_BUFFER, 0, matrices_ubo, 0, 2*sizeof(MAT4));
U32 grid_texture = load_texture("../../data/textures/grid.png");
const char *cube_texture_filenames[6] = {
"../../data/textures/skybox/right.jpg",
"../../data/textures/skybox/left.jpg",
"../../data/textures/skybox/top.jpg",
"../../data/textures/skybox/bottom.jpg",
"../../data/textures/skybox/front.jpg",
"../../data/textures/skybox/back.jpg"
};
U32 skybox_texture = load_cubemap(cube_texture_filenames);
F32 a = 0.5f;
F32 vertices[] = {
-a, -a, -a, 0.0f, 0.0f,
a, -a, -a, 1.0f, 0.0f,
a, a, -a, 1.0f, 1.0f,
a, a, -a, 1.0f, 1.0f,
-a, a, -a, 0.0f, 1.0f,
-a, -a, -a, 0.0f, 0.0f,
-a, -a, a, 0.0f, 0.0f,
a, -a, a, 1.0f, 0.0f,
a, a, a, 1.0f, 1.0f,
a, a, a, 1.0f, 1.0f,
-a, a, a, 0.0f, 1.0f,
-a, -a, a, 0.0f, 0.0f,
-a, a, a, 1.0f, 0.0f,
-a, a, -a, 1.0f, 1.0f,
-a, -a, -a, 0.0f, 1.0f,
-a, -a, -a, 0.0f, 1.0f,
-a, -a, a, 0.0f, 0.0f,
-a, a, a, 1.0f, 0.0f,
a, a, a, 1.0f, 0.0f,
a, a, -a, 1.0f, 1.0f,
a, -a, -a, 0.0f, 1.0f,
a, -a, -a, 0.0f, 1.0f,
a, -a, a, 0.0f, 0.0f,
a, a, a, 1.0f, 0.0f,
-a, -a, -a, 0.0f, 1.0f,
a, -a, -a, 1.0f, 1.0f,
a, -a, a, 1.0f, 0.0f,
a, -a, a, 1.0f, 0.0f,
-a, -a, a, 0.0f, 0.0f,
-a, -a, -a, 0.0f, 1.0f,
-a, a, -a, 0.0f, 1.0f,
a, a, -a, 1.0f, 1.0f,
a, a, a, 1.0f, 0.0f,
a, a, a, 1.0f, 0.0f,
-a, a, a, 0.0f, 0.0f,
-a, a, -a, 0.0f, 1.0f
};
Transform platform_position =
transform_make_scale_translate(v3f(10.0f, 0.2f, 10.0f),
v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f));
U32 cube_vao, vbo;
glGenVertexArrays(1, &cube_vao);
glBindVertexArray(cube_vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)(3*sizeof(F32)));
glBindVertexArray(0);
a = 1.0f;
F32 skybox_cube_vertices[] = {
-a, -a, -a,
a, -a, -a,
a, a, -a,
a, a, -a,
-a, a, -a,
-a, -a, -a,
-a, -a, a,
a, -a, a,
a, a, a,
a, a, a,
-a, a, a,
-a, -a, a,
-a, a, a,
-a, a, -a,
-a, -a, -a,
-a, -a, -a,
-a, -a, a,
-a, a, a,
a, a, a,
a, a, -a,
a, -a, -a,
a, -a, -a,
a, -a, a,
a, a, a,
-a, -a, -a,
a, -a, -a,
a, -a, a,
a, -a, a,
-a, -a, a,
-a, -a, -a,
-a, a, -a,
a, a, -a,
a, a, a,
a, a, a,
-a, a, a,
-a, a, -a,
};
U32 skybox_cube_vao;
glGenVertexArrays(1, &skybox_cube_vao);
glBindVertexArray(skybox_cube_vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(skybox_cube_vertices), skybox_cube_vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(F32), (void *)0);
glBindVertexArray(0);
F32 target_fps = 60.0f;
F32 target_spf = 1.0f/target_fps;
camera_pos = v3f(0.0f, 1.0f, 3.0f);
F32 yaw = 0.0f, pitch = 0.0f;
F32 last_time = glfwGetTime();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
MAT4 view, projection;
projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f);
glBindBuffer(GL_UNIFORM_BUFFER, matrices_ubo);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(MAT4), (const void *)&projection);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
view = mat4_make_translate(v3f_negate(camera_pos));
view = mat4_rotate_angles(view, v3f(pitch, yaw, 0.0f));
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
/* INFO(pryazha): update */
V3F left, up, forward;
left = v3f(-view.m0.x, -view.m1.x, -view.m2.x);
up = v3f(view.m0.y, view.m1.y, view.m2.y);
forward = v3f(-view.m0.z, -view.m1.z, -view.m2.z);
V3F dp = v3f_zero();
F32 speed = 2.0f;
if (key_is_pressed(input.move_right))
dp = v3f_add(dp, v3f_scalef(left, -speed*dt));
if (key_is_pressed(input.move_forward))
dp = v3f_add(dp, v3f_scalef(forward, speed*dt));
if (key_is_pressed(input.move_left))
dp = v3f_add(dp, v3f_scalef(left, speed*dt));
if (key_is_pressed(input.move_backward))
dp = v3f_add(dp, v3f_scalef(forward, -speed*dt));
if (key_is_pressed(input.move_up))
dp = v3f_add(dp, v3f_scalef(up, speed*dt));
if (key_is_pressed(input.move_down))
dp = v3f_add(dp, v3f_scalef(up, -speed*dt));
input_update_last_state(&input);
camera_pos = v3f_add(camera_pos, dp);
/* NOTE(pryazha): Mouse handling */
F32 sensitivity = 0.1f;
input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity);
yaw += input.mouse_offset.x;
pitch += input.mouse_offset.y;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
input.mouse_offset = v2f_zero();
view = mat4_make_translate(v3f_negate(camera_pos));
view = mat4_rotate_angles(view, v3f(pitch, yaw, 0.0f));
glBindBuffer(GL_UNIFORM_BUFFER, matrices_ubo);
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(MAT4), sizeof(MAT4), (const void *)&view);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
/* NOTE(pryazha): Render */
glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(cube_shader);
shader_set_mat4fv(cube_shader, "model", mat4_make_translate(v3f(-2.0f, 0.0f, 0.0f)));
glBindTexture(GL_TEXTURE_2D, grid_texture);
glBindVertexArray(cube_vao);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
glUseProgram(cube_shader);
shader_set_mat4fv(cube_shader, "model", transform_apply(platform_position));
glBindTexture(GL_TEXTURE_2D, grid_texture);
glBindVertexArray(cube_vao);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
glUseProgram(color_cube_shader);
shader_set_mat4fv(cube_shader, "model", mat4_make_translate(v3f(2.0f, 0.0f, 0.0f)));
glBindVertexArray(cube_vao);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glUseProgram(skybox_shader);
MAT4 rotate_view = mat4_identity();
rotate_view.m0 = v4f(view.m0.x, view.m0.y, view.m0.z, 0.0f);
rotate_view.m1 = v4f(view.m1.x, view.m1.y, view.m1.z, 0.0f);
rotate_view.m2 = v4f(view.m2.x, view.m2.y, view.m2.z, 0.0f);
glBindBuffer(GL_UNIFORM_BUFFER, matrices_ubo);
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(MAT4), sizeof(MAT4), (const void *)&rotate_view);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindVertexArray(skybox_cube_vao);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glfwSwapBuffers(window);
F32 elapsed = glfwGetTime()-last_time;
if (elapsed < target_spf)
{
U32 sleep_time = (U32)(target_spf-elapsed);
if (sleep_time > 0)
sleep(sleep_time);
}
F32 current_time = glfwGetTime();
dt = current_time-last_time;
last_time = current_time;
}
glfwTerminate();
return(0);
}
|