Repository: refactor models loading and objects
This commit is contained in:
parent
ce7aff2a11
commit
1919712792
34
gravity.c
34
gravity.c
|
@ -21,6 +21,10 @@ float camera_sensitivity = 0.01f;
|
||||||
float movement_speed = 2.0f;
|
float movement_speed = 2.0f;
|
||||||
GLint screen_viewport[4]; // viewport: x,y,width,height
|
GLint screen_viewport[4]; // viewport: x,y,width,height
|
||||||
int toggle_tracing = 0; // true or false
|
int toggle_tracing = 0; // true or false
|
||||||
|
long added_particles = 0;
|
||||||
|
|
||||||
|
// tmp
|
||||||
|
struct model *sphere_model;
|
||||||
|
|
||||||
// opengl
|
// opengl
|
||||||
unsigned int shader_program;
|
unsigned int shader_program;
|
||||||
|
@ -144,7 +148,7 @@ void display() {
|
||||||
glm_lookat(camera_pos, camera_center, camera_up, view);
|
glm_lookat(camera_pos, camera_center, camera_up, view);
|
||||||
|
|
||||||
glm_mat4_identity(projection);
|
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");
|
view_uniform = glGetUniformLocation(shader_program, "view");
|
||||||
projection_uniform = glGetUniformLocation(shader_program, "projection");
|
projection_uniform = glGetUniformLocation(shader_program, "projection");
|
||||||
|
@ -158,6 +162,7 @@ void display() {
|
||||||
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
||||||
mat4 translation_matrix;
|
mat4 translation_matrix;
|
||||||
glm_mat4_identity(translation_matrix);
|
glm_mat4_identity(translation_matrix);
|
||||||
|
struct model *obj_model = obj->model;
|
||||||
|
|
||||||
// calculate gravity
|
// calculate gravity
|
||||||
for (struct object *target = objects; target != NULL; target = target->next) {
|
for (struct object *target = objects; target != NULL; target = target->next) {
|
||||||
|
@ -204,7 +209,7 @@ void display() {
|
||||||
glUniform1f(scale_uniform, obj->scale);
|
glUniform1f(scale_uniform, obj->scale);
|
||||||
|
|
||||||
glBindVertexArray(obj->vao);
|
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);
|
glBindVertexArray(obj->pvao);
|
||||||
|
|
||||||
|
@ -230,6 +235,7 @@ void setup() {
|
||||||
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
||||||
|
|
||||||
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
||||||
|
struct model *obj_model = obj->model;
|
||||||
glGenVertexArrays(1, &obj->vao);
|
glGenVertexArrays(1, &obj->vao);
|
||||||
glGenVertexArrays(1, &obj->pvao);
|
glGenVertexArrays(1, &obj->pvao);
|
||||||
glGenBuffers(1, &obj->vbo);
|
glGenBuffers(1, &obj->vbo);
|
||||||
|
@ -240,19 +246,19 @@ void setup() {
|
||||||
glBindVertexArray(obj->vao);
|
glBindVertexArray(obj->vao);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER,obj->vbo);
|
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);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *) 0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, obj->nbo);
|
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);
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *) 0);
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,obj->ebo);
|
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);
|
glBindVertexArray(0);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
@ -329,7 +335,9 @@ void keyboard(unsigned char key, int x, int y) {
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
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;
|
float n = 0.05f;
|
||||||
vec4 a_pos = {frand48() * 100, frand48() * 100, -150.0f, 0.0f};
|
vec4 a_pos = {frand48() * 100, frand48() * 100, -150.0f, 0.0f};
|
||||||
|
@ -429,9 +437,15 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// scene setup
|
// scene setup
|
||||||
struct object *a = create_object(100000000.0f, "assets/models/kub.obj");
|
sphere_model = load_model("assets/models/sphere.obj");
|
||||||
struct object *b = create_object(100000.0f, "assets/models/kub.obj");
|
if (sphere_model == NULL) {
|
||||||
struct object *c = create_object(10000000.0f, "assets/models/sphere.obj");
|
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;
|
float distance = -500.0f;
|
||||||
|
|
||||||
vec4 a_pos = {0.0f, 0.0f, distance, 0.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;
|
// b->scale = 2.0f;
|
||||||
a->scale = 5.0f;
|
a->scale = 5.0f;
|
||||||
b->scale = 10.0f;
|
b->scale = 10.0f;
|
||||||
|
// camera_lock = b;
|
||||||
|
|
||||||
|
|
||||||
setup();
|
setup();
|
||||||
glutMainLoop();
|
glutMainLoop();
|
||||||
|
|
16
math.c
16
math.c
|
@ -13,25 +13,25 @@ float frand48(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void calculate_gravity(struct object *src, struct object *target, vec3 force) {
|
void calculate_gravity(struct object *src, struct object *target, vec3 force) {
|
||||||
vec4 tmp;
|
vec4 v4distance;
|
||||||
glm_vec4_sub(target->position, src->position, tmp);
|
glm_vec4_sub(target->position, src->position, v4distance);
|
||||||
|
|
||||||
vec3 distance;
|
vec3 v3distance;
|
||||||
glm_vec3(tmp, distance);
|
glm_vec3(v4distance, v3distance);
|
||||||
|
|
||||||
float distance_xy = sqrt((distance[0] * distance[0]) + (distance[1] * distance[1]));
|
float distance_xy = sqrt((v3distance[0] * v3distance[0]) + (v3distance[1] * v3distance[1]));
|
||||||
float distance_xyz = sqrt((distance_xy * distance_xy) + (distance[2] * distance[2]));
|
float distance_xyz = sqrt((distance_xy * distance_xy) + (v3distance[2] * v3distance[2]));
|
||||||
float force_scale = 4.0f;
|
float force_scale = 4.0f;
|
||||||
|
|
||||||
float g = 6.67f * 1e-11f;
|
float g = 6.67f * 1e-11f;
|
||||||
float top = g * src->mass * target->mass;
|
float top = g * src->mass * target->mass;
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
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++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
if (distance[i] == 0) {
|
if (v3distance[i] == 0) {
|
||||||
force[i] = 0.0f;
|
force[i] = 0.0f;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
88
object.c
88
object.c
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
struct object *objects;
|
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);
|
const struct aiScene *scene = aiImportFile(path, aiProcess_Triangulate);
|
||||||
|
|
||||||
if (scene == NULL) {
|
if (scene == NULL) {
|
||||||
|
@ -64,6 +64,74 @@ int load_model_to_object(const char *path, struct object *obj) {
|
||||||
|
|
||||||
aiReleaseImport(scene);
|
aiReleaseImport(scene);
|
||||||
return 0;
|
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) {
|
int record_path(struct object *obj) {
|
||||||
|
@ -90,7 +158,7 @@ end:
|
||||||
return 0;
|
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));
|
struct object *new_object = (struct object *) calloc(1, sizeof(struct object));
|
||||||
|
|
||||||
if (new_object == NULL) {
|
if (new_object == NULL) {
|
||||||
|
@ -102,8 +170,8 @@ struct object *create_object(float mass, const char *model) {
|
||||||
new_object->mass = mass;
|
new_object->mass = mass;
|
||||||
new_object->scale = 1.0f;
|
new_object->scale = 1.0f;
|
||||||
new_object->paths_max = MAX_PATHS;
|
new_object->paths_max = MAX_PATHS;
|
||||||
|
new_object->model = model;
|
||||||
glm_vec4_one(new_object->position);
|
glm_vec4_one(new_object->position);
|
||||||
glm_vec4_one(new_object->rotation);
|
|
||||||
glm_vec3_one(new_object->color);
|
glm_vec3_one(new_object->color);
|
||||||
|
|
||||||
// choose random 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);
|
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) {
|
if (objects == NULL) {
|
||||||
objects = new_object;
|
objects = new_object;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct object *object = objects;
|
struct object *previous_object = objects;
|
||||||
while (object->next != NULL) {
|
new_object->next = previous_object;
|
||||||
object = object->next;
|
objects = new_object;
|
||||||
}
|
|
||||||
|
|
||||||
object->next = new_object;
|
|
||||||
|
|
||||||
end:
|
end:
|
||||||
return new_object;
|
return new_object;
|
||||||
|
|
26
object.h
26
object.h
|
@ -3,13 +3,21 @@
|
||||||
|
|
||||||
#include <cglm/cglm.h>
|
#include <cglm/cglm.h>
|
||||||
|
|
||||||
#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 {
|
struct object {
|
||||||
vec4 translation_force;
|
vec4 translation_force;
|
||||||
vec4 rotation_force;
|
|
||||||
vec4 position;
|
vec4 position;
|
||||||
vec4 rotation;
|
|
||||||
vec3 color;
|
vec3 color;
|
||||||
float mass;
|
float mass;
|
||||||
void *next;
|
void *next;
|
||||||
|
@ -18,12 +26,7 @@ struct object {
|
||||||
int paths_num;
|
int paths_num;
|
||||||
int paths_max;
|
int paths_max;
|
||||||
|
|
||||||
float *vertices;
|
struct model *model;
|
||||||
unsigned int *indices;
|
|
||||||
float *normals;
|
|
||||||
long vertices_num;
|
|
||||||
long indices_num;
|
|
||||||
long normals_num;
|
|
||||||
float scale;
|
float scale;
|
||||||
|
|
||||||
unsigned int vao; // array object for the actual object
|
unsigned int vao; // array object for the actual object
|
||||||
|
@ -37,8 +40,9 @@ struct object {
|
||||||
|
|
||||||
extern struct object *objects;
|
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);
|
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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user