tags:

views:

402

answers:

1

Hi,

I have searched everywhere on the web and found many similar things but never quite like this and I am not sure what I am doing wrong.

I am trying to read the following file:


4 4 // tot number of vertexes & tot number of triangles

0.693361 0.693361 0.693361 // vertex coordinates

0.693361 -0.693361 -0.693361

-0.693361 -0.693361 0.693361

-0.693361 0.693361 -0.693361

3 1 2 3 // triangles to display (the 3 in front specifies that is a triangle)

3 0 3 2

3 0 1 3

3 0 2 1


I am trying to do this by using dynamic arrays, because I will need to open other files.

so what I have so far is:

 struct Vertex   // Vertex Structure
 {
  float x,y,z;
 };

 struct Triangle   // Triangle Structure
 {
  int vert1, vert2, vert3;
 };

 int vertcount; //total number of vertices
 int tricount;

 int v;        //var to store index value of each vertex
 int t;        //var to store index value of each triangle


 struct Vertex InstVertex;   // Instantiation of Vertex defined as struct with 3 floats to store coordinates
 struct Triangle InstTriangle;  // Instantiation of the Triangle STRUCT

 FILE * pmesh;      // pointer to the mesh file to be opened
 pmesh = fopen ("/home/.../tetra.tri","r");    // Tries to access the file specified. TO BE CHANGED ----> Dialaog window with browse for file function
 long filesize;
 char *buffer;

 fseek (pmesh , 0 , SEEK_END);
 filesize = ftell (pmesh);   // stores the size value of the mesh in bytes in filesize
 rewind (pmesh);

 buffer = (char*) malloc (sizeof filesize);
 if (buffer == NULL) {
  fputs ("Error loading file in buffer",stderr);
  exit (1);
 }

 else {
  buffer = (char*) pmesh;    // copy mesh in buffer
  fclose(pmesh);      // free memory
 }

/* Now read file and store values */
 fscanf(buffer, " %i %i ", &vertcount, &tricount);  //read from file and assign the first two values: tot number of Vertices and Triangles

 int *vertArray[v];
 int *triArray[t];

 vertArray[v] = malloc ((sizeof(vertcount)) * (sizeof(struct Vertex)));   // Array of vertexes - space allocated = total number of vertices * the size of each Vertex
 triArray[t] = malloc ((sizeof(tricount)) * (sizeof(struct Triangle)));  // Array of triangles

 int t1, t2, t3, t4;  // Temp variables where to store the vales of the line read

 for (v=0; v<=vertcount; v++){
  (fscanf(buffer, "%i %i %i %i ", t1, t2, t3, t4));

  if (t4==NULL){
   fscanf(buffer, "%d %d %d", InstVertex.x, InstVertex.y, InstVertex.z);  //read file and store coordinates in
  }

  else if (t1==3 && t4!=NULL){
   InstTriangle.vert1=t2;
   InstTriangle.vert2=t3;
   InstTriangle.vert3=t4;
  }

 }

 fclose(buffer);


When I get to reading the file, the right values are never stored into vertcount and tricount however, so the code from there on is still at its first stage.

The reason for this is to read the coordinates and display a mesh using vertex arrays in openGL.

Thank you in advance

Valerio

+1  A: 

First thing I notice is that you should replace sizeof filesize (does this even compile?) with just filesize in the call to malloc. sizeof(filesize) is the size in memory of the variable filesize and will be 4 or 8 bytes depending on your platform.

You can't copy the content of the file to the buffer with the lines:

 buffer = (char*) pmesh; // copy mesh in buffer
 fclose(pmesh); // free memory

You'll need to replace it with

 fread(buffer, filesize, 1, pmesh);
 fclose(pmesh);

After this you'll also need to replace all your fscanf (which operates on FILE*) to sscanf (which operates on char*), you'll also need to replace fclose(buffer) with free(buffer) and all the *scanf should take pointers to the variables you want to set.

May I point out that the 'buffer' is completely unnecessary, you can just skip it and replace all

fscanf(buffer...)

with

fscanf(pmesh...)

which would give the correct behavior.

/A.B.

enter code here
Andreas Brinck
Hi and thank you very much for your help!yes, I had given up and accessed pmesh directly (just like you suggested) and the results were obviously better as the right values get stored in vertcount and tricount.However reading the file and storing the coordinates into the the arrays is the most complicated part.I guess I will post another question for that.Thanks again.Valerio
Val