summaryrefslogtreecommitdiff
path: root/examples/obj_loading/ex_obj_loading.c
blob: 906816ba812cfe99a6bed59d8ce1142bab265412 (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
#include <assert.h>
#include <glfw3.h>
#include <string.h>

#include "camera.h"
#include "core.h"
#include "maths.h"
#include "maths_types.h"
#include "render.h"
#include "render_types.h"

const vec3 pointlight_positions[4] = {
  { 0.7, 0.2, 2.0 },
  { 2.3, -3.3, -4.0 },
  { -4.0, 2.0, -12.0 },
  { 0.0, 0.0, -3.0 },
};
point_light point_lights[4];

int main() {
  core* core = core_bringup();

  // --- Set up our scene

  // 1. load model from disk
  model_handle backpack_handle =
      model_load_obj(core, "assets/models/obj/backpack/backpack.obj", true);
  model* backpack = &core->models->data[backpack_handle.raw];
  assert(backpack->meshes->data->is_skinned == false);
  // 2. upload vertex data to gpu
  model_upload_meshes(&core->renderer, backpack);
  // 3. create a camera
  vec3 camera_pos = vec3(3., 2., 10.);
  vec3 camera_front = vec3_normalise(vec3_negate(camera_pos));
  camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0));
  // 4. create lights

  // directional (sun) light setup
  directional_light dir_light = { .direction = (vec3){ -0.2, -1.0, -0.3 },
                                  .ambient = (vec3){ 0.2, 0.2, 0.2 },
                                  .diffuse = (vec3){ 0.5, 0.5, 0.5 },
                                  .specular = (vec3){ 1.0, 1.0, 1.0 } };
  // point lights setup
  for (int i = 0; i < 4; i++) {
    point_lights[i].position = pointlight_positions[i];
    point_lights[i].ambient = (vec3){ 0.05, 0.05, 0.05 };
    point_lights[i].diffuse = (vec3){ 0.8, 0.8, 0.8 };
    point_lights[i].specular = (vec3){ 1.0, 1.0, 1.0 };
    point_lights[i].constant = 1.0;
    point_lights[i].linear = 0.09;
    point_lights[i].quadratic = 0.032;
  }

  scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 };
  memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4]));

  // --- Enter Main loop
  while (!should_exit(core)) {
    input_update(&core->input);
    threadpool_process_results(&core->threadpool, 1);

    render_frame_begin(&core->renderer);

    // Draw the backpack
    transform model_tf = transform_create(vec3(0.0, -0.4, 0.0), quat_ident(),
                                          1.8);  // make the backpack a bit bigger
    mat4 model_matrix = transform_to_mat(&model_tf);
    draw_model(&core->renderer, &cam, backpack, &model_matrix, &our_scene);

    render_frame_end(&core->renderer);
  }

  return 0;
}