GLuint material = -1;
GLuint material;
glmWriteOBJ(GLMmodel* model, char* filename, GLuint mode){    GLuint  i;    FILE*   file;    GLMgroup* group;    GLuint material = -1;        assert(model);        /* do a bit of warning */    if (mode & GLM_FLAT && !model->facetnorms) {        __glmWarning("glmWriteOBJ() warning: flat normal output requested "		     "with no facet normals defined.");        mode &= ~GLM_FLAT;    }    if (mode & GLM_SMOOTH && !model->normals) {        __glmWarning("glmWriteOBJ() warning: smooth normal output requested "		     "with no normals defined.");        mode &= ~GLM_SMOOTH;    }    if (mode & GLM_TEXTURE && !model->texcoords) {        __glmWarning("glmWriteOBJ() warning: texture coordinate output requested "		     "with no texture coordinates defined.");        mode &= ~GLM_TEXTURE;    }    if (mode & GLM_FLAT && mode & GLM_SMOOTH) {        __glmWarning("glmWriteOBJ() warning: flat normal output requested "		     "and smooth normal output requested (using smooth).");        mode &= ~GLM_FLAT;    }    if (mode & GLM_COLOR && !model->materials) {        __glmWarning("glmWriteOBJ() warning: color output requested "		     "with no colors (materials) defined.");        mode &= ~GLM_COLOR;    }    if (mode & GLM_MATERIAL && !model->materials) {        __glmWarning("glmWriteOBJ() warning: material output requested "		     "with no materials defined.");        mode &= ~GLM_MATERIAL;    }    if (mode & GLM_COLOR && mode & GLM_MATERIAL) {        __glmWarning("glmWriteOBJ() warning: color and material output requested "		     "outputting only materials.");        mode &= ~GLM_COLOR;    }            /* open the file */    file = fopen(filename, "w");    if (!file) {        __glmFatalError( "glmWriteOBJ() failed: can't open file \"%s\" to write.",			 filename);    }        /* spit out a header */    fprintf(file, "#  \n");    fprintf(file, "#  Wavefront OBJ generated by GLM library\n");    fprintf(file, "#  \n");    fprintf(file, "#  GLM library\n");    fprintf(file, "#  Nate Robins\n");    fprintf(file, "#  ndr@pobox.com\n");    fprintf(file, "#  http://www.pobox.com/~ndr\n");    fprintf(file, "#  \n");        if (mode & GLM_MATERIAL && model->mtllibname) {        fprintf(file, "\nmtllib %s\n\n", model->mtllibname);        glmWriteMTL(model, filename, model->mtllibname);    }        /* spit out the vertices */    fprintf(file, "\n");    fprintf(file, "# %d vertices\n", model->numvertices);    for (i = 1; i <= model->numvertices; i++) {        fprintf(file, "v %f %f %f\n", 		model->vertices[3 * i + 0],		model->vertices[3 * i + 1],		model->vertices[3 * i + 2]);    }        /* spit out the smooth/flat normals */    if (mode & GLM_SMOOTH) {        fprintf(file, "\n");        fprintf(file, "# %d normals\n", model->numnormals);        for (i = 1; i <= model->numnormals; i++) {            fprintf(file, "vn %f %f %f\n", 		    model->normals[3 * i + 0],		    model->normals[3 * i + 1],		    model->normals[3 * i + 2]);        }    } else if (mode & GLM_FLAT) {        fprintf(file, "\n");        fprintf(file, "# %d normals\n", model->numfacetnorms);        for (i = 1; i <= model->numnormals; i++) {            fprintf(file, "vn %f %f %f\n", 		    model->facetnorms[3 * i + 0],		    model->facetnorms[3 * i + 1],		    model->facetnorms[3 * i + 2]);        }    }        /* spit out the texture coordinates */    if (mode & GLM_TEXTURE) {        fprintf(file, "\n");        fprintf(file, "# %d texcoords\n", model->numtexcoords);        for (i = 1; i <= model->numtexcoords; i++) {            fprintf(file, "vt %f %f\n", 		    model->texcoords[2 * i + 0],		    model->texcoords[2 * i + 1]);        }    }        fprintf(file, "\n");    fprintf(file, "# %d groups\n", model->numgroups);    fprintf(file, "# %d faces (triangles)\n", model->numtriangles);    fprintf(file, "\n");        group = model->groups;    while(group) {        fprintf(file, "g %s\n", group->name);        if (mode & GLM_MATERIAL) {            fprintf(file, "usemtl %s\n", model->materials[group->material].name);#ifdef MATERIAL_BY_FACE            material = group->material;#endif        }        for (i = 0; i < group->numtriangles; i++) {#ifdef MATERIAL_BY_FACE            if(T(group->triangles[i]).material && T(group->triangles[i]).material != material) {                material = T(group->triangles[i]).material;                fprintf(file, "usemtl %s\n", model->materials[material].name);            }#endif            if (mode & GLM_SMOOTH && mode & GLM_TEXTURE) {                fprintf(file, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",			T(group->triangles[i]).vindices[0], 			T(group->triangles[i]).nindices[0], 			T(group->triangles[i]).tindices[0],			T(group->triangles[i]).vindices[1],			T(group->triangles[i]).nindices[1],			T(group->triangles[i]).tindices[1],			T(group->triangles[i]).vindices[2],			T(group->triangles[i]).nindices[2],			T(group->triangles[i]).tindices[2]);            } else if (mode & GLM_FLAT && mode & GLM_TEXTURE) {                fprintf(file, "f %d/%d %d/%d %d/%d\n",			T(group->triangles[i]).vindices[0],			T(group->triangles[i]).findex,			T(group->triangles[i]).vindices[1],			T(group->triangles[i]).findex,			T(group->triangles[i]).vindices[2],			T(group->triangles[i]).findex);            } else if (mode & GLM_TEXTURE) {                fprintf(file, "f %d/%d %d/%d %d/%d\n",			T(group->triangles[i]).vindices[0],			T(group->triangles[i]).tindices[0],			T(group->triangles[i]).vindices[1],			T(group->triangles[i]).tindices[1],			T(group->triangles[i]).vindices[2],			T(group->triangles[i]).tindices[2]);            } else if (mode & GLM_SMOOTH) {                fprintf(file, "f %d//%d %d//%d %d//%d\n",			T(group->triangles[i]).vindices[0],			T(group->triangles[i]).nindices[0],			T(group->triangles[i]).vindices[1],			T(group->triangles[i]).nindices[1],			T(group->triangles[i]).vindices[2], 			T(group->triangles[i]).nindices[2]);            } else if (mode & GLM_FLAT) {                fprintf(file, "f %d//%d %d//%d %d//%d\n",			T(group->triangles[i]).vindices[0], 			T(group->triangles[i]).findex,			T(group->triangles[i]).vindices[1],			T(group->triangles[i]).findex,			T(group->triangles[i]).vindices[2],			T(group->triangles[i]).findex);            } else {                fprintf(file, "f %d %d %d\n",			T(group->triangles[i]).vindices[0],			T(group->triangles[i]).vindices[1],			T(group->triangles[i]).vindices[2]);            }        }        fprintf(file, "\n");        group = group->next;    }        fclose(file);}