loading verts from ascii files into arrays (milkshape)

Started by
1 comment, last by outRider 22 years, 10 months ago
I'm having some problems with this. Before, for each vertex I had a struct as such type vertex { float x, y, z, u, v; } type normal { float x, y, z; } type face { short i1, i2, i3; } and for each vertex and normal in the file I'd allocate sizeof(normal) * n_normals and likewise for the verts, and it worked just fine, but I decided to use vertex arrays and at someones suggestion I did this float *vertex; float *normal; float *texcoords short *face Allocated each as vertex = sizeof(vertex) * n_vertex * 3, likewise for normals, and * 2 for the texcoords. Milkshapes ascii files are structured like this: vertices: 2 1 0.3547, 3.3475, -1.4623, 1.346, 0.3467 -1 Where the 5 floats are x, y, z, u, v and the normals are normal: 5 0.57433, 0.45873, 0.2366 the faces are face: 15 0 1 2 0 1 2 where the first 3 are the indices to the verts, the second 3 are the indices to the normals, but theyre always the same as the verts so I ignore them. Here are the loops that read from the file

		for (ii = 0; ii < Model->Mesh.VertexCount; ii++)
		{
			fgets(LineBuffer, 256, File);
			sscanf(LineBuffer, "%hd %f %f %f %f %f %hd", &Temp, &Model->Mesh.Vertex[ii * 3], &Model->Mesh.Vertex[ii * 3 + 1], &Model->Mesh.Vertex[ii * 3 + 2], &Model->Mesh.TexCoord[ii * 2], &Model->Mesh.TexCoord[ii * 2 + 1], &Temp);
		}

		for (ii = 0; ii < Model->Mesh.NormalCount; ii++)
		{
			fgets(LineBuffer, 256, File);
			sscanf(LineBuffer, "%f %f %f", &Model->Mesh.Normal[ii * 3], &Model->Mesh.Normal[ii * 3 + 1], &Model->Mesh.Normal[ii * 3 + 2]);
		}

		for (ii = 0; ii < Model->Mesh.FaceCount; ii++)
		{
			fgets(LineBuffer, 256, File);
			sscanf(LineBuffer, "%hd %hd %hd %hd %hd", &Temp, &Model->Mesh.Face[ii * 3], &Model->Mesh.Face[ii * 3 + 1], &Model->Mesh.Face[ii * 3 + 2], &Temp);
		}
 </pre> 

If anyone can spot the problem I'd very much appreciate it, I can't seem to get it working.   </i>    

————
- outRider -

Edited by - outRider on June 8, 2001 12:10:26 AM    
Advertisement
quote:
Allocated each as vertex = sizeof(vertex) * n_vertex * 3, likewise for normals, and * 2 for the texcoords


That should be "sizeof(float)" if each vertex-component is represented as a float (sizeof(vertex) would equal sizeof(float)*3 since your ''vertex''-structs are declared as containing 3 floats (x, y, z)).

For the tex-coords it should be "sizeof(short) * n_texcoords * 2".

If the file containing the vertex data (etc.) contains commas between the elements shouldn''t the sscanf call also have them?

ex.

to read the line (your example line for normals):
0.57433, 0.45873, 0.2366

sscanf should be called as:
  sscanf(LineBuffer, "%f, %f, %f", &Model->Mesh[i].Normal[ii * 3], &Model->Mesh[i].Normal[ii * 3 + 1], &Model->Mesh[i].Normal[ii * 3 + 2]);  


Not sure about this though, I seldom use scanf myself...
Sorry, those are both typos in the post, that's not the case in my actual source, I was just lazy.

There are no commas in the text file, and there is no type vertex anymore (when I did use the vertex struct everything worked fine, but then it wouldnt be a usable array for glVertexPointer()) and sizeof() is passed float.

I can post the drawing code if it would help, maybe theres a prob there.

------------
- outRider -

Edited by - outRider on June 9, 2001 1:27:32 AM

This topic is closed to new replies.

Advertisement