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
|
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "pwyazh.h"
#include "pwyazh_GL.h"
#include "common.h"
int main(void)
{
GLFWwindow *window;
Arena *arena = 0;
Mesh *cube;
Input input;
F32 target_fps, target_spf, last_time;
MAT4 proj, view, model;
State state;
S32 width, height;
U32 color_shader;
if (glfwInit() == GLFW_FALSE) {
fprintf(stderr, "[ERROR] Failed to initialize glfw.\n");
return(1);
}
width = 1024;
height = 768;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4);
window = glfwCreateWindow(width, height, "Anti Aliasing (MSAA)", 0, 0);
if (!window) {
fprintf(stderr, "[ERROR] Failed to create window.\n");
glfwTerminate();
return(1);
}
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (glewInit() != GLEW_OK) {
fprintf(stderr, "[ERROR] Failed to initialize glew.\n");
glfwTerminate();
return(1);
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
arena = arena_alloc(Kilobytes(4));
cube = mesh_load_obj(arena, "../../data/models/cube.obj");
color_shader = create_shader_program("shaders/color.vert",
"shaders/color.frag");
state.camera = (Camera) {
v3f(0.0f, 0.0f, 3.0f),
90.0f, 0.1f, 1000.0f,
0.0f, 0.0f
};
input = input_init();
target_fps = 60.0f;
target_spf = 1.0f/target_fps;
last_time = glfwGetTime();
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
/* NOTE(pryazha): Update */
glfwGetFramebufferSize(window, &width, &height);
process_glfw_keyboard(window, &input);
process_glfw_mouse_pos(window, &input);
if (key_first_press(input.exit))
glfwSetWindowShouldClose(window, GLFW_TRUE);
V3F dv = get_dv_camera_first_person(&input, &state.camera,
4.0f, state.dt);
state.camera.pos = v3f_add(state.camera.pos, dv);
F32 sensitivity = 0.1f;
input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity);
state.camera.yaw += input.mouse_offset.x;
state.camera.pitch += input.mouse_offset.y;
if (state.camera.pitch > 89.0f)
state.camera.pitch = 89.0f;
if (state.camera.pitch < -89.0f)
state.camera.pitch = -89.0f;
input_update_last_state(&input);
view = get_view_matrix(&state.camera);
proj = perspective(state.camera.fovx, (F32)width/(F32)height,
state.camera.near, state.camera.far);
model = mat4_identity();
/* NOTE(pryazha): Render */
glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(color_shader);
shader_set_mat4fv(color_shader, "proj", proj);
shader_set_mat4fv(color_shader, "view", view);
shader_set_mat4fv(color_shader, "model", model);
mesh_draw(cube);
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();
state.dt = current_time-last_time;
last_time = current_time;
}
arena_release(arena);
glfwTerminate();
return(0);
}
|