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