0

I wanted to load custom models in my OpenGL engine, so I created a load function and when I try to parse the result, the program crashes and produces a bus error. I might've made a typo, so I wanted to see if anyone here could help me, because I've been working on this for a long time, but never seem to get it to work. This is the code, that loads the model and parses the vertices, texture coordinates and normals:

   float verticesmodel[500];
   float texturemodel[500];
   float normalmodel[500];
   unsigned int verticesindices[500];
   unsigned int textureindices[500];
   unsigned int normalindices[500];
   float vertex[500];
   float texturecoords[500];
   float normals[500];
   printf("This is before the load!\n");
   if(load_obj("model.obj", verticesmodel, texturemodel, normalmodel, verticesindices, textureindices, normalindices) != 0){
      printf("Something went wrong!\n");
      return 1;
   }else{
      printf("Model loaded successfully from file!\n");
   }
   printf("This is after the load and before the parse!\n");
   int i;
   for(i = 0; i < sizeof(verticesindices)/sizeof(unsigned int); i++){
      vertex[i] = verticesmodel[verticesindices[i] - 1];
      printf("First stage loop!\n");
   }
   for(i = 0; i < sizeof(textureindices)/sizeof(unsigned int); i++){
      texturecoords[i] = texturemodel[textureindices[i] - 1];
      printf("Second stage loop!\n");
   }
   for(i = 0; i < sizeof(normalindices)/sizeof(unsigned int); i++){
      normals[i] = normalmodel[normalindices[i] - 1];
      printf("Third stage loop!\n");
   }
   printf("This is after the parse!\n");

The function load_obj works as intended and I was able to print out the values of every single one of the variables. This is the function, if you suspect there is an issue in it:

int load_obj(const char* filename, float* vertices, float* texcoords, float* normals, int* indices, int* textureindices, int* normalsindices){
   FILE* file = fopen(filename, "r");
   char lineheader[128];
   int res;
   int i = 0;
   int f = 0;
   int d = 0;
   int g = 0;
   float verticesout[24];
   float texcoordsout[28];
   float normalsout[18];
   unsigned int vertexindex[3];
   unsigned int texindex[3];
   unsigned int normalindex[3];
   vec3 vertex;
   vec2 texcoord;
   vec3 normal;
   int vertexindices[36];
   int texindices[36];
   int normalindices[36];
   if(file == NULL){
      printf("Failed to open file!\n");
      return 1;
   }
   while(1){
      res = fscanf(file, "%s", lineheader);
      if(res == EOF){
         break;
      }
      if(strcmp(lineheader, "v") == 0){
         fscanf(file, "%f %f %f\n", &vertex[0], &vertex[1], &vertex[2]);
         verticesout[i] = vertex[0];
         verticesout[i + 1] = vertex[1];
         verticesout[i + 2] = vertex[2];
         i += 3;
      }else if(strcmp(lineheader, "vt") == 0){
         fscanf(file, "%f %f\n", &texcoord[0], &texcoord[1]);
         texcoordsout[f] = texcoord[0];
         texcoordsout[f + 1] = texcoord[1];
         f += 2;
      }else if(strcmp(lineheader, "vn") == 0){
         fscanf(file, "%f %f %f\n", &normal[0], &normal[1], &normal[2]);
         normalsout[d] = normal[0];
         normalsout[d + 1] = normal[1];
         normalsout[d + 2] = normal[2];
         d += 3;
      }else if(strcmp(lineheader, "f") == 0){
         fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexindex[0], &texindex[0], &normalindex[0], &vertexindex[1], &texindex[1], &normalindex[1], &vertexindex[2], &texindex[2], &normalindex[2]);
         vertexindices[g] = vertexindex[0];
         vertexindices[g + 1] = vertexindex[1];
         vertexindices[g + 2] = vertexindex[2];
         texindices[g] = texindex[0];
         texindices[g + 1] = texindex[1];
         texindices[g + 2] = texindex[2];
         normalindices[g] = normalindex[0];
         normalindices[g + 1] = normalindex[1];
         normalindices[g + 2] = normalindex[2];
         g += 3;
      }
}
   memcpy(vertices, verticesout, sizeof(verticesout));
   memcpy(texcoords, texcoordsout, sizeof(texcoordsout));
   memcpy(normals, normalsout, sizeof(normalsout));
   memcpy(indices, vertexindices, sizeof(vertexindices));
   memcpy(textureindices, texindices, sizeof(texindices));
   memcpy(normalsindices, normalindices, sizeof(normalindices));
   return 0;
}

What could be the cause of this?

RadoslavL
  • 56
  • 6

0 Answers0