Hi,
I'm making a sorta of 3d model builder with OpenGL and any 3d model program (such as Maya or 3ds max) but I'm having some trouble figuring how I'd get the information passing done.
Currently, I'm saving a txt file:
Example.TXT
1 2
|
#modelName
0,10,0 -10,0,0 10,0,0
|
These are 3 vertex points so I can build a basic triangle with OpenGL.
The OpenGL command to get a vertex onscreen is:
|
glVertex(int x, int y, int z);
|
So I'm trying to get a line parser for the Example.TXT that would read, find the line with the vertex info, and save them somewhere as ints.
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
|
//after opening the file and reading the #modelName line
while(currentLine[currentChar] != '\n') //while it's not the end of the line
{
if(currentLine[currentChar] == '\0')
{
break; //get out of the loop if it's the end of the string
}
if(currentLine[currentChar] == ' ')
{
//it's a blank space, so new set of 3 coords / end of the current set
vertexInfo[vertexNum][vertexCharPosition] = '\0'; //end of this string
vertexCharPosition = 0; //reseting char pos
vertexNum++; //next element
//here I'm displaying the vertex coords saved
for(int i = 0; i < 3; i++) //for X, Y and Z
{
cout << "vertex" << i << ": ";
for(int j = 0; j < strlen(vertexInfo[i]); j++)
{
if(vertexInfo[i][j] != '\0')
{
//if it's not the end of the string
cout << vertexInfo[i][j];
}
else
{
break;
}
}
cout << endl;
}
}
else if(currentLine[currentChar] == ',')
{
//it's a comma, so it's just another new vertex (X, Y, Z)
vertexInfo[vertexNum][vertexCharPosition] = '\0'; //end of this string
vertexCharPosition = 0; //reseting char pos
vertexNum++; //next element
}
else
{
//if it's nothing of the above, it's a vertex number that I need to save
vertexInfo[vertexNum][vertexCharPosition] = currentLine[currentChar];
vertexCharPosition++;
}
currentChar++;
}
|
Edit for those who don't want to read the code: After this code, I have a char[][] "vertexInfo" with the values saved.
But... now, how can I convert the numbers from the "vertexInfo[][]" into ints so I can pass them to the OpenGL function?
I was thinking in converting them, but after I wrote this I noticed I wasn't expecting double digits or even the minus signal.
How can I get this working?
If you have any comment on how I could improve this or even if there's another more efficient method, I highly appreciate it.