summaryrefslogtreecommitdiff
path: root/game/src/plugins/texture_view.c
blob: 52dff5791fcac7a0dd28a95be83a02a0215066f9 (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
#include "plugin.h"

#include <gfx/asset.h>
#include <gfx/render_backend.h>
#include <gfx/renderer.h>
#include <gfx/scene.h>
#include <gfx/util/geometry.h>
#include <gfx/util/shader.h>

#include <math/camera.h>

#include <assert.h>
#include <stdlib.h>

// Default texture to load if no texture is provided.
static const char* DEFAULT_TEXTURE = "/assets/skybox/clouds1/clouds1_west.bmp";
// static const char* DEFAULT_TEXTURE = "/assets/checkerboard.jpg";

struct State {
  Scene*       scene;
  SceneCamera* camera;
};

bool init(Game* game, State** pp_state) {
  assert(game);
  assert(pp_state);

  State* state = calloc(1, sizeof(State));
  if (!state) {
    goto cleanup;
  }

  // Usage: [texture file]
  const char* texture_file = game->argc > 1 ? game->argv[1] : DEFAULT_TEXTURE;

  RenderBackend* render_backend = gfx_get_render_backend(game->gfx);

  const Texture* texture = gfx_load_texture(
      game->gfx, &(LoadTextureCmd){
                     .origin                = AssetFromFile,
                     .type                  = LoadTexture,
                     .filtering             = LinearFiltering,
                     .mipmaps               = false,
                     .data.texture.filepath = mstring_make(texture_file)});
  if (!texture) {
    goto cleanup;
  }

  ShaderProgram* shader = gfx_make_view_texture_shader(render_backend);
  if (!shader) {
    goto cleanup;
  }

  Geometry* geometry = gfx_make_quad_11(render_backend);
  if (!geometry) {
    goto cleanup;
  }

  MaterialDesc material_desc = (MaterialDesc){.num_uniforms = 1};
  material_desc.uniforms[0]  = (ShaderUniform){
       .type          = UniformTexture,
       .value.texture = texture,
       .name          = sstring_make("Texture")};
  Material* material = gfx_make_material(&material_desc);
  if (!material) {
    goto cleanup;
  }

  const MeshDesc mesh_desc =
      (MeshDesc){.geometry = geometry, .material = material, .shader = shader};
  Mesh* mesh = gfx_make_mesh(&mesh_desc);
  if (!mesh) {
    goto cleanup;
  }

  SceneObject* object =
      gfx_make_object(&(ObjectDesc){.num_meshes = 1, .meshes = {mesh}});
  if (!object) {
    goto cleanup;
  }

  if (!(state->scene = gfx_make_scene())) {
    goto cleanup;
  }

  SceneNode* node = gfx_make_object_node(object);
  if (!node) {
    goto cleanup;
  }
  SceneNode* root = gfx_get_scene_root(state->scene);
  if (!root) {
    goto cleanup;
  }
  gfx_set_node_parent(node, root);

  if (!(state->camera = gfx_make_camera())) {
    goto cleanup;
  }

  *pp_state = state;
  return true;

cleanup:
  shutdown(game, state);
  if (state) {
    free(state);
  }
  return false;
}

void shutdown(Game* game, State* state) {
  assert(game);
  if (state) {
    gfx_destroy_camera(&state->camera);
    gfx_destroy_scene(&state->scene);
    // State freed by plugin engine.
  }
}

void render(const Game* game, const State* state) {
  assert(game);
  assert(state);

  Renderer* renderer = gfx_get_renderer(game->gfx);
  gfx_render_scene(
      renderer, &(RenderSceneParams){
                    .mode   = RenderDefault,
                    .scene  = state->scene,
                    .camera = state->camera});
}

void resize(Game* game, State* state, int width, int height) {
  assert(game);
  assert(state);

  const R    fovy       = 90 * TO_RAD;
  const R    aspect     = (R)width / (R)height;
  const R    near       = 0.1;
  const R    far        = 1000;
  const mat4 projection = mat4_perspective(fovy, aspect, near, far);

  Camera* camera     = gfx_get_camera_camera(state->camera);
  camera->projection = projection;
}