读取文件并在openGL中显示顶点值

时间:2009-11-25 18:51:53

标签: file opengl

我在网上到处搜索,发现很多类似的东西,但从来都不喜欢这样,我不确定我做错了什么。

我正在尝试阅读以下文件:


4 4 //顶点数& tot三角形

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


我正在尝试使用动态数组,因为我需要打开其他文件。

所以我到目前为止的是:

    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中的顶点数组显示网格。

提前谢谢

瓦莱里奥

1 个答案:

答案 0 :(得分:1)

我注意到的第一件事是你应该在sizeof filesize的调用中仅用filesize替换malloc(这甚至编译?)。 sizeof(filesize)是变量filesize的内存大小,取决于您的平台,将为4或8个字节。

您无法使用以下行复制文件内容:

 buffer = (char*) pmesh; // copy mesh in buffer
 fclose(pmesh); // free memory

您需要将其替换为

 fread(buffer, filesize, 1, pmesh);
 fclose(pmesh);

在此之后,您还需要将所有fscanf(对FILE*进行操作)替换为sscanf(对char*进行操作),您还需要需要将fclose(buffer)替换为free(buffer),并且所有*scanf都应该指向您要设置的变量。

我可以指出'缓冲区'是完全没必要的,你可以跳过它并替换所有

fscanf(buffer...)

fscanf(pmesh...)

会给出正确的行为。

/A.B。

enter code here
相关问题