summaryrefslogtreecommitdiff
path: root/gltfview/src/plugins/texture_view.c
blob: f16c8d145573cc8944c3b8223f4d9249728c0867 (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 "texture_view.h"

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

#include <math/camera.h>

#include <assert.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";

bool boot(State* state, Game* game) {
  assert(game);

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

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

  Texture* texture = gfx_load_texture(
      render_backend, &(LoadTextureCmd){
                          .origin                = TextureFromFile,
                          .type                  = LoadTexture,
                          .filtering             = LinearFiltering,
                          .mipmaps               = false,
                          .data.texture.filepath = mstring_make(texture_file)});
  if (!texture) {
    return false;
  }

  ShaderProgram* shader = gfx_make_view_texture_shader(render_backend);
  if (!shader) {
    return false;
  }

  Geometry* geometry = gfx_make_quad_11(render_backend);
  if (!geometry) {
    return false;
  }

  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) {
    return false;
  }

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

  SceneObject* object = gfx_make_object();
  if (!object) {
    return false;
  }
  gfx_add_object_mesh(object, mesh);

  SceneNode* node = gfx_make_object_node(object);
  SceneNode* root = gfx_get_scene_root(game->scene);
  gfx_set_node_parent(node, root);

  return true;
}