-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.c
196 lines (152 loc) · 6.22 KB
/
object.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "object.h"
#include "math.h"
#include <math.h>
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
struct object *objects;
/*int load_model_to_object(const char *path, struct object *obj) {
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 = obj->vertices_num*3;
obj->vertices_num++;
obj->vertices = (float *) realloc(obj->vertices, obj->vertices_num*3*sizeof(float));
if (obj->vertices == NULL) {
return -1;
}
memcpy(&obj->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 = obj->indices_num;
obj->indices_num += face->mNumIndices;
obj->indices = (unsigned int *) realloc(obj->indices, sizeof(unsigned int)*obj->indices_num);
if (obj->indices == NULL) {
return -1;
}
memcpy(&obj->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 = obj->normals_num*3;
obj->normals_num++;
obj->normals = (float *) realloc(obj->normals,obj->normals_num*3*sizeof(float));
if (obj->normals == NULL) {
return -1;
}
memcpy(&obj->normals[start], normal, sizeof(float)*3);
}
}
aiReleaseImport(scene);
return 0;
}*/
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(float mass, struct model *model) {
struct object *new_object = (struct object *) calloc(1, sizeof(struct object));
if (new_object == NULL) {
fprintf(stderr, "Error: failed allocating memory for a new object\n");
goto error;
}
// initialize default values
new_object->mass = mass;
new_object->scale = 1.0f;
new_object->paths_max = MAX_PATHS;
new_object->model = model;
glm_vec4_one(new_object->position);
glm_vec3_one(new_object->color);
// choose random color
for (int i = 0; i < 3; i++) {
new_object->color[i] = 0.5f + (fabs(frand48()) / 2);
}
if (objects == NULL) {
objects = new_object;
goto end;
}
struct object *previous_object = objects;
new_object->next = previous_object;
objects = new_object;
end:
return new_object;
error:
return NULL;
}