Я искал всюду в Интернете и нашел много похожих вещей, но не совсем так, и я не уверен, что я делаю неправильно.Чтение значений отображаемых и отображаемых вершин в 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 в передней определяет, что представляет собой треугольник)
Я пытаюсь сделать это с помощью динамических массивов, поскольку мне нужно будет открывать другие файлы.
так, что я до сих пор:
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.
Спасибо заранее
Valerio
Привет и большое вам спасибо за помощь! Да, я сразу же отказался и получил доступ к pmesh (как и вы предлагали), и результаты были явно лучше, поскольку правильные значения сохраняются в vertcount и tricount. Однако чтение файла и сохранение координат в массивах - самая сложная часть. Думаю, я отправлю еще один вопрос. Еще раз спасибо. Valerio – Val