#define T(x) (model->triangles[(x)]) /* _GLMnode: general purpose node typedef struct _GLMnode { /* glmMax: returns the maximum of two floats */ glmMax(GLfloat a, GLfloat b) /* glmAbs: returns the absolute value of a float */ /* glmDot: compute the dot product of two vectors * u - array of 3 GLfloats (GLfloat u[3]) * v - array of 3 GLfloats (GLfloat v[3]) glmDot(GLfloat* u, GLfloat* v) return u[0]*v[0] + u[1]*v[1] + u[2]*v[2]; /* glmCross: compute the cross product of two vectors * u - array of 3 GLfloats (GLfloat u[3]) * v - array of 3 GLfloats (GLfloat v[3]) * n - array of 3 GLfloats (GLfloat n[3]) to return the cross product in glmCross(GLfloat* u, GLfloat* v, GLfloat* n) assert(u); assert(v); assert(n); n[0] = u[1]*v[2] - u[2]*v[1]; n[1] = u[2]*v[0] - u[0]*v[2]; n[2] = u[0]*v[1] - u[1]*v[0]; /* glmNormalize: normalize a vector * v - array of 3 GLfloats (GLfloat v[3]) to be normalized l = (GLfloat)sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); /* glmEqual: compares two vectors and returns GL_TRUE if they are * equal (within a certain threshold) or GL_FALSE if not. An epsilon * that works fairly well is 0.000001. * u - array of 3 GLfloats (GLfloat u[3]) * v - array of 3 GLfloats (GLfloat v[3]) glmEqual(GLfloat* u, GLfloat* v, GLfloat epsilon) if (glmAbs(u[0] - v[0]) < epsilon && glmAbs(u[1] - v[1]) < epsilon && glmAbs(u[2] - v[2]) < epsilon) /* glmWeldVectors: eliminate (weld) vectors that are within an * vectors - array of GLfloat[3]'s to be welded * numvectors - number of GLfloat[3]'s in vectors * epsilon - maximum difference between vectors glmWeldVectors(GLfloat* vectors, GLuint* numvectors, GLfloat epsilon) copies = (GLfloat*)malloc(sizeof(GLfloat) * 3 * (*numvectors + 1)); memcpy(copies, vectors, (sizeof(GLfloat) * 3 * (*numvectors + 1))); for (i = 1; i <= *numvectors; i++) { for (j = 1; j <= copied; j++) { if (glmEqual(&vectors[3 * i], &copies[3 * j], epsilon)) { /* must not be any duplicates -- add to the copies array */ copies[3 * copied + 0] = vectors[3 * i + 0]; copies[3 * copied + 1] = vectors[3 * i + 1]; copies[3 * copied + 2] = vectors[3 * i + 2]; j = copied; /* pass this along for below */ /* set the first component of this vector to point at the correct index into the new copies array */ vectors[3 * i + 0] = (GLfloat)j; /* glmFindGroup: Find a group in the model glmFindGroup(GLMmodel* model, char* name) if (!strcmp(name, group->name)) /* glmAddGroup: Add a group to the model glmAddGroup(GLMmodel* model, char* name) group = glmFindGroup(model, name); group = (GLMgroup*)malloc(sizeof(GLMgroup)); group->name = strdup(name); group->next = model->groups; /* glmFindGroup: Find a material in the model glmFindMaterial(GLMmodel* model, char* name) /* XXX doing a linear search on a string key'd list is pretty lame, but it works and is fast enough for now. */ for (i = 0; i < model->nummaterials; i++) { if (!strcmp(model->materials[i].name, name)) /* didn't find the name, so print a warning and return the default //fprintf(stderr, "glmFindMaterial(): can't find material \"%s\".\n", name); /* glmDirName: return the directory given a path * NOTE: the return value should be free'd. void glmSetMat(GLMmat_str* mats, GLint index){ assert(!(index<0 || index >=mats->num_materials)); material = &mats->materials[index]; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material->ambient); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material->diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material->specular); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material->shininess); GLMmat_str* glmMTL(char* name){ fprintf(stderr, "glmReadMTL() failed: can't open material file \"%s\".\n", /* count the number of materials in the file */ while(fscanf(file, "%s", buf) != EOF) { /* eat up rest of line */ fgets(buf, sizeof(buf), file); fgets(buf, sizeof(buf), file); sscanf(buf, "%s %s", buf, buf); /* eat up rest of line */ fgets(buf, sizeof(buf), file); ret = (GLMmat_str*)malloc(sizeof(GLMmat_str)); ret->materials = (GLMmaterial*)malloc(sizeof(GLMmaterial) * nummaterials); ret->num_materials = nummaterials; /* set the default material */ for (i = 0; i < nummaterials; i++) { ret->materials[i].name = NULL; ret->materials[i].shininess = 65.0; ret->materials[i].diffuse[0] = 0.8; ret->materials[i].diffuse[1] = 0.1; ret->materials[i].diffuse[2] = 0.1; ret->materials[i].diffuse[3] = 1.0; ret->materials[i].ambient[0] = 0.2; ret->materials[i].ambient[1] = 0.2; ret->materials[i].ambient[2] = 0.2; ret->materials[i].ambient[3] = 1.0; ret->materials[i].specular[0] = 0.0; ret->materials[i].specular[1] = 0.0; ret->materials[i].specular[2] = 0.0; ret->materials[i].specular[3] = 1.0; ret->materials[0].name = strdup("default"); /* now, read in the data */ while(fscanf(file, "%s", buf) != EOF) { /* eat up rest of line */ fgets(buf, sizeof(buf), file); fgets(buf, sizeof(buf), file); sscanf(buf, "%s %s", buf, buf); ret->materials[nummaterials].name = strdup(buf); fscanf(file, "%f", &ret->materials[nummaterials].shininess); /* wavefront shininess is from [0, 1000], so scale for OpenGL */ ret->materials[nummaterials].shininess /= 1000.0; ret->materials[nummaterials].shininess *= 128.0; &ret->materials[nummaterials].diffuse[0], &ret->materials[nummaterials].diffuse[1], &ret->materials[nummaterials].diffuse[2]); &ret->materials[nummaterials].specular[0], &ret->materials[nummaterials].specular[1], &ret->materials[nummaterials].specular[2]); &ret->materials[nummaterials].ambient[0], &ret->materials[nummaterials].ambient[1], &ret->materials[nummaterials].ambient[2]); /* eat up rest of line */ fgets(buf, sizeof(buf), file); /* eat up rest of line */ fgets(buf, sizeof(buf), file); /* glmReadMTL: read a wavefront material library file * model - properly initialized GLMmodel structure * name - name of the material library glmReadMTL(GLMmodel* model, char* name) dir = glmDirName(model->pathname); filename = (char*)malloc(sizeof(char) * (strlen(dir) + strlen(name) + 1)); file = fopen(filename, "r"); fprintf(stderr, "glmReadMTL() failed: can't open material file \"%s\".\n", /* count the number of materials in the file */ while(fscanf(file, "%s", buf) != EOF) { /* eat up rest of line */ fgets(buf, sizeof(buf), file); fgets(buf, sizeof(buf), file); sscanf(buf, "%s %s", buf, buf); /* eat up rest of line */ fgets(buf, sizeof(buf), file); model->materials = (GLMmaterial*)malloc(sizeof(GLMmaterial) * nummaterials); model->nummaterials = nummaterials; /* set the default material */ for (i = 0; i < nummaterials; i++) { model->materials[i].name = NULL; model->materials[i].shininess = 65.0; model->materials[i].diffuse[0] = 0.8; model->materials[i].diffuse[1] = 0.1; model->materials[i].diffuse[2] = 0.1; model->materials[i].diffuse[3] = 1.0; model->materials[i].ambient[0] = 0.2; model->materials[i].ambient[1] = 0.2; model->materials[i].ambient[2] = 0.2; model->materials[i].ambient[3] = 1.0; model->materials[i].specular[0] = 0.0; model->materials[i].specular[1] = 0.0; model->materials[i].specular[2] = 0.0; model->materials[i].specular[3] = 1.0; model->materials[0].name = strdup("default"); /* now, read in the data */ while(fscanf(file, "%s", buf) != EOF) { /* eat up rest of line */ fgets(buf, sizeof(buf), file); fgets(buf, sizeof(buf), file); sscanf(buf, "%s %s", buf, buf); model->materials[nummaterials].name = strdup(buf); fscanf(file, "%f", &model->materials[nummaterials].shininess); /* wavefront shininess is from [0, 1000], so scale for OpenGL */ model->materials[nummaterials].shininess /= 1000.0; model->materials[nummaterials].shininess *= 128.0; &model->materials[nummaterials].diffuse[0], &model->materials[nummaterials].diffuse[1], &model->materials[nummaterials].diffuse[2]); &model->materials[nummaterials].specular[0], &model->materials[nummaterials].specular[1], &model->materials[nummaterials].specular[2]); &model->materials[nummaterials].ambient[0], &model->materials[nummaterials].ambient[1], &model->materials[nummaterials].ambient[2]); /* eat up rest of line */ fgets(buf, sizeof(buf), file); /* eat up rest of line */ fgets(buf, sizeof(buf), file); /* glmWriteMTL: write a wavefront material library file * model - properly initialized GLMmodel structure * modelpath - pathname of the model being written * mtllibname - name of the material library to be written glmWriteMTL(GLMmodel* model, char* modelpath, char* mtllibname) dir = glmDirName(modelpath); filename = (char*)malloc(sizeof(char) * (strlen(dir)+strlen(mtllibname))); strcat(filename, mtllibname); file = fopen(filename, "w"); fprintf(stderr, "glmWriteMTL() failed: can't open file \"%s\".\n", fprintf(file, "# Wavefront MTL generated by GLM library\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"); for (i = 0; i < model->nummaterials; i++) { material = &model->materials[i]; fprintf(file, "newmtl %s\n", material->name); fprintf(file, "Ka %f %f %f\n", material->ambient[0], material->ambient[1], material->ambient[2]); fprintf(file, "Kd %f %f %f\n", material->diffuse[0], material->diffuse[1], material->diffuse[2]); fprintf(file, "Ks %f %f %f\n", material->specular[0],material->specular[1],material->specular[2]); fprintf(file, "Ns %f\n", material->shininess / 128.0 * 1000.0); /* glmFirstPass: first pass at a Wavefront OBJ file that gets all the * statistics of the model (such as #vertices, #normals, etc) * model - properly initialized GLMmodel structure * file - (fopen'd) file descriptor glmFirstPass(GLMmodel* model, FILE* file) GLuint numvertices; /* number of vertices in model */ GLuint numnormals; /* number of normals in model */ GLuint numtexcoords; /* number of texcoords in model */ GLuint numtriangles; /* number of triangles in model */ GLMgroup* group; /* current group */ /* make a default group */ group = glmAddGroup(model, "default"); numvertices = numnormals = numtexcoords = numtriangles = 0; while(fscanf(file, "%s", buf) != EOF) { /* eat up rest of line */ fgets(buf, sizeof(buf), file); case 'v': /* v, vn, vt */ /* eat up rest of line */ fgets(buf, sizeof(buf), file); /* eat up rest of line */ fgets(buf, sizeof(buf), file); /* eat up rest of line */ fgets(buf, sizeof(buf), file); printf("glmFirstPass(): Unknown token \"%s\".\n", buf); fgets(buf, sizeof(buf), file); sscanf(buf, "%s %s", buf, buf); model->mtllibname = strdup(buf); //glmReadMTL(model, buf); /* eat up rest of line */ fgets(buf, sizeof(buf), file); /* eat up rest of line */ fgets(buf, sizeof(buf), file); #if SINGLE_STRING_GROUP_NAMES buf[strlen(buf)-1] = '\0'; /* nuke '\n' */ group = glmAddGroup(model, buf); /* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */ sscanf(buf, "%d//%d", &v, &n); fscanf(file, "%d//%d", &v, &n); fscanf(file, "%d//%d", &v, &n); while(fscanf(file, "%d//%d", &v, &n) > 0) { } else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) { fscanf(file, "%d/%d/%d", &v, &t, &n); fscanf(file, "%d/%d/%d", &v, &t, &n); while(fscanf(file, "%d/%d/%d", &v, &t, &n) > 0) { } else if (sscanf(buf, "%d/%d", &v, &t) == 2) { fscanf(file, "%d/%d", &v, &t); fscanf(file, "%d/%d", &v, &t); while(fscanf(file, "%d/%d", &v, &t) > 0) { while(fscanf(file, "%d", &v) > 0) { /* eat up rest of line */ fgets(buf, sizeof(buf), file); /* set the stats in the model structure */ model->numvertices = numvertices; model->numnormals = numnormals; model->numtexcoords = numtexcoords; model->numtriangles = numtriangles; /* allocate memory for the triangles in each group */ group->triangles = (GLuint*)malloc(sizeof(GLuint) * group->numtriangles); /* glmSecondPass: second pass at a Wavefront OBJ file that gets all * model - properly initialized GLMmodel structure * file - (fopen'd) file descriptor glmSecondPass(GLMmodel* model, FILE* file) GLuint numvertices; /* number of vertices in model */ GLuint numnormals; /* number of normals in model */ GLuint numtexcoords; /* number of texcoords in model */ GLuint numtriangles; /* number of triangles in model */ GLfloat* vertices; /* array of vertices */ GLfloat* normals; /* array of normals */ GLfloat* texcoords; /* array of texture coordinates */ GLMgroup* group; /* current group pointer */ GLuint material; /* current material */ /* set the pointer shortcuts */ vertices = model->vertices; normals = model->normals; texcoords = model->texcoords; /* on the second pass through the file, read all the data into the numvertices = numnormals = numtexcoords = 1; while(fscanf(file, "%s", buf) != EOF) { /* eat up rest of line */ fgets(buf, sizeof(buf), file); case 'v': /* v, vn, vt */ &vertices[3 * numvertices + 0], &vertices[3 * numvertices + 1], &vertices[3 * numvertices + 2]); &normals[3 * numnormals + 0], &normals[3 * numnormals + 1], &normals[3 * numnormals + 2]); &texcoords[2 * numtexcoords + 0], &texcoords[2 * numtexcoords + 1]); fgets(buf, sizeof(buf), file); sscanf(buf, "%s %s", buf, buf); group->material = material = glmFindMaterial(model, buf); /* eat up rest of line */ fgets(buf, sizeof(buf), file); #if SINGLE_STRING_GROUP_NAMES buf[strlen(buf)-1] = '\0'; /* nuke '\n' */ group = glmFindGroup(model, buf); group->material = material; /* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */ sscanf(buf, "%d//%d", &v, &n); T(numtriangles).vindices[0] = v; T(numtriangles).nindices[0] = n; fscanf(file, "%d//%d", &v, &n); T(numtriangles).vindices[1] = v; T(numtriangles).nindices[1] = n; fscanf(file, "%d//%d", &v, &n); T(numtriangles).vindices[2] = v; T(numtriangles).nindices[2] = n; group->triangles[group->numtriangles++] = numtriangles; while(fscanf(file, "%d//%d", &v, &n) > 0) { T(numtriangles).vindices[0] = T(numtriangles-1).vindices[0]; T(numtriangles).nindices[0] = T(numtriangles-1).nindices[0]; T(numtriangles).vindices[1] = T(numtriangles-1).vindices[2]; T(numtriangles).nindices[1] = T(numtriangles-1).nindices[2]; T(numtriangles).vindices[2] = v; T(numtriangles).nindices[2] = n; group->triangles[group->numtriangles++] = numtriangles; } else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) { T(numtriangles).vindices[0] = v; T(numtriangles).tindices[0] = t; T(numtriangles).nindices[0] = n; fscanf(file, "%d/%d/%d", &v, &t, &n); T(numtriangles).vindices[1] = v; T(numtriangles).tindices[1] = t; T(numtriangles).nindices[1] = n; fscanf(file, "%d/%d/%d", &v, &t, &n); T(numtriangles).vindices[2] = v; T(numtriangles).tindices[2] = t; T(numtriangles).nindices[2] = n; group->triangles[group->numtriangles++] = numtriangles; while(fscanf(file, "%d/%d/%d", &v, &t, &n) > 0) { T(numtriangles).vindices[0] = T(numtriangles-1).vindices[0]; T(numtriangles).tindices[0] = T(numtriangles-1).tindices[0]; T(numtriangles).nindices[0] = T(numtriangles-1).nindices[0]; T(numtriangles).vindices[1] = T(numtriangles-1).vindices[2]; T(numtriangles).tindices[1] = T(numtriangles-1).tindices[2]; T(numtriangles).nindices[1] = T(numtriangles-1).nindices[2]; T(numtriangles).vindices[2] = v; T(numtriangles).tindices[2] = t; T(numtriangles).nindices[2] = n; group->triangles[group->numtriangles++] = numtriangles; } else if (sscanf(buf, "%d/%d", &v, &t) == 2) { T(numtriangles).vindices[0] = v; T(numtriangles).tindices[0] = t; fscanf(file, "%d/%d", &v, &t); T(numtriangles).vindices[1] = v; T(numtriangles).tindices[1] = t; fscanf(file, "%d/%d", &v, &t); T(numtriangles).vindices[2] = v; T(numtriangles).tindices[2] = t; group->triangles[group->numtriangles++] = numtriangles; while(fscanf(file, "%d/%d", &v, &t) > 0) { T(numtriangles).vindices[0] = T(numtriangles-1).vindices[0]; T(numtriangles).tindices[0] = T(numtriangles-1).tindices[0]; T(numtriangles).vindices[1] = T(numtriangles-1).vindices[2]; T(numtriangles).tindices[1] = T(numtriangles-1).tindices[2]; T(numtriangles).vindices[2] = v; T(numtriangles).tindices[2] = t; group->triangles[group->numtriangles++] = numtriangles; T(numtriangles).vindices[0] = v; T(numtriangles).vindices[1] = v; T(numtriangles).vindices[2] = v; group->triangles[group->numtriangles++] = numtriangles; while(fscanf(file, "%d", &v) > 0) { T(numtriangles).vindices[0] = T(numtriangles-1).vindices[0]; T(numtriangles).vindices[1] = T(numtriangles-1).vindices[2]; T(numtriangles).vindices[2] = v; group->triangles[group->numtriangles++] = numtriangles; /* eat up rest of line */ fgets(buf, sizeof(buf), file); /* announce the memory requirements */ printf(" Memory: %d bytes\n", numvertices * 3*sizeof(GLfloat) + numnormals * 3*sizeof(GLfloat) * (numnormals ? 1 : 0) + numtexcoords * 3*sizeof(GLfloat) * (numtexcoords ? 1 : 0) + numtriangles * sizeof(GLMtriangle)); /* glmUnitize: "unitize" a model by translating it to the origin and * scaling it to fit in a unit cube around the origin. Modelurns the * model - properly initialized GLMmodel structure glmUnitize(GLMmodel* model) GLfloat maxx, minx, maxy, miny, maxz, minz; GLfloat cx, cy, cz, w, h, d; maxx = minx = model->vertices[3 + 0]; maxy = miny = model->vertices[3 + 1]; maxz = minz = model->vertices[3 + 2]; for (i = 1; i <= model->numvertices; i++) { if (maxx < model->vertices[3 * i + 0]) maxx = model->vertices[3 * i + 0]; if (minx > model->vertices[3 * i + 0]) minx = model->vertices[3 * i + 0]; if (maxy < model->vertices[3 * i + 1]) maxy = model->vertices[3 * i + 1]; if (miny > model->vertices[3 * i + 1]) miny = model->vertices[3 * i + 1]; if (maxz < model->vertices[3 * i + 2]) maxz = model->vertices[3 * i + 2]; if (minz > model->vertices[3 * i + 2]) minz = model->vertices[3 * i + 2]; /* calculate model width, height, and depth */ w = glmAbs(maxx) + glmAbs(minx); h = glmAbs(maxy) + glmAbs(miny); d = glmAbs(maxz) + glmAbs(minz); /* calculate center of the model */ cx = (maxx + minx) / 2.0; cy = (maxy + miny) / 2.0; cz = (maxz + minz) / 2.0; /* calculate unitizing scale factor */ scale = 2.0 / glmMax(glmMax(w, h), d); /* translate around center then scale */ for (i = 1; i <= model->numvertices; i++) { model->vertices[3 * i + 0] -= cx; model->vertices[3 * i + 1] -= cy; model->vertices[3 * i + 2] -= cz; //charlie, i took this out, i just want to center model->vertices[3 * i + 0] *= scale; model->vertices[3 * i + 1] *= scale; model->vertices[3 * i + 2] *= scale; /* glmDimensions: Calculates the dimensions (width, height, depth) of * model - initialized GLMmodel structure * dimensions - array of 3 GLfloats (GLfloat dimensions[3]) glmDimensions(GLMmodel* model, GLfloat* dimensions) GLfloat maxx, minx, maxy, miny, maxz, minz; maxx = minx = model->vertices[3 + 0]; maxy = miny = model->vertices[3 + 1]; maxz = minz = model->vertices[3 + 2]; for (i = 1; i <= model->numvertices; i++) { if (maxx < model->vertices[3 * i + 0]) maxx = model->vertices[3 * i + 0]; if (minx > model->vertices[3 * i + 0]) minx = model->vertices[3 * i + 0]; if (maxy < model->vertices[3 * i + 1]) maxy = model->vertices[3 * i + 1]; if (miny > model->vertices[3 * i + 1]) miny = model->vertices[3 * i + 1]; if (maxz < model->vertices[3 * i + 2]) maxz = model->vertices[3 * i + 2]; if (minz > model->vertices[3 * i + 2]) minz = model->vertices[3 * i + 2]; /* calculate model width, height, and depth */ dimensions[0] = glmAbs(maxx) + glmAbs(minx); dimensions[1] = glmAbs(maxy) + glmAbs(miny); dimensions[2] = glmAbs(maxz) + glmAbs(minz); /* glmScale: Scales a model by a given amount. * model - properly initialized GLMmodel structure * scale - scalefactor (0.5 = half as large, 2.0 = twice as large) glmScale(GLMmodel* model, GLfloat scale) for (i = 1; i <= model->numvertices; i++) { model->vertices[3 * i + 0] *= scale; model->vertices[3 * i + 1] *= scale; model->vertices[3 * i + 2] *= scale; /* glmReverseWinding: Reverse the polygon winding for all polygons in * this model. Default winding is counter-clockwise. Also changes * the direction of the normals. * model - properly initialized GLMmodel structure glmReverseWinding(GLMmodel* model) for (i = 0; i < model->numtriangles; i++) { T(i).vindices[0] = T(i).vindices[2]; T(i).nindices[0] = T(i).nindices[2]; if (model->numtexcoords) { T(i).tindices[0] = T(i).tindices[2]; /* reverse facet normals */ for (i = 1; i <= model->numfacetnorms; i++) { model->facetnorms[3 * i + 0] = -model->facetnorms[3 * i + 0]; model->facetnorms[3 * i + 1] = -model->facetnorms[3 * i + 1]; model->facetnorms[3 * i + 2] = -model->facetnorms[3 * i + 2]; /* reverse vertex normals */ for (i = 1; i <= model->numnormals; i++) { model->normals[3 * i + 0] = -model->normals[3 * i + 0]; model->normals[3 * i + 1] = -model->normals[3 * i + 1]; model->normals[3 * i + 2] = -model->normals[3 * i + 2]; /* glmFacetNormals: Generates facet normals for a model (by taking the * cross product of the two vectors derived from the sides of each * triangle). Assumes a counter-clockwise winding. * model - initialized GLMmodel structure glmFacetNormals(GLMmodel* model) /* clobber any old facetnormals */ /* allocate memory for the new facet normals */ model->numfacetnorms = model->numtriangles; model->facetnorms = (GLfloat*)malloc(sizeof(GLfloat) * 3 * (model->numfacetnorms + 1)); for (i = 0; i < model->numtriangles; i++) { model->triangles[i].findex = i+1; u[0] = model->vertices[3 * T(i).vindices[1] + 0] - model->vertices[3 * T(i).vindices[0] + 0]; u[1] = model->vertices[3 * T(i).vindices[1] + 1] - model->vertices[3 * T(i).vindices[0] + 1]; u[2] = model->vertices[3 * T(i).vindices[1] + 2] - model->vertices[3 * T(i).vindices[0] + 2]; v[0] = model->vertices[3 * T(i).vindices[2] + 0] - model->vertices[3 * T(i).vindices[0] + 0]; v[1] = model->vertices[3 * T(i).vindices[2] + 1] - model->vertices[3 * T(i).vindices[0] + 1]; v[2] = model->vertices[3 * T(i).vindices[2] + 2] - model->vertices[3 * T(i).vindices[0] + 2]; glmCross(u, v, &model->facetnorms[3 * (i+1)]); glmNormalize(&model->facetnorms[3 * (i+1)]); /* glmVertexNormals: Generates smooth vertex normals for a model. * First builds a list of all the triangles each vertex is in. Then * loops through each vertex in the the list averaging all the facet * normals of the triangles each vertex is in. Finally, sets the * normal index in the triangle for the vertex to the generated smooth * normal. If the dot product of a facet normal and the facet normal * associated with the first triangle in the list of triangles the * current vertex is in is greater than the cosine of the angle * parameter to the function, that facet normal is not added into the * average normal calculation and the corresponding vertex is given * the facet normal. This tends to preserve hard edges. The angle to * use depends on the model, but 90 degrees is usually a good start. * model - initialized GLMmodel structure * angle - maximum angle (in degrees) to smooth across glmVertexNormals(GLMmodel* model, GLfloat angle) assert(model->facetnorms); /* calculate the cosine of the angle (in degrees) */ cos_angle = cos(angle * M_PI / 180.0); /* nuke any previous normals */ /* allocate space for new normals */ model->numnormals = model->numtriangles * 3; /* 3 normals per triangle */ model->normals = (GLfloat*)malloc(sizeof(GLfloat)* 3* (model->numnormals+1)); /* allocate a structure that will hold a linked list of triangle indices for each vertex */ members = (GLMnode**)malloc(sizeof(GLMnode*) * (model->numvertices + 1)); for (i = 1; i <= model->numvertices; i++) /* for every triangle, create a node for each vertex in it */ for (i = 0; i < model->numtriangles; i++) { node = (GLMnode*)malloc(sizeof(GLMnode)); node->next = members[T(i).vindices[0]]; members[T(i).vindices[0]] = node; node = (GLMnode*)malloc(sizeof(GLMnode)); node->next = members[T(i).vindices[1]]; members[T(i).vindices[1]] = node; node = (GLMnode*)malloc(sizeof(GLMnode)); node->next = members[T(i).vindices[2]]; members[T(i).vindices[2]] = node; /* calculate the average normal for each vertex */ for (i = 1; i <= model->numvertices; i++) { /* calculate an average normal for this vertex by averaging the facet normal of every triangle this vertex is in */ fprintf(stderr, "glmVertexNormals(): vertex w/o a triangle\n"); average[0] = 0.0; average[1] = 0.0; average[2] = 0.0; /* only average if the dot product of the angle between the two facet normals is greater than the cosine of the threshold angle -- or, said another way, the angle between the two facet normals is less than (or equal to) the threshold angle */ dot = glmDot(&model->facetnorms[3 * T(node->index).findex], &model->facetnorms[3 * T(members[i]->index).findex]); node->averaged = GL_TRUE; average[0] += model->facetnorms[3 * T(node->index).findex + 0]; average[1] += model->facetnorms[3 * T(node->index).findex + 1]; average[2] += model->facetnorms[3 * T(node->index).findex + 2]; avg = 1; /* we averaged at least one normal! */ node->averaged = GL_FALSE; /* normalize the averaged normal */ /* add the normal to the vertex normals list */ model->normals[3 * numnormals + 0] = average[0]; model->normals[3 * numnormals + 1] = average[1]; model->normals[3 * numnormals + 2] = average[2]; /* set the normal of this vertex in each triangle it is in */ /* if this node was averaged, use the average normal */ if (T(node->index).vindices[0] == i) T(node->index).nindices[0] = avg; else if (T(node->index).vindices[1] == i) T(node->index).nindices[1] = avg; else if (T(node->index).vindices[2] == i) T(node->index).nindices[2] = avg; /* if this node wasn't averaged, use the facet normal */ model->normals[3 * numnormals + 0] = model->facetnorms[3 * T(node->index).findex + 0]; model->normals[3 * numnormals + 1] = model->facetnorms[3 * T(node->index).findex + 1]; model->normals[3 * numnormals + 2] = model->facetnorms[3 * T(node->index).findex + 2]; if (T(node->index).vindices[0] == i) T(node->index).nindices[0] = numnormals; else if (T(node->index).vindices[1] == i) T(node->index).nindices[1] = numnormals; else if (T(node->index).vindices[2] == i) T(node->index).nindices[2] = numnormals; model->numnormals = numnormals - 1; /* free the member information */ for (i = 1; i <= model->numvertices; i++) { /* pack the normals array (we previously allocated the maximum number of normals that could possibly be created (numtriangles * 3), so get rid of some of them (usually alot unless none of the facet normals were averaged)) */ normals = model->normals; model->normals = (GLfloat*)malloc(sizeof(GLfloat)* 3* (model->numnormals+1)); for (i = 1; i <= model->numnormals; i++) { model->normals[3 * i + 0] = normals[3 * i + 0]; model->normals[3 * i + 1] = normals[3 * i + 1]; model->normals[3 * i + 2] = normals[3 * i + 2]; /* glmLinearTexture: Generates texture coordinates according to a * linear projection of the texture map. It generates these by * linearly mapping the vertices onto a square. * model - pointer to initialized GLMmodel structure glmLinearTexture(GLMmodel* model) GLfloat x, y, scalefactor; model->numtexcoords = model->numvertices; model->texcoords=(GLfloat*)malloc(sizeof(GLfloat)*2*(model->numtexcoords+1)); glmDimensions(model, dimensions); glmAbs(glmMax(glmMax(dimensions[0], dimensions[1]), dimensions[2])); /* do the calculations */ for(i = 1; i <= model->numvertices; i++) { x = model->vertices[3 * i + 0] * scalefactor; y = model->vertices[3 * i + 2] * scalefactor; model->texcoords[2 * i + 0] = (x + 1.0) / 2.0; model->texcoords[2 * i + 1] = (y + 1.0) / 2.0; /* go through and put texture coordinate indices in all the triangles */ for(i = 0; i < group->numtriangles; i++) { T(group->triangles[i]).tindices[0] = T(group->triangles[i]).vindices[0]; T(group->triangles[i]).tindices[1] = T(group->triangles[i]).vindices[1]; T(group->triangles[i]).tindices[2] = T(group->triangles[i]).vindices[2]; printf("glmLinearTexture(): generated %d linear texture coordinates\n", /* glmSpheremapTexture: Generates texture coordinates according to a * spherical projection of the texture map. Sometimes referred to as * spheremap, or reflection map texture coordinates. It generates * these by using the normal to calculate where that vertex would map * onto a sphere. Since it is impossible to map something flat * perfectly onto something spherical, there is distortion at the * poles. This particular implementation causes the poles along the X * model - pointer to initialized GLMmodel structure glmSpheremapTexture(GLMmodel* model) GLfloat theta, phi, rho, x, y, z, r; model->numtexcoords = model->numnormals; model->texcoords=(GLfloat*)malloc(sizeof(GLfloat)*2*(model->numtexcoords+1)); for (i = 1; i <= model->numnormals; i++) { z = model->normals[3 * i + 0]; /* re-arrange for pole distortion */ y = model->normals[3 * i + 1]; x = model->normals[3 * i + 2]; r = sqrt((x * x) + (y * y)); rho = sqrt((r * r) + (z * z)); theta = 3.141592365 / 2.0; theta = asin(y / r) + (3.14159265 / 2.0); model->texcoords[2 * i + 0] = theta / 3.14159265; model->texcoords[2 * i + 1] = phi / 3.14159265; /* go through and put texcoord indices in all the triangles */ for (i = 0; i < group->numtriangles; i++) { T(group->triangles[i]).tindices[0] = T(group->triangles[i]).nindices[0]; T(group->triangles[i]).tindices[1] = T(group->triangles[i]).nindices[1]; T(group->triangles[i]).tindices[2] = T(group->triangles[i]).nindices[2]; /* glmDelete: Deletes a GLMmodel structure. * model - initialized GLMmodel structure glmDelete(GLMmodel* model) if (model->pathname) free(model->pathname); if (model->mtllibname) free(model->mtllibname); if (model->vertices) free(model->vertices); if (model->normals) free(model->normals); if (model->texcoords) free(model->texcoords); if (model->facetnorms) free(model->facetnorms); if (model->triangles) free(model->triangles); for (i = 0; i < model->nummaterials; i++) free(model->materials[i].name); model->groups = model->groups->next; /* glmReadOBJ: Reads a model description from a Wavefront .OBJ file. * Modelurns a pointer to the created object which should be free'd with * filename - name of the file containing the Wavefront .OBJ format data. glmReadOBJ(char* filename) file = fopen(filename, "r"); fprintf(stderr, "glmReadOBJ() failed: can't open data file \"%s\".\n", /* allocate a new model */ model = (GLMmodel*)malloc(sizeof(GLMmodel)); model->pathname = strdup(filename); model->mtllibname = NULL; model->numfacetnorms = 0; model->facetnorms = NULL; model->position[0] = 0.0; model->position[1] = 0.0; model->position[2] = 0.0; /* make a first pass through the file to get a count of the number of vertices, normals, texcoords & triangles */ glmFirstPass(model, file); model->vertices = (GLfloat*)malloc(sizeof(GLfloat) * 3 * (model->numvertices + 1)); model->triangles = (GLMtriangle*)malloc(sizeof(GLMtriangle) * model->normals = (GLfloat*)malloc(sizeof(GLfloat) * 3 * (model->numnormals + 1)); if (model->numtexcoords) { model->texcoords = (GLfloat*)malloc(sizeof(GLfloat) * 2 * (model->numtexcoords + 1)); /* rewind to beginning of file and read in the data this pass */ glmSecondPass(model, file); /* glmWriteOBJ: Writes a model description in Wavefront .OBJ format to * model - initialized GLMmodel structure * filename - name of the file to write the Wavefront .OBJ format data to * mode - a bitwise or of values describing what is written to the file * GLM_NONE - render with only vertices * GLM_FLAT - render with facet normals * GLM_SMOOTH - render with vertex normals * GLM_TEXTURE - render with texture coords * GLM_COLOR - render with colors (color material) * GLM_MATERIAL - render with materials * GLM_COLOR and GLM_MATERIAL should not both be specified. * GLM_FLAT and GLM_SMOOTH should not both be specified. glmWriteOBJ(GLMmodel* model, char* filename, GLuint mode) /* do a bit of warning */ if (mode & GLM_FLAT && !model->facetnorms) { printf("glmWriteOBJ() warning: flat normal output requested " "with no facet normals defined.\n"); if (mode & GLM_SMOOTH && !model->normals) { printf("glmWriteOBJ() warning: smooth normal output requested " "with no normals defined.\n"); if (mode & GLM_TEXTURE && !model->texcoords) { printf("glmWriteOBJ() warning: texture coordinate output requested " "with no texture coordinates defined.\n"); if (mode & GLM_FLAT && mode & GLM_SMOOTH) { printf("glmWriteOBJ() warning: flat normal output requested " "and smooth normal output requested (using smooth).\n"); if (mode & GLM_COLOR && !model->materials) { printf("glmWriteOBJ() warning: color output requested " "with no colors (materials) defined.\n"); if (mode & GLM_MATERIAL && !model->materials) { printf("glmWriteOBJ() warning: material output requested " "with no materials defined.\n"); if (mode & GLM_COLOR && mode & GLM_MATERIAL) { printf("glmWriteOBJ() warning: color and material output requested " "outputting only materials.\n"); file = fopen(filename, "w"); fprintf(stderr, "glmWriteOBJ() failed: can't open file \"%s\" to write.\n", fprintf(file, "# Wavefront OBJ generated by GLM library\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"); 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, "# %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 */ 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, "# %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, "# %d texcoords\n", model->texcoords); 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, "# %d groups\n", model->numgroups); fprintf(file, "# %d faces (triangles)\n", model->numtriangles); fprintf(file, "g %s\n", group->name); fprintf(file, "usemtl %s\n", model->materials[group->material].name); for (i = 0; i < group->numtriangles; i++) { 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); 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]); /* glmDraw: Renders the model to the current OpenGL context using the * model - initialized GLMmodel structure * mode - a bitwise OR of values describing what is to be rendered. * GLM_NONE - render with only vertices * GLM_FLAT - render with facet normals * GLM_SMOOTH - render with vertex normals * GLM_TEXTURE - render with texture coords * GLM_COLOR - render with colors (color material) * GLM_MATERIAL - render with materials * GLM_COLOR and GLM_MATERIAL should not both be specified. * GLM_FLAT and GLM_SMOOTH should not both be specified. glmDraw(GLMmodel* model, GLuint mode) static GLMtriangle* triangle; static GLMmaterial* material; /* do a bit of warning */ if (mode & GLM_FLAT && !model->facetnorms) { printf("glmDraw() warning: flat render mode requested " "with no facet normals defined.\n"); if (mode & GLM_SMOOTH && !model->normals) { printf("glmDraw() warning: smooth render mode requested " "with no normals defined.\n"); if (mode & GLM_TEXTURE && !model->texcoords) { printf("glmDraw() warning: texture render mode requested " "with no texture coordinates defined.\n"); if (mode & GLM_FLAT && mode & GLM_SMOOTH) { printf("glmDraw() warning: flat render mode requested " "and smooth render mode requested (using smooth).\n"); if (mode & GLM_COLOR && !model->materials) { printf("glmDraw() warning: color render mode requested " "with no materials defined.\n"); if (mode & GLM_MATERIAL && !model->materials) { printf("glmDraw() warning: material render mode requested " "with no materials defined.\n"); if (mode & GLM_COLOR && mode & GLM_MATERIAL) { printf("glmDraw() warning: color and material render mode requested " "using only material mode.\n"); glEnable(GL_COLOR_MATERIAL); else if (mode & GLM_MATERIAL) glDisable(GL_COLOR_MATERIAL); /* perhaps this loop should be unrolled into material, color, flat, smooth, etc. loops? since most cpu's have good branch prediction schemes (and these branches will always go one way), probably wouldn't gain too much? */ if (mode & GLM_MATERIAL) { material = &model->materials[group->material]; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material->ambient); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material->diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material->specular); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material->shininess); glColor3fv(material->diffuse); for (i = 0; i < group->numtriangles; i++) { triangle = &T(group->triangles[i]); glNormal3fv(&model->facetnorms[3 * triangle->findex]); glNormal3fv(&model->normals[3 * triangle->nindices[0]]); glTexCoord2fv(&model->texcoords[2 * triangle->tindices[0]]); glVertex3fv(&model->vertices[3 * triangle->vindices[0]]); glNormal3fv(&model->normals[3 * triangle->nindices[1]]); glTexCoord2fv(&model->texcoords[2 * triangle->tindices[1]]); glVertex3fv(&model->vertices[3 * triangle->vindices[1]]); glNormal3fv(&model->normals[3 * triangle->nindices[2]]); glTexCoord2fv(&model->texcoords[2 * triangle->tindices[2]]); glVertex3fv(&model->vertices[3 * triangle->vindices[2]]); /* glmList: Generates and modelurns a display list for the model using * model - initialized GLMmodel structure * mode - a bitwise OR of values describing what is to be rendered. * GLM_NONE - render with only vertices * GLM_FLAT - render with facet normals * GLM_SMOOTH - render with vertex normals * GLM_TEXTURE - render with texture coords * GLM_COLOR - render with colors (color material) * GLM_MATERIAL - render with materials * GLM_COLOR and GLM_MATERIAL should not both be specified. * GLM_FLAT and GLM_SMOOTH should not both be specified. */ glmList(GLMmodel* model, GLuint mode) glNewList(list, GL_COMPILE); /* glmWeld: eliminate (weld) vectors that are within an epsilon of * model - initialized GLMmodel structure * epsilon - maximum difference between vertices * ( 0.00001 is a good start for a unitized model) glmWeld(GLMmodel* model, GLfloat epsilon) numvectors = model->numvertices; vectors = model->vertices; copies = glmWeldVectors(vectors, &numvectors, epsilon); printf("glmWeld(): %d redundant vertices.\n", model->numvertices - numvectors - 1); for (i = 0; i < model->numtriangles; i++) { T(i).vindices[0] = (GLuint)vectors[3 * T(i).vindices[0] + 0]; T(i).vindices[1] = (GLuint)vectors[3 * T(i).vindices[1] + 0]; T(i).vindices[2] = (GLuint)vectors[3 * T(i).vindices[2] + 0]; /* free space for old vertices */ /* allocate space for the new vertices */ model->numvertices = numvectors; model->vertices = (GLfloat*)malloc(sizeof(GLfloat) * 3 * (model->numvertices + 1)); /* copy the optimized vertices into the actual vertex list */ for (i = 1; i <= model->numvertices; i++) { model->vertices[3 * i + 0] = copies[3 * i + 0]; model->vertices[3 * i + 1] = copies[3 * i + 1]; model->vertices[3 * i + 2] = copies[3 * i + 2]; numvectors = model->numnormals; vectors = model->normals; copies = glmOptimizeVectors(vectors, &numvectors); printf("glmOptimize(): %d redundant normals.\n", model->numnormals - numvectors); for (i = 0; i < model->numtriangles; i++) { T(i).nindices[0] = (GLuint)vectors[3 * T(i).nindices[0] + 0]; T(i).nindices[1] = (GLuint)vectors[3 * T(i).nindices[1] + 0]; T(i).nindices[2] = (GLuint)vectors[3 * T(i).nindices[2] + 0]; /* free space for old normals */ /* allocate space for the new normals */ model->numnormals = numvectors; model->normals = (GLfloat*)malloc(sizeof(GLfloat) * 3 * (model->numnormals + 1)); /* copy the optimized vertices into the actual vertex list */ for (i = 1; i <= model->numnormals; i++) { model->normals[3 * i + 0] = copies[3 * i + 0]; model->normals[3 * i + 1] = copies[3 * i + 1]; model->normals[3 * i + 2] = copies[3 * i + 2]; if (model->numtexcoords) { numvectors = model->numtexcoords; vectors = model->texcoords; copies = glmOptimizeVectors(vectors, &numvectors); printf("glmOptimize(): %d redundant texcoords.\n", model->numtexcoords - numvectors); for (i = 0; i < model->numtriangles; i++) { for (j = 0; j < 3; j++) { T(i).tindices[j] = (GLuint)vectors[3 * T(i).tindices[j] + 0]; /* free space for old texcoords */ /* allocate space for the new texcoords */ model->numtexcoords = numvectors; model->texcoords = (GLfloat*)malloc(sizeof(GLfloat) * 2 * (model->numtexcoords + 1)); /* copy the optimized vertices into the actual vertex list */ for (i = 1; i <= model->numtexcoords; i++) { model->texcoords[2 * i + 0] = copies[2 * i + 0]; model->texcoords[2 * i + 1] = copies[2 * i + 1]; /* look for unused vertices */ /* look for unused normals */ /* look for unused texcoords */ for (i = 1; i <= model->numvertices; i++) { for (j = 0; j < model->numtriangles; i++) { if (T(j).vindices[0] == i ||