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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#include "plugin.h"
#include <gfx/app.h>
#include <gfx/asset.h>
#include <gfx/renderer.h>
#include <gfx/scene.h>
#include <gfx/util/skyquad.h>
#include <math/camera.h>
#include <math/spatial3.h>
#include <log/log.h>
#include <stdlib.h>
// Paths to various scene files.
static const char* BOX = "/assets/models/box.gltf";
static const char* SUZANNE = "/assets/models/suzanne.gltf";
static const char* SPONZA =
"/assets/glTF-Sample-Models/2.0/Sponza/glTF/Sponza.gltf";
static const char* FLIGHT_HELMET =
"/assets/glTF-Sample-Models/2.0/FlightHelmet/glTF/FlightHelmet.gltf";
static const char* DAMAGED_HELMET =
"/assets/glTF-Sample-Models/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf";
static const char* GIRL =
"/home/jeanne/Nextcloud/assets/models/girl/girl-with-ground.gltf";
static const char* BOXES =
"/home/jeanne/Nextcloud/assets/models/boxes/boxes.gltf";
#define DEFAULT_SCENE_FILE GIRL
static const bool RenderBoundingBoxes = false;
static const R DefaultCameraSpeed = (R)6.0;
static const R DefaultMouseSensitivity = (R)(10 * TO_RAD);
static const vec3 DefaultCameraPosition = (vec3){0, 2, 5};
typedef struct CameraCommand {
bool CameraMoveLeft : 1;
bool CameraMoveRight : 1;
bool CameraMoveForward : 1;
bool CameraMoveBackward : 1;
} CameraCommand;
typedef struct CameraController {
R camera_speed; // Camera movement speed.
R mouse_sensitivity; // Controls the degree with which mouse movements
// rotate the camera.
vec2 prev_mouse_position; // Mouse position in the previous frame.
bool rotating; // When true, subsequent mouse movements cause the
// camera to rotate.
} CameraController;
typedef struct State {
Scene* scene;
Model* model;
SceneCamera* camera;
CameraController camera_controller;
} State;
/// Load the skyquad texture.
static const Texture* load_environment_map(Gfx* gfx) {
assert(gfx);
return gfx_load_texture(
gfx, &(LoadTextureCmd){
.origin = AssetFromFile,
.type = LoadCubemap,
.colour_space = sRGB,
.filtering = NearestFiltering,
.mipmaps = false,
.data.cubemap.filepaths = {
mstring_make("/assets/skybox/clouds1/clouds1_east.bmp"),
mstring_make("/assets/skybox/clouds1/clouds1_west.bmp"),
mstring_make("/assets/skybox/clouds1/clouds1_up.bmp"),
mstring_make("/assets/skybox/clouds1/clouds1_down.bmp"),
mstring_make("/assets/skybox/clouds1/clouds1_south.bmp"),
mstring_make("/assets/skybox/clouds1/clouds1_north.bmp")}
});
}
/// Load the skyquad and return the environment light node.
static SceneNode* load_skyquad(Gfx* gfx, SceneNode* root) {
assert(gfx);
assert(root);
GfxCore* gfxcore = gfx_get_core(gfx);
const Texture* environment_map = load_environment_map(gfx);
if (!environment_map) {
return 0;
}
return gfx_setup_skyquad(gfxcore, root, environment_map);
}
/// Load the 3D scene.
/// Return the loaded model.
static Model* load_scene(Game* game, State* state, const char* scene_filepath) {
assert(game);
assert(game->gfx);
assert(state);
assert(state->scene);
Camera* camera = gfx_get_camera_camera(state->camera);
spatial3_set_position(&camera->spatial, vec3_make(0, 0, 2));
SceneNode* root = gfx_get_scene_root(state->scene);
SceneNode* sky_light_node = load_skyquad(game->gfx, root);
if (!sky_light_node) {
return 0; // test
}
Model* model = gfx_load_model(
game->gfx,
&(LoadModelCmd){
.origin = AssetFromFile, .filepath = mstring_make(scene_filepath)});
if (!model) {
return 0;
}
SceneNode* model_node = gfx_make_model_node(model);
if (!model_node) {
return 0;
}
gfx_set_node_parent(model_node, sky_light_node);
gfx_log_node_hierarchy(root);
return model;
}
bool init(Game* game, State** pp_state) {
assert(game);
// Usage: <scene file>
const char* scene_filepath =
game->argc > 1 ? game->argv[1] : DEFAULT_SCENE_FILE;
State* state = calloc(1, sizeof(State));
if (!state) {
goto cleanup;
}
if (!(state->scene = gfx_make_scene())) {
goto cleanup;
}
if (!(state->camera = gfx_make_camera())) {
goto cleanup;
}
state->model = load_scene(game, state, scene_filepath);
if (!state->model) {
goto cleanup;
}
Anima* anima = gfx_get_model_anima(state->model);
if (anima) {
gfx_play_animation(
anima, &(AnimationPlaySettings){.name = "Walk", .loop = true});
// TODO: Interpolate animations.
/*gfx_play_animation(
anima,
&(AnimationPlaySettings){.name = "Jumping-jack-lower", .loop = true});
gfx_play_animation(
anima, &(AnimationPlaySettings){
.name = "Jumping-jack-arms-mid", .loop = true});*/
}
spatial3_set_position(
&gfx_get_camera_camera(state->camera)->spatial, DefaultCameraPosition);
state->camera_controller.camera_speed = DefaultCameraSpeed;
state->camera_controller.mouse_sensitivity = DefaultMouseSensitivity;
*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.
}
}
static void update_camera(
CameraController* controller, R dt, vec2 mouse_position,
CameraCommand command, Spatial3* camera) {
assert(controller);
assert(camera);
// Translation.
const R move_x = (R)(command.CameraMoveLeft ? -1 : 0) +
(R)(command.CameraMoveRight ? 1 : 0);
const R move_y = (R)(command.CameraMoveForward ? 1 : 0) +
(R)(command.CameraMoveBackward ? -1 : 0);
const vec2 translation =
vec2_scale(vec2_make(move_x, move_y), controller->camera_speed * dt);
spatial3_move_right(camera, translation.x);
spatial3_move_forwards(camera, translation.y);
// Rotation.
if (controller->rotating) {
const vec2 mouse_delta =
vec2_sub(mouse_position, controller->prev_mouse_position);
const vec2 rotation =
vec2_scale(mouse_delta, controller->mouse_sensitivity * dt);
spatial3_global_yaw(camera, -rotation.x);
spatial3_pitch(camera, -rotation.y);
}
// Update controller state.
controller->prev_mouse_position = mouse_position;
}
void update(Game* game, State* state, double t, double dt) {
assert(game);
assert(state);
assert(state->scene);
assert(state->camera);
double mouse_x, mouse_y;
gfx_app_get_mouse_position(&mouse_x, &mouse_y);
const vec2 mouse_position = {(R)mouse_x, (R)mouse_y};
const CameraCommand camera_command = (CameraCommand){
.CameraMoveLeft = gfx_app_is_key_pressed(KeyA),
.CameraMoveRight = gfx_app_is_key_pressed(KeyD),
.CameraMoveForward = gfx_app_is_key_pressed(KeyW),
.CameraMoveBackward = gfx_app_is_key_pressed(KeyS),
};
state->camera_controller.rotating = gfx_app_is_mouse_button_pressed(LMB);
update_camera(
&state->camera_controller, (R)dt, mouse_position, camera_command,
&gfx_get_camera_camera(state->camera)->spatial);
// const vec3 orbit_point = vec3_make(0, 2, 0);
// Camera* camera = gfx_get_camera_camera(state->camera);
// spatial3_orbit(
// &camera->spatial, orbit_point,
// /*radius=*/5,
// /*azimuth=*/(R)(t * 0.5), /*zenith=*/0);
// spatial3_lookat(&camera->spatial, orbit_point);
gfx_update(state->scene, state->camera, (R)t);
}
/// Render the bounding boxes of all scene objects.
static void render_bounding_boxes_rec(
ImmRenderer* imm, const Anima* anima, const mat4* parent_model_matrix,
const SceneNode* node) {
assert(imm);
assert(node);
const mat4 model_matrix =
mat4_mul(*parent_model_matrix, gfx_get_node_transform(node));
const NodeType node_type = gfx_get_node_type(node);
if (node_type == ModelNode) {
const Model* model = gfx_get_node_model(node);
const SceneNode* root = gfx_get_model_root(model);
render_bounding_boxes_rec(imm, anima, &model_matrix, root);
} else if (node_type == AnimaNode) {
anima = gfx_get_node_anima(node);
} else if (node_type == ObjectNode) {
gfx_imm_set_model_matrix(imm, &model_matrix);
const SceneObject* obj = gfx_get_node_object(node);
const Skeleton* skeleton = gfx_get_object_skeleton(obj);
if (skeleton) { // Animated model.
assert(anima);
const size_t num_joints = gfx_get_skeleton_num_joints(skeleton);
for (size_t i = 0; i < num_joints; ++i) {
if (gfx_joint_has_box(anima, skeleton, i)) {
const Box box = gfx_get_joint_box(anima, skeleton, i);
gfx_imm_draw_box3(imm, box.vertices);
}
}
} else { // Static model.
const aabb3 box = gfx_get_object_aabb(obj);
gfx_imm_draw_aabb3(imm, box);
}
}
// Render children's boxes.
const SceneNode* child = gfx_get_node_child(node);
while (child) {
render_bounding_boxes_rec(imm, anima, &model_matrix, child);
child = gfx_get_node_sibling(child);
}
}
/// Render the bounding boxes of all scene objects.
static void render_bounding_boxes(const Game* game, const State* state) {
assert(game);
assert(state);
GfxCore* gfxcore = gfx_get_core(game->gfx);
ImmRenderer* imm = gfx_get_imm_renderer(game->gfx);
assert(gfxcore);
assert(imm);
const mat4 id = mat4_id();
Anima* anima = 0;
gfx_set_blending(gfxcore, true);
gfx_set_depth_mask(gfxcore, false);
gfx_set_polygon_offset(gfxcore, -1.5f, -1.0f);
gfx_imm_start(imm);
gfx_imm_set_camera(imm, gfx_get_camera_camera(state->camera));
gfx_imm_set_colour(imm, vec4_make(0.3, 0.3, 0.9, 0.1));
render_bounding_boxes_rec(imm, anima, &id, gfx_get_scene_root(state->scene));
gfx_imm_end(imm);
gfx_reset_polygon_offset(gfxcore);
gfx_set_depth_mask(gfxcore, true);
gfx_set_blending(gfxcore, false);
}
void render(const Game* game, const State* state) {
assert(state);
assert(game);
assert(game->gfx);
assert(state->scene);
assert(state->camera);
Renderer* renderer = gfx_get_renderer(game->gfx);
assert(renderer);
gfx_render_scene(
renderer, &(RenderSceneParams){
.mode = RenderDefault,
.scene = state->scene,
.camera = state->camera});
if (RenderBoundingBoxes) {
render_bounding_boxes(game, state);
}
}
void resize(Game* game, State* state, int width, int height) {
assert(game);
assert(state);
const R fovy = 60 * 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;
}
|