512 | | Sometimes we hit limitations of the visualisation tools for the data that we want to visualize. For example [attachment:motorBike.obj] from [http://openfoam.org OpenFOAM] contains object groups that we want to show colored separately and not as whole. Neither [http://wci.llnl.gov/codes/visit/ VisIt] and [http://paraview.org ParaView] can read [http://en.wikipedia.org/wiki/Wavefront_.obj_file Wavefront OBJ] file with group separation. We are forced to convert [attachment:motorBike.obj] into bunch of files and read them one by one. |
| 512 | Sometimes we hit limitations of the visualisation tools for the data that we want to visualize. For example [attachment:motorBike.obj] from [http://openfoam.org OpenFOAM] contains object groups that we want to show colored separately and not as whole. Neither [http://wci.llnl.gov/codes/visit/ VisIt] and [http://paraview.org ParaView] can read [http://en.wikipedia.org/wiki/Wavefront_.obj_file Wavefront OBJ] file with group separation. We are forced to convert [attachment:motorBike.obj] into bunch of files and read them one by one. The following {{{wavefront.c}}} converts [attachment:motorBike.obj] into 67 files. Try to open them in VisIt and ParaView. |
| 513 | {{{ |
| 514 | #!c |
| 515 | #include <stdio.h> |
| 516 | #include <GL/glew.h> |
| 517 | #include <GL/glut.h> |
| 518 | |
| 519 | #define MaxVertices 400000 |
| 520 | #define MaxFaces 400000 |
| 521 | #define MaxGroups 100 |
| 522 | |
| 523 | GLfloat vertex[MaxVertices][3]; |
| 524 | GLuint face[MaxFaces][3]; |
| 525 | char group_name[MaxGroups][80]; |
| 526 | int start_face[MaxGroups]; |
| 527 | |
| 528 | int vertices = 0; |
| 529 | int faces = 0; |
| 530 | int groups = 0; |
| 531 | |
| 532 | void read_wavefront(const char *filename) |
| 533 | { |
| 534 | char line[80]; |
| 535 | FILE *f = fopen(filename, "r"); |
| 536 | while(fgets(line, sizeof(line), f)) |
| 537 | switch(line[0]) |
| 538 | { |
| 539 | case 'v': |
| 540 | sscanf(&line[1], "%f %f %f", &vertex[vertices][0], |
| 541 | &vertex[vertices][1], &vertex[vertices][2]); |
| 542 | ++vertices; |
| 543 | break; |
| 544 | case 'g': |
| 545 | sscanf(&line[1], "%s", group_name[groups]); |
| 546 | start_face[groups++] = faces; |
| 547 | break; |
| 548 | case 'f': |
| 549 | sscanf(&line[1], "%d %d %d", &face[faces][0], |
| 550 | &face[faces][1], &face[faces][2]); |
| 551 | ++faces; |
| 552 | break; |
| 553 | } |
| 554 | fclose(f); |
| 555 | start_face[groups] = faces; |
| 556 | printf("Read %d vertices and %d faces within %d groups from %s\n", |
| 557 | vertices, faces, groups, filename); |
| 558 | } |
| 559 | |
| 560 | void write_wavefront(int group_number) |
| 561 | { |
| 562 | int i = 0; char n[80], *p = group_name[group_number]; |
| 563 | while (*p != '%' && *p != '\0') n[i++] = *p++; // remove % from name |
| 564 | n[i++] = '.'; n[i++] = 'o'; n[i++] = 'b'; n[i++] = 'j'; n[i] = '\0'; |
| 565 | FILE *f = fopen(n, "w"); fprintf(f, "# Wavefront OBJ file\n"); |
| 566 | for (i = 0; i < vertices; i++) |
| 567 | fprintf(f, "v %g %g %g\n", vertex[i][0], vertex[i][1], vertex[i][2]); |
| 568 | fprintf(f, "g %s\n", group_name[group_number]); |
| 569 | for (i = start_face[group_number]; i < start_face[group_number+1]; ++i) |
| 570 | fprintf(f, "f %d %d %d\n", face[i][0], face[i][1], face[i][2]); |
| 571 | fclose(f); |
| 572 | } |
| 573 | |
| 574 | int main(int argc, char **argv) |
| 575 | { |
| 576 | int i; |
| 577 | read_wavefront("motorBike.obj"); |
| 578 | for(i = 0; i < groups; i++) write_wavefront(i); |
| 579 | return 0; |
| 580 | } |
| 581 | }}} |