Advertisement
KeithS

Load / Save - Text / Binary files

Apr 21st, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.76 KB | None | 0 0
  1. /*
  2. WK Shearer, April 2012. This is a prototype program for converting text files of 3D objects
  3. into binary files to save objects for OpenGL programs. Maybe a lot of polishing required.
  4. The eventual objective is to be able to handle multiple objects and dump them onto one binary file.
  5. At present it is formatted for GL clientstate vertex array, with indices, but could also handle
  6. texture points, colors and normals.
  7. */
  8.  
  9. #include <cstdio>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. struct vector3f
  15. {
  16.     float x;
  17.     float y;
  18.     float z;
  19. };
  20.  
  21. struct vector3i
  22. {
  23.     int a;
  24.     int b;
  25.     int c;
  26. };
  27.  
  28. // *******************************************************************************
  29.  
  30. // Read the data from the text file
  31. bool read_text_data(vector<vector3f> &vec_3f, vector<vector3i> &vec_3i, FILE **file)
  32. {
  33.     int c_size = 0;
  34.     int i;
  35.  
  36.     vector3f temp_vec_3f;
  37.     vector3i temp_vec_3i;
  38.  
  39.     fscanf(*file, "%d", &c_size);
  40.     vec_3f.reserve((size_t)c_size);
  41.     for(i = 0; i < c_size; i++)
  42.     {
  43.         fscanf(*file, "%f %f %f", &temp_vec_3f.x, &temp_vec_3f.y, &temp_vec_3f.z);
  44.         vec_3f.push_back(temp_vec_3f);
  45.         if(feof(*file) || ferror(*file))
  46.             return false;
  47.     }
  48.  
  49.     fscanf(*file, "%d", &c_size);
  50.     for(i = 0; i < c_size; i++)
  51.     {
  52.         fscanf(*file, "%d %d %d", &temp_vec_3i.a, &temp_vec_3i.b, &temp_vec_3i.c);
  53.         vec_3i.push_back(temp_vec_3i);
  54.         if(feof(*file) || ferror(*file))
  55.             return false;
  56.     }
  57.     return true;
  58. }
  59.  
  60. // *******************************************************************************
  61.  
  62. bool write_bin_data(vector<vector3f> &vec_3f, vector<vector3i> &vec_3i, FILE **file, FILE **g_file)
  63. {
  64.     int i;
  65.  
  66.     for(i = 0; i < (int)vec_3f.size(); i++)
  67.     {
  68.         fwrite(&vec_3f.at((size_t)i), sizeof(struct vector3f), 1, *file);
  69.         if(ferror(*file))
  70.             return false;
  71.     }
  72.  
  73.     for(i = 0; i < (int)vec_3i.size(); i++)
  74.     {
  75.         fwrite(&vec_3i.at((size_t)i), sizeof(struct vector3i), 1, *file);
  76.         if(ferror(*file))
  77.             return false;
  78.     }
  79.  
  80.     fprintf(*g_file, "%d %d\n", (int)vec_3f.size(), (int)vec_3i.size());
  81.     if(ferror(*g_file))
  82.         return false;
  83.  
  84.     return true;
  85. }
  86.  
  87. // *******************************************************************************
  88.  
  89. bool read_bin_data(vector<vector3f> &new_vec_3f, vector<vector3i> &new_vec_3i, FILE **file, FILE **g_file)
  90. {
  91.     int i;
  92.     int i_vec_3f_size = 0;
  93.     int i_vec_3i_size = 0;
  94.  
  95.     vector3f temp_vec_3f;
  96.     vector3i temp_vec_3i;
  97.  
  98.     fscanf(*g_file, "%d %d", &i_vec_3f_size, &i_vec_3i_size);
  99.     if(feof(*g_file) || ferror(*g_file))
  100.     {
  101.             printf("Error reading guide file\n");
  102.             return false;
  103.     }
  104.  
  105.     new_vec_3f.reserve((size_t)i_vec_3f_size);
  106.     new_vec_3i.reserve((size_t)i_vec_3i_size);
  107.     for(i = 0; i < i_vec_3f_size; i++)
  108.     {
  109.         fread(&temp_vec_3f, sizeof(struct vector3f), 1, *file);
  110.         if(feof(*file) || ferror(*file))
  111.         {
  112.             printf("Error reading binary - fp_points\n");
  113.             return false;
  114.         }
  115.  
  116.         new_vec_3f.push_back(temp_vec_3f);
  117.     }
  118.     for(i = 0; i < i_vec_3i_size; i++)
  119.     {
  120.         fread(&temp_vec_3i, sizeof(struct vector3i), 1, *file);
  121.         if(feof(*file) || ferror(*file))
  122.         {
  123.             printf("Error reading binary - i_lines\n");
  124.             return false;
  125.         }
  126.  
  127.         new_vec_3i.push_back(temp_vec_3i);
  128.     }
  129.  
  130.     return true;
  131. }
  132.  
  133. // *******************************************************************************
  134.  
  135. void convert_to_vector_contiguous(vector<float> &vec_3f_cont, vector<int> &vec_3i_cont, vector<vector3f> new_vec_3f, vector<vector3i> new_vec_3i)
  136. {
  137.     int i;
  138.  
  139.     vec_3f_cont.reserve(new_vec_3f.size() * 3);
  140.     vec_3i_cont.reserve(new_vec_3i.size() * 3);
  141.  
  142.     for(i = 0; i < (int)new_vec_3f.size(); i++)
  143.     {
  144.         vec_3f_cont.push_back(new_vec_3f.at(i).x);
  145.         vec_3f_cont.push_back(new_vec_3f.at(i).y);
  146.         vec_3f_cont.push_back(new_vec_3f.at(i).z);
  147.     }
  148.  
  149.     for(i = 0; i < (int)new_vec_3i.size(); i++)
  150.     {
  151.         vec_3i_cont.push_back(new_vec_3i.at(i).a);
  152.         vec_3i_cont.push_back(new_vec_3i.at(i).b);
  153.         vec_3i_cont.push_back(new_vec_3i.at(i).c);
  154.     }
  155. }
  156.  
  157. // *******************************************************************************
  158. int main()
  159. {
  160.     vector<vector3f> fp_points;
  161.     vector<vector3i> i_lines;
  162.  
  163.     int i;
  164.  
  165.     // read data from the text data file...
  166.     // -------------------------------------------------------------------
  167.     FILE *p_datafile;
  168.     p_datafile = fopen("data.txt", "r");
  169.     if(!p_datafile)
  170.     {
  171.         printf("No data file.");
  172.         return -1;
  173.     }
  174.  
  175.     if(read_text_data(fp_points, i_lines, &p_datafile) == true)
  176.     {
  177.         fclose(p_datafile);
  178.     }
  179.     else
  180.     {
  181.         fclose(p_datafile);
  182.         printf("Error in the text source file\n");
  183.         return -1;
  184.     }
  185.  
  186.     // Print out the read data file as it resides in the vector structures...
  187.     // -------------------------------------------------------------------
  188.     printf("\nDATA AS READ FROM TEXT DATA FILE\n");
  189.     for(i = 0; i < (int)fp_points.size(); i++)
  190.     {
  191.         printf("x = %.2f, y = %.2f, z = %.2f\n", fp_points.at((size_t)i).x, fp_points.at((size_t)i).y, fp_points.at((size_t)i).z);
  192.     }
  193.     for(i = 0; i < (int)i_lines.size(); i++)
  194.     {
  195.         printf("lp1 = %d, lp2 = %d, lp3 = %d\n", i_lines.at((size_t)i).a, i_lines.at((size_t)i).b, i_lines.at((size_t)i).c);
  196.     }
  197.  
  198.     // Save the data as a binary file...
  199.     // -------------------------------------------------------------------
  200.     FILE *p_guidefile;
  201.     p_guidefile = fopen("bin_guide.txt", "w");
  202.     p_datafile = fopen("bin_data.bin", "wb");
  203.     if(!p_datafile)
  204.     {
  205.         printf("No binary data file created.");
  206.         return -1;
  207.     }
  208.  
  209.     if(write_bin_data(fp_points, i_lines, &p_datafile, &p_guidefile) == true)
  210.     {
  211.         fclose(p_datafile);
  212.         fclose(p_guidefile);
  213.     }
  214.     else
  215.     {
  216.         fclose(p_datafile);
  217.         fclose(p_guidefile);
  218.         printf("Error writing to binary or guide file\n");
  219.         return -1;
  220.     }
  221.  
  222.     // Now load the binary file into a new, identical vector to test it...
  223.     vector<vector3f> fp_new_points;
  224.     vector<vector3i> i_new_lines;
  225.     //fp_new_points.reserve(fp_points.size());
  226.     //vector3f fp_temp_vec;
  227.     p_guidefile = fopen("bin_guide.txt", "r");
  228.     if(!p_guidefile)
  229.     {
  230.         printf("No data guide file opened");
  231.         return -1;
  232.     }
  233.  
  234.     p_datafile = fopen("bin_data.bin", "rb");
  235.     if(!p_datafile)
  236.     {
  237.         printf("No binary data file opened");
  238.         return -1;
  239.     }
  240.  
  241.     if(read_bin_data(fp_new_points, i_new_lines, &p_datafile, &p_guidefile) == true)
  242.     {
  243.         fclose(p_datafile);
  244.         fclose(p_guidefile);
  245.     }
  246.     else
  247.     {
  248.         fclose(p_datafile);
  249.         fclose(p_guidefile);
  250.         printf("Error reading binary or guide file\n");
  251.         return -1;
  252.     }
  253.  
  254.     // print the data as read from the binary file into the new vectors. It should be identical...
  255.     // -------------------------------------------------------------------
  256.     printf("\nDATA AS READ FROM BINARY DATA FILE\n");
  257.     for(i = 0; i < (int)fp_new_points.size(); i++)
  258.     {
  259.         printf("x = %.2f, y = %.2f, z = %.2f\n", fp_new_points.at((size_t)i).x, fp_new_points.at((size_t)i).y, fp_new_points.at((size_t)i).z);
  260.     }
  261.     for(i = 0; i < (int)i_new_lines.size(); i++)
  262.     {
  263.          printf("lp1 = %d, lp2 = %d, lp3 = %d\n", i_new_lines.at((size_t)i).a, i_new_lines.at((size_t)i).b, i_new_lines.at((size_t)i).c);
  264.     }
  265.  
  266.     // FOR OPENGL...
  267.     // Here the points and the lines are loaded into a contiguous, non structured vector
  268.     // so that it can be used by glClientState, vertex array, etc...
  269.     printf("\nDATA AS READ FROM BINARY DATA FILE TO CONTIGUOUS VECTOR\n");
  270.     vector<float> fp_points_cont;
  271.     vector<int> i_lines_cont;
  272.  
  273.     convert_to_vector_contiguous(fp_points_cont, i_lines_cont, fp_new_points, i_new_lines);
  274.  
  275.     for(i = 0; i < (int)fp_points_cont.size(); i += 3)
  276.     {
  277.         printf("x = %.2f, y = %.2f, z = %.2f\n", fp_points_cont.at((size_t)i), fp_points_cont.at(size_t(i + 1)), fp_points_cont.at(size_t(i + 2)));
  278.     }
  279.  
  280.     for(i = 0; i < (int)i_lines_cont.size(); i += 3)
  281.     {
  282.         printf("lp1 = %d, lp2 = %d, lp3 = %d\n", i_lines_cont.at((size_t)i), i_lines_cont.at(size_t(i + 1)), i_lines_cont.at(size_t(i + 2)));
  283.     }
  284.  
  285.     return 0;
  286. }
  287.  
  288.  
  289. /*
  290. The data text file used by the program. Save as;
  291. data.txt
  292. ...and lop it in with the compiled executable.
  293.  
  294. 8
  295. -2.5 -2.5 2.5
  296. 2.5 -2.5 2.5
  297. 2.5 2.5 2.5
  298. -2.5 2.5 2.5
  299. 2.5 -2.5 -2.5
  300. -2.5 -2.5 -2.5
  301. -2.5 2.5 -2.5
  302. 2.5 2.5 -2.5
  303. 12
  304. 0 1 3
  305. 1 2 3
  306. 1 4 2
  307. 4 7 2
  308. 4 5 7
  309. 5 6 7
  310. 5 0 6
  311. 0 3 6
  312. 3 2 6
  313. 2 7 6
  314. 1 0 4
  315. 0 5 4
  316.  
  317. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement