Lectura de valores del flujo de archivos y almacenamiento en una matriz de tamaño variable: OpenGL y C

StackOverflow https://stackoverflow.com/questions/1812975

  •  06-07-2019
  •  | 
  •  

Pregunta

Estoy tratando de leer el siguiente archivo (Los comentarios no están en el original):

Tetra.tri


4 4 // número total de vértices & amp; total de triángulos

0.693361 0.693361 0.693361 // coordenadas de vértice

0.693361 -0.693361 -0.693361

-0.693361 -0.693361 0.693361

-0.693361 0.693361 -0.693361

3 1 2 3 // triángulos para mostrar (el 3 en el frente especifica que es un triángulo)

3 0 3 2

3 0 1 3

3 0 2 1


Estoy tratando de hacer esto usando matrices dinámicas, porque necesitaré abrir otros archivos y porque estoy usando matrices de vértices para dibujar en la pantalla.

Obtengo los primeros valores correctos (vertcount y tricount que 4 y 4 según el archivo anterior) pero estoy haciendo algo mal después.

Aquí está el Código:


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

/ ********** Leer archivos y almacenar valores ********** /

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;

}


Entre los errores que estoy cometiendo también está la forma en que se asigna la memoria de matriz, ya que no obtengo los valores correctos.

No puedo encontrar el error en los bucles de lectura y en la declaración de la matriz. ¿También es esta la forma correcta de almacenar valores en las matrices?

Gracias de antemano, Valerio

¿Fue útil?

Solución

He resuelto el problema de leer y asignar valores a las matrices cambiando los valores para leer en el primer bucle for de% d a flotantes (% f) que me dieron los valores correctos.

Sin embargo, a pesar de cambiar la llamada de malloc a:

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

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

donde v y t ya tienen valores (4 y 4 en este caso) ya que he editado el código anterior para incluir también dos llamadas:

v=vertcount;
t=tricount;

Pero sigo teniendo el siguiente error:

Error: se requiere conversión a tipo no escalar

¿Alguna idea de por qué?

¡Gracias!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top