我在網上到處搜索,發現很多類似的東西,但從來沒有像這樣,我不知道我做錯了什麼。讀取文件並在OpenGL中顯示頂點值
我想閱讀下列文件:頂點
4 4 // TOT數三角形
0.693361 0.693361 0.693361 //頂點& TOT數座標
0.693361 - 0.693361 -0.693361
-0.693361 -0.693361 0.693361
-0.693361 0.693361 -0.693361
3 1 2 3個//三角形顯示(3前面指定爲三角形)
我試圖通過使用動態數組做到這一點,因爲我需要打開其他文件。
所以我至今是:
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);
當我到讀取文件,正確的價值觀永遠不會保存到vertcount和tricount然而,這樣從那裏的代碼仍然是在第一階段。
其原因是讀取座標並在openGL中使用頂點數組顯示網格。
預先感謝您,我注意到
瓦萊里奧
嗨,非常感謝您的幫助! 是的,我已經放棄並直接訪問pmesh(就像你建議的那樣),結果顯然更好,因爲正確的值存儲在vertcount和tricount中。 但是,讀取文件並將座標存儲到數組中是最複雜的部分。 我想我會爲此發佈另一個問題。 再次感謝。 Valerio – Val 2009-11-26 20:21:48