Splitting Strings.

Hey im trying to read a .obj file in openGL and I have no idea how to take the / out of the the faces.

here is the code im using
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>


using namespace std;

struct point3D
{
	float x;
	float y;
	float z;
};

point3D v[529] = {};
point3D vn[529] = {};
point3D vt[529] = {};
point3D f[529][3] = {};


void display() 
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}



int main(int argc, char* argv[]) 
{
	int numVert = 0;
	int numNormals= 0;
	int numcoords = 0;
	int numFaces = 0;
	string test;
	ifstream inputFile;
	inputFile.open("teapot.obj");
	while(!inputFile.eof())
	{
		inputFile >> test;


		if (test == "v")
		{
			inputFile >> v[numVert].x;
			inputFile >> v[numVert].y;
			inputFile >> v[numVert].z;
			numVert++;
		}
		else if(test == "vn")
		{

			inputFile >> vn[numNormals].x;
			inputFile >> vn[numNormals].y;
			inputFile >> vn[numNormals].z;
			numNormals++;
		}
		else if(test == "vt")
		{
			inputFile >> vt[numcoords].x;
			inputFile >> vt[numcoords].y;
			inputFile >> vt[numcoords].z;
			numcoords++;
		}
		else if(test == "f")
		{
			string temp;






			
			for(int count = 0; count < 3; count++)
			{
				inputFile >> f[numFaces][count].x;
				inputFile >> f[numFaces].y;
				inputFile >> f[numFaces].z;
			}
			
			numFaces++;
		}


	}
	
	glutInit(&argc, argv);
	glutCreateWindow("rendering a teapot");
	glutDisplayFunc(display);
	glutMainLoop();
}


I have no idea how to how to split the numbers and put them into the 2d array called f. any help is appreciated.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < 529; i++)
{
  f[i][0].x = v[i].x;
  f[i][0].y = v[i].y;
  f[i][0].z = v[i].y;

  f[i][1].x = vn[i].x;
  f[i][1].y = vn[i].y;
  f[i][1].z = vn[i].y;

  f[i][2].x = vt[i].x;
  f[i][2].y = vt[i].y;
  f[i][2].z = vt[i].y;
}

I think.

Actually, maybe I made it too simple. You have different amounts of verts/normals/textures?

If that is the case then you should also have a set (or two or three) or integers. These integers correspond to the idx array that you just made.

For example, consider a cube:
1
2
3
4
5
6
7
8
v 0,0,0
v 0,0,1
v 0,1,0
v 0,1,1
v 1,0,0
v 1,0,1
v 1,1,0
v 1,1,0


(The order of the vertices is not important for this post.)

But OpenGL can only draw a face of this cube at a time, it needs instructions for what idx to draw: Something that would look like this:
0 1 2 1 2 3 2 3 4 4 5 6 5 6 7 6 7 0 7 0 1

Note that the length of that list is longer than the vertices list. A normal's list can have a different amount of values (a cube it should have 6). However, it's instruction list should be the same length as the vertices instruction list.

So basically. You read the vertices/normals/textures each into there own array (vectors are much easier here). Then read the instructions and fill up the final array. From my example above:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<Point3D> verts;
while (/* the file is reading verts */)
{
  Point3d temp;
  file >> temp.x >> temp.y >> temp.z;
  verts.push_back(temp);
}

std::vector<Point3D> results;
while (/*file is reading instructions*/)
{
  results.push_back( temp[instructions] );
}


Also to note that if you have a vector of floats, a float pointer can by simulated via &results[0]
Last edited on
thats helpful as well but. when the file is read which is a .obj has multilple floats inside. lines beggining with v are vectors which are easy to input but the last set of floats is 3 sets of 3 such as 1/2/3 4/5/6 7/8/9 which I need the / removed before they can be stored into the array.

from your edit I can see this working im in the middle of a lecture atm but as soon as I get back ill give it a try and comment later.
Last edited on
ok where you put commented the file is reading verts I cannot remember or find the argument to do while (/& the file is reading verts/*) im kinda a noob and this is my first time dabbling within c++/opengl i know the basics from doing java but this stumps me.
Hello Guys my name is Mark.According to that topic i think that the OpenGL OBJ Loading machine is an 'OBJ' capable loader & audience.It is published in C++ and uses OpenGL & SDL.The designs second version flows regular, structure, and mtl information.The OBJ information structure is known by most contemporary 3D modelers.Thanks!!


Last edited on
Topic archived. No new replies allowed.