From 1919712792d894caa2651132b02e3297fd46e651 Mon Sep 17 00:00:00 2001 From: 0xdeadbeer <64986162+0xdeadbeer@users.noreply.github.com> Date: Sat, 11 Nov 2023 19:53:11 +0100 Subject: [PATCH] Repository: refactor models loading and objects --- gravity.c | 34 +++++++++++++++------ math.c | 16 +++++----- object.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++--------- object.h | 26 +++++++++------- 4 files changed, 122 insertions(+), 42 deletions(-) diff --git a/gravity.c b/gravity.c index 191c2db..fd24bdf 100644 --- a/gravity.c +++ b/gravity.c @@ -21,6 +21,10 @@ float camera_sensitivity = 0.01f; float movement_speed = 2.0f; GLint screen_viewport[4]; // viewport: x,y,width,height int toggle_tracing = 0; // true or false +long added_particles = 0; + +// tmp +struct model *sphere_model; // opengl unsigned int shader_program; @@ -144,7 +148,7 @@ void display() { glm_lookat(camera_pos, camera_center, camera_up, view); glm_mat4_identity(projection); - glm_perspective(glm_rad(fov), (float) screen_viewport[2]/(float) screen_viewport[3], 0.01f, 10000.0f, projection); + glm_perspective(glm_rad(fov), (float) screen_viewport[2]/(float) screen_viewport[3], 0.01f, 100000.0f, projection); view_uniform = glGetUniformLocation(shader_program, "view"); projection_uniform = glGetUniformLocation(shader_program, "projection"); @@ -158,6 +162,7 @@ void display() { for (struct object *obj = objects; obj != NULL; obj = obj->next) { mat4 translation_matrix; glm_mat4_identity(translation_matrix); + struct model *obj_model = obj->model; // calculate gravity for (struct object *target = objects; target != NULL; target = target->next) { @@ -204,7 +209,7 @@ void display() { glUniform1f(scale_uniform, obj->scale); glBindVertexArray(obj->vao); - glDrawElements(GL_TRIANGLES, obj->indices_num, GL_UNSIGNED_INT, (void *) 0); + glDrawElements(GL_TRIANGLES, obj_model->indices_num, GL_UNSIGNED_INT, (void *) 0); glBindVertexArray(obj->pvao); @@ -230,6 +235,7 @@ void setup() { glGetIntegerv(GL_VIEWPORT, screen_viewport); for (struct object *obj = objects; obj != NULL; obj = obj->next) { + struct model *obj_model = obj->model; glGenVertexArrays(1, &obj->vao); glGenVertexArrays(1, &obj->pvao); glGenBuffers(1, &obj->vbo); @@ -240,19 +246,19 @@ void setup() { glBindVertexArray(obj->vao); glBindBuffer(GL_ARRAY_BUFFER,obj->vbo); - glBufferData(GL_ARRAY_BUFFER,obj->vertices_num*3*sizeof(float),obj->vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER,obj_model->vertices_num*3*sizeof(float),obj_model->vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *) 0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, obj->nbo); - glBufferData(GL_ARRAY_BUFFER, obj->normals_num*3*sizeof(float), obj->normals, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, obj_model->normals_num*3*sizeof(float), obj_model->normals, GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *) 0); glEnableVertexAttribArray(1); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,obj->ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER,obj->indices_num*sizeof(unsigned int),obj->indices, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, obj_model->indices_num*sizeof(unsigned int), obj_model->indices, GL_STATIC_DRAW); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -329,7 +335,9 @@ void keyboard(unsigned char key, int x, int y) { break; case 'c': case 'C': { - struct object *a = create_object(1000000.0f, "assets/models/sphere.obj"); + added_particles++; + fprintf(stdout, "INFO: ADDED PARTICLES COUNT %ld\n", added_particles); + struct object *a = create_object(1000000.0f, sphere_model); float n = 0.05f; vec4 a_pos = {frand48() * 100, frand48() * 100, -150.0f, 0.0f}; @@ -429,9 +437,15 @@ int main(int argc, char **argv) { } // scene setup - struct object *a = create_object(100000000.0f, "assets/models/kub.obj"); - struct object *b = create_object(100000.0f, "assets/models/kub.obj"); - struct object *c = create_object(10000000.0f, "assets/models/sphere.obj"); + sphere_model = load_model("assets/models/sphere.obj"); + if (sphere_model == NULL) { + fprintf(stderr, "Error: loading model"); + return EXIT_FAILURE; + } + + struct object *a = create_object(100000000.0f, sphere_model); + struct object *b = create_object(100000.0f, sphere_model); + struct object *c = create_object(10000000.0f, sphere_model); float distance = -500.0f; vec4 a_pos = {0.0f, 0.0f, distance, 0.0f}; @@ -457,6 +471,8 @@ int main(int argc, char **argv) { // b->scale = 2.0f; a->scale = 5.0f; b->scale = 10.0f; +// camera_lock = b; + setup(); glutMainLoop(); diff --git a/math.c b/math.c index a375aba..a1ebd48 100644 --- a/math.c +++ b/math.c @@ -13,25 +13,25 @@ float frand48(void) { } void calculate_gravity(struct object *src, struct object *target, vec3 force) { - vec4 tmp; - glm_vec4_sub(target->position, src->position, tmp); + vec4 v4distance; + glm_vec4_sub(target->position, src->position, v4distance); - vec3 distance; - glm_vec3(tmp, distance); + vec3 v3distance; + glm_vec3(v4distance, v3distance); - float distance_xy = sqrt((distance[0] * distance[0]) + (distance[1] * distance[1])); - float distance_xyz = sqrt((distance_xy * distance_xy) + (distance[2] * distance[2])); + float distance_xy = sqrt((v3distance[0] * v3distance[0]) + (v3distance[1] * v3distance[1])); + float distance_xyz = sqrt((distance_xy * distance_xy) + (v3distance[2] * v3distance[2])); float force_scale = 4.0f; float g = 6.67f * 1e-11f; float top = g * src->mass * target->mass; for (int i = 0; i < 3; i++) { - distance[i] = (distance[i] * distance[i] * distance[i]); + v3distance[i] = (v3distance[i] * v3distance[i] * v3distance[i]); } for (int i = 0; i < 3; i++) { - if (distance[i] == 0) { + if (v3distance[i] == 0) { force[i] = 0.0f; continue; } diff --git a/object.c b/object.c index 09b0863..ee6ae7b 100644 --- a/object.c +++ b/object.c @@ -8,7 +8,7 @@ struct object *objects; -int load_model_to_object(const char *path, struct object *obj) { +/*int load_model_to_object(const char *path, struct object *obj) { const struct aiScene *scene = aiImportFile(path, aiProcess_Triangulate); if (scene == NULL) { @@ -64,6 +64,74 @@ int load_model_to_object(const char *path, struct object *obj) { 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) { @@ -90,7 +158,7 @@ end: return 0; } -struct object *create_object(float mass, const char *model) { +struct object *create_object(float mass, struct model *model) { struct object *new_object = (struct object *) calloc(1, sizeof(struct object)); if (new_object == NULL) { @@ -102,8 +170,8 @@ struct object *create_object(float mass, const char *model) { 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_vec4_one(new_object->rotation); glm_vec3_one(new_object->color); // choose random color @@ -111,22 +179,14 @@ struct object *create_object(float mass, const char *model) { new_object->color[i] = 0.5f + (fabs(frand48()) / 2); } - if (load_model_to_object(model, new_object) == -1) { - fprintf(stderr, "Error: failed loading model '%s' to object", model); - goto error; - } - if (objects == NULL) { objects = new_object; goto end; } - struct object *object = objects; - while (object->next != NULL) { - object = object->next; - } - - object->next = new_object; + struct object *previous_object = objects; + new_object->next = previous_object; + objects = new_object; end: return new_object; diff --git a/object.h b/object.h index 9bb37a2..173e97f 100644 --- a/object.h +++ b/object.h @@ -3,13 +3,21 @@ #include -#define MAX_PATHS 5500 +#define MAX_PATHS 2000 + +struct model { + float *vertices; + unsigned int *indices; + float *normals; + + long vertices_num; + long indices_num; + long normals_num; +}; struct object { vec4 translation_force; - vec4 rotation_force; vec4 position; - vec4 rotation; vec3 color; float mass; void *next; @@ -18,12 +26,7 @@ struct object { int paths_num; int paths_max; - float *vertices; - unsigned int *indices; - float *normals; - long vertices_num; - long indices_num; - long normals_num; + struct model *model; float scale; unsigned int vao; // array object for the actual object @@ -37,8 +40,9 @@ struct object { extern struct object *objects; -int load_model_to_object(const char *path, struct object *obj); +//int load_model_to_object(const char *path, struct object *obj); +struct model *load_model(const char *path); int record_path(struct object *obj); -struct object *create_object(float mass, const char *model); +struct object *create_object(float mass, struct model *model); #endif