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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <stdio.h>
#include <stdlib.h>
typedef float Tvertexf[3];
typedef int Tvertexi[3];
typedef char Tmaterial;
int main (void)
{
FILE *objfile = fopen ("monFichier.obj", "r");
if (objfile == NULL)
{
printf ("error opening .OBJ file\n");
exit (EXIT_FAILURE);
}
/* read material (.mtl ) */
{
char FileMTL[12];
fseek (objfile, 127 * sizeof (char), SEEK_SET);
fscanf (objfile, "%s ", FileMTL);
printf (" materials file : %s \n", FileMTL);
while (fgetc (objfile) != EOF)
{
int nbVertices = 0;
int nbNormals = 0;
int nbFaces = 0;
fscanf (objfile, "#begin %d vertices", &nbVertices);
fscanf (objfile, "#begin %d normals", &nbNormals);
fscanf (objfile, "#begin %d faces", &nbFaces);
if (nbVertices != 0)
{
printf (" nb vertices : %d \n", nbVertices);
Tvertexf *vertices = malloc (nbVertices * sizeof (*vertices));
/* read vertices */
{
int row;
for (row = 0; row < nbVertices; row++)
{
fscanf (objfile, "v %f %f %f", &vertices[row][0],
&vertices[row][1], &vertices[row][2]);
}
}
/*print out vertices to check */
{
int row;
for (row = 0; row < nbVertices; row++)
{
int col;
for (col = 0; col < 3; col++)
{
printf ("%f\t", vertices[row][col]);
}
printf ("\n");
}
}
}
else if (nbNormals != 0)
{
printf (" nb normals : %d \n", nbNormals);
Tvertexf *normals = malloc (nbNormals * sizeof (*normals));
/*read NormalsLists */
{
int row;
for (row = 0; row < nbNormals; row++)
{
fscanf (objfile, "vn %f %f %f", &normals[row][0],
&normals[row][1], &normals[row][2]);
}
}
/*print out normalslist to check */
{
int row;
for (row = 0; row < nbNormals; row++)
{
int col;
for (col = 0; col < 2; col++)
{
printf ("%f\t", normals[row][col]);
}
printf ("\n");
}
}
}
else if (nbFaces != 0)
{
Tvertexi *facelist = malloc (nbFaces * sizeof (*facelist));
Tmaterial *material = malloc (nbFaces * sizeof (*material));
/* read facelist */
/* f vector1//normal1 vector2//normal2 vector3//normal3 */
{
int row;
for (row = 0; row < nbFaces; row++)
{
fscanf (objfile, "usemtl %s", &material[row]);
fscanf (objfile, "f %d//%d %d//%d %d//%d",
&facelist[row][0], &facelist[row][3],
&facelist[row][1], &facelist[row][4],
&facelist[row][2], &facelist[row][5]);
// facelists[no of face][data for face]
// data are : [vert1 | vert2 | vert3 | norm1 | norm2 | norm3]
}
}
/*print out facelist to check */
{
int row;
for (row = 0; row < 6; row++)
{
int col;
for (col = 0; col < 4; col++)
printf ("%d\t", facelist[row][col]);
printf ("\n");
}
}
}
}
}
return 0;
} |
Partager