latest goat3d changes
[deeprace] / libs / goat3d / src / g3dscn.h
1 /*
2 goat3d - 3D scene, and animation file format library.
3 Copyright (C) 2013-2023  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef GOAT3D_SCENE_H_
19 #define GOAT3D_SCENE_H_
20
21 #include "cgmath/cgmath.h"
22 #include "goat3d.h"
23 #include "g3danm.h"
24 #include "aabox.h"
25
26 enum {
27         OBJTYPE_UNKNOWN,
28         OBJTYPE_MESH,
29         OBJTYPE_LIGHT,
30         OBJTYPE_CAMERA
31 };
32
33 enum {
34         LTYPE_POINT,
35         LTYPE_DIR,
36         LTYPE_SPOT
37 };
38
39 enum {
40         CAMTYPE_PRS,
41         CAMTYPE_TARGET
42 };
43
44
45 struct face {
46         int v[3];
47 };
48
49 typedef struct int4 {
50         int x, y, z, w;
51 } int4;
52
53 struct material_attrib {
54         char *name;
55         cgm_vec4 value;
56         char *map;
57 };
58
59 struct goat3d_material {
60         char *name;
61         int idx;
62         struct material_attrib *attrib; /* dynarr */
63 };
64
65
66 #define OBJECT_COMMON   \
67         int type; \
68         char *name; \
69         cgm_vec3 pos; \
70         cgm_quat rot; \
71         cgm_vec3 scale; \
72         void *next
73
74 struct object {
75         OBJECT_COMMON;
76 };
77
78 struct goat3d_mesh {
79         OBJECT_COMMON;
80         struct goat3d_material *mtl;
81
82         /* dynamic arrays */
83         cgm_vec3 *vertices;
84         cgm_vec3 *normals;
85         cgm_vec3 *tangents;
86         cgm_vec2 *texcoords;
87         cgm_vec4 *skin_weights;
88         int4 *skin_matrices;
89         cgm_vec4 *colors;
90         struct face *faces;
91         struct goat3d_node **bones;
92 };
93
94 struct goat3d_light {
95         OBJECT_COMMON;
96         int ltype;
97         cgm_vec3 color;
98         cgm_vec3 attenuation;
99         float max_dist;
100         cgm_vec3 dir;                                   /* for LTYPE_DIR */
101         float inner_cone, outer_cone;   /* for LTYPE_SPOT */
102 };
103
104 struct goat3d_camera {
105         OBJECT_COMMON;
106         int camtype;
107         float fov;
108         float near_clip, far_clip;
109         cgm_vec3 target, up;
110 };
111
112 struct goat3d_node {
113         char *name;
114         enum goat3d_node_type type;
115         void *obj;
116         int child_count;
117
118         cgm_vec3 pivot;
119         /* local transformation */
120         cgm_vec3 pos, scale;
121         cgm_quat rot;
122         /* values from animation evaluation, take precedence over the above if
123          * has_anim is true
124          */
125         cgm_vec3 apos, ascale;
126         cgm_quat arot;
127         int has_anim;
128         /* matrix computed from the above*/
129         float matrix[16];
130         int matrix_valid;
131
132         struct goat3d_node *parent;
133         struct goat3d_node *child;
134         struct goat3d_node *next;
135 };
136
137
138 struct goat3d {
139         unsigned int flags;
140         char *search_path;
141
142         char *name;
143         cgm_vec3 ambient;
144
145         /* dynamic arrays */
146         struct goat3d_material **materials;
147         struct goat3d_mesh **meshes;
148         struct goat3d_light **lights;
149         struct goat3d_camera **cameras;
150         struct goat3d_node **nodes;
151         struct goat3d_anim **anims;
152
153         struct aabox bbox;
154         int bbox_valid;
155
156         /* namegen */
157         unsigned int namecnt[7];
158         char namebuf[64];
159 };
160
161 extern int goat3d_log_level;
162
163 /* defined in goat3d.c, declared here to keep them out of the public API */
164 int goat3d_init(struct goat3d *g);
165 void goat3d_destroy(struct goat3d *g);
166
167 void goat3d_clear(struct goat3d *g);
168
169 /* defined in g3dscn.c */
170 int g3dimpl_obj_init(struct object *o, int type);
171 void g3dimpl_obj_destroy(struct object *o);
172
173 void g3dimpl_mesh_bounds(struct aabox *bb, struct goat3d_mesh *m, float *xform);
174
175 int g3dimpl_mtl_init(struct goat3d_material *mtl);
176 void g3dimpl_mtl_destroy(struct goat3d_material *mtl);
177 struct material_attrib *g3dimpl_mtl_findattr(struct goat3d_material *mtl, const char *name);
178 struct material_attrib *g3dimpl_mtl_getattr(struct goat3d_material *mtl, const char *name);
179
180 void g3dimpl_node_bounds(struct aabox *bb, struct goat3d_node *n);
181
182 /*
183 void io_fprintf(goat3d_io *io, const char *fmt, ...);
184 void io_vfprintf(goat3d_io *io, const char *fmt, va_list ap);
185 */
186
187 /* defined in read.c */
188 int g3dimpl_scnload(struct goat3d *g, struct goat3d_io *io);
189 int g3dimpl_anmload(struct goat3d *g, struct goat3d_io *io);
190
191 /* defined in write.c */
192 int g3dimpl_scnsave(const struct goat3d *g, struct goat3d_io *io);
193 int g3dimpl_anmsave(const struct goat3d *g, struct goat3d_io *io);
194
195 /* defined in extmesh.c */
196 int g3dimpl_loadmesh(struct goat3d_mesh *mesh, const char *fname);
197
198 /* defined in readgltf.c */
199 int g3dimpl_loadgltf(struct goat3d *g, struct goat3d_io *io);
200
201 #endif  /* GOAT3D_SCENE_H_ */