Question

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

Was it helpful?

Solution

I have solved the problem of reading and assigning values to the arrays by changing the values to read in the first for loop from %d to floats (%f) which gave me the right values.

However despite changing the malloc call to:

vertArray[v] = (struct Vertex) malloc(vertcount*(sizeof(struct Vertex)));

triArray[t] = (struct Triangle) malloc(tricount*(sizeof(struct Triangle)));

where v and t have already values (4 and 4 in this case) since I have edited the code above to also include two calls:

v=vertcount;
t=tricount;

But I still get the folllowing error:

Error: conversion to non-scalar type requested!

Any ideas why?

Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top