aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-04-17 09:04:23 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-04-17 09:04:23 +0000
commit557dd30015bc696a087feee122f3c112fef21459 (patch)
treefed008dabc4dab83f7eba3394761d710ab354bab
parent6c8866fbc243e59d1b3a6d58525a449cbeb9e974 (diff)
assplode() stuff
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@5 84d2e878-0bd5-11dd-ad15-13eda11d74c5
-rw-r--r--src/model.c37
-rw-r--r--src/utils.c12
-rw-r--r--src/utils.h7
3 files changed, 45 insertions, 11 deletions
diff --git a/src/model.c b/src/model.c
index 40ecde8..8a8fd50 100644
--- a/src/model.c
+++ b/src/model.c
@@ -175,6 +175,7 @@ static int model_load_obj(ModelContext *ctx, const char *name, RenderContext *re
texcoords = malloc(MAX_VERTICES*2*sizeof(GLfloat));
num_vertices = 0;
n_vtmp = 0;
+ n_vntmp = 0;
texture = NULL;
while ( !feof(fh) ) {
@@ -215,22 +216,42 @@ static int model_load_obj(ModelContext *ctx, const char *name, RenderContext *re
if ( line[s] == 'f' ) {
char **bits;
int i, n;
- n = assplode(line, " \t\r\n", &bits);
- printf("The %i bits are:\n", n);
+ n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
for ( i=0; i<n; i++ ) {
char **sp;
- int np, ip;
- printf("%3i: '%s'\n", i, bits[i]);
- np = assplode(bits[i], "/", &sp);
- for ( ip=0; ip<np; ip++ ) {
- printf("Subbit %i: '%s'\n", ip, sp[ip]);
+ 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;
}
- if ( np < 3 ) {
+ if ( nnum >= n_vntmp ) {
+ fprintf(stderr, "Normal index is too high\n");
continue;
}
+ 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);
}
}
diff --git a/src/utils.c b/src/utils.c
index a407416..c837eb5 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -16,6 +16,8 @@
#include <string.h>
#include <stdlib.h>
+#include "utils.h"
+
void chomp(char *a) {
unsigned int i;
@@ -56,16 +58,17 @@ static int assplode_extract(char ***pbits, int n, size_t n_captured, size_t star
* The array of bits also needs to be freed with free() when finished with, unless
* n=0 in which case bits==NULL
*/
-int assplode(const char *a, const char *delims, char ***pbits) {
+int assplode(const char *a, const char *delims, char ***pbits, AssplodeFlag flags) {
size_t i, start, n_captured;
- int n;
+ int n, last_was_delim;
char **bits;
n = 0;
i = 0;
n_captured = 0;
start = 0;
+ last_was_delim = 0;
bits = NULL;
while ( i < strlen(a) ) {
@@ -77,6 +80,10 @@ int assplode(const char *a, const char *delims, char ***pbits) {
}
n_captured = 0;
+ if ( (flags & ASSPLODE_DUPS) && last_was_delim ) {
+ n = assplode_extract(&bits, n, 0, start, a);
+ }
+ last_was_delim = 1;
} else {
@@ -85,6 +92,7 @@ int assplode(const char *a, const char *delims, char ***pbits) {
start = i;
}
n_captured++;
+ last_was_delim = 0;
}
diff --git a/src/utils.h b/src/utils.h
index ff34a5f..c670c6a 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -21,8 +21,13 @@
#define rad2deg(a) ((a)*180/M_PI)
#define deg2rad(a) ((a)*M_PI/180)
+typedef enum {
+ ASSPLODE_NONE = 0,
+ ASSPLODE_DUPS = 1<<0
+} AssplodeFlag;
+
extern void chomp(char *a);
-extern int assplode(const char *a, const char *delims, char ***pbits);
+extern int assplode(const char *a, const char *delims, char ***pbits, AssplodeFlag flags);
#endif /* UTILS_H */