130 lines
4.0 KiB
C
130 lines
4.0 KiB
C
|
#include "include/object.h"
|
||
|
|
||
|
#include "include/math.h"
|
||
|
#include <math.h>
|
||
|
#include <assimp/cimport.h>
|
||
|
#include <assimp/scene.h>
|
||
|
#include <assimp/postprocess.h>
|
||
|
|
||
|
struct model *load_model(const char *path) {
|
||
|
struct model *new_model = (struct model *) calloc(1, sizeof(struct model));
|
||
|
const struct aiScene *scene = aiImportFile(path, aiProcess_Triangulate);
|
||
|
|
||
|
if (scene == NULL) {
|
||
|
fprintf(stderr, "Error: failed importing file from path '%s'", path);
|
||
|
}
|
||
|
|
||
|
for (int mesh_index = 0; mesh_index < scene->mNumMeshes; mesh_index++) {
|
||
|
struct aiMesh *mesh = scene->mMeshes[mesh_index];
|
||
|
|
||
|
// fetch vertices
|
||
|
for (int vertex_index = 0; vertex_index < mesh->mNumVertices; vertex_index++) {
|
||
|
struct aiVector3D *vertex = &(mesh->mVertices[vertex_index]);
|
||
|
long start = new_model->vertices_num*3;
|
||
|
|
||
|
new_model->vertices_num++;
|
||
|
new_model->vertices = (float *) realloc(new_model->vertices, new_model->vertices_num*3*sizeof(float));
|
||
|
if (new_model->vertices == NULL) {
|
||
|
fprintf(stderr, "Error: failed allocating memory for vertices\n");
|
||
|
goto error;
|
||
|
}
|
||
|
|
||
|
memcpy(&new_model->vertices[start], vertex, sizeof(float)*3);
|
||
|
}
|
||
|
|
||
|
// fetch indices
|
||
|
for (int face_index = 0; face_index < mesh->mNumFaces; face_index++) {
|
||
|
struct aiFace *face = &(mesh->mFaces[face_index]);
|
||
|
long start = new_model->indices_num;
|
||
|
|
||
|
new_model->indices_num += face->mNumIndices;
|
||
|
new_model->indices = (unsigned int *) realloc(new_model->indices, sizeof(unsigned int)*new_model->indices_num);
|
||
|
if (new_model->indices == NULL) {
|
||
|
fprintf(stderr, "Error: failed allocating memory for indices\n");
|
||
|
goto error;
|
||
|
}
|
||
|
|
||
|
memcpy(&new_model->indices[start], face->mIndices, sizeof(unsigned int)*face->mNumIndices);
|
||
|
}
|
||
|
|
||
|
// fetch normals
|
||
|
for (int normal_index = 0; normal_index < mesh->mNumVertices; normal_index++) {
|
||
|
struct aiVector3D *normal = &(mesh->mNormals[normal_index]);
|
||
|
long start = new_model->normals_num*3;
|
||
|
|
||
|
new_model->normals_num++;
|
||
|
new_model->normals = (float *) realloc(new_model->normals, new_model->normals_num*3*sizeof(float));
|
||
|
if (new_model->normals == NULL) {
|
||
|
fprintf(stderr, "Error: failed allocating memory for normals\n");
|
||
|
goto error;
|
||
|
}
|
||
|
|
||
|
memcpy(&new_model->normals[start], normal, sizeof(float)*3);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return new_model;
|
||
|
|
||
|
error:
|
||
|
aiReleaseImport(scene);
|
||
|
free(new_model->vertices);
|
||
|
free(new_model->indices);
|
||
|
free(new_model->normals);
|
||
|
free(new_model);
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
int record_path(struct object *obj) {
|
||
|
if (obj->paths_num <= obj->paths_max) {
|
||
|
obj->paths = (float *) reallocarray(obj->paths, (obj->paths_num+1)*3, sizeof(float));
|
||
|
}
|
||
|
|
||
|
if (obj->paths == NULL) {
|
||
|
fprintf(stderr, "Error: failed allocating memory for paths of object\n");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
memcpy(obj->paths+(obj->paths_num*3), obj->position, 3*sizeof(float));
|
||
|
|
||
|
if (obj->paths_num < obj->paths_max) {
|
||
|
obj->paths_num++;
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
// pop first element
|
||
|
memmove(obj->paths, &obj->paths[3], (obj->paths_num)*3*sizeof(float));
|
||
|
|
||
|
end:
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
struct object *create_object(struct object **o, float mass, struct model *model) {
|
||
|
struct object *no = malloc(sizeof(struct object));
|
||
|
if (no == NULL) {
|
||
|
return NULL;
|
||
|
}
|
||
|
memset(no, 0, sizeof(struct object));
|
||
|
|
||
|
no->mass = mass;
|
||
|
no->scale = 1.0f;
|
||
|
no->paths_max = MAX_PATHS;
|
||
|
no->model = model;
|
||
|
glm_vec4_one(no->position);
|
||
|
glm_vec3_one(no->color);
|
||
|
|
||
|
for (int i = 0; i < 3; i++) {
|
||
|
no->color[i] = 0.5f + (fabs(frand48()) / 2);
|
||
|
}
|
||
|
|
||
|
if (*o == NULL) {
|
||
|
*o = no;
|
||
|
return no;
|
||
|
}
|
||
|
|
||
|
struct object *previous_object = *o;
|
||
|
no->next = previous_object;
|
||
|
*o = no;
|
||
|
|
||
|
return no;
|
||
|
}
|