goat3d

view src/goat3d.h @ 69:66cd8266f078

fixed animation export
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Apr 2014 03:44:10 +0300
parents dad392c710df 76d0f55f9d5f
children 9785847d52d4
line source
1 /*
2 goat3d - 3D scene, character, and animation file format library.
3 Copyright (C) 2013-2014 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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_H_
19 #define GOAT3D_H_
21 #include <stdio.h>
22 #include <stdlib.h>
24 #ifdef WIN32
25 #define GOAT3DAPI __declspec(dllexport)
26 #else
27 #define GOAT3DAPI
28 #endif
30 #define GOAT3D_MAT_ATTR_DIFFUSE "diffuse"
31 #define GOAT3D_MAT_ATTR_SPECULAR "specular"
32 #define GOAT3D_MAT_ATTR_SHININESS "shininess"
33 #define GOAT3D_MAT_ATTR_NORMAL "normal"
34 #define GOAT3D_MAT_ATTR_BUMP "bump"
35 #define GOAT3D_MAT_ATTR_REFLECTION "reflection"
36 #define GOAT3D_MAT_ATTR_TRANSMISSION "transmission"
37 #define GOAT3D_MAT_ATTR_IOR "ior"
38 #define GOAT3D_MAT_ATTR_ALPHA "alpha"
40 enum goat3d_mesh_attrib {
41 GOAT3D_MESH_ATTR_VERTEX,
42 GOAT3D_MESH_ATTR_NORMAL,
43 GOAT3D_MESH_ATTR_TANGENT,
44 GOAT3D_MESH_ATTR_TEXCOORD,
45 GOAT3D_MESH_ATTR_SKIN_WEIGHT,
46 GOAT3D_MESH_ATTR_SKIN_MATRIX,
47 GOAT3D_MESH_ATTR_COLOR,
49 NUM_GOAT3D_MESH_ATTRIBS
50 };
52 enum goat3d_node_type {
53 GOAT3D_NODE_NULL,
54 GOAT3D_NODE_MESH,
55 GOAT3D_NODE_LIGHT,
56 GOAT3D_NODE_CAMERA
57 };
59 /* immediate mode mesh construction primitive type */
60 enum goat3d_im_primitive {
61 GOAT3D_TRIANGLES,
62 GOAT3D_QUADS
63 };
66 enum goat3d_option {
67 GOAT3D_OPT_SAVEXML, /* save in XML format */
69 NUM_GOAT3D_OPTIONS
70 };
72 struct goat3d;
73 struct goat3d_material;
74 struct goat3d_mesh;
75 struct goat3d_light;
76 struct goat3d_camera;
77 struct goat3d_node;
79 struct goat3d_io {
80 void *cls; /* closure data */
82 long (*read)(void *buf, size_t bytes, void *uptr);
83 long (*write)(const void *buf, size_t bytes, void *uptr);
84 long (*seek)(long offs, int whence, void *uptr);
85 };
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
91 /* construction/destruction */
92 GOAT3DAPI struct goat3d *goat3d_create(void);
93 GOAT3DAPI void goat3d_free(struct goat3d *g);
95 GOAT3DAPI void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val);
96 GOAT3DAPI int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt);
98 /* load/save */
99 GOAT3DAPI int goat3d_load(struct goat3d *g, const char *fname);
100 GOAT3DAPI int goat3d_save(const struct goat3d *g, const char *fname);
102 GOAT3DAPI int goat3d_load_file(struct goat3d *g, FILE *fp);
103 GOAT3DAPI int goat3d_save_file(const struct goat3d *g, FILE *fp);
105 GOAT3DAPI int goat3d_load_io(struct goat3d *g, struct goat3d_io *io);
106 GOAT3DAPI int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io);
108 /* load/save animation files (g must already be loaded to load animations) */
109 GOAT3DAPI int goat3d_load_anim(struct goat3d *g, const char *fname);
110 GOAT3DAPI int goat3d_save_anim(const struct goat3d *g, const char *fname);
112 GOAT3DAPI int goat3d_load_anim_file(struct goat3d *g, FILE *fp);
113 GOAT3DAPI int goat3d_save_anim_file(const struct goat3d *g, FILE *fp);
115 GOAT3DAPI int goat3d_load_anim_io(struct goat3d *g, struct goat3d_io *io);
116 GOAT3DAPI int goat3d_save_anim_io(const struct goat3d *g, struct goat3d_io *io);
118 /* misc scene properties */
119 GOAT3DAPI int goat3d_set_name(struct goat3d *g, const char *name);
120 GOAT3DAPI const char *goat3d_get_name(const struct goat3d *g);
122 GOAT3DAPI void goat3d_set_ambient(struct goat3d *g, const float *ambient);
123 GOAT3DAPI void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab);
124 GOAT3DAPI const float *goat3d_get_ambient(const struct goat3d *g);
126 /* materials */
127 GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl);
128 GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g);
129 GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx);
130 GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name);
132 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void);
133 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl);
135 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name);
136 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl);
138 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val);
139 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val);
140 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b);
141 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a);
142 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib);
144 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname);
145 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib);
147 /* meshes */
148 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh);
149 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g);
150 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx);
151 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name);
153 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void);
154 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh);
156 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name);
157 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh);
159 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl);
160 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh);
162 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib);
163 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh);
165 /* sets all the data for a single vertex attribute array in one go.
166 * vnum is the number of *vertices* to be set, not the number of floats, ints or whatever
167 * data is expected to be something different depending on the attribute:
168 * - GOAT3D_MESH_ATTR_VERTEX - 3 floats per vertex
169 * - GOAT3D_MESH_ATTR_NORMAL - 3 floats per vertex
170 * - GOAT3D_MESH_ATTR_TANGENT - 3 floats per vertex
171 * - GOAT3D_MESH_ATTR_TEXCOORD - 2 floats per vertex
172 * - GOAT3D_MESH_ATTR_SKIN_WEIGHT - 4 floats per vertex
173 * - GOAT3D_MESH_ATTR_SKIN_MATRIX - 4 ints per vertex
174 * - GOAT3D_MESH_ATTR_COLOR - 4 floats per vertex
175 */
176 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
177 const void *data, int vnum);
178 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, float val);
179 GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
180 float x, float y);
181 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
182 float x, float y, float z);
183 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
184 float x, float y, float z, float w);
185 /* returns a pointer to the beginning of the requested mesh attribute array */
186 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib);
187 /* returns a pointer to the requested mesh attribute */
188 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx);
190 /* sets all the faces in one go. data is an array of 3 int vertex indices per face */
191 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int fnum);
192 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c);
193 /* returns a pointer to the beginning of the face index array */
194 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh);
195 /* returns a pointer to a face index */
196 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx);
198 /* immediate mode OpenGL-like interface for setting mesh data
199 * NOTE: using this interface will result in no vertex sharing between faces
200 * NOTE2: the immedate mode interface is not thread-safe, either use locks, or don't
201 * use it at all in multithreaded situations.
202 */
203 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim);
204 GOAT3DAPI void goat3d_end(void);
205 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z);
206 GOAT3DAPI void goat3d_normal3f(float x, float y, float z);
207 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z);
208 GOAT3DAPI void goat3d_texcoord2f(float x, float y);
209 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w);
210 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w);
211 GOAT3DAPI void goat3d_color3f(float x, float y, float z);
212 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w);
214 /* lights */
215 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt);
216 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g);
217 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx);
218 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name);
220 GOAT3DAPI struct goat3d_light *goat3d_create_light(void);
221 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt);
223 /* cameras */
224 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam);
225 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g);
226 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx);
227 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name);
229 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void);
230 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam);
232 /* nodes */
233 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node);
234 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g);
235 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx);
236 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name);
238 GOAT3DAPI struct goat3d_node *goat3d_create_node(void);
239 GOAT3DAPI void goat3d_destroy_node(struct goat3d_node *node);
241 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name);
242 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node);
244 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj);
245 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node);
246 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node);
248 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child);
249 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node);
250 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx);
251 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node);
253 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx);
254 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t);
255 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name);
256 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t);
258 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which);
259 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node);
261 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node);
262 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root);
264 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name);
265 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node);
267 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec);
268 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec);
269 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec);
270 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz);
272 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec);
273 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec);
274 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec);
275 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr);
277 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec);
279 #ifdef __cplusplus
280 }
281 #endif
283 #endif /* GOAT3D_H_ */