return -1;
}
+#define HAS_VN 1
+#define HAS_VT 2
+
int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
{
+ static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"};
int i, j, num, nelem;
+ unsigned int aflags = 0;
if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
return -1;
}
if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
+ aflags |= HAS_VN;
nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
if((num = dynarr_size(cm->vattr[CMESH_ATTR_NORMAL].data)) != cm->nverts * nelem) {
warning_log("normal array size (%d) != nverts (%d)\n", num, cm->nverts);
}
if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
+ aflags |= HAS_VT;
nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem;
if((num = dynarr_size(cm->vattr[CMESH_ATTR_TEXCOORD].data)) != cm->nverts * nelem) {
warning_log("texcoord array size (%d) != nverts (%d)\n", num, cm->nverts);
fputc('f', fp);
for(j=0; j<3; j++) {
unsigned int idx = *idxptr++ + 1 + voffs;
- fprintf(fp, " %u/%u/%u", idx, idx, idx);
+ fprintf(fp, fmtstr[aflags], idx, idx, idx);
}
fputc('\n', fp);
}
for(i=0; i<numtri; i++) {
fputc('f', fp);
for(j=0; j<3; j++) {
- fprintf(fp, " %u/%u/%u", idx, idx, idx);
+ fprintf(fp, fmtstr[aflags], idx, idx, idx);
++idx;
}
fputc('\n', fp);
{
int i, line_num = 0, result = -1;
int found_quad = 0;
+ int found_color = 0;
FILE *fp = 0;
char buf[256];
struct vertex_pos_color *varr = 0;
fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
goto err;
}
+ if(num > 3) found_color = 1;
switch(num) {
case 3:
v.r = 1.0f;
if((node = rb_find(rbtree, &fv))) {
unsigned int idx = (unsigned int)node->data;
+ assert(idx < cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX));
if(cmesh_push_index(mesh, idx) == -1) {
fprintf(stderr, "load_mesh: failed to resize index array\n");
goto err;
unsigned int newidx = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
struct facevertex *newfv;
struct vertex_pos_color *vptr = varr + fv.vidx;
- float tu, tv;
if(cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, vptr->x, vptr->y, vptr->z) == -1) {
fprintf(stderr, "load_mesh: failed to resize vertex array\n");
goto err;
}
- if(cmesh_push_attrib(mesh, CMESH_ATTR_COLOR, &vptr->r) == -1) {
- fprintf(stderr, "load_mesh: failed to resize color array\n");
- goto err;
+ if(found_color) {
+ if(cmesh_push_attrib(mesh, CMESH_ATTR_COLOR, &vptr->r) == -1) {
+ fprintf(stderr, "load_mesh: failed to resize color array\n");
+ goto err;
+ }
}
- if(fv.tidx >= 0) {
- tu = tarr[fv.tidx].x;
- tv = tarr[fv.tidx].y;
- } else {
- tu = vptr->x;
- tv = vptr->y;
+ if(fv.nidx >= 0) {
+ float nx = narr[fv.nidx].x;
+ float ny = narr[fv.nidx].y;
+ float nz = narr[fv.nidx].z;
+ if(cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, nx, ny, nz) == -1) {
+ fprintf(stderr, "load_mesh: failed to resize normal array\n");
+ goto err;
+ }
}
- if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) {
- fprintf(stderr, "load_mesh: failed to resize texcoord array\n");
- goto err;
+ if(fv.tidx >= 0) {
+ float tu = tarr[fv.tidx].x;
+ float tv = tarr[fv.tidx].y;
+ if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) {
+ fprintf(stderr, "load_mesh: failed to resize texcoord array\n");
+ goto err;
+ }
}
if((newfv = malloc(sizeof *newfv))) {