summaryrefslogtreecommitdiff
path: root/gltfview/src/plugins/texture_view.c
diff options
context:
space:
mode:
Diffstat (limited to 'gltfview/src/plugins/texture_view.c')
-rw-r--r--gltfview/src/plugins/texture_view.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/gltfview/src/plugins/texture_view.c b/gltfview/src/plugins/texture_view.c
new file mode 100644
index 0000000..f2c650f
--- /dev/null
+++ b/gltfview/src/plugins/texture_view.c
@@ -0,0 +1,94 @@
1#include "texture_view.h"
2
3#include <gfx/render_backend.h>
4#include <gfx/util/geometry.h>
5#include <gfx/util/shader.h>
6#include <gfx/util/texture.h>
7
8#include <math/camera.h>
9
10#include <assert.h>
11#include <stdlib.h>
12
13// Default texture to load if no texture is provided.
14static const char* CLOUDS1_TEXTURE = "/assets/skybox/clouds1/clouds1_west.bmp";
15
16State* init(Game* game) {
17 assert(game);
18
19 State* state = calloc(1, sizeof(State));
20 return state;
21}
22
23bool boot(State* state, Game* game) {
24 assert(state);
25 assert(game);
26
27 // Usage: [texture file]
28 const char* texture_file = game->argc > 1 ? game->argv[1] : CLOUDS1_TEXTURE;
29
30 RenderBackend* render_backend = gfx_get_render_backend(game->gfx);
31
32 Texture* texture = gfx_load_texture(
33 render_backend, &(LoadTextureCmd){
34 .origin = TextureFromFile,
35 .type = LoadTexture,
36 .filtering = LinearFiltering,
37 .mipmaps = false,
38 .data.texture.filepath = mstring_make(texture_file)});
39
40 Camera* camera = gfx_get_camera_camera(game->camera);
41 spatial3_set_position(&camera->spatial, vec3_make(0, 0, 1));
42
43 ShaderProgram* shader = gfx_make_view_texture_shader(render_backend);
44 if (!shader) {
45 return false;
46 }
47
48 Geometry* geometry = gfx_make_quad_11(render_backend);
49 if (!geometry) {
50 return false;
51 }
52
53 MaterialDesc material_desc = (MaterialDesc){0};
54 material_desc.uniforms[0] = (ShaderUniform){
55 .type = UniformTexture,
56 .value.texture = texture,
57 .name = sstring_make("Texture")};
58 material_desc.num_uniforms = 1;
59 Material* material = gfx_make_material(&material_desc);
60 if (!material) {
61 return false;
62 }
63
64 MeshDesc mesh_desc = (MeshDesc){0};
65 mesh_desc.geometry = geometry;
66 mesh_desc.material = material;
67 mesh_desc.shader = shader;
68 Mesh* mesh = gfx_make_mesh(&mesh_desc);
69 if (!mesh) {
70 return false;
71 }
72
73 SceneObject* object = gfx_make_object();
74 if (!object) {
75 return false;
76 }
77 gfx_add_object_mesh(object, mesh);
78
79 SceneNode* node = gfx_make_object_node(object);
80 SceneNode* root = gfx_get_scene_root(game->scene);
81 gfx_set_node_parent(node, root);
82
83 return true;
84}
85
86void update(State* state, Game* game, double t, double dt) {
87 assert(state);
88 assert(game);
89}
90
91void render(State* state, const Game* game) {
92 assert(state);
93 assert(game);
94}