#include <assert.h>
#include "opengl.h"
#include "cmesh.h"
+#include "logger.h"
struct cmesh_vattrib {
int nelem; /* num elements per attribute [1, 4] */
- float *data; /* dynarr */
+ float *data;
+ unsigned int count;
unsigned int vbo;
int vbo_valid, data_valid;
};
unsigned int buffer_objects[CMESH_NUM_ATTR + 1];
struct cmesh_vattrib vattr[CMESH_NUM_ATTR];
- unsigned int *idata; /* dynarr */
+ unsigned int *idata;
+ unsigned int icount;
unsigned int ibo;
int ibo_valid, idata_valid;
glGenBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
for(i=0; i<CMESH_NUM_ATTR; i++) {
- if(!(cm->vattr[i].data = dynarr_alloc(0, sizeof(float)))) {
- cmesh_destroy(cm);
- return -1;
- }
cm->vattr[i].vbo = cm->buffer_objects[i];
}
cm->ibo = cm->buffer_objects[CMESH_NUM_ATTR];
- if(!(cm->idata = dynarr_alloc(0, sizeof *cm->idata))) {
- cmesh_destroy(cm);
- return -1;
- }
return 0;
}
free(cm->name);
for(i=0; i<CMESH_NUM_ATTR; i++) {
- dynarr_free(cm->vattr[i].data);
+ free(cm->vattr[i].data);
}
- dynarr_free(cm->idata);
+ free(cm->idata);
glDeleteBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
if(cm->wire_ibo) {
cm->vattr[i].nelem = 0;
cm->vattr[i].vbo_valid = 0;
cm->vattr[i].data_valid = 0;
- cm->vattr[i].data = dynarr_clear(cm->vattr[i].data);
+ free(cm->vattr[i].data);
+ cm->vattr[i].data = 0;
+ cm->vattr[i].count = 0;
}
cm->ibo_valid = cm->idata_valid = 0;
- cm->idata = dynarr_clear(cm->idata);
+ free(cm->idata);
+ cm->idata = 0;
+ cm->icount = 0;
cm->wire_ibo_valid = 0;
cm->nverts = cm->nfaces = 0;
int cmesh_clone(struct cmesh *cmdest, struct cmesh *cmsrc)
{
- int i, num, nelem;
+ int i, nelem;
char *name = 0;
float *varr[CMESH_NUM_ATTR] = {0};
unsigned int *iarr = 0;
strcpy(name, cmsrc->name);
}
if(cmesh_indexed(cmsrc)) {
- num = dynarr_size(cmsrc->idata);
- if(!(iarr = dynarr_alloc(num, sizeof *iarr))) {
+ if(!(iarr = malloc(cmsrc->icount * sizeof *iarr))) {
free(name);
return -1;
}
for(i=0; i<CMESH_NUM_ATTR; i++) {
if(cmesh_has_attrib(cmsrc, i)) {
nelem = cmsrc->vattr[i].nelem;
- num = dynarr_size(cmsrc->vattr[i].data);
- if(!(varr[i] = dynarr_alloc(num * nelem, sizeof(float)))) {
+ if(!(varr[i] = malloc(cmsrc->vattr[i].count * nelem * sizeof(float)))) {
while(--i >= 0) {
- dynarr_free(varr[i]);
+ free(varr[i]);
}
- dynarr_free(iarr);
+ free(iarr);
free(name);
return -1;
}
cmesh_clear(cmdest);
for(i=0; i<CMESH_NUM_ATTR; i++) {
- dynarr_free(cmdest->vattr[i].data);
+ free(cmdest->vattr[i].data);
if(cmesh_has_attrib(cmsrc, i)) {
cmesh_attrib(cmsrc, i); /* force validation of the actual data on the source mesh */
nelem = cmsrc->vattr[i].nelem;
cmdest->vattr[i].nelem = nelem;
- num = dynarr_size(cmsrc->vattr[i].data);
cmdest->vattr[i].data = varr[i];
- memcpy(cmdest->vattr[i].data, cmsrc->vattr[i].data, num * nelem * sizeof(float));
+ cmdest->vattr[i].count = cmsrc->vattr[i].count;
+ memcpy(cmdest->vattr[i].data, cmsrc->vattr[i].data, cmsrc->vattr[i].count * nelem * sizeof(float));
cmdest->vattr[i].data_valid = 1;
cmdest->vattr[i].vbo_valid = 0;
} else {
}
}
- dynarr_free(cmdest->idata);
+ free(cmdest->idata);
if(cmesh_indexed(cmsrc)) {
cmesh_index(cmsrc); /* force validation .... */
- num = dynarr_size(cmsrc->idata);
cmdest->idata = iarr;
- memcpy(cmdest->idata, cmsrc->idata, num * sizeof *cmdest->idata);
+ cmdest->icount = cmsrc->icount;
+ memcpy(cmdest->idata, cmsrc->idata, cmsrc->icount * sizeof *cmdest->idata);
cmdest->idata_valid = 1;
} else {
cmdest->idata = 0;
return 0;
}
- if(!(newarr = dynarr_alloc(num * nelem, sizeof *newarr))) {
+ if(!(newarr = malloc(num * nelem * sizeof *newarr))) {
return 0;
}
if(vdata) {
cm->nverts = num;
- dynarr_free(cm->vattr[attr].data);
+ free(cm->vattr[attr].data);
cm->vattr[attr].data = newarr;
+ cm->vattr[attr].count = num * nelem;
cm->vattr[attr].nelem = nelem;
cm->vattr[attr].data_valid = 1;
cm->vattr[attr].vbo_valid = 0;
const float *cmesh_attrib_ro(struct cmesh *cm, int attr)
{
- float *tmp;
+ void *tmp;
int nelem;
if(attr < 0 || attr >= CMESH_NUM_ATTR) {
/* local data copy unavailable, grab the data from the vbo */
nelem = cm->vattr[attr].nelem;
- if(!(tmp = dynarr_resize(cm->vattr[attr].data, cm->nverts * nelem))) {
+ if(!(cm->vattr[attr].data = malloc(cm->nverts * nelem * sizeof(float)))) {
return 0;
}
- cm->vattr[attr].data = tmp;
+ cm->vattr[attr].count = cm->nverts * nelem;
glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[attr].vbo);
tmp = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
cm->vattr[attr].nelem = def_nelem[attr];
}
- cursz = dynarr_size(cm->vattr[attr].data);
+ cursz = cm->vattr[attr].count;
newsz = cursz + cm->vattr[attr].nelem;
- if(!(vptr = dynarr_resize(cm->vattr[attr].data, newsz))) {
+ if(!(vptr = realloc(cm->vattr[attr].data, newsz * sizeof(float)))) {
return -1;
}
cm->vattr[attr].data = vptr;
+ cm->vattr[attr].count = newsz;
vptr += cursz;
for(i=0; i<cm->vattr[attr].nelem; i++) {
return 0;
}
- if(!(tmp = dynarr_alloc(num, sizeof *tmp))) {
+ if(!(tmp = malloc(num * sizeof *tmp))) {
return 0;
}
if(indices) {
memcpy(tmp, indices, num * sizeof *tmp);
}
- dynarr_free(cm->idata);
+ free(cm->idata);
cm->idata = tmp;
+ cm->icount = num;
cm->idata_valid = 1;
cm->ibo_valid = 0;
return tmp;
/* local copy is unavailable, grab the data from the ibo */
nidx = cm->nfaces * 3;
- if(!(tmp = dynarr_alloc(nidx, sizeof *cm->idata))) {
+ if(!(tmp = malloc(nidx * sizeof *cm->idata))) {
return 0;
}
- dynarr_free(cm->idata);
+ free(cm->idata);
cm->idata = tmp;
+ cm->icount = nidx;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
tmp = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
int cmesh_push_index(struct cmesh *cm, unsigned int idx)
{
unsigned int *iptr;
- if(!(iptr = dynarr_push(cm->idata, &idx))) {
+ unsigned int cur_sz = cm->icount;
+ if(!(iptr = realloc(cm->idata, (cur_sz + 1) * sizeof *iptr))) {
return -1;
}
+ iptr[cur_sz] = idx;
cm->idata = iptr;
+ cm->icount = cur_sz + 1;
cm->idata_valid = 1;
cm->ibo_valid = 0;
- cm->nfaces = dynarr_size(cm->idata) / 3;
+ cm->nfaces = cm->icount / 3;
return 0;
}
origsz = cmdest->nverts * nelem;
newsz = cmdest->nverts + cmsrc->nverts * nelem;
- if(!(vptr = dynarr_resize(cmdest->vattr[i].data, newsz))) {
+ if(!(vptr = realloc(cmdest->vattr[i].data, newsz * sizeof *vptr))) {
return -1;
}
memcpy(vptr + origsz, cmsrc->vattr[i].data, cmsrc->nverts * nelem * sizeof(float));
cmdest->vattr[i].data = vptr;
+ cmdest->vattr[i].count = newsz;
}
}
cmesh_index_ro(cmsrc);
idxoffs = cmdest->nverts;
- origsz = dynarr_size(cmdest->idata);
- srcsz = dynarr_size(cmsrc->idata);
+ origsz = cmdest->icount;
+ srcsz = cmsrc->icount;
newsz = origsz + srcsz;
- if(!(iptr = dynarr_resize(cmdest->idata, newsz))) {
+ if(!(iptr = realloc(cmdest->idata, newsz * sizeof *iptr))) {
return -1;
}
cmdest->idata = iptr;
+ cmdest->icount = newsz;
/* copy and fixup all the new indices */
iptr += origsz;
cm->vattr[CMESH_ATTR_VERTEX].data_valid = 1;
cm->vattr[CMESH_ATTR_VERTEX].nelem = 3;
- for(i=0; i<CMESH_ATTR_VERTEX; i++) {
+ for(i=0; i<CMESH_NUM_ATTR; i++) {
if(cm->vattr[i].data_valid) {
- for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
- float *tmp = dynarr_push(cm->vattr[i].data, &cm->cur_val[i].x + j);
- if(!tmp) return -1;
- cm->vattr[i].data = tmp;
+ int newsz = cm->vattr[i].count + cm->vattr[i].nelem;
+ float *tmp = realloc(cm->vattr[i].data, newsz * sizeof *tmp);
+ if(!tmp) return -1;
+ tmp += cm->vattr[i].count;
+
+ cm->vattr[i].data = tmp;
+ cm->vattr[i].count = newsz;
+
+ for(j=0; j<cm->vattr[i].nelem; j++) {
+ *tmp++ = *(&cm->cur_val[i].x + j);
}
}
cm->vattr[i].vbo_valid = 0;
}
if(cm->idata_valid) {
- cm->idata = dynarr_clear(cm->idata);
+ free(cm->idata);
+ cm->idata = 0;
+ cm->icount = 0;
}
cm->ibo_valid = cm->idata_valid = 0;
return 0;
if(!cmesh_has_attrib(cm, i)) continue;
srcbuf = cmesh_attrib(cm, i);
- if(!(tmpbuf = dynarr_alloc(nnverts * cm->vattr[i].nelem, sizeof(float)))) {
+ if(!(tmpbuf = malloc(nnverts * cm->vattr[i].nelem * sizeof(float)))) {
return -1;
}
dstptr = tmpbuf;
}
}
- dynarr_free(cm->vattr[i].data);
+ free(cm->vattr[i].data);
cm->vattr[i].data = tmpbuf;
+ cm->vattr[i].count = nnverts * cm->vattr[i].nelem;
cm->vattr[i].data_valid = 1;
}
cm->ibo_valid = 0;
cm->idata_valid = 0;
- cm->idata = dynarr_clear(cm->idata);
+ free(cm->idata);
+ cm->idata = 0;
+ cm->icount = 0;
cm->nverts = nnverts;
cm->nfaces = idxnum / 3;
void cmesh_texcoord_gen_box(struct cmesh *cm);
void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
-int cmesh_dump(struct cmesh *cm, const char *fname);
-int cmesh_dump_file(struct cmesh *cm, FILE *fp);
-int cmesh_dump_obj(struct cmesh *cm, const char *fname);
-int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs);
+int cmesh_dump(struct cmesh *cm, const char *fname)
+{
+ FILE *fp = fopen(fname, "wb");
+ if(fp) {
+ int res = cmesh_dump_file(cm, fp);
+ fclose(fp);
+ return res;
+ }
+ return -1;
+}
+
+int cmesh_dump_file(struct cmesh *cm, FILE *fp)
+{
+ static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid", "tex2" };
+ static const char *elemfmt[] = { 0, " %s(%g)", " %s(%g, %g)", " %s(%g, %g, %g)", " %s(%g, %g, %g, %g)", 0 };
+ int i, j;
+
+ if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
+ return -1;
+ }
+
+ fprintf(fp, "VERTEX ATTRIBUTES\n");
+
+ for(i=0; i<cm->nverts; i++) {
+ fprintf(fp, "%5u:", i);
+ for(j=0; j<CMESH_NUM_ATTR; j++) {
+ if(cmesh_has_attrib(cm, j)) {
+ const float *v = cmesh_attrib_at_ro(cm, j, i);
+ int nelem = cm->vattr[j].nelem;
+ fprintf(fp, elemfmt[nelem], label[j], v[0], nelem > 1 ? v[1] : 0.0f,
+ nelem > 2 ? v[2] : 0.0f, nelem > 3 ? v[3] : 0.0f);
+ }
+ }
+ fputc('\n', fp);
+ }
+
+ if(cmesh_indexed(cm)) {
+ const unsigned int *idx = cmesh_index_ro(cm);
+ int numidx = cmesh_index_count(cm);
+ int numtri = numidx / 3;
+ assert(numidx % 3 == 0);
+
+ fprintf(fp, "FACES\n");
+
+ for(i=0; i<numtri; i++) {
+ fprintf(fp, "%5d: %d %d %d\n", i, idx[0], idx[1], idx[2]);
+ idx += 3;
+ }
+ }
+ return 0;
+}
+
+int cmesh_dump_obj(struct cmesh *cm, const char *fname)
+{
+ FILE *fp = fopen(fname, "wb");
+ if(fp) {
+ int res = cmesh_dump_obj_file(cm, fp, 0);
+ fclose(fp);
+ return res;
+ }
+ 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;
+ }
+
+
+ nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
+ if((num = cm->vattr[CMESH_ATTR_VERTEX].count) != cm->nverts * nelem) {
+ warning_log("vertex array size (%d) != nverts (%d)\n", num, cm->nverts);
+ }
+ for(i=0; i<cm->nverts; i++) {
+ const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
+ fprintf(fp, "v %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
+ }
+
+ if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
+ aflags |= HAS_VN;
+ nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
+ if((num = cm->vattr[CMESH_ATTR_NORMAL].count) != cm->nverts * nelem) {
+ warning_log("normal array size (%d) != nverts (%d)\n", num, cm->nverts);
+ }
+ for(i=0; i<cm->nverts; i++) {
+ const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_NORMAL, i);
+ fprintf(fp, "vn %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
+ }
+ }
+
+ if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
+ aflags |= HAS_VT;
+ nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem;
+ if((num = cm->vattr[CMESH_ATTR_TEXCOORD].count) != cm->nverts * nelem) {
+ warning_log("texcoord array size (%d) != nverts (%d)\n", num, cm->nverts);
+ }
+ for(i=0; i<cm->nverts; i++) {
+ const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_TEXCOORD, i);
+ fprintf(fp, "vt %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f);
+ }
+ }
+
+ if(cmesh_indexed(cm)) {
+ const unsigned int *idxptr = cmesh_index_ro(cm);
+ int numidx = cmesh_index_count(cm);
+ int numtri = numidx / 3;
+ assert(numidx % 3 == 0);
+
+ for(i=0; i<numtri; i++) {
+ fputc('f', fp);
+ for(j=0; j<3; j++) {
+ unsigned int idx = *idxptr++ + 1 + voffs;
+ fprintf(fp, fmtstr[aflags], idx, idx, idx);
+ }
+ fputc('\n', fp);
+ }
+ } else {
+ int numtri = cm->nverts / 3;
+ unsigned int idx = 1 + voffs;
+ for(i=0; i<numtri; i++) {
+ fputc('f', fp);
+ for(j=0; j<3; j++) {
+ fprintf(fp, fmtstr[aflags], idx, idx, idx);
+ ++idx;
+ }
+ fputc('\n', fp);
+ }
+ }
+ return 0;
+}