working on the .OBJ parser

This commit is contained in:
tth 2021-05-18 17:14:00 +02:00
parent 02c13e0613
commit bcd64245d2
3 changed files with 83 additions and 16 deletions

View File

@ -4,7 +4,7 @@
/* --------------------------------------------------------------------- */
#define LIBBB_VERSION 55
#define LIBBB_VERSION 56
#define SZ_BUBULLE_TEXT 51 /* arbitrary value */

View File

@ -15,13 +15,66 @@ extern int verbosity;
/* --------------------------------------------------------------------- */
#define LINE_SZ 666
/* --------------------------------------------------------------------- */
typedef struct {
char *token;
int id;
} Tokens;
enum token_id { T_comment=1, T_vertice };
Tokens TokenList[] = {
{ "#", T_comment },
{ "v", T_vertice },
{ NULL, 0 }
};
static int type_of_the_line(char *text)
{
Tokens *token;
int id;
#if DEBUG_LEVEL
fprintf(stderr, "%s is searching '%s'\n", __func__, text);
#endif
for (token=TokenList; token->token; token++) {
// fprintf(stderr, " %5s -> %d\n", token->token, token->id);
if (!strcmp(token->token, text)) {
return token->id;
}
}
return -1;
}
/* --------------------------------------------------------------------- */
static int parse_vertice(char *cptr, float *px, float *py, float *pz)
{
float x, y, z;
int foo;
foo = 0;
cptr = strtok(NULL, " ");
foo += sscanf(cptr, "%f", &x);
cptr = strtok(NULL, " ");
foo += sscanf(cptr, "%f", &y);
cptr = strtok(NULL, " ");
foo += sscanf(cptr, "%f", &z);
if (3 == foo) {
*px = x; *py = y; *pz = z;
}
return foo;
}
/* --------------------------------------------------------------------- */
int try_to_read_an_OBJ_file(char *fname, int notused)
{
FILE *fpin;
char line[LINE_SZ+1], *cptr;
float x, y, z;
int foo, nbre;
int foo, nbre, tokenid;
BBList *bublist;
Bubulle bubulle;
@ -44,24 +97,38 @@ print_bublist_desc(bublist, 0);
nbre = 0;
while(NULL!=(cptr=fgets(line, LINE_SZ, fpin))) {
if (verbosity>1) fputs(line, stderr);
if ('\n' != line[strlen(line)-1]) {
fprintf(stderr, "%s: short read, exiting...\n", __func__);
return -2;
}
line[strlen(line)-1] = '\0'; /* kill the newline */
if (verbosity>1) fprintf(stderr, "line read ===%s===\n", line);
cptr = strtok(line, " ");
if (strcmp(cptr, "v")) continue;
cptr = strtok(NULL, " ");
foo = sscanf(cptr, "%f", &x);
cptr = strtok(NULL, " ");
foo = sscanf(cptr, "%f", &y);
cptr = strtok(NULL, " ");
foo = sscanf(cptr, "%f", &z);
if (NULL == cptr) {
fprintf(stderr, "no token ?\n");
continue;
}
tokenid = type_of_the_line(cptr);
fprintf(stderr, "tok '%s' --> %d\n", cptr, tokenid);
memset(&bubulle, 0, sizeof(Bubulle));
bubulle.p.x = x;
bubulle.p.y = y;
bubulle.p.z = z;
if (verbosity > 1) niceprint_bubulle(&bubulle, 0);
switch (tokenid) {
case T_comment:
/* do nothing */
break;
case T_vertice:
foo = parse_vertice(cptr, &x, &y, &z);
bubulle.p.x = x;
bubulle.p.y = y;
bubulle.p.z = z;
if (verbosity > 1) niceprint_bubulle(&bubulle, 0);
break;
default:
continue; /* wtf ? */
}
foo = push_bubulle(bublist, &bubulle);
if (foo) {

View File

@ -18,7 +18,7 @@ if (2 != argc) {
exit(0);
}
verbosity = 1;
verbosity = 2;
foo = try_to_read_an_OBJ_file(argv[1], 0);
fprintf(stderr, "try to read -> %d\n", foo);