Hi there,
I have been trying to load certain file formats but have not had luck due to my lack of knowledge in file i/o. The 2 file formats are .obj and .ply.
I will show the file types and then explain how I'm going to load it and I hope you guys can point me in the right direction.
.ply:
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
|
ply
format ascii 1.0
comment Created by Blender3D 249 - www.blender.org, source file:
element vertex 24
property float x
property float y
property float z
element face 6
property list uchar uint vertex_indices
end_header
1.000000 1.000000 -1.000000
1.000000 -1.000000 -1.000000
-1.000000 -1.000000 -1.000000
-1.000000 1.000000 -1.000000
1.000000 0.999999 1.000000
-1.000000 1.000000 1.000000
-1.000000 -1.000000 1.000000
0.999999 -1.000001 1.000000
1.000000 1.000000 -1.000000
1.000000 0.999999 1.000000
0.999999 -1.000001 1.000000
1.000000 -1.000000 -1.000000
1.000000 -1.000000 -1.000000
0.999999 -1.000001 1.000000
-1.000000 -1.000000 1.000000
-1.000000 -1.000000 -1.000000
-1.000000 -1.000000 -1.000000
-1.000000 -1.000000 1.000000
-1.000000 1.000000 1.000000
-1.000000 1.000000 -1.000000
1.000000 0.999999 1.000000
1.000000 1.000000 -1.000000
-1.000000 1.000000 -1.000000
-1.000000 1.000000 1.000000
4 0 1 2 3
4 4 5 6 7
4 8 9 10 11
4 12 13 14 15
4 16 17 18 19
4 20 21 22 23
|
for the .ply file what I want to do is read the entire file, then use a function to search for 'element vertex' and then extract the number beside it(24 in this case). Then I want to do the same but for 'element face'. after this since I know there are 24 vertices I will find the 'end_header' and read 24 lines after it(those coordinates). And then I want to use a for loop and seperate each coordinate and use .push_back() after converting it to put it in my vector.
for example for line 11:
|
1.000000 1.000000 -1.000000
|
i'll use a for loop to pushback 1.000000 first then the next one (1.00000) and the last one(1.00000) and it'll do that for all the lines till the counter hits 24 or the number that has the # of vertices stored in it. After I do that it'll read the next 6 lines of the variable set for the # faces. Here I want to omit the 4 at the start of each line and then use the same for loop i used earlier to .push_back each face in a vector (0) then (1) then (2) then (3) etc..
and that's all I need for this one so to summarize how do I find a certain string in a file and then read the characters beside it till we hit a space.
Then after find another string get the index # of that line and then we will use that number and read lines using index's (for example reach everything on line 14). Finally make a for loop which will split up each element in that string. I know how to do this sort of use a function to read each character till it hit's a space and repeat that twice for each line.
Anyways sorry for this bulky post I have been trying to do this but i can't figure it out in c++. I know how to do this in python but that won't help....
here's my code if you need it it's not much.
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
|
#include <iostream>
#include <fstream>
#include <string>
#include <glew.h>
#include <vector>
using namespace std;
using std::vector;
int main()
{
string line;
string dummy;
vector<GLfloat>vertex;
char *content = "";
int line_num = 0;
int vertices_num = 0;
int faces_num = 0;
ifstream obj;
obj.open("cube.ply",ios::in || ios::binary);
while(!obj.eof())
{
getline(obj,dummy);
line_num = line_num +1;
}
obj.close();
cout << dummy<<endl;
//double temp = atof(num.c_str());
system("pause");
return 0;
}
|