aboutsummaryrefslogtreecommitdiff
path: root/src/model.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/model.c')
-rw-r--r--src/model.c127
1 files changed, 87 insertions, 40 deletions
diff --git a/src/model.c b/src/model.c
index 8a8fd50..afbb5bb 100644
--- a/src/model.c
+++ b/src/model.c
@@ -26,7 +26,7 @@
#include "render.h"
/* Maximum number of vertices per primitive */
-#define MAX_VERTICES 256
+#define MAX_VERTICES 4096
ModelContext *model_init() {
@@ -169,14 +169,9 @@ static int model_load_obj(ModelContext *ctx, const char *name, RenderContext *re
return -1;
}
- model = model_new(name);
- vertices = malloc(MAX_VERTICES*3*sizeof(GLfloat));
- normals = malloc(MAX_VERTICES*3*sizeof(GLfloat));
- texcoords = malloc(MAX_VERTICES*2*sizeof(GLfloat));
- num_vertices = 0;
+ /* Zip through and find all the vertices */
n_vtmp = 0;
n_vntmp = 0;
- texture = NULL;
while ( !feof(fh) ) {
char line[1024];
@@ -212,48 +207,100 @@ static int model_load_obj(ModelContext *ctx, const char *name, RenderContext *re
n_vntmp++;
continue;
}
+
+ }
+
+ /* Go through again and look for faces */
+ model = model_new(name);
+ vertices = malloc(MAX_VERTICES*3*sizeof(GLfloat));
+ normals = malloc(MAX_VERTICES*3*sizeof(GLfloat));
+ texcoords = malloc(MAX_VERTICES*2*sizeof(GLfloat));
+ texture = NULL;
+ num_vertices = 0;
+ rewind(fh);
+ while ( !feof(fh) ) {
+
+ char line[1024];
+ char **bits;
+ int n, i;
+
+ fgets(line, 1023, fh);
+ n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
- if ( line[s] == 'f' ) {
- char **bits;
- int i, n;
- n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
- for ( i=0; i<n; i++ ) {
+ /* Read in a face */
+ if ( strcmp(bits[0], "f") == 0 ) {
+
+ for ( i=1; i<n; i++ ) {
+
char **sp;
- int np, ip, vnum, nnum;
- np = assplode(bits[i], "/", &sp, ASSPLODE_DUPS);
- if ( np != 3 ) {
- continue;
- }
- vnum = atoi(sp[0]);
- nnum = atoi(sp[2]);
- if ( vnum >= n_vtmp ) {
- fprintf(stderr, "Vertex index is too high\n");
- continue;
+ int np, ip, vnum, nnum, j, nslash;
+
+ nslash = 0;
+ for ( j=0; j<strlen(bits[i]); j++ ) {
+ if ( bits[i][j] == '/' ) nslash++;
}
- if ( nnum >= n_vntmp ) {
- fprintf(stderr, "Normal index is too high\n");
- continue;
+ if ( nslash == 2 ) {
+
+ np = assplode(bits[i], "/", &sp, ASSPLODE_DUPS);
+ if ( np != 3 ) {
+ continue;
+ }
+ vnum = atoi(sp[0])-1;
+ nnum = atoi(sp[2])-1;
+ if ( vnum >= n_vtmp ) {
+ fprintf(stderr, "Vertex index is too high (%i/%i)\n", vnum, n_vtmp);
+ continue;
+ }
+ if ( nnum >= n_vntmp ) {
+ fprintf(stderr, "Normal index is too high (%i/%i)\n", nnum, n_vntmp);
+ continue;
+ }
+ if ( num_vertices < MAX_VERTICES ) {
+ vertices[3*num_vertices+0] = vtmp[3*vnum+0];
+ vertices[3*num_vertices+1] = vtmp[3*vnum+1];
+ vertices[3*num_vertices+2] = vtmp[3*vnum+2];
+ normals[3*num_vertices+0] = vntmp[3*nnum+0];
+ normals[3*num_vertices+1] = vntmp[3*nnum+1];
+ normals[3*num_vertices+2] = vntmp[3*nnum+2];
+ texcoords[2*num_vertices+0] = 0.0;
+ texcoords[2*num_vertices+1] = 0.0;
+ num_vertices++;
+ } else {
+ fprintf(stderr, "Too many vertices\n");
+ }
+ free(sp[0]);
+ free(sp[1]);
+ free(sp[2]);
+ free(sp);
+
+ } else if ( nslash == 0 ) {
+
+ vnum = atoi(bits[i]);
+ if ( num_vertices < MAX_VERTICES ) {
+ vertices[3*num_vertices+0] = vtmp[3*vnum+0];
+ vertices[3*num_vertices+1] = vtmp[3*vnum+1];
+ vertices[3*num_vertices+2] = vtmp[3*vnum+2];
+ normals[3*num_vertices+0] = 1.0;
+ normals[3*num_vertices+1] = 0.0;
+ normals[3*num_vertices+2] = 0.0;
+ num_vertices++;
+ } else {
+ fprintf(stderr, "Too many vertices\n");
+ }
+
}
- vertices[3*num_vertices+0] = vtmp[3*vnum+0];
- vertices[3*num_vertices+1] = vtmp[3*vnum+1];
- vertices[3*num_vertices+2] = vtmp[3*vnum+2];
- normals[3*num_vertices+0] = vntmp[3*nnum+0];
- normals[3*num_vertices+1] = vntmp[3*nnum+1];
- normals[3*num_vertices+2] = vntmp[3*nnum+2];
- texcoords[2*num_vertices+0] = 0.0;
- texcoords[2*num_vertices+1] = 0.0;
- num_vertices++;
- free(sp[0]);
- free(sp[1]);
- free(sp[2]);
- free(sp);
- free(bits[i]);
+
}
- free(bits);
+
model_add_primitive(model, GL_POLYGON, vertices, normals, texcoords, num_vertices,
ATTRIB_NONE, 1.0, 1.0, 1.0, texture);
+ num_vertices = 0;
+
}
+ for ( i=0; i<n; i++ ) free(bits[i]);
+ free(bits);
+
}
fclose(fh);