goat3d

view src/goat3d.cc @ 16:cb6c1a945a11

goat3d is starting to become functional inch by inch
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Sep 2013 14:16:09 +0300
parents
children 1d85d7dd0038
line source
1 #include <string.h>
2 #include <errno.h>
3 #include "goat3d.h"
4 #include "goat3d_impl.h"
5 #include "log.h"
7 struct goat3d {
8 Scene *scn;
9 unsigned int flags;
10 };
12 struct goat3d_material : public Material {};
13 struct goat3d_mesh : public Mesh {};
14 struct goat3d_light : public Light {};
15 struct goat3d_camera : public Camera {};
16 struct goat3d_node : public Node {};
19 static long read_file(void *buf, size_t bytes, void *uptr);
20 static long write_file(const void *buf, size_t bytes, void *uptr);
21 static long seek_file(long offs, int whence, void *uptr);
23 extern "C" {
25 struct goat3d *goat3d_create(void)
26 {
27 goat3d *goat = new goat3d;
28 goat->scn = new Scene;
29 return goat;
30 }
32 void goat3d_free(struct goat3d *g)
33 {
34 delete g->scn;
35 delete g;
36 }
38 void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
39 {
40 if(val) {
41 g->flags |= (1 << (int)opt);
42 } else {
43 g->flags &= ~(1 << (int)opt);
44 }
45 }
47 int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
48 {
49 return (g->flags >> (int)opt) & 1;
50 }
52 int goat3d_load(struct goat3d *g, const char *fname)
53 {
54 FILE *fp = fopen(fname, "rb");
55 if(!fp) {
56 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
57 return -1;
58 }
60 int res = goat3d_load_file(g, fp);
61 fclose(fp);
62 return res;
63 }
65 int goat3d_save(const struct goat3d *g, const char *fname)
66 {
67 FILE *fp = fopen(fname, "wb");
68 if(!fp) {
69 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
70 return -1;
71 }
73 int res = goat3d_save_file(g, fp);
74 fclose(fp);
75 return res;
76 }
78 int goat3d_load_file(struct goat3d *g, FILE *fp)
79 {
80 goat3d_io io;
81 io.cls = fp;
82 io.read = read_file;
83 io.write = write_file;
84 io.seek = seek_file;
86 return goat3d_load_io(g, &io);
87 }
89 int goat3d_save_file(const struct goat3d *g, FILE *fp)
90 {
91 goat3d_io io;
92 io.cls = fp;
93 io.read = read_file;
94 io.write = write_file;
95 io.seek = seek_file;
97 return goat3d_save_io(g, &io);
98 }
100 int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
101 {
102 if(!g->scn->load(io)) {
103 if(g->scn->loadxml(io)) {
104 return -1;
105 }
106 }
107 return 0;
108 }
110 int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
111 {
112 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
113 return g->scn->savexml(io) ? 0 : -1;
114 }
115 return g->scn->save(io) ? 0 : -1;
116 }
118 int goat3d_set_name(struct goat3d *g, const char *name)
119 {
120 g->scn->set_name(name);
121 return 0;
122 }
124 const char *goat3d_get_name(const struct goat3d *g)
125 {
126 return g->scn->get_name();
127 }
129 void goat3d_set_ambient(struct goat3d *g, const float *amb)
130 {
131 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
132 }
134 void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
135 {
136 g->scn->set_ambient(Vector3(ar, ag, ab));
137 }
139 const float *goat3d_get_ambient(const struct goat3d *g)
140 {
141 return &g->scn->get_ambient().x;
142 }
144 // ---- materials ----
145 struct goat3d_material *goat3d_create_mtl(void)
146 {
147 return new goat3d_material;
148 }
150 void goat3d_destroy_mtl(struct goat3d_material *mtl)
151 {
152 delete mtl;
153 }
155 void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
156 {
157 mtl->name = std::string(name);
158 }
160 const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
161 {
162 return mtl->name.c_str();
163 }
165 void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
166 {
167 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
168 }
170 void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
171 {
172 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
173 }
175 void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
176 {
177 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
178 }
180 void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
181 {
182 (*mtl)[attrib].value = Vector4(r, g, b, a);
183 }
185 const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
186 {
187 return &(*mtl)[attrib].value.x;
188 }
190 void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
191 {
192 (*mtl)[attrib].map = std::string(mapname);
193 }
195 const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
196 {
197 return (*mtl)[attrib].map.c_str();
198 }
200 void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
201 {
202 g->scn->add_material(mtl);
203 }
205 // ---- meshes ----
206 struct goat3d_mesh *goat3d_create_mesh(void)
207 {
208 return new goat3d_mesh;
209 }
211 void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
212 {
213 delete mesh;
214 }
216 void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
217 {
218 mesh->name = std::string(name);
219 }
221 const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
222 {
223 return mesh->name.c_str();
224 }
226 void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
227 {
228 mesh->material = mtl;
229 }
231 struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
232 {
233 return (goat3d_material*)mesh->material;
234 }
236 int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
237 {
238 return (int)mesh->vertices.size();
239 }
241 int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
242 {
243 return (int)mesh->faces.size();
244 }
246 #if __cplusplus >= 201103L
247 #define MOVE(x) std::move(x)
248 #else
249 #define MOVE(x) x
250 #endif
252 #define VECDATA(type, data, num) \
253 MOVE(std::vector<type>((type*)(data), (type*)(data) + (num)))
255 void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
256 {
257 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
258 mesh->vertices = VECDATA(Vector3, data, vnum);
259 return;
260 }
262 if(vnum != (int)mesh->vertices.size()) {
263 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
264 return;
265 }
267 switch(attrib) {
268 case GOAT3D_MESH_ATTR_NORMAL:
269 mesh->normals = VECDATA(Vector3, data, vnum);
270 break;
271 case GOAT3D_MESH_ATTR_TANGENT:
272 mesh->tangents = VECDATA(Vector3, data, vnum);
273 break;
274 case GOAT3D_MESH_ATTR_TEXCOORD:
275 mesh->texcoords = VECDATA(Vector2, data, vnum);
276 break;
277 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
278 mesh->skin_weights = VECDATA(Vector4, data, vnum);
279 break;
280 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
281 mesh->skin_matrices = VECDATA(Int4, data, vnum);
282 break;
283 case GOAT3D_MESH_ATTR_COLOR:
284 mesh->colors = VECDATA(Vector4, data, vnum);
285 default:
286 break;
287 }
288 }
290 void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
291 {
292 return goat3d_get_mesh_attrib(mesh, attrib, 0);
293 }
295 void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
296 {
297 switch(attrib) {
298 case GOAT3D_MESH_ATTR_VERTEX:
299 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
300 case GOAT3D_MESH_ATTR_NORMAL:
301 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
302 case GOAT3D_MESH_ATTR_TANGENT:
303 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
304 case GOAT3D_MESH_ATTR_TEXCOORD:
305 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
306 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
307 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
308 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
309 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
310 case GOAT3D_MESH_ATTR_COLOR:
311 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
312 default:
313 break;
314 }
315 return 0;
316 }
319 void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
320 {
321 mesh->faces = VECDATA(Face, data, num);
322 }
324 int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
325 {
326 return goat3d_get_mesh_face(mesh, 0);
327 }
329 int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
330 {
331 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
332 }
334 // immedate mode state
335 static enum goat3d_im_primitive im_prim;
336 static struct goat3d_mesh *im_mesh;
337 static Vector3 im_norm, im_tang;
338 static Vector2 im_texcoord;
339 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
340 static Int4 im_skinmat;
341 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
344 void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
345 {
346 mesh->vertices.clear();
347 mesh->normals.clear();
348 mesh->tangents.clear();
349 mesh->texcoords.clear();
350 mesh->skin_weights.clear();
351 mesh->skin_matrices.clear();
352 mesh->colors.clear();
353 mesh->faces.clear();
355 im_mesh = mesh;
356 memset(im_use, 0, sizeof im_use);
358 im_prim = prim;
359 }
361 void goat3d_end(void)
362 {
363 static int tri_offs[] = {0, 1, 2};
364 static int quad_offs[] = {0, 1, 2, 0, 2, 3};
365 int *index_offs;
367 int num_faces, in_face_verts, out_face_verts;
368 switch(im_prim) {
369 case GOAT3D_TRIANGLES:
370 in_face_verts = 3;
371 out_face_verts = 3;
372 index_offs = tri_offs;
373 break;
375 case GOAT3D_QUADS:
376 in_face_verts = 4;
377 out_face_verts = 6;
378 index_offs = quad_offs;
379 break;
381 default:
382 return;
383 };
385 num_faces = (int)im_mesh->vertices.size() / in_face_verts;
386 if(!num_faces) {
387 return;
388 }
390 im_mesh->faces.resize(num_faces);
392 int vidx = 0;
393 for(int i=0; i<num_faces; i++) {
394 for(int j=0; j<out_face_verts; j++) {
395 im_mesh->faces[i].v[j] = vidx + index_offs[j];
396 }
397 vidx += 4;
398 }
399 }
401 void goat3d_vertex3f(float x, float y, float z)
402 {
403 im_mesh->vertices.push_back(Vector3(x, y, z));
404 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
405 im_mesh->normals.push_back(im_norm);
406 }
407 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
408 im_mesh->tangents.push_back(im_tang);
409 }
410 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
411 im_mesh->texcoords.push_back(im_texcoord);
412 }
413 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
414 im_mesh->skin_weights.push_back(im_skinw);
415 }
416 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
417 im_mesh->skin_matrices.push_back(im_skinmat);
418 }
419 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
420 im_mesh->colors.push_back(im_color);
421 }
422 }
424 void goat3d_normal3f(float x, float y, float z)
425 {
426 im_norm = Vector3(x, y, z);
427 }
429 void goat3d_tangent3f(float x, float y, float z)
430 {
431 im_tang = Vector3(x, y, z);
432 }
434 void goat3d_texcoord2f(float x, float y)
435 {
436 im_texcoord = Vector2(x, y);
437 }
439 void goat3d_skin_weight4f(float x, float y, float z, float w)
440 {
441 im_skinw = Vector4(x, y, z, w);
442 }
444 void goat3d_skin_matrix4i(int x, int y, int z, int w)
445 {
446 im_skinmat.x = x;
447 im_skinmat.y = y;
448 im_skinmat.z = z;
449 im_skinmat.w = w;
450 }
452 void goat3d_color3f(float x, float y, float z)
453 {
454 im_color = Vector4(x, y, z, 1.0f);
455 }
457 void goat3d_color4f(float x, float y, float z, float w)
458 {
459 im_color = Vector4(x, y, z, w);
460 }
462 void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
463 {
464 g->scn->add_mesh(mesh);
465 }
468 } // extern "C"
471 static long read_file(void *buf, size_t bytes, void *uptr)
472 {
473 return (long)fread(buf, 1, bytes, (FILE*)uptr);
474 }
476 static long write_file(const void *buf, size_t bytes, void *uptr)
477 {
478 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
479 }
481 static long seek_file(long offs, int whence, void *uptr)
482 {
483 if(fseek((FILE*)uptr, offs, whence) == -1) {
484 return -1;
485 }
486 return ftell((FILE*)uptr);
487 }