Repository: refactor the project
Created object and math files for operations specific to objects and math.
This commit is contained in:
parent
68f6e2eeb8
commit
cb348fe1b8
|
@ -1,7 +1,7 @@
|
||||||
cmake_minimum_required(VERSION 3.25)
|
cmake_minimum_required(VERSION 3.25)
|
||||||
project(gravity C)
|
project(gravity C)
|
||||||
|
|
||||||
set(SOURCE_FILES gravity.c)
|
set(SOURCE_FILES gravity.c math.c object.c)
|
||||||
set(HEADER_FILES )
|
set(HEADER_FILES )
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES})
|
add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES})
|
||||||
|
|
|
@ -11,11 +11,11 @@ void main() {
|
||||||
vec4 light_color = vec4(0.7, 0.7, 0.7, 1.0);
|
vec4 light_color = vec4(0.7, 0.7, 0.7, 1.0);
|
||||||
vec4 color = vec4(object_color.xyz, 1.0f);
|
vec4 color = vec4(object_color.xyz, 1.0f);
|
||||||
|
|
||||||
vec4 light_location = vec4(5.0, 5.0, -10.0, 0.0);
|
vec4 light_location = vec4(0.0, 100.0, 0.0, 1.0);
|
||||||
vec4 light_direction = normalize(light_location - frag_pos);
|
vec4 light_direction = normalize(light_location - frag_pos);
|
||||||
float diff = max(dot(norm.xyz, light_direction.xyz), 0.0);
|
float diff = max(dot(norm.xyz, light_direction.xyz), 0.0);
|
||||||
|
|
||||||
vec4 diffuse = diff * light_color;
|
vec4 diffuse = diff * light_color;
|
||||||
|
|
||||||
output = color;
|
output = color + diffuse;
|
||||||
}
|
}
|
||||||
|
|
272
gravity.c
272
gravity.c
|
@ -1,76 +1,28 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GL/freeglut.h>
|
#include <GL/freeglut.h>
|
||||||
#include <cglm/cglm.h>
|
#include <cglm/cglm.h>
|
||||||
#include <assimp/cimport.h>
|
#include "math.h"
|
||||||
#include <assimp/scene.h>
|
#include "object.h"
|
||||||
#include <assimp/postprocess.h>
|
|
||||||
|
|
||||||
#define MAX_PATHS 1500
|
|
||||||
|
|
||||||
float frand48(void) {
|
|
||||||
float number = (float) rand() / (float) (RAND_MAX + 1.0);
|
|
||||||
float side = rand() % 2;
|
|
||||||
if (side == 0) {
|
|
||||||
number = -number;
|
|
||||||
}
|
|
||||||
|
|
||||||
return number;
|
|
||||||
}
|
|
||||||
|
|
||||||
// structs
|
|
||||||
struct object {
|
|
||||||
vec4 translation_force;
|
|
||||||
vec4 rotation_force;
|
|
||||||
vec4 position;
|
|
||||||
vec4 rotation;
|
|
||||||
vec3 color;
|
|
||||||
float mass;
|
|
||||||
void *next;
|
|
||||||
|
|
||||||
float *paths;
|
|
||||||
int paths_num;
|
|
||||||
int paths_max;
|
|
||||||
|
|
||||||
float *vertices;
|
|
||||||
unsigned int *indices;
|
|
||||||
float *normals;
|
|
||||||
long vertices_num;
|
|
||||||
long indices_num;
|
|
||||||
long normals_num;
|
|
||||||
float scale;
|
|
||||||
|
|
||||||
unsigned int vao; // array object for the actual object
|
|
||||||
unsigned int vbo; // buffer for vertices
|
|
||||||
unsigned int ebo; // buffer for indices
|
|
||||||
unsigned int nbo; // buffer for normals
|
|
||||||
|
|
||||||
unsigned int pvao; // array object for paths
|
|
||||||
unsigned int pbo; // buffer for paths
|
|
||||||
};
|
|
||||||
|
|
||||||
|
// global settings
|
||||||
float fov = 80.0f; // default fov
|
float fov = 80.0f; // default fov
|
||||||
float fov_change = 1.0f;
|
float fov_change = 1.0f;
|
||||||
vec3 camera_pos = { 0.0f, 0.0f, 100.0f };
|
vec3 camera_pos = { 0.0f, 0.0f, 0.0f };
|
||||||
vec3 camera_front = { 0.0f, 0.0f, -1.0f };
|
vec3 camera_front = { 0.0f, 0.0f, -1.0f };
|
||||||
vec3 camera_up = { 0.0f, 1.0f, 0.0f };
|
vec3 camera_up = { 0.0f, 1.0f, 0.0f };
|
||||||
struct object *camera_lock = NULL; // is camera locked to any object?
|
struct object *camera_lock = NULL; // is camera locked to any object?
|
||||||
float camera_yaw; // x rotation
|
float camera_yaw = -90.0f; // x rotation
|
||||||
float camera_pitch; // y rotation
|
float camera_pitch = 0.0f; // y rotation
|
||||||
float camera_sensitivity = 0.01f;
|
float camera_sensitivity = 0.01f;
|
||||||
float movement_speed = 2.0f;
|
float movement_speed = 2.0f;
|
||||||
float time_speed = 33.0f;
|
|
||||||
float time_speed_change = 1.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
|
||||||
|
|
||||||
|
// opengl
|
||||||
unsigned int shader_program;
|
unsigned int shader_program;
|
||||||
unsigned int vertex_shader;
|
unsigned int vertex_shader;
|
||||||
unsigned int fragment_shader;
|
unsigned int fragment_shader;
|
||||||
|
@ -79,9 +31,6 @@ unsigned int fragment_shader;
|
||||||
const char *object_vertex_shader_location = "assets/shaders/shader.vert";
|
const char *object_vertex_shader_location = "assets/shaders/shader.vert";
|
||||||
const char *object_fragment_shader_location = "assets/shaders/shader.frag";
|
const char *object_fragment_shader_location = "assets/shaders/shader.frag";
|
||||||
|
|
||||||
// global objects information
|
|
||||||
struct object* objects = NULL;
|
|
||||||
|
|
||||||
int load_shader(const char *path, unsigned int shader) {
|
int load_shader(const char *path, unsigned int shader) {
|
||||||
FILE *fp = fopen(path, "r");
|
FILE *fp = fopen(path, "r");
|
||||||
int len = 0;
|
int len = 0;
|
||||||
|
@ -133,63 +82,6 @@ int load_shader(const char *path, unsigned int shader) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_model_to_object(const char *path, struct object *obj) {
|
|
||||||
const struct aiScene *scene = aiImportFile(path, aiProcess_Triangulate);
|
|
||||||
|
|
||||||
if (scene == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int load_shaders() {
|
int load_shaders() {
|
||||||
glDeleteProgram(shader_program);
|
glDeleteProgram(shader_program);
|
||||||
shader_program = glCreateProgram();
|
shader_program = glCreateProgram();
|
||||||
|
@ -231,60 +123,6 @@ int load_shaders() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void calculate_gravity(struct object *src, struct object *target, vec3 force) {
|
|
||||||
vec4 tmp;
|
|
||||||
glm_vec4_sub(target->position, src->position, tmp);
|
|
||||||
|
|
||||||
vec3 distance;
|
|
||||||
glm_vec3(tmp, distance);
|
|
||||||
|
|
||||||
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 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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
if (distance[i] == 0) {
|
|
||||||
force[i] = 0.0f;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
force[i] = (top / (distance_xyz / (target->position[i] - src->position[i]))) * force_scale;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// records the latest obj position to the path ring
|
|
||||||
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++;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// pop first element
|
|
||||||
memmove(obj->paths, &obj->paths[3], (obj->paths_num)*3*sizeof(float));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void display() {
|
void display() {
|
||||||
mat4 view;
|
mat4 view;
|
||||||
mat4 projection;
|
mat4 projection;
|
||||||
|
@ -383,14 +221,13 @@ void display() {
|
||||||
glDrawArrays(GL_LINE_STRIP, 0, obj->paths_num);
|
glDrawArrays(GL_LINE_STRIP, 0, obj->paths_num);
|
||||||
}
|
}
|
||||||
|
|
||||||
glutSwapBuffers();
|
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
|
glutSwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// setup default mouse position
|
// setup default mouse position
|
||||||
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
||||||
glutWarpPointer(screen_viewport[2]/2, screen_viewport[3]/2);
|
|
||||||
|
|
||||||
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
||||||
glGenVertexArrays(1, &obj->vao);
|
glGenVertexArrays(1, &obj->vao);
|
||||||
|
@ -425,55 +262,6 @@ void setup() {
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct object *create_object(float mass, const char *model) {
|
|
||||||
struct object *new_object = (struct object *) malloc(sizeof(struct object));
|
|
||||||
|
|
||||||
if (new_object == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
new_object->mass = mass;
|
|
||||||
glm_vec4_one(new_object->position);
|
|
||||||
glm_vec4_one(new_object->rotation);
|
|
||||||
glm_vec4_zero(new_object->translation_force);
|
|
||||||
glm_vec4_zero(new_object->rotation_force);
|
|
||||||
new_object->vertices_num = 0;
|
|
||||||
new_object->indices_num = 0;
|
|
||||||
new_object->normals_num = 0;
|
|
||||||
new_object->scale = 1.0f;
|
|
||||||
new_object->vertices = NULL;
|
|
||||||
new_object->indices = NULL;
|
|
||||||
new_object->normals = NULL;
|
|
||||||
new_object->next = NULL;
|
|
||||||
new_object->paths = NULL;
|
|
||||||
new_object->paths_num = 0;
|
|
||||||
new_object->paths_max = MAX_PATHS;
|
|
||||||
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 (load_model_to_object(model, new_object) == -1) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (objects == NULL) {
|
|
||||||
objects = new_object;
|
|
||||||
return new_object;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct object *obj = objects;
|
|
||||||
while (obj->next != NULL) {
|
|
||||||
obj = obj->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
obj->next = new_object;
|
|
||||||
|
|
||||||
return new_object;
|
|
||||||
}
|
|
||||||
|
|
||||||
void keyboard(unsigned char key, int x, int y) {
|
void keyboard(unsigned char key, int x, int y) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case '\x1B':
|
case '\x1B':
|
||||||
|
@ -503,7 +291,7 @@ void keyboard(unsigned char key, int x, int y) {
|
||||||
}
|
}
|
||||||
case 'd':
|
case 'd':
|
||||||
case 'D': {
|
case 'D': {
|
||||||
vec3 side_scalar = {movement_speed, movement_speed, movement_speed};
|
vec3 side_scalar = { movement_speed, movement_speed, movement_speed };
|
||||||
vec3 camera_side;
|
vec3 camera_side;
|
||||||
glm_cross(camera_front, camera_up, camera_side);
|
glm_cross(camera_front, camera_up, camera_side);
|
||||||
glm_normalize(camera_side);
|
glm_normalize(camera_side);
|
||||||
|
@ -513,14 +301,14 @@ void keyboard(unsigned char key, int x, int y) {
|
||||||
}
|
}
|
||||||
case 's':
|
case 's':
|
||||||
case 'S': {
|
case 'S': {
|
||||||
vec3 front_scalar = {movement_speed, movement_speed, movement_speed};
|
vec3 front_scalar = { movement_speed, movement_speed, movement_speed};
|
||||||
glm_vec3_mul(front_scalar, camera_front, front_scalar);
|
glm_vec3_mul(front_scalar, camera_front, front_scalar);
|
||||||
glm_vec3_sub(camera_pos, front_scalar, camera_pos);
|
glm_vec3_sub(camera_pos, front_scalar, camera_pos);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'w':
|
case 'w':
|
||||||
case 'W': {
|
case 'W': {
|
||||||
vec3 front_scalar = {movement_speed, movement_speed, movement_speed};
|
vec3 front_scalar = { movement_speed, movement_speed, movement_speed };
|
||||||
glm_vec3_mul(front_scalar, camera_front, front_scalar);
|
glm_vec3_mul(front_scalar, camera_front, front_scalar);
|
||||||
glm_vec3_add(camera_pos, front_scalar, camera_pos);
|
glm_vec3_add(camera_pos, front_scalar, camera_pos);
|
||||||
break;
|
break;
|
||||||
|
@ -578,7 +366,8 @@ void mouse(int button, int state, int x, int y) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int warped_pointer = 1;
|
int warped_pointer = 0;
|
||||||
|
int first_pointer = 1;
|
||||||
void mouse_motion(int x, int y) {
|
void mouse_motion(int x, int y) {
|
||||||
if (warped_pointer == 1) {
|
if (warped_pointer == 1) {
|
||||||
warped_pointer = 0;
|
warped_pointer = 0;
|
||||||
|
@ -587,6 +376,12 @@ void mouse_motion(int x, int y) {
|
||||||
|
|
||||||
warped_pointer = 1;
|
warped_pointer = 1;
|
||||||
glutWarpPointer((screen_viewport[2]/2), screen_viewport[3]/2);
|
glutWarpPointer((screen_viewport[2]/2), screen_viewport[3]/2);
|
||||||
|
|
||||||
|
if (first_pointer == 1) {
|
||||||
|
first_pointer = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
float offset_x = (float) (x - (screen_viewport[2]/2)) * camera_sensitivity;
|
float offset_x = (float) (x - (screen_viewport[2]/2)) * camera_sensitivity;
|
||||||
float offset_y = (float) (y - (screen_viewport[3]/2)) * camera_sensitivity;
|
float offset_y = (float) (y - (screen_viewport[3]/2)) * camera_sensitivity;
|
||||||
|
|
||||||
|
@ -633,34 +428,37 @@ int main(int argc, char **argv) {
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// objects
|
// scene setup
|
||||||
struct object *a = create_object(1.0f, "assets/models/sphere.obj");
|
struct object *a = create_object(100000000.0f, "assets/models/kub.obj");
|
||||||
struct object *b = create_object(1000000000.0f, "assets/models/sphere.obj");
|
struct object *b = create_object(100000.0f, "assets/models/kub.obj");
|
||||||
struct object *c = create_object(1.0f, "assets/models/sphere.obj");
|
struct object *c = create_object(10000000.0f, "assets/models/sphere.obj");
|
||||||
float distance = -200.0f;
|
float distance = -500.0f;
|
||||||
|
|
||||||
vec4 a_pos = {0.0f, 50.0f, distance, 0.0f};
|
vec4 a_pos = {0.0f, 0.0f, distance, 0.0f};
|
||||||
glm_vec4_add(a->position, a_pos, a->position);
|
glm_vec4_add(a->position, a_pos, a->position);
|
||||||
|
vec4 b_pos= {100.0f, 300.0f, distance, 0.0f};
|
||||||
|
glm_vec4_add(b->position, b_pos, b->position);
|
||||||
//vec4 a_pos = {0.0f, -0.0f, -150.0f, 0.0f};
|
//vec4 a_pos = {0.0f, -0.0f, -150.0f, 0.0f};
|
||||||
//glm_vec4_add(a->position, a_pos, a->position);
|
//glm_vec4_add(a->position, a_pos, a->position);
|
||||||
|
|
||||||
vec4 b_pos = {0.0f, -75.0f, -150.0f, 0.0f};
|
// vec4 b_pos = {0.0f, -75.0f, -150.0f, 0.0f};
|
||||||
glm_vec4_add(b->position, b_pos, b->position);
|
// glm_vec4_add(b->position, b_pos, b->position);
|
||||||
|
|
||||||
vec4 c_pos = {0.0f, -20.0f, distance, 0.0f};
|
vec4 c_pos = {-100.0f, -400.0f, distance, 0.0f};
|
||||||
glm_vec4_add(c->position, c_pos, c->position);
|
glm_vec4_add(c->position, c_pos, c->position);
|
||||||
|
|
||||||
float n = 0.05f;
|
float n = 0.05f;
|
||||||
|
|
||||||
vec3 b_boost = {10*n, 0.0f, 0.0f};
|
vec3 b_boost = {-70*n, 0.0f, 0.0f};
|
||||||
|
|
||||||
glm_vec3_add(b->translation_force, b_boost , b->translation_force);
|
glm_vec3_add(b->translation_force, b_boost , b->translation_force);
|
||||||
|
glm_vec3_add(c->translation_force, b_boost , c->translation_force);
|
||||||
|
|
||||||
b->scale = 2.0f;
|
// b->scale = 2.0f;
|
||||||
camera_lock = b;
|
a->scale = 5.0f;
|
||||||
|
b->scale = 10.0f;
|
||||||
|
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
glutMainLoop();
|
glutMainLoop();
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
42
math.c
Normal file
42
math.c
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
#include "math.h"
|
||||||
|
#include "object.h"
|
||||||
|
#include <cglm/cglm.h>
|
||||||
|
|
||||||
|
float frand48(void) {
|
||||||
|
float number = (float) rand() / (float) (RAND_MAX + 1.0);
|
||||||
|
float side = rand() % 2;
|
||||||
|
if (side == 0) {
|
||||||
|
number = -number;
|
||||||
|
}
|
||||||
|
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
void calculate_gravity(struct object *src, struct object *target, vec3 force) {
|
||||||
|
vec4 tmp;
|
||||||
|
glm_vec4_sub(target->position, src->position, tmp);
|
||||||
|
|
||||||
|
vec3 distance;
|
||||||
|
glm_vec3(tmp, distance);
|
||||||
|
|
||||||
|
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 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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
if (distance[i] == 0) {
|
||||||
|
force[i] = 0.0f;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
force[i] = (top / (distance_xyz / (target->position[i] - src->position[i]))) * force_scale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
10
math.h
Normal file
10
math.h
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#ifndef MATH_H
|
||||||
|
#define MATH_H
|
||||||
|
|
||||||
|
#include "object.h"
|
||||||
|
#include <cglm/cglm.h>
|
||||||
|
|
||||||
|
float frand48(void);
|
||||||
|
void calculate_gravity(struct object *src, struct object *target, vec3 force);
|
||||||
|
|
||||||
|
#endif
|
136
object.c
Normal file
136
object.c
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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, const char *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;
|
||||||
|
glm_vec4_one(new_object->position);
|
||||||
|
glm_vec4_one(new_object->rotation);
|
||||||
|
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 (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;
|
||||||
|
|
||||||
|
end:
|
||||||
|
return new_object;
|
||||||
|
|
||||||
|
error:
|
||||||
|
return NULL;
|
||||||
|
}
|
44
object.h
Normal file
44
object.h
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#ifndef OBJECT_H
|
||||||
|
#define OBJECT_H
|
||||||
|
|
||||||
|
#include <cglm/cglm.h>
|
||||||
|
|
||||||
|
#define MAX_PATHS 5500
|
||||||
|
|
||||||
|
struct object {
|
||||||
|
vec4 translation_force;
|
||||||
|
vec4 rotation_force;
|
||||||
|
vec4 position;
|
||||||
|
vec4 rotation;
|
||||||
|
vec3 color;
|
||||||
|
float mass;
|
||||||
|
void *next;
|
||||||
|
|
||||||
|
float *paths;
|
||||||
|
int paths_num;
|
||||||
|
int paths_max;
|
||||||
|
|
||||||
|
float *vertices;
|
||||||
|
unsigned int *indices;
|
||||||
|
float *normals;
|
||||||
|
long vertices_num;
|
||||||
|
long indices_num;
|
||||||
|
long normals_num;
|
||||||
|
float scale;
|
||||||
|
|
||||||
|
unsigned int vao; // array object for the actual object
|
||||||
|
unsigned int vbo; // buffer for vertices
|
||||||
|
unsigned int ebo; // buffer for indices
|
||||||
|
unsigned int nbo; // buffer for normals
|
||||||
|
|
||||||
|
unsigned int pvao; // array object for paths
|
||||||
|
unsigned int pbo; // buffer for paths
|
||||||
|
};
|
||||||
|
|
||||||
|
extern struct object *objects;
|
||||||
|
|
||||||
|
int load_model_to_object(const char *path, struct object *obj);
|
||||||
|
int record_path(struct object *obj);
|
||||||
|
struct object *create_object(float mass, const char *model);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user