summaryrefslogtreecommitdiff
path: root/demo.c
blob: 26e54e0d2880f3659e19eb7f87efbd188552dbf9 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "include/honey.h"

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;

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 processInput(GLFWwindow* window, float dt) {
  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);
  }
}

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

int main() {
  honey_window window = honey_setup(screen_width, screen_height, "hello, world!");

  honey_set_resize_callback(window, framebufferResizeCallback);
  honey_set_mouse_move_callback(window, mouseCallback);

  /* load box texture */
  unsigned int boxTex;
  glGenTextures(1, &boxTex);
  glBindTexture(GL_TEXTURE_2D, boxTex);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  int imgWidth, imgHeight, imgChannels;
  unsigned char* imageData = stbi_load("container.jpg", &imgWidth, &imgHeight, &imgChannels, 0);
  if (imageData == NULL) {
    fprintf(stderr, "ERROR: failed to load 'container.jpg'\n");
    return 1;
  }

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
  glGenerateMipmap(GL_TEXTURE_2D);
  stbi_image_free(imageData);

  /* load happy face texture */
  unsigned int happyTex;
  glGenTextures(1, &happyTex);
  glBindTexture(GL_TEXTURE_2D, happyTex);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  imageData = stbi_load("happy.png", &imgWidth, &imgHeight, &imgChannels, 0);
  if (imageData == NULL) {
    fprintf(stderr, "ERROR: failed to load 'happy.png'\n");
    return 1;
  }

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
  glGenerateMipmap(GL_TEXTURE_2D);
  stbi_image_free(imageData);
  
  honey_shader shader;
  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 };

  honey_mesh cube;
  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);

  mat4 model, view, projection;
  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);

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

  float prevTime = 0;
  float currentTime = 0;
  float dt = 0;

  while(!glfwWindowShouldClose(window)) {
    currentTime = (float) glfwGetTime();
    dt = currentTime - prevTime;
    prevTime = currentTime;

    processInput(window, dt);

    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);
    
    glm_rotate_x(model, glm_rad(10*dt), model);
    honey_shader_set_matrix_4fv(shader, "model", (float*) model);
    
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, boxTex);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, happyTex);

    honey_mesh_draw(cube, shader);
    
    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  honey_mesh_delete(cube);
  honey_shader_delete(shader);

  honey_quit();

  return 0;
}

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