Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- WK Shearer, April 2012. This is a prototype program for converting text files of 3D objects
- into binary files to save objects for OpenGL programs. Maybe a lot of polishing required.
- The eventual objective is to be able to handle multiple objects and dump them onto one binary file.
- At present it is formatted for GL clientstate vertex array, with indices, but could also handle
- texture points, colors and normals.
- */
- #include <cstdio>
- #include <vector>
- using namespace std;
- struct vector3f
- {
- float x;
- float y;
- float z;
- };
- struct vector3i
- {
- int a;
- int b;
- int c;
- };
- // *******************************************************************************
- // Read the data from the text file
- bool read_text_data(vector<vector3f> &vec_3f, vector<vector3i> &vec_3i, FILE **file)
- {
- int c_size = 0;
- int i;
- vector3f temp_vec_3f;
- vector3i temp_vec_3i;
- fscanf(*file, "%d", &c_size);
- vec_3f.reserve((size_t)c_size);
- for(i = 0; i < c_size; i++)
- {
- fscanf(*file, "%f %f %f", &temp_vec_3f.x, &temp_vec_3f.y, &temp_vec_3f.z);
- vec_3f.push_back(temp_vec_3f);
- if(feof(*file) || ferror(*file))
- return false;
- }
- fscanf(*file, "%d", &c_size);
- for(i = 0; i < c_size; i++)
- {
- fscanf(*file, "%d %d %d", &temp_vec_3i.a, &temp_vec_3i.b, &temp_vec_3i.c);
- vec_3i.push_back(temp_vec_3i);
- if(feof(*file) || ferror(*file))
- return false;
- }
- return true;
- }
- // *******************************************************************************
- bool write_bin_data(vector<vector3f> &vec_3f, vector<vector3i> &vec_3i, FILE **file, FILE **g_file)
- {
- int i;
- for(i = 0; i < (int)vec_3f.size(); i++)
- {
- fwrite(&vec_3f.at((size_t)i), sizeof(struct vector3f), 1, *file);
- if(ferror(*file))
- return false;
- }
- for(i = 0; i < (int)vec_3i.size(); i++)
- {
- fwrite(&vec_3i.at((size_t)i), sizeof(struct vector3i), 1, *file);
- if(ferror(*file))
- return false;
- }
- fprintf(*g_file, "%d %d\n", (int)vec_3f.size(), (int)vec_3i.size());
- if(ferror(*g_file))
- return false;
- return true;
- }
- // *******************************************************************************
- bool read_bin_data(vector<vector3f> &new_vec_3f, vector<vector3i> &new_vec_3i, FILE **file, FILE **g_file)
- {
- int i;
- int i_vec_3f_size = 0;
- int i_vec_3i_size = 0;
- vector3f temp_vec_3f;
- vector3i temp_vec_3i;
- fscanf(*g_file, "%d %d", &i_vec_3f_size, &i_vec_3i_size);
- if(feof(*g_file) || ferror(*g_file))
- {
- printf("Error reading guide file\n");
- return false;
- }
- new_vec_3f.reserve((size_t)i_vec_3f_size);
- new_vec_3i.reserve((size_t)i_vec_3i_size);
- for(i = 0; i < i_vec_3f_size; i++)
- {
- fread(&temp_vec_3f, sizeof(struct vector3f), 1, *file);
- if(feof(*file) || ferror(*file))
- {
- printf("Error reading binary - fp_points\n");
- return false;
- }
- new_vec_3f.push_back(temp_vec_3f);
- }
- for(i = 0; i < i_vec_3i_size; i++)
- {
- fread(&temp_vec_3i, sizeof(struct vector3i), 1, *file);
- if(feof(*file) || ferror(*file))
- {
- printf("Error reading binary - i_lines\n");
- return false;
- }
- new_vec_3i.push_back(temp_vec_3i);
- }
- return true;
- }
- // *******************************************************************************
- 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)
- {
- int i;
- vec_3f_cont.reserve(new_vec_3f.size() * 3);
- vec_3i_cont.reserve(new_vec_3i.size() * 3);
- for(i = 0; i < (int)new_vec_3f.size(); i++)
- {
- vec_3f_cont.push_back(new_vec_3f.at(i).x);
- vec_3f_cont.push_back(new_vec_3f.at(i).y);
- vec_3f_cont.push_back(new_vec_3f.at(i).z);
- }
- for(i = 0; i < (int)new_vec_3i.size(); i++)
- {
- vec_3i_cont.push_back(new_vec_3i.at(i).a);
- vec_3i_cont.push_back(new_vec_3i.at(i).b);
- vec_3i_cont.push_back(new_vec_3i.at(i).c);
- }
- }
- // *******************************************************************************
- int main()
- {
- vector<vector3f> fp_points;
- vector<vector3i> i_lines;
- int i;
- // read data from the text data file...
- // -------------------------------------------------------------------
- FILE *p_datafile;
- p_datafile = fopen("data.txt", "r");
- if(!p_datafile)
- {
- printf("No data file.");
- return -1;
- }
- if(read_text_data(fp_points, i_lines, &p_datafile) == true)
- {
- fclose(p_datafile);
- }
- else
- {
- fclose(p_datafile);
- printf("Error in the text source file\n");
- return -1;
- }
- // Print out the read data file as it resides in the vector structures...
- // -------------------------------------------------------------------
- printf("\nDATA AS READ FROM TEXT DATA FILE\n");
- for(i = 0; i < (int)fp_points.size(); i++)
- {
- 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);
- }
- for(i = 0; i < (int)i_lines.size(); i++)
- {
- 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);
- }
- // Save the data as a binary file...
- // -------------------------------------------------------------------
- FILE *p_guidefile;
- p_guidefile = fopen("bin_guide.txt", "w");
- p_datafile = fopen("bin_data.bin", "wb");
- if(!p_datafile)
- {
- printf("No binary data file created.");
- return -1;
- }
- if(write_bin_data(fp_points, i_lines, &p_datafile, &p_guidefile) == true)
- {
- fclose(p_datafile);
- fclose(p_guidefile);
- }
- else
- {
- fclose(p_datafile);
- fclose(p_guidefile);
- printf("Error writing to binary or guide file\n");
- return -1;
- }
- // Now load the binary file into a new, identical vector to test it...
- vector<vector3f> fp_new_points;
- vector<vector3i> i_new_lines;
- //fp_new_points.reserve(fp_points.size());
- //vector3f fp_temp_vec;
- p_guidefile = fopen("bin_guide.txt", "r");
- if(!p_guidefile)
- {
- printf("No data guide file opened");
- return -1;
- }
- p_datafile = fopen("bin_data.bin", "rb");
- if(!p_datafile)
- {
- printf("No binary data file opened");
- return -1;
- }
- if(read_bin_data(fp_new_points, i_new_lines, &p_datafile, &p_guidefile) == true)
- {
- fclose(p_datafile);
- fclose(p_guidefile);
- }
- else
- {
- fclose(p_datafile);
- fclose(p_guidefile);
- printf("Error reading binary or guide file\n");
- return -1;
- }
- // print the data as read from the binary file into the new vectors. It should be identical...
- // -------------------------------------------------------------------
- printf("\nDATA AS READ FROM BINARY DATA FILE\n");
- for(i = 0; i < (int)fp_new_points.size(); i++)
- {
- 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);
- }
- for(i = 0; i < (int)i_new_lines.size(); i++)
- {
- 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);
- }
- // FOR OPENGL...
- // Here the points and the lines are loaded into a contiguous, non structured vector
- // so that it can be used by glClientState, vertex array, etc...
- printf("\nDATA AS READ FROM BINARY DATA FILE TO CONTIGUOUS VECTOR\n");
- vector<float> fp_points_cont;
- vector<int> i_lines_cont;
- convert_to_vector_contiguous(fp_points_cont, i_lines_cont, fp_new_points, i_new_lines);
- for(i = 0; i < (int)fp_points_cont.size(); i += 3)
- {
- 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)));
- }
- for(i = 0; i < (int)i_lines_cont.size(); i += 3)
- {
- 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)));
- }
- return 0;
- }
- /*
- The data text file used by the program. Save as;
- data.txt
- ...and lop it in with the compiled executable.
- 8
- -2.5 -2.5 2.5
- 2.5 -2.5 2.5
- 2.5 2.5 2.5
- -2.5 2.5 2.5
- 2.5 -2.5 -2.5
- -2.5 -2.5 -2.5
- -2.5 2.5 -2.5
- 2.5 2.5 -2.5
- 12
- 0 1 3
- 1 2 3
- 1 4 2
- 4 7 2
- 4 5 7
- 5 6 7
- 5 0 6
- 0 3 6
- 3 2 6
- 2 7 6
- 1 0 4
- 0 5 4
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement