summaryrefslogtreecommitdiff
path: root/src/model.c
blob: 48f66539086e39df190384004d29ec310973793a (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "../include/model.h"

static honey_mesh assimp_to_honey_mesh(struct aiMesh* mesh, struct aiScene* scene) {
  unsigned int vertex_step = 6;
  bool mesh_has_uvs = false;
  unsigned int n_vertices = mesh->mNumVertices;
  
  if (mesh->mTextureCoords[0]) {
    mesh_has_uvs = true;
    vertex_step = 8;
  }

  float* vertices = malloc(sizeof(float) * vertex_step * n_vertices);
  for (int i=0; i<n_vertices; i++) {
    int j = i*vertex_step;
    /* positions */
    vertices[j+0] = mesh->mVertices[i].x;
    vertices[j+1] = mesh->mVertices[i].y;
    vertices[j+2] = mesh->mVertices[i].z;

    /* normals */
    vertices[j+3] = mesh->mNormals[i].x;
    vertices[j+4] = mesh->mNormals[i].y;
    vertices[j+5] = mesh->mNormals[i].z;

    /* uvs? */
    if (mesh_has_uvs) {
      vertices[j+6] = mesh->mTextureCoords[0][i].x;
      vertices[j+7] = mesh->mTextureCoords[0][i].y;
    }
  }

  unsigned int n_indices = mesh->mNumFaces*3;
  unsigned int* indices = malloc(sizeof(unsigned int) * n_indices);
  for (int i=0; i<mesh->mNumFaces; i++) {
    int j = 3*i;
    struct aiFace face = mesh->mFaces[i];
    indices[j+0] = face.mIndices[0];
    indices[j+1] = face.mIndices[1];
    indices[j+2] = face.mIndices[2];
  }

  honey_mesh result;

  if (mesh_has_uvs) {
    unsigned int n_attributes = 3;
    unsigned int attribute_sizes[] = { 3, 3, 2 };
    honey_mesh_new(&result,
                   vertices, n_vertices,
                   n_attributes, attribute_sizes,
                   indices, n_indices);
  }
  else {
    unsigned int n_attributes = 2;
    unsigned int attribute_sizes[] = { 3, 3 };
    honey_mesh_new(&result,
                   vertices, n_vertices,
                   n_attributes, attribute_sizes,
                   indices, n_indices);
  }

  free(vertices);
  free(indices);

  return result;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

static void process_assimp_node(honey_model* model, struct aiNode* node, struct aiScene* scene) {
  for (int i=0; i<node->mNumMeshes; i++) {
    struct aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
    model->meshes[model->n_meshes] = assimp_to_honey_mesh(mesh, scene);
    model->n_meshes++;
  }

  for (int i=0; i<node->mNumChildren; i++) {
    process_assimp_node(model, node->mChildren[i], scene);
  }
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  
honey_result honey_model_load(honey_model* model,
                             char* path) {
  model->n_meshes = 0;
  
  struct aiScene* scene = aiImportFile(path,
                                             aiProcess_Triangulate |
                                             aiProcess_FlipUVs);
  if (scene == NULL) {
    honey_error_set_string1(path);
    return HONEY_FILE_READ_ERROR;
  }

  if (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE ||
      scene->mRootNode == NULL) {
    honey_error_set_string1(path);
    return HONEY_MODEL_LOAD_ERROR;
  }
  
  process_assimp_node(model, scene->mRootNode, scene);

  return HONEY_OK;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

void honey_model_draw(honey_model* model, honey_shader shader) {
  for (int i=0; i<model->n_meshes; i++) {
    honey_mesh_draw(model->meshes[i], shader);
  }
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

void honey_model_delete(honey_model* model) {
  for (int i=0; i<model->n_meshes; i++) {
    honey_mesh_delete(model->meshes[i]);
  }
}