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
|
#include "include/honey.h"
honey_window window;
unsigned int screen_width = 640;
unsigned int screen_height = 480;
honey_camera camera;
float cameraSpeed = 3.0;
float camera_roll_speed = 1.0;
const float cameraMouseSensitivity = 0.1;
honey_mesh cube;
mat4 model;
honey_shader cube_shader;
honey_texture container;
honey_texture happy_face;
honey_mesh light_cube;
vec3 light_color = { 1, 0, 0 };
mat4 light_model;
honey_shader light_shader;
bool wireframe = false;
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void framebufferResizeCallback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
screen_width = width;
screen_height = height;
}
void mouseCallback(GLFWwindow* window, double x, double y) {
static float prevX, prevY;
static bool firstMouse = true;
if (firstMouse) {
prevX = x;
prevY = y;
firstMouse = false;
}
float xOffset = x - prevX;
float yOffset = y - prevY;
prevX = x;
prevY = y;
xOffset *= cameraMouseSensitivity;
yOffset *= cameraMouseSensitivity;
float yaw = glm_deg(camera.angle[1]) + xOffset;
float pitch = glm_deg(camera.angle[0]) - yOffset;
if (pitch > 89) { pitch = 89; }
if (pitch < -89) { pitch = -89; }
camera.angle[0] = glm_rad(pitch);
camera.angle[1] = glm_rad(yaw);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void toggle_wireframe(void* data, int action) {
if (action == HONEY_KEY_PRESS) { wireframe = !wireframe; }
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void update(float dt) {
glfwPollEvents();
glm_rotate_x(model, glm_rad(10*dt), model);
if (honey_key_down(HONEY_KEY_ESCAPE)) {
glfwSetWindowShouldClose(window, true);
}
if (honey_key_down(HONEY_KEY_W)) {
vec3 step;
glm_vec3_scale(camera.look_direction, cameraSpeed*dt, step);
glm_vec3_add(camera.position, step, camera.position);
}
if (honey_key_down(HONEY_KEY_S)) {
vec3 step;
glm_vec3_scale(camera.look_direction, -cameraSpeed*dt, step);
glm_vec3_add(camera.position, step, camera.position);
}
if (honey_key_down(HONEY_KEY_A)) {
vec3 step;
glm_vec3_scale(camera.right, cameraSpeed*dt, step);
glm_vec3_add(camera.position, step, camera.position);
}
if (honey_key_down(HONEY_KEY_D)) {
vec3 step;
glm_vec3_scale(camera.right, -cameraSpeed*dt, step);
glm_vec3_add(camera.position, step, camera.position);
}
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void draw() {
glClearColor(0.4f, 0.4f, 0.4f, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
honey_camera_calculate_view(&camera);
honey_shader_set_mat4(cube_shader, "view", camera.view);
honey_shader_set_mat4(light_shader, "view", camera.view);
honey_shader_set_mat4(cube_shader, "model", model);
honey_shader_set_mat4(light_shader, "model", light_model);
honey_texture_use(container, 0);
honey_texture_use(happy_face, 1);
honey_mesh_draw(cube, cube_shader);
honey_mesh_draw(light_cube, light_shader);
glfwSwapBuffers(window);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int main() {
window = honey_setup(screen_width, screen_height, "hello, world!");
honey_set_resize_callback(window, framebufferResizeCallback);
honey_set_mouse_move_callback(window, mouseCallback);
honey_key_bind(HONEY_KEY_F, toggle_wireframe, NULL);
/* load container texture */
if (honey_texture_new(&container, "container.jpg", false) != TEXTURE_OK) {
return 1;
}
/* load happy face texture */
if (honey_texture_new(&happy_face, "happy.png", true) != TEXTURE_OK) {
return 1;
}
if (honey_shader_load(&cube_shader, "demo.vs", "demo.fs") != SHADER_OK) {
return 1;
}
if (honey_shader_load(&light_shader, "light.vs", "light.fs") != SHADER_OK) {
return 1;
}
honey_shader_set_vec3(light_shader, "light_color", light_color);
if (honey_mesh_new_textured_cube(&cube, 1, 1, 1) != MESH_OK) {
fprintf(stderr, "Failed to load cube\n");
return 1;
}
if (honey_mesh_new_cube(&light_cube, 0.5, 0.5, 0.5) != MESH_OK) {
return 1;
}
glm_mat4_identity(light_model);
glm_translate(light_model, (vec3){4, 5, 0});
honey_shader_set_mat4(light_shader, "model", light_model);
honey_shader_set_int(cube_shader, "box_texture", 0);
honey_shader_set_int(cube_shader, "happy_texture", 1);
honey_shader_set_vec3(cube_shader, "light_color", light_color);
glm_mat4_identity(model);
//glm_rotate_x(model, glm_rad(-55), model);
honey_shader_set_mat4(cube_shader, "model", model);
vec3 camera_pos = { -4, 0, 0 };
vec3 camera_angle = { 0, 0, 0 };
float camera_near = 0.1;
float camera_far = 100;
float camera_fov = glm_rad(45);
float camera_aspect_ratio = ((float) screen_width)/screen_height;
honey_camera_new_perspective(&camera,
camera_pos,
camera_angle,
camera_aspect_ratio,
camera_near, camera_far,
camera_fov);
honey_shader_set_mat4(cube_shader, "view", camera.view);
honey_shader_set_mat4(cube_shader, "projection", camera.projection);
honey_shader_set_mat4(light_shader, "projection", camera.projection);
glEnable(GL_DEPTH_TEST);
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
honey_set_update_callback(&update);
honey_set_draw_callback(&draw);
honey_run(window);
honey_mesh_delete(cube);
honey_shader_delete(cube_shader);
honey_quit();
return 0;
}
/*
,d88b.d88b,
88888888888
`Y8888888Y'
`Y888Y'
`Y'
*/
|