]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/model.h
8ca5ed274b663e3c9cbf36f47f164348c119d367
[dragonblocks_alpha.git] / src / client / model.h
1 #ifndef _MODEL_H_
2 #define _MODEL_H_
3
4 #include <dragonstd/array.h>
5 #include <dragonstd/list.h>
6 #include <GL/glew.h>
7 #include <GL/gl.h>
8 #include <linmath.h/linmath.h>
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include "client/mesh.h"
12 #include "client/texture.h"
13 #include "types.h"
14
15 typedef struct {
16         GLuint prog;
17         GLint loc_transform;
18 } ModelShader;
19
20 typedef struct {
21         ModelShader *shader;
22         VertexLayout *layout;
23         size_t off_tex_idx;
24         Array textures;
25 } ModelBatch;
26
27 typedef struct ModelNode {
28         char *name;
29         v3f32 pos, rot, scale;
30         mat4x4 abs, rel;
31         Array meshes;
32         struct ModelNode *parent;
33         List children;
34         unsigned int visible: 1;
35         unsigned int clockwise: 1;
36 } ModelNode;
37
38 typedef struct {
39         Mesh *mesh;
40         GLuint *textures;
41         GLuint num_textures;
42         ModelShader *shader;
43 } ModelMesh;
44
45 typedef struct {
46         char *name;
47         ModelNode **node;
48 } ModelBoneMapping;
49
50 typedef struct Model {
51         ModelNode *root;
52         void *extra;
53         aabb3f32 box;
54         f32 distance;
55         struct Model *replace;
56         struct {
57                 void (*step)(struct Model *model, f64 dtime);
58                 void (*delete)(struct Model *model);
59         } callbacks;
60         struct {
61                 unsigned int delete: 1;
62                 unsigned int wireframe: 1;
63                 unsigned int frustum_culling: 1;
64                 unsigned int transparent: 1;
65         } flags;
66 } Model;
67
68 // initialize
69 void model_init();
70 // ded
71 void model_deinit();
72
73 // create empty model
74 Model *model_create();
75 // load model from file
76 Model *model_load(const char *path, const char *textures_path, Mesh *cube, ModelShader *shader);
77 // clone existing model
78 Model *model_clone(Model *model);
79 // delete model
80 void model_delete(Model *model);
81 // use this as delete callback to free all meshes associated with the model on delete
82 void model_free_meshes(Model *model);
83 // get bone locations
84 void model_get_bones(Model *model, ModelBoneMapping *mappings, size_t num_mappings);
85
86 // add a new mode
87 ModelNode *model_node_create(ModelNode *parent);
88 // recalculate transform matrices
89 void model_node_transform(ModelNode *node);
90 // add a mesh to model node (will be copied)
91 void model_node_add_mesh(ModelNode *node, const ModelMesh *mesh);
92 // add and delete batch (may add multiple meshes depending on available texture units)
93 void model_node_add_batch(ModelNode *node, ModelBatch *batch);
94
95 // create batch
96 ModelBatch *model_batch_create(ModelShader *shader, VertexLayout *layout, size_t off_tex_idx);
97 // delete batch
98 void model_batch_free(ModelBatch *batch);
99 // add a vertex to batch
100 void model_batch_add_vertex(ModelBatch *batch, GLuint texture, const void *vertex);
101
102 // add model to scene
103 void model_scene_add(Model *model);
104 // render scene
105 void model_scene_render(f64 dtime);
106
107 #endif // _MODEL_H_