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
|
#include "pwyazh.h"
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "pwyazh_GL.h"
#include "common.h"
static S32 global_width = 1024, global_height = 768;
static Input global_input;
static V3F camera_dp = (V3F){ 0.0f, 0.0f, 0.0f };
static V3F light_pos = (V3F){ 0.0f, 0.0f, 0.0f };
static S32 blinn = 0;
void
key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
switch (action)
{
case GLFW_PRESS: {
switch (key)
{
case GLFW_KEY_D: {
global_input.move_right.state = KeyState_PRESS;
} break;
case GLFW_KEY_W: {
global_input.move_forward.state = KeyState_PRESS;
} break;
case GLFW_KEY_A: {
global_input.move_left.state = KeyState_PRESS;
} break;
case GLFW_KEY_S: {
global_input.move_backward.state = KeyState_PRESS;
} break;
case GLFW_KEY_E: {
global_input.move_up.state = KeyState_PRESS;
} break;
case GLFW_KEY_Q: {
global_input.move_down.state = KeyState_PRESS;
} break;
case GLFW_KEY_SPACE: {
global_input.jump.state = KeyState_PRESS;
} break;
case GLFW_KEY_RIGHT: {
global_input.action_right.state = KeyState_PRESS;
} break;
case GLFW_KEY_UP: {
global_input.action_up.state = KeyState_PRESS;
} break;
case GLFW_KEY_LEFT: {
global_input.action_left.state = KeyState_PRESS;
} break;
case GLFW_KEY_ESCAPE: {
global_input.exit.state = KeyState_PRESS;
} break;
}
} break;
case GLFW_RELEASE: {
switch (key)
{
case GLFW_KEY_D: {
global_input.move_right.state = KeyState_RELEASE;
} break;
case GLFW_KEY_W: {
global_input.move_forward.state = KeyState_RELEASE;
} break;
case GLFW_KEY_A: {
global_input.move_left.state = KeyState_RELEASE;
} break;
case GLFW_KEY_S: {
global_input.move_backward.state = KeyState_RELEASE;
} break;
case GLFW_KEY_E: {
global_input.move_up.state = KeyState_RELEASE;
} break;
case GLFW_KEY_Q: {
global_input.move_down.state = KeyState_RELEASE;
} break;
case GLFW_KEY_SPACE: {
global_input.jump.state = KeyState_RELEASE;
} break;
case GLFW_KEY_RIGHT: {
global_input.action_right.state = KeyState_RELEASE;
} break;
case GLFW_KEY_UP: {
global_input.action_up.state = KeyState_RELEASE;
} break;
case GLFW_KEY_LEFT: {
global_input.action_left.state = KeyState_RELEASE;
} break;
case GLFW_KEY_ESCAPE: {
global_input.exit.state = KeyState_RELEASE;
} break;
}
} break;
}
}
void
resize_callback(GLFWwindow* window, int width, int height)
{
global_width = width;
global_height = height;
glViewport(0, 0, global_width, global_height);
}
void
error_callback(int error, const char *desc)
{
fprintf(stderr, "[ERROR] GLFW: %s\n", desc);
}
void
update(State *state)
{
V3F camera_dv = get_dv_camera_orbital(&global_input, state->camera.pos,
v3f_zero(), state->dt, 2.0f);
if (key_is_pressed(global_input.action_up)) {
camera_dv = v3f_scalef(camera_dv, 3.0f);
}
camera_dp = v3f_add(camera_dp, camera_dv);
camera_dp = v3f_scalef(camera_dp, 0.7f);
state->camera.pos = v3f_add(state->camera.pos, camera_dp);
if (key_first_press(global_input.jump))
blinn = blinn ? 0 : 1;
input_update_last_state(&global_input);
}
void
render(State *state, U32 *shaders, U32 texture, Mesh **meshes)
{
MAT4 projection, view, model;
U32 shader;
Mesh *mesh;
glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
projection = perspective(state->camera.fovx, (F32)global_width/(F32)global_height,
state->camera.near, state->camera.far);
view = look_at(state->camera.pos, v3f_zero(), v3f(0.0f, 1.0f, 0.0f));
model = mat4_identity();
U32 mesh_index = 0;
shader = shaders[mesh_index];
mesh = meshes[mesh_index];
glUseProgram(shader);
shader_set_mat4fv(shader, "projection", projection);
shader_set_mat4fv(shader, "view", view);
shader_set_mat4fv(shader, "model", model);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
shader_set_3fv(shader, "light_pos", light_pos);
shader_set_3fv(shader, "view_pos", state->camera.pos);
shader_set_1i(shader, "blinn", blinn);
mesh_draw(mesh);
mesh_index++;
shader = shaders[mesh_index];
mesh = meshes[mesh_index];
glUseProgram(shader);
shader_set_mat4fv(shader, "projection", projection);
shader_set_mat4fv(shader, "view", view);
model = mat4_make_scale(v3f(0.1f, 0.1f, 0.1f));
shader_set_mat4fv(shader, "model", model);
mesh_draw(mesh);
glBindTexture(GL_TEXTURE_2D, 0);
}
int
main(void)
{
GLFWwindow *window;
Arena *arena;
State state;
Mesh *meshes[2];
U32 shaders[2];
F64 time, last_time;
U32 texture;
glfwSetErrorCallback(error_callback);
if (glfwInit() == GLFW_FALSE)
return(1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_SAMPLES, 4);
window = glfwCreateWindow(global_width, global_height, "Blinn-Phong shading", 0, 0);
if (!window)
goto error;
glfwSetKeyCallback(window, key_callback);
glfwSetWindowSizeCallback(window, resize_callback);
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
goto error;
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
/* NOTE(pryazha): init */
arena = arena_alloc(Megabytes(64));
state.camera = (Camera){ v3f(0.0f, 0.0f, 3.0f), 90.0f, 0.1f, 1000.0f };
Vertex vertices[] = {
// positions // normals // texcoords
vertex(v3f( 10.0f, -0.5f, 10.0f), v3f(0.0f, 1.0f, 0.0f), v2f(10.0f, 0.0f)),
vertex(v3f(-10.0f, -0.5f, 10.0f), v3f(0.0f, 1.0f, 0.0f), v2f( 0.0f, 0.0f)),
vertex(v3f(-10.0f, -0.5f, -10.0f), v3f(0.0f, 1.0f, 0.0f), v2f( 0.0f, 10.0f)),
vertex(v3f( 10.0f, -0.5f, 10.0f), v3f(0.0f, 1.0f, 0.0f), v2f(10.0f, 0.0f)),
vertex(v3f(-10.0f, -0.5f, -10.0f), v3f(0.0f, 1.0f, 0.0f), v2f( 0.0f, 10.0f)),
vertex(v3f( 10.0f, -0.5f, -10.0f), v3f(0.0f, 1.0f, 0.0f), v2f(10.0f, 10.0f))
};
U32 indices[] = { 0, 1, 2, 3, 4, 5 };
meshes[0] = mesh_init(arena, vertices, 6, indices, 6);
meshes[1] = mesh_load_obj(arena, "../../data/models/cube.obj");
shaders[0] = create_shader_program("shaders/blinn_phong.vs", "shaders/blinn_phong.fs");
shaders[1] = create_shader_program("shaders/light.vs", "shaders/light.fs");
texture = load_texture("../../data/textures/wood.png");
last_time = glfwGetTime();
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
if (key_first_press(global_input.exit))
glfwSetWindowShouldClose(window, GLFW_TRUE);
update(&state);
render(&state, shaders, texture, meshes);
glfwSwapBuffers(window);
time = glfwGetTime();
state.dt = time-last_time;
last_time = time;
#if 0
fprintf(stdout, "[INFO]: dt: %f\n", state.dt);
#endif
}
glfwTerminate();
return(0);
error:
glfwTerminate();
return(1);
}
|