goat3d

view src/goat3d.h @ 103:45a9d493e98c

fixed the input latency issue by calling QWidget::update() instead of QGLWidget::updateGL() update schedules an update instead of redrawing immediately.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Sep 2015 17:40:02 +0300
parents 7941e89798e5
children
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 GOAT3DAPI int goat3d_get_bounds(const struct goat3d *g, float *bmin, float *bmax);
128 /* materials */
129 GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl);
130 GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g);
131 GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx);
132 GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name);
134 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void);
135 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl);
137 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name);
138 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl);
140 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val);
141 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val);
142 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b);
143 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a);
144 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib);
146 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname);
147 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib);
149 /* meshes */
150 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh);
151 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g);
152 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx);
153 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name);
155 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void);
156 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh);
158 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name);
159 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh);
161 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl);
162 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh);
164 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib);
165 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh);
167 /* sets all the data for a single vertex attribute array in one go.
168 * vnum is the number of *vertices* to be set, not the number of floats, ints or whatever
169 * data is expected to be something different depending on the attribute:
170 * - GOAT3D_MESH_ATTR_VERTEX - 3 floats per vertex
171 * - GOAT3D_MESH_ATTR_NORMAL - 3 floats per vertex
172 * - GOAT3D_MESH_ATTR_TANGENT - 3 floats per vertex
173 * - GOAT3D_MESH_ATTR_TEXCOORD - 2 floats per vertex
174 * - GOAT3D_MESH_ATTR_SKIN_WEIGHT - 4 floats per vertex
175 * - GOAT3D_MESH_ATTR_SKIN_MATRIX - 4 ints per vertex
176 * - GOAT3D_MESH_ATTR_COLOR - 4 floats per vertex
177 */
178 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
179 const void *data, int vnum);
180 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, float val);
181 GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
182 float x, float y);
183 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
184 float x, float y, float z);
185 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
186 float x, float y, float z, float w);
187 /* returns a pointer to the beginning of the requested mesh attribute array */
188 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib);
189 /* returns a pointer to the requested mesh attribute */
190 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx);
192 /* sets all the faces in one go. data is an array of 3 int vertex indices per face */
193 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int fnum);
194 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c);
195 /* returns a pointer to the beginning of the face index array */
196 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh);
197 /* returns a pointer to a face index */
198 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx);
200 /* immediate mode OpenGL-like interface for setting mesh data
201 * NOTE: using this interface will result in no vertex sharing between faces
202 * NOTE2: the immedate mode interface is not thread-safe, either use locks, or don't
203 * use it at all in multithreaded situations.
204 */
205 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim);
206 GOAT3DAPI void goat3d_end(void);
207 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z);
208 GOAT3DAPI void goat3d_normal3f(float x, float y, float z);
209 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z);
210 GOAT3DAPI void goat3d_texcoord2f(float x, float y);
211 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w);
212 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w);
213 GOAT3DAPI void goat3d_color3f(float x, float y, float z);
214 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w);
216 GOAT3DAPI void goat3d_get_mesh_bounds(const struct goat3d_mesh *mesh, float *bmin, float *bmax);
218 /* lights */
219 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt);
220 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g);
221 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx);
222 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name);
224 GOAT3DAPI struct goat3d_light *goat3d_create_light(void);
225 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt);
227 /* cameras */
228 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam);
229 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g);
230 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx);
231 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name);
233 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void);
234 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam);
236 /* nodes */
237 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node);
238 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g);
239 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx);
240 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name);
242 GOAT3DAPI struct goat3d_node *goat3d_create_node(void);
243 GOAT3DAPI void goat3d_destroy_node(struct goat3d_node *node);
245 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name);
246 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node);
248 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj);
249 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node);
250 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node);
252 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child);
253 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node);
254 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx);
255 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node);
257 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx);
258 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t);
259 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name);
260 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t);
262 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which);
263 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node);
265 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node);
266 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root);
268 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name);
269 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node);
271 GOAT3DAPI long goat3d_get_anim_timeline(struct goat3d_node *root, long *tstart, long *tend);
273 GOAT3DAPI int goat3d_get_node_position_key_count(struct goat3d_node *node);
274 GOAT3DAPI int goat3d_get_node_rotation_key_count(struct goat3d_node *node);
275 GOAT3DAPI int goat3d_get_node_scaling_key_count(struct goat3d_node *node);
276 GOAT3DAPI long goat3d_get_node_position_key(struct goat3d_node *node, int idx, float *xptr, float *yptr, float *zptr);
277 GOAT3DAPI long goat3d_get_node_rotation_key(struct goat3d_node *node, int idx, float *xptr, float *yptr, float *zptr, float *wptr);
278 GOAT3DAPI long goat3d_get_node_scaling_key(struct goat3d_node *node, int idx, float *xptr, float *yptr, float *zptr);
280 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec);
281 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec);
282 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec);
283 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz);
285 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec);
286 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec);
287 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec);
288 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr);
290 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec);
292 GOAT3DAPI void goat3d_get_node_bounds(const struct goat3d_node *node, float *bmin, float *bmax);
294 #ifdef __cplusplus
295 }
296 #endif
298 #endif /* GOAT3D_H_ */