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

File 在openGL中读取文件并显示顶点值,file,opengl,File,Opengl,我在网上到处搜索,发现了许多类似的东西,但从来没有像这样,我不知道我做错了什么 我正在尝试读取以下文件: 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指定为三角形) 302 3013 3021 我试图通过使用动态数组来实现这一点,因为我需

我在网上到处搜索,发现了许多类似的东西,但从来没有像这样,我不知道我做错了什么

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


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指定为三角形)

302

3013

3021


我试图通过使用动态数组来实现这一点,因为我需要打开其他文件

到目前为止,我得到的是:

    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);
struct Vertex//顶点结构
{
浮动x,y,z;
};
结构三角形//三角形结构
{
int vert1、vert2、vert3;
};
整数计数//顶点总数
国际三角计数;
INTV//var来存储每个顶点的索引值
int t//var来存储每个三角形的索引值
结构顶点InstVertex;//实例化定义为struct的顶点,使用3个浮点数存储坐标
结构三角形InstTriangle;//三角形结构的实例化
文件*pmesh;//指向要打开的网格文件的指针
pmesh=fopen(“/home/../tetra.tri”,“r”);//尝试访问指定的文件。要更改-->Dialaog窗口,具有文件浏览功能
长文件大小;
字符*缓冲区;
fseek(pmesh,0,SEEK_END);
filesize=ftell(pmesh);//在filesize中以字节为单位存储网格的大小值
倒带(pmesh);
buffer=(char*)malloc(sizeof filesize);
if(buffer==NULL){
fputs(“在缓冲区中加载文件时出错”,stderr);
出口(1);
}
否则{
buffer=(char*)pmesh;//在缓冲区中复制网格
fclose(pmesh);//释放内存
}
/*现在读取文件并存储值*/
fscanf(缓冲区、%i%i、&vertcount和tricount)//从文件中读取并指定前两个值:tot顶点数和三角形数
int*vertArray[v];
int*triArray[t];
vertArray[v]=malloc((sizeof(vertcount))*(sizeof(struct Vertex));//顶点数组-分配的空间=顶点总数*每个顶点的大小
triArray[t]=malloc((sizeof(tricount))*(sizeof(struct Triangle));//三角形阵列
int t1、t2、t3、t4;//存储读取行数值的临时变量

对于(v=0;v我注意到的第一件事是,在调用
malloc
sizeof(filesize)
时,您应该将
sizeof filesize
替换为
filesize
(这甚至可以编译吗?)

无法使用以下行将文件内容复制到缓冲区:

 buffer = (char*) pmesh; // copy mesh in buffer
 fclose(pmesh); // free memory
你需要把它换成新的

 fread(buffer, filesize, 1, pmesh);
 fclose(pmesh);
在此之后,您还需要将所有的
fscanf
(操作于
文件*
)替换为
sscanf
(操作于
char*
),您还需要将
fclose(缓冲区)
替换为
空闲(缓冲区)
和所有的
*scanf
都应该带有指向要设置的变量的指针

请允许我指出,“缓冲区”是完全不必要的,您可以跳过它并替换所有缓冲区

fscanf(buffer...)

这将给出正确的行为

/A.B


我注意到的第一件事是,在调用
malloc
sizeof(filesize)
时,您应该将
sizeof filesize
替换为
filesize
变量在内存中的大小,根据您的平台而定,它将是4或8个字节

无法使用以下行将文件内容复制到缓冲区:

 buffer = (char*) pmesh; // copy mesh in buffer
 fclose(pmesh); // free memory
你需要把它换成新的

 fread(buffer, filesize, 1, pmesh);
 fclose(pmesh);
在此之后,您还需要将所有的
fscanf
(操作于
文件*
)替换为
sscanf
(操作于
char*
),您还需要将
fclose(缓冲区)
替换为
空闲(缓冲区)
和所有的
*scanf
都应该带有指向要设置的变量的指针

请允许我指出,“缓冲区”是完全不必要的,您可以跳过它并替换所有缓冲区

fscanf(buffer...)

这将给出正确的行为

/A.B


您好,非常感谢您的帮助!是的,我已经放弃并直接访问了pmesh(正如您所建议的)结果明显更好,因为正确的值存储在vertcount和tricount中。然而,读取文件并将坐标存储到数组中是最复杂的部分。我想我将为此发布另一个问题。再次感谢。ValerioHi,非常感谢您的帮助!是的,我已经放弃并访问了pm直接esh(就像你建议的那样),结果明显更好,因为正确的值存储在vertcount和tricount中。然而,读取文件并将坐标存储到数组中是最复杂的部分。我想我将为此发布另一个问题。再次感谢。Valerio