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

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

#include <math/camera.h>

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

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

State* init(Game* game) {
  assert(game);

  State* state = calloc(1, sizeof(State));
  return state;
}

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

  // Usage: [texture file]
  const char* texture_file = game->argc > 1 ? game->argv[1] : CLOUDS1_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)});

  Camera* camera = gfx_get_camera_camera(game->camera);
  spatial3_set_position(&camera->spatial, vec3_make(0, 0, 1));

  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){0};
  material_desc.uniforms[0]  = (ShaderUniform){
       .type          = UniformTexture,
       .value.texture = texture,
       .name          = sstring_make("Texture")};
  material_desc.num_uniforms = 1;
  Material* material         = gfx_make_material(&material_desc);
  if (!material) {
    return false;
  }

  MeshDesc mesh_desc = (MeshDesc){0};
  mesh_desc.geometry = geometry;
  mesh_desc.material = material;
  mesh_desc.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;
}

void update(State* state, Game* game, double t, double dt) {
  assert(state);
  assert(game);
}

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