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