static BOOL ParseVertexUVs(char **data, Mesh3D *mesh) { FLOAT scale_u = SPFlt(16); FLOAT scale_v = SPFlt(16); UVCoord *uv; WORD n; if (!(ReadShort(data, &n) && EndOfLine(data))) return FALSE; Log("[3D] Mesh has %ld uv coordinates\n", (LONG)n); mesh->uv = MemAlloc(sizeof(Point2D) * n, MEMF_PUBLIC); uv = mesh->uv ; while (NextLine(data) && !MatchString(data, "@end")) { FLOAT u, v; if (!(ReadFloat(data, &u) && ReadFloat(data, &v) && EndOfLine(data))) return FALSE; uv->u = SPFix(SPMul(u, scale_u)); uv->v = SPFix(SPMul(v, scale_v)); uv++; n--; } return n == 0; }
static BOOL ParseVertices(char **data, Mesh3D *mesh) { FLOAT scale = SPMul(mesh->scale, SPFlt(16)); Point3D *vertex; WORD n; if (!(ReadShort(data, &n) && EndOfLine(data))) return FALSE; Log("[3D] Mesh has %ld points\n", (LONG)n); mesh->vertices = n; mesh->vertex = MemAlloc(sizeof(Point3D) * n, MEMF_PUBLIC); vertex = mesh->vertex; while (NextLine(data) && !MatchString(data, "@end") && n > 0) { FLOAT x, y, z; if (!(ReadFloat(data, &x) && ReadFloat(data, &y) && ReadFloat(data, &z) && EndOfLine(data))) return FALSE; vertex->x = SPFix(SPMul(x, scale)); vertex->y = SPFix(SPMul(y, scale)); vertex->z = SPFix(SPMul(z, scale)); vertex++; n--; } return n == 0; }
__regargs BOOL ReadFloat(char **data, FLOAT *numptr) { char *str = *data; char c; LONG p = 0, q = 1; BOOL minus = FALSE, dot = FALSE; /* Skip white spaces. */ if (!NextWord(&str)) return FALSE; /* Read optional sign character. */ if (*str == '-') str++, minus = TRUE; /* Read at least one digit. */ c = *str; if (!isdigit(c)) return FALSE; while (1) { if (!dot && c == '.') { dot = TRUE; } else if (!isdigit(c)) { break; } else { p = p * 10 + digit(c); if (dot) q *= 10; } c = *(++str); } *data = str; if (numptr) *numptr = SPDiv(SPFlt(q), SPFlt(minus ? -p : p)); return TRUE; }