Object* objectFromFile(FILE *file1, FILE *file2);
Object* createObject(char *fileName) {
FILE *file1 = fopen(fileName, "r");
FILE *file2 = fopen(fileName, "r");
if (file1 == NULL) {
printf("Failed opening the file , Exiting !\n ");
return NULL;
} else if (file2 == NULL) {
printf("Failed opening the file , Exiting !\n ");
return NULL;
}
return objectFromFile(file1, file2);
}
//creates vertex
Vertex* createVertex(char *line, Vertex *v) {
float x, y, z;
sscanf(line, "%*c %f %f %f", &x, &y, &z);
v->x = x;
v->y = y;
v->z = z;
return v;
}
int countVertexes(FILE *file) {
char *line = (char*) calloc(MAX_SIZE, sizeof(char));
int counter = 0;
while (fgets(line, MAX_SIZE, file)) {
if (line[0] == 'v' && line[1] == ' ')
counter++;
}
free(line);
return counter;
}
//creates face
Face* createFace(char *line, Face *face) {
char *delimeters = " ";
char *temp = NULL;
temp = (char*) calloc(1, sizeof(char));
temp = strtok(line, delimeters);
face->vertex = (int*) calloc(face->size, sizeof(int));
temp = strtok(NULL, delimeters);
while (temp != NULL && strcmp(temp, "\n") != 0) {
(face->vertex[face->size]) = atoi(temp);
face->size++;
temp = strtok(NULL, delimeters);
}
return face;
}
int countFaces(FILE *file) {
char *line = (char*) calloc(MAX_SIZE, sizeof(char));
int counter = 0;
while (fgets(line, MAX_SIZE, file)) {
if (line[0] == 'f' && line[1] == ' ')
counter++;
}
free(line);
return counter;
}
//creates object
Object* objectFromFile(FILE *file1, FILE *file2) {
Object *obj = (Object*) malloc(sizeof(Object));
if (obj == NULL) {
printf("Failed allocating memory for object");
return NULL;
}
obj->vertexes = (Vertex*) malloc(sizeof(Vertex));
if (obj->vertexes == NULL) {
printf("Failed allocating memory for vertex");
return NULL;
}
obj->faces = malloc(sizeof(Face));
if (obj->faces == NULL) {
printf("Failed allocating memory for face");
return NULL;
}
char *line = (char*) calloc(1, sizeof(char));
obj->numberOfVertexes = countVertexes(file1);
rewind(file1);
obj->numberOfFaces = countFaces(file2);
rewind(file2);
int i = 0;
obj->vertexes = (Vertex*) realloc(obj->vertexes,
(obj->numberOfVertexes + 1) * sizeof(Vertex));
while (fgets(line, MAX_SIZE, file1)) {
if (line[0] == 'v' && line[1] == ' ') {
if (obj->vertexes == NULL) {
printf("Failed allocating vertexes");
return NULL;
}
(obj->vertexes[i]) = *(createVertex(line, obj->vertexes));
i++;
}
}
int j = 0;
obj->faces = (Face*) realloc(obj->faces,
(obj->numberOfFaces + 1) * sizeof(Face));
while (fgets(line, MAX_SIZE, file2)) {
if (line[0] == 'f' && line[1] == ' ') {
if (obj->faces == NULL) {
printf("Failed allocating faces");
return NULL;
}
obj->faces[j] = *createFace(line, obj->faces);
j++;
}
}
free(line);
fclose(file1);
fclose(file2);
return obj;
}
char* currentFile(const char *fileName) {
char *temp = (char*) calloc(1, sizeof(char));
int counter = 0;
while (temp[counter] != '\0') {
temp[counter] = fileName[counter];
counter++;
}
return temp;
}
Scene* createScene(char *fileName, ...) {
va_list files;
va_start(files, fileName);
Scene *scene = (Scene*) calloc(1, sizeof(Scene));
if (scene == NULL) {
printf("Can't allocate memory , canceling");
return NULL;
}
FILE *file = fopen(fileName, "r");
if (file == NULL) {
printf("Cannot access file, canceling !\n");
return NULL;
}
char *fileRead = currentFile(fileName);
scene->head = (Lnode*) calloc(1, sizeof(Lnode));
Lnode *nodePtr = (*scene).head;
nodePtr = calloc(1, sizeof(scene->head));
while (fileRead != NULL) {
Object *object = createObject(fileName);
scene->numofobjects++;
nodePtr->next = object;
fileRead = va_arg(files, char*);
}
va_end(files);
scene->head = nodePtr;
return scene;
}
программа предполагает создание сцены, представляющей собой связанный список объектов, создание происходит после чтения информации из файла obj. каждый объект содержит вершины и грани. при попытке инициализировать объект я получаю сбой и не могу найти проблему. точка сбоя находится в методе createScene при вызове объекта create. работая на виртуальной машине Ubuntu с Eclipse, мой компилятор g cc.