Browse Source

added euler to quaternion

IverMartinson 5 months ago
parent
commit
cd02130eba
5 changed files with 28 additions and 5 deletions
  1. BIN
      build/librasteriver.so
  2. BIN
      build/main.bin
  3. 1 0
      src/headers/functions.h
  4. 9 1
      src/launch_program/main.c
  5. 18 4
      src/library/rasteriver.c

BIN
build/librasteriver.so


BIN
build/main.bin


+ 1 - 0
src/headers/functions.h

@@ -12,5 +12,6 @@ int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title); /
 int RI_stop(int result); // Stop RasterIver safely and free memory
 int RI_stop(int result); // Stop RasterIver safely and free memory
 int RI_render(RI_scene *scene, RI_texture *target_texture); // Render a scene to a texture
 int RI_render(RI_scene *scene, RI_texture *target_texture); // Render a scene to a texture
 int RI_add_actors_to_scene(int RI_number_of_actors_to_add_to_scene, RI_actor *actors, RI_scene *scene);
 int RI_add_actors_to_scene(int RI_number_of_actors_to_add_to_scene, RI_actor *actors, RI_scene *scene);
+void RI_euler_rotation_to_quaternion(RI_vector_4f* quaternion, RI_vector_3f euler_rotation);
 
 
 #endif
 #endif

+ 9 - 1
src/launch_program/main.c

@@ -70,17 +70,25 @@ int main(){
     screen->mesh_reference = unit_plane_mesh;
     screen->mesh_reference = unit_plane_mesh;
     screen->transform.scale = (RI_vector_3f){50, 50, 50};
     screen->transform.scale = (RI_vector_3f){50, 50, 50};
     screen->transform.position = (RI_vector_3f){0, 0, 250};
     screen->transform.position = (RI_vector_3f){0, 0, 250};
-    screen->transform.rotation = (RI_vector_4f){0.70710678, 0.70710678};
+    screen->transform.rotation = (RI_vector_4f){0.70710678, 0.70710678, 0, 0};
 
 
     RI_add_actors_to_scene(4, actors, scene);
     RI_add_actors_to_scene(4, actors, scene);
 
 
     ri->FOV = 1.5; // 90 degrees in radians
     ri->FOV = 1.5; // 90 degrees in radians
 
 
+    float y_rotation = 0;
+
     while (running){
     while (running){
         bill_cube->transform.position = (RI_vector_3f){sin(ri->frame * 0.1) * 50 - 100, sin(ri->frame * 0.2 + 0.4) * 50, sin(ri->frame * 0.1) * 10 + 200};
         bill_cube->transform.position = (RI_vector_3f){sin(ri->frame * 0.1) * 50 - 100, sin(ri->frame * 0.2 + 0.4) * 50, sin(ri->frame * 0.1) * 10 + 200};
         
         
         scene->camera_position = (RI_vector_3f){cos(ri->frame * 0.07) * 50 * sin(ri->frame * 0.2), sin(ri->frame * 0.07) * 50 * sin(ri->frame * 0.2), -150};
         scene->camera_position = (RI_vector_3f){cos(ri->frame * 0.07) * 50 * sin(ri->frame * 0.2), sin(ri->frame * 0.07) * 50 * sin(ri->frame * 0.2), -150};
 
 
+        RI_euler_rotation_to_quaternion(&floor->transform.rotation, (RI_vector_3f){0, y_rotation, 0});
+        
+        RI_euler_rotation_to_quaternion(&bill_cube->transform.rotation, (RI_vector_3f){y_rotation, y_rotation, y_rotation});
+
+        y_rotation += 0.1;
+
         RI_render(scene, ri->frame_buffer);
         RI_render(scene, ri->frame_buffer);
     }
     }
 }
 }

+ 18 - 4
src/library/rasteriver.c

@@ -292,7 +292,7 @@ RI_mesh* RI_request_meshes(int RI_number_of_requested_meshes, char **filenames,
     else return mesh;
     else return mesh;
 }
 }
 
 
-void quaternion_rotation(RI_vector_3f *position, RI_vector_4f rotation){
+void quaternion_rotate(RI_vector_3f *position, RI_vector_4f rotation){
     RI_vector_4f pos_quat = {0, position->x, position->y, position->z};
     RI_vector_4f pos_quat = {0, position->x, position->y, position->z};
 
 
     RI_vector_4f rotation_conjugation = rotation;
     RI_vector_4f rotation_conjugation = rotation;
@@ -305,6 +305,20 @@ void quaternion_rotation(RI_vector_3f *position, RI_vector_4f rotation){
     *position = (RI_vector_3f){rotation.x, rotation.y, rotation.z};
     *position = (RI_vector_3f){rotation.x, rotation.y, rotation.z};
 }
 }
 
 
+void RI_euler_rotation_to_quaternion(RI_vector_4f *quaternion, RI_vector_3f euler_rotation){
+    float cx = cosf(euler_rotation.x * 0.5f);
+    float sx = sinf(euler_rotation.x * 0.5f);
+    float cy = cosf(euler_rotation.y * 0.5f);
+    float sy = sinf(euler_rotation.y * 0.5f);
+    float cz = cosf(euler_rotation.z * 0.5f);
+    float sz = sinf(euler_rotation.z * 0.5f);
+
+    quaternion->w = cx * cy * cz + sx * sy * sz;
+    quaternion->x = sx * cy * cz - cx * sy * sz;
+    quaternion->y = cx * sy * cz + sx * cy * sz;
+    quaternion->z = cx * cy * sz - sx * sy * cz;
+}
+
 int RI_render(RI_scene *scene, RI_texture *target_texture){
 int RI_render(RI_scene *scene, RI_texture *target_texture){
     // do rendering stuff
     // do rendering stuff
     if (ri.running){
     if (ri.running){
@@ -329,9 +343,9 @@ int RI_render(RI_scene *scene, RI_texture *target_texture){
                 current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_2_index].uv = current_actor->mesh_reference->vertecies[current_actor->mesh_reference->faces[polygon_index].vertex_2_index].uv;
                 current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_2_index].uv = current_actor->mesh_reference->vertecies[current_actor->mesh_reference->faces[polygon_index].vertex_2_index].uv;
 
 
 
 
-                quaternion_rotation(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_0_index].position, current_actor->transform.rotation);
-                quaternion_rotation(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_1_index].position, current_actor->transform.rotation);
-                quaternion_rotation(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_2_index].position, current_actor->transform.rotation);
+                quaternion_rotate(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_0_index].position, current_actor->transform.rotation);
+                quaternion_rotate(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_1_index].position, current_actor->transform.rotation);
+                quaternion_rotate(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_2_index].position, current_actor->transform.rotation);
 
 
                 vector_3f_hadamard(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_0_index].position, current_actor->transform.scale);
                 vector_3f_hadamard(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_0_index].position, current_actor->transform.scale);
                 vector_3f_hadamard(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_1_index].position, current_actor->transform.scale);
                 vector_3f_hadamard(&current_actor->transformed_verticies[current_actor->mesh_reference->faces[polygon_index].vertex_1_index].position, current_actor->transform.scale);