goat3d

view src/goat3d.cc @ 17:1d85d7dd0038

goatprim done
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Sep 2013 02:29:52 +0300
parents cb6c1a945a11
children b35427826b60
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 switch(im_prim) {
364 case GOAT3D_TRIANGLES:
365 {
366 int num_faces = (int)im_mesh->vertices.size() / 3;
367 im_mesh->faces.resize(num_faces);
369 int vidx = 0;
370 for(int i=0; i<num_faces; i++) {
371 im_mesh->faces[i].v[0] = vidx++;
372 im_mesh->faces[i].v[1] = vidx++;
373 im_mesh->faces[i].v[2] = vidx++;
374 }
375 }
376 break;
378 case GOAT3D_QUADS:
379 {
380 int num_quads = (int)im_mesh->vertices.size() / 4;
381 im_mesh->faces.resize(num_quads * 2);
383 int vidx = 0;
384 for(int i=0; i<num_quads; i++) {
385 im_mesh->faces[i * 2].v[0] = vidx;
386 im_mesh->faces[i * 2].v[1] = vidx + 1;
387 im_mesh->faces[i * 2].v[2] = vidx + 2;
389 im_mesh->faces[i * 2 + 1].v[0] = vidx;
390 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
391 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
393 vidx += 4;
394 }
395 }
396 break;
398 default:
399 return;
400 };
401 }
403 void goat3d_vertex3f(float x, float y, float z)
404 {
405 im_mesh->vertices.push_back(Vector3(x, y, z));
406 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
407 im_mesh->normals.push_back(im_norm);
408 }
409 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
410 im_mesh->tangents.push_back(im_tang);
411 }
412 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
413 im_mesh->texcoords.push_back(im_texcoord);
414 }
415 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
416 im_mesh->skin_weights.push_back(im_skinw);
417 }
418 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
419 im_mesh->skin_matrices.push_back(im_skinmat);
420 }
421 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
422 im_mesh->colors.push_back(im_color);
423 }
424 }
426 void goat3d_normal3f(float x, float y, float z)
427 {
428 im_norm = Vector3(x, y, z);
429 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
430 }
432 void goat3d_tangent3f(float x, float y, float z)
433 {
434 im_tang = Vector3(x, y, z);
435 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
436 }
438 void goat3d_texcoord2f(float x, float y)
439 {
440 im_texcoord = Vector2(x, y);
441 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
442 }
444 void goat3d_skin_weight4f(float x, float y, float z, float w)
445 {
446 im_skinw = Vector4(x, y, z, w);
447 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
448 }
450 void goat3d_skin_matrix4i(int x, int y, int z, int w)
451 {
452 im_skinmat.x = x;
453 im_skinmat.y = y;
454 im_skinmat.z = z;
455 im_skinmat.w = w;
456 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
457 }
459 void goat3d_color3f(float x, float y, float z)
460 {
461 goat3d_color4f(x, y, z, 1.0f);
462 }
464 void goat3d_color4f(float x, float y, float z, float w)
465 {
466 im_color = Vector4(x, y, z, w);
467 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
468 }
470 void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
471 {
472 g->scn->add_mesh(mesh);
473 }
476 } // extern "C"
479 static long read_file(void *buf, size_t bytes, void *uptr)
480 {
481 return (long)fread(buf, 1, bytes, (FILE*)uptr);
482 }
484 static long write_file(const void *buf, size_t bytes, void *uptr)
485 {
486 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
487 }
489 static long seek_file(long offs, int whence, void *uptr)
490 {
491 if(fseek((FILE*)uptr, offs, whence) == -1) {
492 return -1;
493 }
494 return ftell((FILE*)uptr);
495 }