goat3d

view src/goat3d.h @ 54:dad392c710df

added copyright headers and license files + readme
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Apr 2014 08:50:36 +0300
parents 498ca7ac7047
children 99715321ad6d
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 struct goat3d_node *root, 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, const struct goat3d_node *root, 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, const struct goat3d_node *root, 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);
129 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void);
130 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl);
132 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name);
133 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl);
135 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val);
136 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val);
137 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b);
138 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a);
139 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib);
141 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname);
142 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib);
144 /* meshes */
145 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh);
146 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g);
147 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx);
148 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name);
150 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void);
151 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh);
153 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name);
154 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh);
156 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl);
157 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh);
159 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib);
160 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh);
162 /* sets all the data for a single vertex attribute array in one go.
163 * vnum is the number of *vertices* to be set, not the number of floats, ints or whatever
164 * data is expected to be something different depending on the attribute:
165 * - GOAT3D_MESH_ATTR_VERTEX - 3 floats per vertex
166 * - GOAT3D_MESH_ATTR_NORMAL - 3 floats per vertex
167 * - GOAT3D_MESH_ATTR_TANGENT - 3 floats per vertex
168 * - GOAT3D_MESH_ATTR_TEXCOORD - 2 floats per vertex
169 * - GOAT3D_MESH_ATTR_SKIN_WEIGHT - 4 floats per vertex
170 * - GOAT3D_MESH_ATTR_SKIN_MATRIX - 4 ints per vertex
171 * - GOAT3D_MESH_ATTR_COLOR - 4 floats per vertex
172 */
173 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
174 const void *data, int vnum);
175 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, float val);
176 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
177 float x, float y, float z);
178 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
179 float x, float y, float z, float w);
180 /* returns a pointer to the beginning of the requested mesh attribute array */
181 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib);
182 /* returns a pointer to the requested mesh attribute */
183 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx);
185 /* sets all the faces in one go. data is an array of 3 int vertex indices per face */
186 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int fnum);
187 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c);
188 /* returns a pointer to the beginning of the face index array */
189 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh);
190 /* returns a pointer to a face index */
191 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx);
193 /* immediate mode OpenGL-like interface for setting mesh data
194 * NOTE: using this interface will result in no vertex sharing between faces
195 * NOTE2: the immedate mode interface is not thread-safe, either use locks, or don't
196 * use it at all in multithreaded situations.
197 */
198 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim);
199 GOAT3DAPI void goat3d_end(void);
200 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z);
201 GOAT3DAPI void goat3d_normal3f(float x, float y, float z);
202 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z);
203 GOAT3DAPI void goat3d_texcoord2f(float x, float y);
204 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w);
205 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w);
206 GOAT3DAPI void goat3d_color3f(float x, float y, float z);
207 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w);
209 /* lights */
210 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt);
211 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g);
212 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx);
213 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name);
215 GOAT3DAPI struct goat3d_light *goat3d_create_light(void);
216 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt);
218 /* cameras */
219 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam);
220 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g);
221 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx);
222 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name);
224 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void);
225 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam);
227 /* nodes */
228 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node);
229 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g);
230 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx);
231 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name);
233 GOAT3DAPI struct goat3d_node *goat3d_create_node(void);
234 GOAT3DAPI void goat3d_destroy_node(struct goat3d_node *node);
236 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name);
237 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node);
239 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj);
240 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node);
241 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node);
243 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child);
244 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node);
245 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx);
246 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node);
248 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx);
249 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t);
250 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name);
251 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t);
253 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which);
254 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node);
256 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node);
257 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root);
259 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name);
260 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node);
262 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec);
263 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec);
264 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec);
265 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz);
267 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec);
268 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec);
269 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec);
270 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr);
272 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec);
274 #ifdef __cplusplus
275 }
276 #endif
278 #endif /* GOAT3D_H_ */