次のファイルを読み込もうとしています (コメントは元のファイルにはありません)。
テトラトリ
4 4 //頂点の総数と三角形の総数
0.693361 0.693361 0.693361 //頂点座標
0.693361 -0.693361 -0.693361
-0.693361 -0.693361 0.693361
-0.693361 0.693361 -0.693361
3 1 2 3 // 表示する三角形 (前の 3 は三角形であることを示します)
3 0 3 2
3 0 1 3
3 0 2 1
他のファイルを開く必要があり、画面に描画するために頂点配列を使用しているため、動的配列を使用してこれを実行しようとしています。
私は最初の値を正しく取得します (上記のファイルによると vertcount と tricount which 4 と 4) が、後で何か間違ったことをしています。
コードは次のとおりです。
ボイドメイン(){
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
/************ ファイルを読み取り、値を格納 *********/
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;
}
私が犯している間違いの中には、正しい値を取得していないため、配列メモリが割り当てられている方法もあります。
読み取りループと配列宣言にエラーが見つかりません。また、これは配列に値を格納する正しい方法ですか?
前もってありがとう、ヴァレリオ