arrayscstringwavefront

Trying to read the lines starting with "f" (indices) in a Wavefront obj file causes the other output to be broken


When I try to read the other lines in the file, commenting out the lines, that check for the "f" lines, everything works as expected, but as soon as I uncomment them, every single value is getting altered except the texture coordinate values. I have no idea what is causing this and have tried to find the issue, but I haven't used any variables, that persist through all the line checks. What could be causing this? Here is the code in obj.h:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<cglm/cglm.h>

int loadobj(const char* filename, float* vertices, float* texcoords, float* normals, int* indices){
   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], texindex[3], normalindex[3];
    vec3 vertex;
    vec2 texcoord;
    vec3 normal;
   vec3 vertexindices;
    vec3 texindices;
    vec3 normalindices;
    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));
    return 0;
}

This is the main.c file:

#include<stdio.h>
#include<stdlib.h>
#include<cglm/cglm.h>
#include"obj.h"

int main(){
   float vertices[24];
    float texcoords[24];
    float normals[24];
    int indices[36];
   if(loadobj("model.obj", vertices, texcoords, normals, indices) != 0){
      printf("Errors encountered!\n");
    }
    int i;
    int f;
    int d;
    int g;
    for(i = 0; i < sizeof(vertices)/sizeof(float); i++){
       if(i % 3 == 0 && i != 0){
         printf("\n");
        }
        if(vertices[i] >= 0){
         printf(" ");
      }
      printf("%f ", vertices[i]);
    }
    printf("\n\n");
    for(f = 0; f < sizeof(texcoords)/sizeof(float); f++){
      if(f % 2 == 0 && f != 0){
         printf("\n");
        }
        printf("%f ", texcoords[f]);
    }
    printf("\n\n");
    for(d = 0; d < sizeof(normals)/sizeof(float); d++){
      if(d % 3 == 0 && d != 0){
         printf("\n");
        }
        if(normals[d] >= 0){
         printf(" ");
        }
        printf("%f ", normals[d]);
    }
    printf("\n\n");
    for(g = 0; g < sizeof(indices)/sizeof(int); g++){
      if(g % 3 == 0 && g != 0){
         printf("\n");
        }
        printf("%d ", indices[g]);
    }
    printf("\n");
}

Solution

  • Solved! As it turns out I forgot to set the type for the output as an array of int and set it as vec3.