summaryrefslogtreecommitdiff
path: root/demo.c
blob: 94fdd66aefac80e8dd920da468855914fec02e4a (plain)
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
#include "include/honey.h"

honey_window window;

unsigned int screen_width = 640;
unsigned int screen_height = 480;

vec3 cameraPosition, cameraFacing, cameraUp;

vec3 cameraPosition = { 0, 0, 3 };
vec3 cameraFacing = { 0, 0, -1 };
vec3 cameraUp = { 0, 1, 0 };
float cameraSpeed = 2.0;
float cameraPitch = 0;
float cameraYaw = 0;
const float cameraMouseSensitivity = 0.1;

honey_mesh cube;
honey_shader shader;
honey_texture container;
honey_texture happy_face;

mat4 model, view, projection;

bool wireframe = false;
bool fKeyDown = 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;

  cameraYaw += xOffset;
  cameraPitch -= yOffset;

  if (cameraPitch > 89) { cameraPitch = 89; }
  if (cameraPitch < -89) { cameraPitch = -89; }

  cameraFacing[0] = cos(glm_rad(cameraYaw))*cos(glm_rad(cameraPitch));
  cameraFacing[1] = sin(glm_rad(cameraPitch));
  cameraFacing[2] = sin(glm_rad(cameraYaw)) * cos(glm_rad(cameraPitch));
  glm_vec3_normalize(cameraFacing);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

void update(float dt) {
  glfwPollEvents();

  glm_rotate_x(model, glm_rad(10*dt), model);
  
  if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
    glfwSetWindowShouldClose(window, true);
  }
  if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS) {
    if (!fKeyDown) {
      wireframe = !wireframe;
      fKeyDown = true;
    }
  }
  if (glfwGetKey(window, GLFW_KEY_F) == GLFW_RELEASE) {
    fKeyDown = false;
  }

  if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
    vec3 step;
    glm_vec3_scale(cameraFacing, cameraSpeed*dt, step);
    glm_vec3_add(cameraPosition, step, cameraPosition);
  }
  if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
    vec3 step;
    glm_vec3_scale(cameraFacing, -cameraSpeed*dt, step);
    glm_vec3_add(cameraPosition, step, cameraPosition);
  }
  if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
    vec3 direction, step;
    glm_vec3_cross(cameraFacing, cameraUp, direction);
    glm_vec3_normalize(direction);
    glm_vec3_scale(direction, -cameraSpeed*dt, step);
    glm_vec3_add(cameraPosition, step, cameraPosition);
  }
  if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
    vec3 direction, step;
    glm_vec3_cross(cameraFacing, cameraUp, direction);
    glm_vec3_normalize(direction);
    glm_vec3_scale(direction, cameraSpeed*dt, step);
    glm_vec3_add(cameraPosition, step, cameraPosition);
  }
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

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);
  }

  glm_perspective_default(((float)screen_width)/screen_height, projection);
  honey_shader_set_matrix_4fv(shader, "projection", (float*) projection);

  vec3 cameraDirection;
  glm_vec3_add(cameraPosition, cameraFacing, cameraDirection);
  glm_lookat(cameraPosition, cameraDirection, cameraUp, view);
  honey_shader_set_matrix_4fv(shader, "view", (float*) view);
    
  honey_shader_set_matrix_4fv(shader, "model", (float*) model);
    
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, container.texture_id);
  glActiveTexture(GL_TEXTURE1);
  glBindTexture(GL_TEXTURE_2D, happy_face.texture_id);

  honey_mesh_draw(cube, 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);

  /* 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(&shader, "demo.vs", "demo.fs") != SHADER_OK) {
    return 1;
  }

  /* create triangle */
  float vertices[] = {
    /* positions           colors          tex coords */
    -0.5, -0.5,  0.5,     1.0, 0.0, 0.0,    0.0, 0.0,
    0.5,  -0.5,  0.5,     0.0, 1.0, 0.0,    1.0, 0.0,
    -0.5,  0.5,  0.5,     0.0, 0.0, 1.0,    0.0, 1.0,
    0.5,   0.5,  0.5,     1.0, 1.0, 1.0,    1.0, 1.0, 
    -0.5, -0.5, -0.5,     1.0, 0.0, 0.0,    0.0, 0.0,
    0.5,  -0.5, -0.5,     0.0, 1.0, 0.0,    1.0, 0.0,
    -0.5,  0.5, -0.5,     0.0, 0.0, 1.0,    0.0, 1.0,
    0.5,   0.5, -0.5,     1.0, 1.0, 1.0,    1.0, 1.0 };

  unsigned int indices[] = { 0, 1, 2,
                             1, 2, 3,
                             4, 5, 6,
                             5, 6, 7,
                             0, 2, 4,
                             2, 4, 6,
                             1, 3, 5,
                             3, 5, 7,
                             2, 3, 6,
                             3, 6, 7,
                             0, 1, 4,
                             1, 4, 5 };

  unsigned int attribute_sizes[] = { 3, 3, 2 }; /* position, color, texture coordinate */
  enum honey_mesh_result result = honey_mesh_new(&cube,
                                                 vertices, 8, 3, attribute_sizes,
                                                 indices,
                                                 sizeof(indices)/sizeof(unsigned int));
  if (result != MESH_OK) {
    fprintf(stderr, "Failed to load cube\n");
    return 1;
  }

  honey_shader_set_int(shader, "boxTexture", 0);
  honey_shader_set_int(shader, "happyTexture", 1);

  glm_mat4_identity(model);
  glm_rotate_x(model, glm_rad(-55), model);
  honey_shader_set_matrix_4fv(shader, "model", (float*) model);

  glm_lookat(cameraPosition, cameraFacing, cameraUp, view);
  honey_shader_set_matrix_4fv(shader, "view", (float*) view);

  glm_mat4_identity(projection);
  /* glm_perspective(glm_rad(90), float(screen_width)/screen_height, 0.1, 100); */
  glm_perspective_default(((float)screen_width)/screen_height, projection);
  honey_shader_set_matrix_4fv(shader, "projection", (float*) 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(shader);

  honey_quit();

  return 0;
}

/*
           ,d88b.d88b,
           88888888888
           `Y8888888Y'
             `Y888Y'
               `Y'
*/