]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/model.h
Rework structure
[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>
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 (*before_render)(struct Model *model);
59                 void (*after_render)(struct Model *model);
60                 void (*delete)(struct Model *model);
61         } callbacks;
62         struct {
63                 unsigned int delete: 1;
64                 unsigned int frustum_culling: 1;
65                 unsigned int transparent: 1;
66         } flags;
67 } Model;
68
69 // initialize
70 void model_init();
71 // ded
72 void model_deinit();
73
74 // create empty model
75 Model *model_create();
76 // load model from file
77 Model *model_load(const char *path, const char *textures_path, Mesh *cube, ModelShader *shader);
78 // clone existing model
79 Model *model_clone(Model *model);
80 // delete model
81 void model_delete(Model *model);
82 // use this as delete callback to free all meshes associated with the model on delete
83 void model_free_meshes(Model *model);
84 // get bone locations
85 void model_get_bones(Model *model, ModelBoneMapping *mappings, size_t num_mappings);
86
87 // add a new mode
88 ModelNode *model_node_create(ModelNode *parent);
89 // recalculate transform matrices
90 void model_node_transform(ModelNode *node);
91 // add a mesh to model node (will be copied)
92 void model_node_add_mesh(ModelNode *node, const ModelMesh *mesh);
93 // add and delete batch (may add multiple meshes depending on available texture units)
94 void model_node_add_batch(ModelNode *node, ModelBatch *batch);
95
96 // create batch
97 ModelBatch *model_batch_create(ModelShader *shader, VertexLayout *layout, size_t off_tex_idx);
98 // delete batch
99 void model_batch_free(ModelBatch *batch);
100 // add a vertex to batch
101 void model_batch_add_vertex(ModelBatch *batch, GLuint texture, const void *vertex);
102
103 // add model to scene
104 void model_scene_add(Model *model);
105 // render scene
106 void model_scene_render(f64 dtime);
107
108 #endif // _MODEL_H_