summaryrefslogtreecommitdiff
path: root/examples/game_demo/game_demo.c
blob: 96888081e72623c246e00882b64d54272f7f03fc (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
// Load a model
// Animate it
// Collide with the ground
// Have a skybox

#include <assert.h>
#include "camera.h"
#include "core.h"
#include "input.h"
#include "keys.h"
#include "loaders.h"
#include "maths.h"
#include "primitives.h"
#include "ral_types.h"
#include "render.h"
#include "render_scene.h"
#include "render_types.h"
#include "shadows.h"
#include "skybox.h"
#include "terrain.h"

static const char* faces[6] = { "assets/demo/skybox/right.jpg", "assets/demo/skybox/left.jpg",
                                "assets/demo/skybox/top.jpg",   "assets/demo/skybox/bottom.jpg",
                                "assets/demo/skybox/front.jpg", "assets/demo/skybox/back.jpg" };

int main() {
  Core_Bringup();

  // TODO: Load humanoid model + weapon
  // TODO: Animate it with WASD keys
  // TODO: Skybox (ALMOST)
  // TODO: Add a ground terrain
  // TODO: Move camera with model

  // --- Render Scene
  Vec3 camera_pos = vec3(0.0, 1.3, 3.0);
  Camera cam = Camera_Create(camera_pos, VEC3_NEG_Z, VEC3_Y, 45.0);
  SetCamera(cam);  // update the camera in RenderScene

  DirectionalLight sun = {
    .ambient = vec3(1.0, 1.0, 1.0),
  };
  SetMainLight(sun);

  // --- Terrain
  Heightmap hmap = Heightmap_FromImage(str8("assets/test_heightmap.png"));
  Terrain_Storage* terrain = Render_GetTerrainStorage();
  Terrain_LoadHeightmap(terrain, hmap, 2.0, false);
  // assert(Terrain_IsActive());

  // --- Skybox
  Skybox skybox = Skybox_Create(faces, 6);

  // --- Models
  // ModelHandle player_model = ModelLoad_gltf("Player Model", "assets/demo/player.gltf");
  // ModelHandle sword_model = ModelLoad("Sword Model", "assets/demo/sword.gltf");

  // create a wooden crate - loads mesh and material directly rather than via loading a model from a
  // gltf file
  Geometry cube_geo = Geo_CreateCuboid(f32x3(2.0, 2.0, 2.0));
  Mesh crate_mesh = Mesh_Create(&cube_geo, false);  // dont free as we may use later
  TextureHandle albedo_map = TextureLoadFromFile("assets/demo/crate/Wood_Crate_001_basecolor.jpg");
  TextureHandle roughness_map =
      TextureLoadFromFile("assets/demo/crate/Wood_Crate_001_roughness.jpg");
  TextureHandle normal_map = TextureLoadFromFile("assets/demo/crate/Wood_Crate_001_normal.jpg");
  TextureHandle ao_map =
      TextureLoadFromFile("assets/demo/crate/Wood_Crate_001_ambientOcclusion.jpg");
  Material crate_mat = { .name = "Wood_Crate",
                         .kind = MAT_PBR,
                         .metal_roughness_combined = true,
                         .pbr_albedo_map = albedo_map,
                         .pbr_metallic_map = roughness_map,
                         .pbr_normal_map = normal_map,
                         .pbr_ao_map = ao_map };

  // ModelHandle cube_handle = ModelLoad_gltf("assets/models/gltf/Cube/glTF/Cube.gltf", false);
  ModelHandle cube_handle = ModelLoad_gltf("../../assets/prototyper/prototyper_m.gltf", false);
  Model* Cube = MODEL_GET(cube_handle);
  RenderEnt cube_r = { .mesh = &Cube->meshes->data[0],
                       .material = &Cube->materials->data[0],
                       .affine = mat4_ident(),
                       .casts_shadows = true };

  RenderEnt crate_renderable = {
    .mesh = &crate_mesh, .material = &crate_mat, .affine = mat4_scale(3.0), .casts_shadows = true
  };

  RenderEnt entities[] = { cube_r, crate_renderable };
  size_t entity_count = 1;

  // --- Transforms
  // TransformHierarchy* scene_tree =  TransformHierarchy_Create();
  // TODO: parent camera to model - to start with I can just manually update it every frame
  // TODO: query joints of the gltf to get the hand bone to parent a sword to

  bool draw_debug = true;

  while (!ShouldExit()) {
    Frame_Begin();
    if (key_just_released(KEYCODE_TAB)) {
      draw_debug = !draw_debug;
    }

    Camera_Update(&cam);
    SetCamera(cam);

    // BEGIN Draw calls

    Shadow_Run(entities, entity_count);

    if (draw_debug) {
      // draw the player model with shadows
      Render_RenderEntities(entities, entity_count);
      // Render_DrawTerrain();
      Skybox_Draw(&skybox, cam);
    } else {
      Shadow_DrawDebugQuad();
    }

    Terrain_Draw(terrain);

    // END Draw calls
    Frame_Draw();
    Frame_End();
  }

  Core_Shutdown();
  return 0;
}