Browse Source

added vertex shader

Iver 4 months ago
parent
commit
34e507098f

+ 2 - 1
.vscode/settings.json

@@ -12,7 +12,8 @@
         "rasteriver.h": "c",
         "custom_types.h": "c",
         "time.h": "c",
-        "sourparse.h": "c"
+        "sourparse.h": "c",
+        "cl.h": "c"
     },
     "OpenCL.explorer.localizedProperties": false,
     "C_Cpp.default.compilerPath": "/bin/gcc"

BIN
build/librasteriver.so


BIN
build/main.bin


BIN
compiled_libs/librasteriver.so


+ 14 - 1
src/headers/custom_types.h

@@ -3,6 +3,7 @@
 
 #include "math.h"
 #include <stddef.h>
+#include "sourparse.h"
 
 // ----- Meshes -----
 typedef struct {
@@ -74,7 +75,8 @@ typedef struct {
     uint64_t flags;
     RI_vector_2f uv_loop_multiplier;
     RI_vector_2f texture_render_size;
-    double (*shader_function_pointer) (int pixel_x, int pixel_y, RI_vector_3f v_pos_0, RI_vector_3f v_pos_1, RI_vector_3f v_pos_2, RI_vector_3f normal, RI_vector_2f uv, uint32_t color);
+    double (*fragment_shader) (int pixel_x, int pixel_y, RI_vector_3f v_pos_0, RI_vector_3f v_pos_1, RI_vector_3f v_pos_2, RI_vector_3f normal, RI_vector_2f uv, uint32_t color);
+    void (*vertex_shader) (RI_vector_3f *v_pos_0, RI_vector_3f *v_pos_1, RI_vector_3f *v_pos_2, double horizontal_fov_factor, double vertical_fov_factor);
 } RI_material;
 
 typedef struct { // An entity that has an mesh, transform, materials, etc
@@ -125,6 +127,17 @@ typedef struct {
     uint64_t flags;
 } RI_scene;
 
+// ----- Fonts -----
+typedef struct {
+    char character;
+    int is_rendered;
+} rendered_character_map;
+
+typedef struct {
+    SP_font *sp_font;
+    rendered_character_map *rendered_character_map;
+} RI_font;
+
 // ----- Memory Manager -----
 typedef struct {
     size_t size;

+ 27 - 0
src/headers/math.h

@@ -227,4 +227,31 @@ RI_vector_2 v2f_to_2(RI_vector_2f v){
     return (RI_vector_2){v.x, v.y};
 }
 
+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 rotation_conjugation = rotation;
+    quaternion_conjugate(&rotation_conjugation);
+
+    quaternion_multiply(&rotation, pos_quat);
+
+    quaternion_multiply(&rotation, rotation_conjugation);
+
+    *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){
+    double cx = cosf(euler_rotation.x * 0.5f);
+    double sx = sinf(euler_rotation.x * 0.5f);
+    double cy = cosf(euler_rotation.y * 0.5f);
+    double sy = sinf(euler_rotation.y * 0.5f);
+    double cz = cosf(euler_rotation.z * 0.5f);
+    double 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;
+}
+
 #endif

+ 5 - 0
src/headers/rasteriver.h

@@ -15,6 +15,11 @@ typedef struct {
     double *z_buffer;
     RI_vector_2 z_buffer_resolution;
  
+    double (*default_fragment_shader) (int pixel_x, int pixel_y, RI_vector_3f v_pos_0, RI_vector_3f v_pos_1, RI_vector_3f v_pos_2, RI_vector_3f normal, RI_vector_2f uv, uint32_t color);
+    void (*default_vertex_shader) (RI_vector_3f *v_pos_0, RI_vector_3f *v_pos_1, RI_vector_3f *v_pos_2, double horizontal_fov_factor, double vertical_fov_factor);
+    void (*perspective_vertex_shader) (RI_vector_3f *v_pos_0, RI_vector_3f *v_pos_1, RI_vector_3f *v_pos_2, double horizontal_fov_factor, double vertical_fov_factor);
+    void (*orthographic_vertex_shader) (RI_vector_3f *v_pos_0, RI_vector_3f *v_pos_1, RI_vector_3f *v_pos_2, double horizontal_fov_factor, double vertical_fov_factor);
+
     int window_width;
     int window_height;
     char *window_title;

+ 4 - 3
src/launch_program/main.c

@@ -38,20 +38,19 @@ int main(){
     text_plane_material->flags = RI_MATERIAL_HAS_TEXTURE | RI_MATERIAL_DOUBLE_SIDED;
     text_plane_material->texture_reference = RI_request_empty_texture((RI_vector_2){400, 400});
     text_plane_material->albedo = 0xFFFFFFFF;
-    text_plane_material->shader_function_pointer = shader_function;
+    text_plane_material->fragment_shader = shader_function;
 
     RI_material* bill_material = &materials[1];
     bill_material->flags = RI_MATERIAL_HAS_TEXTURE | RI_MATERIAL_DOUBLE_SIDED;
     RI_texture_creation_data tex_data[1] = {(RI_texture_creation_data){"textures/THIS IS THE WALL.png", {0, 0}}};
     bill_material->texture_reference = RI_request_textures(1, tex_data);
     bill_material->albedo = 0xFFFFFFFF;
-    bill_material->shader_function_pointer = NULL;
 
     // actors
     RI_actor* text_plane = &actors[0];
     text_plane->material_reference = text_plane_material;
     text_plane->mesh_reference = plane_mesh;
-    text_plane->transform.scale = (RI_vector_3f){100, 100, 100};
+    text_plane->transform.scale = (RI_vector_3f){300, 300, 300};
     text_plane->transform.position = (RI_vector_3f){0, 0, 400};
     text_plane->transform.rotation = (RI_vector_4f){0, 1, 0, 0};
     RI_euler_rotation_to_quaternion(&text_plane->transform.rotation, (RI_vector_3f){-3.1415926 / 2, 0, 0});
@@ -73,6 +72,8 @@ int main(){
 
     RI_euler_rotation_to_quaternion(&scene->camera_rotation, (RI_vector_3f){0, 0, 0});
 
+    scene->camera_position = (RI_vector_3f){0, 0, 0};
+
     double y_rotation = 0;
 
     scene->antialiasing_subsample_resolution = 8;

+ 43 - 50
src/library/rasteriver.c

@@ -342,7 +342,7 @@ void RI_render_text(SP_font *font, RI_texture *target_texture, RI_vector_2f posi
             }
         }
         
-        position.x += (float)font->h_metrics[glyph].advance_width / font->units_per_em * size;
+        position.x += (float)(font->h_metrics[glyph].advance_width) / font->units_per_em * size;
     }
 }
 
@@ -399,10 +399,15 @@ RI_actor* RI_request_actors(int RI_number_of_requested_actors){
 }
 
 RI_material* RI_request_materials(int RI_number_of_requested_materials){
+    int previous_material_count = ri.material_count;
     ri.material_count += RI_number_of_requested_materials;
 
     ri.materials = RI_realloc(ri.materials, sizeof(RI_material) * ri.material_count);
 
+    for (int i = previous_material_count; i < ri.material_count; ++i){
+        ri.materials[i] = ri.error_material;
+    }
+
     return ri.materials;
 }
 
@@ -621,33 +626,6 @@ RI_mesh* RI_request_meshes(int RI_number_of_requested_meshes, char **filenames,
     else return mesh;
 }
 
-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 rotation_conjugation = rotation;
-    quaternion_conjugate(&rotation_conjugation);
-
-    quaternion_multiply(&rotation, pos_quat);
-
-    quaternion_multiply(&rotation, rotation_conjugation);
-
-    *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){
-    double cx = cosf(euler_rotation.x * 0.5f);
-    double sx = sinf(euler_rotation.x * 0.5f);
-    double cy = cosf(euler_rotation.y * 0.5f);
-    double sy = sinf(euler_rotation.y * 0.5f);
-    double cz = cosf(euler_rotation.z * 0.5f);
-    double 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;
-}
-
 double mod(double a, double b){
     if(b < 0.0)
         return -mod(-a, -b);   
@@ -672,7 +650,7 @@ uint32_t multiply_rgb(uint32_t color, float factor) {
 
 int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
     // do rendering stuff
-    if (ri.running){
+    if (ri.running){        
         double horizontal_fov_factor = target_texture->resolution.x / tanf(0.5 * scene->FOV);
         double vertical_fov_factor = target_texture->resolution.y / tanf(0.5 * scene->FOV);
 
@@ -747,21 +725,16 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
                 // object position
                 vector_3f_element_wise_add(&cur_r_face->position_0, current_actor->transform.position);
                 vector_3f_element_wise_add(&cur_r_face->position_1, current_actor->transform.position);
-                vector_3f_element_wise_add(&cur_r_face->position_2, current_actor->transform.position);
-                
-                // camera rotation
+                vector_3f_element_wise_add(&cur_r_face->position_2, current_actor->transform.position);    
+
+                // camera position & rotation
                 vector_3f_element_wise_subtract(&cur_r_face->position_0, scene->camera_position);
                 vector_3f_element_wise_subtract(&cur_r_face->position_1, scene->camera_position);
                 vector_3f_element_wise_subtract(&cur_r_face->position_2, scene->camera_position);
 
                 quaternion_rotate(&cur_r_face->position_0, scene->camera_rotation);
                 quaternion_rotate(&cur_r_face->position_1, scene->camera_rotation);
-                quaternion_rotate(&cur_r_face->position_2, scene->camera_rotation);
-                
-                // camera position
-                // vector_3f_element_wise_subtract(&cur_r_face->position_0, scene->camera_position);
-                // vector_3f_element_wise_subtract(&cur_r_face->position_1, scene->camera_position);
-                // vector_3f_element_wise_subtract(&cur_r_face->position_2, scene->camera_position);
+                quaternion_rotate(&cur_r_face->position_2, scene->camera_rotation);        
 
                 RI_vector_3f *pos_0 = &cur_r_face->position_0;
                 RI_vector_3f *pos_1 = &cur_r_face->position_1;
@@ -976,14 +949,7 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
                         break;
                 }
 
-                cur_r_face->position_0.x = cur_r_face->position_0.x / cur_r_face->position_0.z * horizontal_fov_factor;
-                cur_r_face->position_0.y = cur_r_face->position_0.y / cur_r_face->position_0.z * vertical_fov_factor;
-                
-                cur_r_face->position_1.x = cur_r_face->position_1.x / cur_r_face->position_1.z * horizontal_fov_factor;
-                cur_r_face->position_1.y = cur_r_face->position_1.y / cur_r_face->position_1.z * vertical_fov_factor;
-
-                cur_r_face->position_2.x = cur_r_face->position_2.x / cur_r_face->position_2.z * horizontal_fov_factor;
-                cur_r_face->position_2.y = cur_r_face->position_2.y / cur_r_face->position_2.z * vertical_fov_factor;
+                current_actor->material_reference->vertex_shader(&cur_r_face->position_0, &cur_r_face->position_1, &cur_r_face->position_2, horizontal_fov_factor, vertical_fov_factor);
 
                 cur_r_face->min_screen_x = pos_0->x; 
                 if (pos_1->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_1->x;
@@ -1176,9 +1142,7 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
                         else pixel_color = 0xFF777777;
                     }
                     
-                    double shader_result = 1;
-                    
-                    if (current_face->material_reference->shader_function_pointer != NULL) shader_result = current_face->material_reference->shader_function_pointer(x, y, *pos_0, *pos_1, *pos_2, normal, (RI_vector_2f){ux, uy}, pixel_color);
+                    double shader_result = shader_result = current_face->material_reference->fragment_shader(x, y, *pos_0, *pos_1, *pos_2, normal, (RI_vector_2f){ux, uy}, pixel_color);
 
                     // set alpha after checking shader result becuase things with alpha 0 should still depth write
 
@@ -1356,6 +1320,24 @@ void signal_interupt_handler(int signal) {
     RI_stop(1);
 }
 
+double default_fragment_shader(int pixel_x, int pixel_y, RI_vector_3f v_pos_0, RI_vector_3f v_pos_1, RI_vector_3f v_pos_2, RI_vector_3f normal, RI_vector_2f uv, uint32_t color){
+    return 1;
+}
+
+void perspective_vertex_shader(RI_vector_3f *v_pos_0, RI_vector_3f *v_pos_1, RI_vector_3f *v_pos_2, double horizontal_fov_factor, double vertical_fov_factor){
+    v_pos_0->x = v_pos_0->x / v_pos_0->z * horizontal_fov_factor;
+    v_pos_0->y = v_pos_0->y / v_pos_0->z * vertical_fov_factor;
+    
+    v_pos_1->x = v_pos_1->x / v_pos_1->z * horizontal_fov_factor;
+    v_pos_1->y = v_pos_1->y / v_pos_1->z * vertical_fov_factor;
+
+    v_pos_2->x = v_pos_2->x / v_pos_2->z * horizontal_fov_factor;
+    v_pos_2->y = v_pos_2->y / v_pos_2->z * vertical_fov_factor;
+}
+
+void orthographic_vertex_shader(RI_vector_3f *v_pos_0, RI_vector_3f *v_pos_1, RI_vector_3f *v_pos_2, double horizontal_fov_factor, double vertical_fov_factor){
+}
+
 int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title){
     signal(SIGINT, signal_interupt_handler);
     
@@ -1407,8 +1389,19 @@ int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title){
     ri.error_texture.image_buffer[0] = 0xFFFF00FF;
     ri.error_texture.resolution = (RI_vector_2){1, 1};
 
+    ri.default_fragment_shader = default_fragment_shader;
+    ri.default_vertex_shader = perspective_vertex_shader;
+    ri.perspective_vertex_shader = perspective_vertex_shader;
+    ri.orthographic_vertex_shader = orthographic_vertex_shader;
+
+    ri.error_material.bump_map_reference = NULL;
+    ri.error_material.normal_map_reference = NULL;
+    ri.error_material.uv_loop_multiplier = (RI_vector_2f){1.0, 1.0};
+    ri.error_material.wireframe_width = 0.2;
     ri.error_material.texture_reference = &ri.error_texture;
-    ri.error_material.albedo = 0xFF5522CC;
+    ri.error_material.albedo = 0xFFFF00FF;
+    ri.error_material.fragment_shader = ri.default_fragment_shader;
+    ri.error_material.vertex_shader = ri.default_vertex_shader;
     ri.error_material.flags = RI_MATERIAL_UNLIT | RI_MATERIAL_DONT_DEPTH_TEST | RI_MATERIAL_DONT_RECEIVE_SHADOW | RI_MATERIAL_HAS_TEXTURE | RI_MATERIAL_DOUBLE_SIDED;
 
     return 0;