Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Floats From Text Then Using Them In Functions 1

Status
Not open for further replies.

DrNektar

Programmer
May 21, 2001
2
GR
Hi all, I' quite new in C programming and so here is my problem. Though i've been searching a lot(...) through the posts here I haven't found answer to my question.

I am writing a graphics program (using OpenGL) that must read vertexes from a file and do some stuff like computing the normals for them and after that it should be able to demonstrate the result.

My basic problem is that the text files with the coordinates are written in a way I cannot manage to retrieve. e.g.

triangle { <32.3494, -53.9491, -15.0150>, <30.6827, -53.9491, -16.6817>, <30.6827, -53.9491, -15.0150> }
smooth_triangle { <32.3494, -47.0260, -18.3483>, <1.000, 0.000, 0.000>, <32.3494, -40.1029, -20.0150>, <1.000, 0.000, 0.000>, <32.3494, -47.0260, -20.0150>, <1.000, 0.000, 0.000> }

For the lines with &quot;smooth_triangle&quot; I have to retrieve only the coordinates, while for the ones with &quot;triangle&quot; I have to retrieve and compute the normals (witch I don't think would be a problem).

To become more specific (1)I don't know how to distinguish the charachters from the floating point numbers. Should I read the lines as strings and then convert part as floats, should I skip charachters and read only the numbers...
(2)How should I store the data in arrays so tha I could be able to use the with functions like glVertex3f that gl libraries use to create triangles? You can see I'm bit of confused. Is there something anyone knows that could help...a bit?

Thanks in advance, and I hope I was specific enough. My C compiler is Microsoft Visual C++ 6.0.

 
Hey though the problem seems a bit tough. Lets try something like this. must have heard of strtol() function which converts string to float, if the string carries a float. now the problem is how to capture the float in an array. well C if this can work. Read one character at a time, now check when '<' is encountered following that will be three co-ordinates. so read them one by one in a character array, till u reach comma(,) this will be one co-ordinate now read two more such co-ordinates into array and remember the third co-ordinate will be terminated by '>'. So go ahead, reading the next set of character array. Once all the arrays have been read. Apply strtol() and u have required no. in ur desired variable.

Hope I didn't confuse u, If so pls cool down and read it again its very simple.

Regards,
SwapSawe.
 
Since you can not change format of this file (somebody else has created it) do like SwapSpace recomended.

But in general, this is kind of stupid to save geometry data in format like this.
1) it will take too much space (for some model with a lot of triangles)
Always better put XYZ points in one array
and set triangles as 3 indecies (integer) of points from this array. Triangles will use the same XYZ points (verticies) and number of Xyz points will be much less.
2) Slow read and write (use binary file, with some effort you can even make it platform independent)
3) In format %f.4 you are loosing values of float (again binary file will solve it or you have to make bit by bit coding and it will take twice space on disk then a real data). If model set in small scale for example XYZ values less then 1.e-4, your model will be a dot.

So I think this is not a real format for geometry data, it will not work in real life. Since triangle in this file presented as a geomtry not a topology, you better create vertex for each Xyz of triangle. It is not worth of optimization.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top