summaryrefslogtreecommitdiff
path: root/src/model.c
blob: 172c409ad483453c75595d5bc8d1830f3bb92738 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <daw/daw.h>
#include <daw/logging.h>
#include <daw/model.h>
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>

Model load_model(const Asset_ModelSpec *restrict ms) {
  ModelType format = Model_error;

  /*
  isize vertices_len = 0;
  vec3 *vertices = malloc(sizeof(vec3) * 512);
  usize *vertices_idx = malloc(sizeof(usize) * 512);

  isize uvs_len = 0;
  vec2 *uvs = malloc(sizeof(vec2) * 512);
  usize *uvs_idx = malloc(sizeof(usize) * 512);

  isize normals_len = 0;
  vec3 *normals = malloc(sizeof(vec3) * 512);
  usize *normals_idx = malloc(sizeof(usize) * 512);
  */

  // For now, just default to obj
  format = Model_obj;

  FILE* f = fopen(ms->path, "r");
  if (f == NULL) {
    ERROR("Failed to load file " TERM_COLOR_YELLOW "%s" TERM_COLOR_RESET, ms->path);
    return (Model){.format = format};
  }
  const usize filesz = f_get_sz(f);

  char* filecontets = calloc(filesz, sizeof(char));
  fread(filecontets, sizeof(char), filesz, f);

  // Start the import on the given file with some example postprocessing
  // Usually - if speed is not the most important aspect for you - you'll t
  // probably to request more postprocessing than we do in this example.
  const struct aiScene* scene = aiImportFile( filecontets,
    aiProcess_CalcTangentSpace       |
    aiProcess_Triangulate            |
    aiProcess_JoinIdenticalVertices  |
    aiProcess_SortByPType);

  free(filecontets);
  // If the import failed, report it
  if( NULL == scene) {
    ERROR("Failed to import %s: %s", ms->path, aiGetErrorString());
    return (Model){.format = format};
  }

  //// Now we can access the file's contents
  //DoTheSceneProcessing(scene);

  //// We're done. Release all resources associated with this import
  aiReleaseImport(scene);

  {
    Model m = {0};
    m.format = format;

    // TODO add index array

    return m;
  }
}