1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#define FILENAME_LENGTH 100
#define LINE_MAXLENGTH 100
#define VERTICES_LENGTH 10000
#define FACES_LENGTH 10000
int main(void) {
char fileName[FILENAME_LENGTH];
double vertices[VERTICES_LENGTH];
int faces[FACES_LENGTH];
promptFileName(fileName);
readObjFile(fileName, vertices, faces);
return 0;
}
void readObjFile(char* fileName, double* vertices, int* faces){
FILE* infile;
char line[LINE_MAXLENGTH];
int vertices_counter = 0;
int faces_counter = 0;
if((infile = fopen(fileName, "r")) == NULL) {
printf("Error Opening File.\n");
exit(1);
}
printf("parsing du fichier... (%s)\n", fileName);
while( fgets(line, LINE_MAXLENGTH, infile) != NULL ) {
if(line[0] == 'v'){
double* vert = getVertices(line);
printf("contenu de vert : %lf %lf %lf\n", vert[0], vert[1], vert[2]); // affiche les bonnes valeurs
for(int i = 0; i < 3; i++){
vertices[vertices_counter] = vert[i]; // ceci ne fonctionne pas
vertices_counter++;
}
}
else if(line[0] == 'f'){
//int* faces = getFaces(line);
}
}
fclose(infile);
printf("End of parsing\n");
}
double* getVertices(char* line){
double vert[3];
int i = 0;
while(line[i] == 'v' || issep(line[i])){
i++;
}
char res[20];
int pos = 0;
do{
res[pos] = line[i];
pos++;
i++;
}
while(!issep(line[i]) && line[i] != '\n');
res[pos] = '\0';
vert[0] = atof(res); // string -> double
printf("1er vertice est : %lf\n", vert[0]);
// second vertex
i++;
pos = 0;
do{
res[pos] = line[i];
pos++;
i++;
}
while(!issep(line[i]) && line[i] != '\n');
res[pos] = '\0';
vert[1] = atof(res);
printf("2e vertice est : %lf\n", vert[1]);
// third vertex
i++;
pos = 0;
do{
res[pos] = line[i];
pos++;
i++;
}
while(!issep(line[i]) && line[i] != '\n');
res[pos] = '\0';
vert[2] = atof(res);
printf("3e vertex est : %lf\n", vert[2]);
return vert;
} |
Partager