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.c147
1 files changed, 0 insertions, 147 deletions
diff --git a/gltfview/src/plugins/texture_view.c b/gltfview/src/plugins/texture_view.c
deleted file mode 100644
index b424158..0000000
--- a/gltfview/src/plugins/texture_view.c
+++ /dev/null
@@ -1,147 +0,0 @@
1#include "plugin.h"
2
3#include <gfx/render_backend.h>
4#include <gfx/renderer.h>
5#include <gfx/scene.h>
6#include <gfx/util/geometry.h>
7#include <gfx/util/shader.h>
8#include <gfx/util/texture.h>
9
10#include <math/camera.h>
11
12#include <assert.h>
13#include <stdlib.h>
14
15// Default texture to load if no texture is provided.
16static const char* DEFAULT_TEXTURE = "/assets/skybox/clouds1/clouds1_west.bmp";
17// static const char* DEFAULT_TEXTURE = "/assets/checkerboard.jpg";
18
19struct State {
20 Scene* scene;
21 SceneCamera* camera;
22};
23
24bool init(Game* game, State** pp_state) {
25 assert(game);
26 assert(pp_state);
27
28 State* state = calloc(1, sizeof(State));
29 if (!state) {
30 goto cleanup;
31 }
32
33 // Usage: [texture file]
34 const char* texture_file = game->argc > 1 ? game->argv[1] : DEFAULT_TEXTURE;
35
36 RenderBackend* render_backend = gfx_get_render_backend(game->gfx);
37
38 Texture* texture = gfx_load_texture(
39 render_backend, &(LoadTextureCmd){
40 .origin = TextureFromFile,
41 .type = LoadTexture,
42 .filtering = LinearFiltering,
43 .mipmaps = false,
44 .data.texture.filepath = mstring_make(texture_file)});
45 if (!texture) {
46 goto cleanup;
47 }
48
49 ShaderProgram* shader = gfx_make_view_texture_shader(render_backend);
50 if (!shader) {
51 goto cleanup;
52 }
53
54 Geometry* geometry = gfx_make_quad_11(render_backend);
55 if (!geometry) {
56 goto cleanup;
57 }
58
59 MaterialDesc material_desc = (MaterialDesc){.num_uniforms = 1};
60 material_desc.uniforms[0] = (ShaderUniform){
61 .type = UniformTexture,
62 .value.texture = texture,
63 .name = sstring_make("Texture")};
64 Material* material = gfx_make_material(&material_desc);
65 if (!material) {
66 goto cleanup;
67 }
68
69 const MeshDesc mesh_desc =
70 (MeshDesc){.geometry = geometry, .material = material, .shader = shader};
71 Mesh* mesh = gfx_make_mesh(&mesh_desc);
72 if (!mesh) {
73 goto cleanup;
74 }
75
76 SceneObject* object = gfx_make_object();
77 if (!object) {
78 goto cleanup;
79 }
80 gfx_add_object_mesh(object, mesh);
81
82 if (!(state->scene = gfx_make_scene())) {
83 goto cleanup;
84 }
85
86 SceneNode* node = gfx_make_object_node(object);
87 if (!node) {
88 goto cleanup;
89 }
90 SceneNode* root = gfx_get_scene_root(state->scene);
91 if (!root) {
92 goto cleanup;
93 }
94 gfx_set_node_parent(node, root);
95
96 if (!(state->camera = gfx_make_camera())) {
97 goto cleanup;
98 }
99
100 *pp_state = state;
101 return true;
102
103cleanup:
104 shutdown(game, state);
105 if (state) {
106 free(state);
107 }
108 return false;
109}
110
111void shutdown(Game* game, State* state) {
112 assert(game);
113 if (state) {
114 gfx_destroy_camera(&state->camera);
115 gfx_destroy_scene(&state->scene);
116 // State freed by plugin engine.
117 }
118}
119
120void render(const Game* game, const State* state) {
121 assert(game);
122 assert(state);
123
124 Renderer* renderer = gfx_get_renderer(game->gfx);
125 gfx_render_scene(
126 renderer, &(RenderSceneParams){
127 .mode = RenderDefault,
128 .scene = state->scene,
129 .camera = state->camera});
130}
131
132void resize(Game* game, State* state, int width, int height) {
133 assert(game);
134 assert(state);
135
136 RenderBackend* render_backend = gfx_get_render_backend(game->gfx);
137 gfx_set_viewport(render_backend, width, height);
138
139 const R fovy = 90 * TO_RAD;
140 const R aspect = (R)width / (R)height;
141 const R near = 0.1;
142 const R far = 1000;
143 const mat4 projection = mat4_perspective(fovy, aspect, near, far);
144
145 Camera* camera = gfx_get_camera_camera(state->camera);
146 camera->projection = projection;
147}