// PhilVaz ASC Model Loading Demo // Put model.asc in same folder as .exe // change the file name to model.asc // NOTE: only reads in first Named object of model // Nov 5, 2004 #include #include #include #include // for exit() #define MAX_BUF 255 #define MAX_VERT 99999 #define MAX_LINES 61 FILE *file = NULL; // file handle char line[MAX_BUF]; // line buffer int page_lines = 0; // lines per page int total_tris = 0; // total triangles (polygons) float verts[MAX_VERT][3]; // vertex array int tris[MAX_VERT][3]; // triangles array (index to verts) void WaitForKey(); void ExitError(int); void ReadALine(); void main() { int i = 0, j = 0; // loop counters int v = 0, f = 0; // vertices and faces (triangles) float x = 0.0, y = 0.0, z = 0.0; // temp X Y Z vertex int a = 0, b = 0, c = 0; // temp A B C face file = fopen("model.asc","r"); // open the model file if (file == NULL) ExitError(1); printf("File model.asc opened ok\n"); ReadALine(); // read Ambient light line ReadALine(); // read blank line ReadALine(); // read Named object line ReadALine(); // read # vertices and faces line sscanf(line, "Tri-mesh, Vertices: %d Faces: %d", &v, &f); printf("Verts = %d Faces = %d\n", v, f); if (v == 0 || f == 0) ExitError(2); if (v > MAX_VERT || f > MAX_VERT) ExitError(3); ReadALine(); // read Vertex list line for (i = 0; i < v; i++) { ReadALine(); // read X Y Z vertex point sscanf(line, "Vertex %d: X:%f Y:%f Z:%f", &j, &x, &y, &z); // save the vertex point in array verts[i][0] = x; verts[i][1] = y; verts[i][2] = z; } printf("Last Vertex line read: X = %f Y = %f Z = %f\n", x, y, z); ReadALine(); // read Face list line total_tris += f; if (total_tris > MAX_VERT) ExitError(3); for (i = 0; i < f; i++) { ReadALine(); // read A B C face (index to vertex array) sscanf(line, "Face %d: A:%d B:%d C:%d", &j, &a, &b, &c); // save the face indexes (triangles) in array tris[i][0] = a; tris[i][1] = b; tris[i][2] = c; ReadALine(); // read Material line ReadALine(); // read Smoothing line } fclose(file); printf("Last Face line read: A = %d B = %d C = %d\n", a, b, c); ExitError(0); } // END OF main void ExitError(int n) { if (n == 0) printf("No Errors! Reached end of program!\n\n"); if (n == 1) printf("Error: File not opened\n\n"); if (n == 2) printf("Error: Faces or Vertices = 0\n\n"); if (n == 3) printf("Error: greater than 100,000 Polygons\n\n"); if (file) fclose(file); WaitForKey(); exit(0); } // END OF ExitError void ReadALine() { char temp[MAX_BUF]; fgets(line, MAX_BUF, file); page_lines++; if (page_lines == MAX_LINES) // we hit end of page { page_lines = 0; fgets(temp, MAX_BUF, file); // read a line fgets(temp, MAX_BUF, file); // read a line fgets(temp, MAX_BUF, file); // read a line fgets(temp, MAX_BUF, file); // read a line fgets(temp, MAX_BUF, file); // read a line } } // END OF ReadALine void WaitForKey() { printf("Waiting for key...\n"); do { // nothing } while (!kbhit()); return; } // END OF WaitForKey