Hi,
I am trying to read the following file (The comments are not there in the original):
Tetra.tri
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 using dynamic arrays, because I will need to open other files and because I am using vertex arrays to draw to screen.
I get the first values right (vertcount and tricount which 4 and 4 according to file above) but I am doing something wrong after.
Here is the Code:
void main(){
struct Vertex // Vertex Structure
{
float x,y,z;
};
struct Triangle // Triangle Structure
{
struct Vertex vert1, vert2, vert3;
};
int vertcount=0; //total number of vertices
int tricount=0; // number of triangles to display
int v=0; //var to store index value of each vertex
int t=0; //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
long filesize;
char buffer;
struct Vertex vertArray[v];
struct Triangle triArray[t];
FILE * pmesh; // pointer to the mesh file to be opened
pmesh = fopen ("/Users/.../tetra.tri","r"); // Tries to access the file specified. TO BE CHANGED ----> Dialaog window with browse for file function
/****** Read file and store values ******/
fscanf(pmesh, " %i %i ", &vertcount, &tricount); //read from file and assign the first two values: number of Vertices and Triangles
vertArray[v] = malloc(vertcount*(sizeof(struct InstVertex))); // Array of vertexes - space allocated = total number of vertices * the size of each Vertex
triArray[t] = malloc(tricount*(sizeof(struct InstTriangle))); // Array of triangles
int i=0, j=0; // Temp variables for for loops
for (i=0; i<=vertcount; i++){
fscanf(pmesh, "%d %d %d", &InstVertex.x, &InstVertex.y, &InstVertex.z); //read file and store coordinates in
vertArray[v]=InstVertex;
v++;
}
int check=0;
for (j=0; j<=tricount; j++){
fscanf(pmesh, "%i %i %i %i", &check, &InstTriangle.vert1, &InstTriangle.vert2, &InstTriangle.vert3);
triArray[t]=InstTriangle;
t++;
}
fclose(pmesh);
/************************************/
glutMainLoop();
return 0;
}
Among the mistakes I am making there is also the way the array memory is being allocated, since I am not getting the right values.
I cannot find the error in the reading loops and in the array declaration. Also is this the correct way to store values in the arrays?
Thank you in advance, Valerio