Browse Source

implemented sourparse

IverMartinson 5 months ago
parent
commit
a17168dd45

+ 3 - 1
.vscode/settings.json

@@ -7,7 +7,9 @@
         "array": "c",
         "string_view": "c",
         "initializer_list": "c",
-        "utility": "c"
+        "utility": "c",
+        "compare": "c",
+        "rasteriver.h": "c"
     },
     "OpenCL.explorer.localizedProperties": false,
     "C_Cpp.default.compilerPath": "/bin/gcc"

+ 3 - 3
Makefile

@@ -1,13 +1,13 @@
 COMPILER=gcc
-FLAGS_ALL=-fsanitize=address -g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter
-FLAGS_EXAMPLE=-Lbuild/ -lrasteriver -Wl,-rpath=build/ -lm -lSDL2
+FLAGS_ALL=-fsanitize=address -g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-sequence-point
+FLAGS_EXAMPLE=-Lcompiled_libs/ -lrasteriver -lsourparse -Wl,-rpath=build/ -lm -lSDL2
 FLAGS_LIB=-D CL_TARGET_OPENCL_VERSION=120 -fPIC -shared -lc -lSDL2 -lm -lOpenCL
 
 main.bin: rasteriver.so
 	$(COMPILER) $(FLAGS_ALL) src/launch_program/main.c -o build/main.bin $(FLAGS_EXAMPLE) 
 
 rasteriver.so:
-	$(COMPILER) $(FLAGS_ALL) src/library/rasteriver.c -o build/librasteriver.so $(FLAGS_LIB) 
+	$(COMPILER) $(FLAGS_ALL) src/library/rasteriver.c -o compiled_libs/librasteriver.so $(FLAGS_LIB) 
 
 clean:
 	rm build/*

BIN
build/librasteriver.so


BIN
build/libsourparse.so


BIN
build/main.bin


+ 8 - 0
build/sourparse.h

@@ -0,0 +1,8 @@
+#ifndef SOURPARSE_H
+#define SOURPARSE_H
+
+#include "functions.h"
+#include "values.h"
+#include "custom_types.h"
+
+#endif

BIN
compiled_libs/librasteriver.so


BIN
compiled_libs/libsourparse.so


BIN
fonts/CalSans-Regular.ttf


BIN
fonts/JetBrainsMono-Regular.ttf


+ 1 - 1
src/headers/functions.h

@@ -10,7 +10,7 @@ RI_texture* RI_request_textures(int RI_number_of_requested_textures, RI_texture_
 RI_scene* RI_request_scenes(int RI_number_of_requested_scenes);
 int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title); // Initialize RasterIver
 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, int clear_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);
 void RI_euler_rotation_to_quaternion(RI_vector_4f* quaternion, RI_vector_3f euler_rotation);
 void RI_tick();

+ 41 - 12
src/headers/math.h

@@ -26,52 +26,69 @@ typedef struct {
     double z; 
 } RI_vector_4f;
 
-void vector_2f_times_double(RI_vector_2f *vector, double value){
+// value-wise multiplacation.
+// multiply the whole vector by 1 value
+void vector_2f_times(RI_vector_2f *vector, double value){
     vector->x *= value;
     vector->y *= value;
 }
 
-void vector_3f_times_double(RI_vector_3f *vector, double value){
+// value-wise multiplacation.
+// multiply the whole vector by 1 value
+void vector_3f_times(RI_vector_3f *vector, double value){
     vector->x *= value;
     vector->y *= value;
     vector->z *= value;
 }
 
+// hadamard multiplacation.
+// multiply each value of one vector with the matching one on the other vector
 void vector_3f_hadamard(RI_vector_3f *multiplicand, RI_vector_3f multiplicator){
     multiplicand->x *= multiplicator.x;
     multiplicand->y *= multiplicator.y;
     multiplicand->z *= multiplicator.z;
 }
 
+// "hadamard" addition.
+// add each value of one vector with the matching one on the other vector
 void vector_2f_element_wise_add(RI_vector_2f *addend_a, RI_vector_2f addend_b){
     addend_a->x += addend_b.x;
     addend_a->y += addend_b.y;
 }
 
+// "hadamard" addition.
+// add each value of one vector with the matching one on the other vector
 void vector_3f_element_wise_add(RI_vector_3f *addend_a, RI_vector_3f addend_b){
     addend_a->x += addend_b.x;
     addend_a->y += addend_b.y;
     addend_a->z += addend_b.z;
 }
 
+// "hadamard" subtraction.
+// subtraction each value of one vector with the matching one on the other vector
 void vector_3f_element_wise_subtract(RI_vector_3f *minuend, RI_vector_3f subtrahend){
     minuend->x -= subtrahend.x;
     minuend->y -= subtrahend.y;
     minuend->z -= subtrahend.z;
 }
 
-void vector_3f_divide_double(RI_vector_3f *dividend, double divisor){
+// "hadamard" division.
+// divide each value of one vector with the matching one on the other vector
+void vector_3f_divide(RI_vector_3f *dividend, double divisor){
     dividend->x /= divisor;
     dividend->y /= divisor;
     dividend->z /= divisor;
 }
 
+// conjugate a quaterion.
+// (flip the sign of the x, y, z values)
 void quaternion_conjugate(RI_vector_4f* quaternion){
-    quaternion->x *= -1;
-    quaternion->y *= -1;
-    quaternion->z *= -1;
+    quaternion->x *= -1.0;
+    quaternion->y *= -1.0;
+    quaternion->z *= -1.0;
 }
 
+// quaternion multiplacation
 void quaternion_multiply(RI_vector_4f* a, RI_vector_4f b){
     double w1 = a->w; double x1 = a->x; double y1 = a->y; double z1 = a->z;
     double w2 = b.w; double x2 = b.x; double y2 = b.y; double z2 = b.z;
@@ -84,25 +101,37 @@ void quaternion_multiply(RI_vector_4f* a, RI_vector_4f b){
     *a = (RI_vector_4f){w, x, y, z};
 }
 
+// linear interpolate between 2 vectors
 void vector_2f_lerp(RI_vector_2f vector_a, RI_vector_2f vector_b, RI_vector_2f *result, double w1){
     double w0 = 1.0 - w1;
 
-    vector_2f_times_double(result, 0);
+    vector_2f_times(result, 0);
 
-    vector_2f_times_double(&vector_a, w0);
-    vector_2f_times_double(&vector_b, w1);
+    vector_2f_times(&vector_a, w0);
+    vector_2f_times(&vector_b, w1);
 
     vector_2f_element_wise_add(result, vector_a);
     vector_2f_element_wise_add(result, vector_b);
 }
 
+// beziate between 2 vectors
+void vector_2f_bezier_interpolate(RI_vector_2f vector_a, RI_vector_2f vector_b, RI_vector_2f vector_c, RI_vector_2f *result, double w1){
+    double w0 = 1.0 - w1;
+
+    vector_2f_lerp(vector_a, vector_b, &vector_b, w1); // this works because the first vector b is a copy and the second is a reference
+    vector_2f_lerp(vector_b, vector_c, &vector_c, w1);
+
+    vector_2f_lerp(vector_b, vector_c, result, w1);
+}
+
+// linear interpolate between 2 vectors
 void vector_3f_lerp(RI_vector_3f vector_a, RI_vector_3f vector_b, RI_vector_3f *result, double w1){
     double w0 = 1.0 - w1;
 
-    vector_3f_times_double(result, 0);
+    vector_3f_times(result, 0);
 
-    vector_3f_times_double(&vector_a, w0);
-    vector_3f_times_double(&vector_b, w1);
+    vector_3f_times(&vector_a, w0);
+    vector_3f_times(&vector_b, w1);
 
     vector_3f_element_wise_add(result, vector_a);
     vector_3f_element_wise_add(result, vector_b);

+ 1 - 0
src/headers/rasteriver.h

@@ -5,6 +5,7 @@
 #include "values.h"
 #include "custom_types.h"
 #include "math.h"
+#include "sourparse.h"
 #include <SDL2/SDL.h>
 
 typedef struct {

+ 22 - 0
src/headers/sourparse.h

@@ -0,0 +1,22 @@
+#ifndef SOURPARSE_H
+#define SOURPARSE_H
+
+#include "stdint.h"
+
+typedef struct {
+    int *x_coords, *y_coords, *contour_end_indicies, number_of_points, number_of_contours;
+    uint8_t *flags;
+} SP_glyph;
+
+typedef struct {
+    int current_byte, number_of_glyphs, *glyph_offsets; 
+    uint16_t *unicode_to_glyph_indicies, units_per_em;
+    uint8_t *buffer;
+    SP_glyph *glyphs;
+    int16_t index_to_loca_format;
+} SP_font;
+
+SP_font SP_load_font(char *filename);
+void SP_free_font(SP_font* font);
+
+#endif

+ 29 - 15
src/launch_program/main.c

@@ -3,7 +3,10 @@
 int main(){
     // get RasterIver context
     RasterIver *ri = RI_get_ri();
+    ri->prefix = "--------------------------";
 
+    SP_font font = SP_load_font("fonts/CalSans-Regular.ttf");
+    
     ri->debug_memory = 0;
 
     RI_init(1000, 1000, "This is RasterIver 2.0!!");
@@ -57,7 +60,7 @@ int main(){
     screen_material->albedo = 0xFFFFFFFF;
 
     // actors
-    RI_actor* floor = &actors[0];
+    RI_actor* floor = &actors[2];
     floor->material_reference = floor_material;
     floor->mesh_reference = unit_plane_mesh;
     floor->transform.scale = (RI_vector_3f){1000, 100, 1000};
@@ -71,10 +74,10 @@ int main(){
     wall->transform.position = (RI_vector_3f){0, 0, 300};
     wall->transform.rotation = (RI_vector_4f){0.70710678, 0.70710678, 0, 0};
 
-    RI_actor* test_object = &actors[2];
+    RI_actor* test_object = &actors[0];
     test_object->material_reference = test_object_material;
     test_object->mesh_reference = test_object_mesh;
-    test_object->transform.scale = (RI_vector_3f){50, 50, 50};
+    test_object->transform.scale = (RI_vector_3f){2, 2, 2};
     test_object->transform.position = (RI_vector_3f){-50, 100, 100};
     test_object->transform.rotation = (RI_vector_4f){0, 1, 0, 0};
 
@@ -85,7 +88,7 @@ int main(){
     screen->transform.position = (RI_vector_3f){0, 0, 250};
     screen->transform.rotation = (RI_vector_4f){0, 1, 0, 0};
 
-    RI_add_actors_to_scene(4, actors, scene);
+    RI_add_actors_to_scene(1, actors, scene);
 
     scene->FOV = 1.5; // 90 degrees in radians
     scene->minimum_clip_distance = 0.1;
@@ -97,27 +100,38 @@ int main(){
     scene->antialiasing_subsample_resolution = 8;
     scene->flags = RI_SCENE_DONT_USE_AA;
 
+    int glyph = 536;
+
     while (running){
-        test_object->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};
+        // test_object->transform.position = (RI_vector_3f){0, 0, 200};
 
-        RI_euler_rotation_to_quaternion(&screen->transform.rotation, (RI_vector_3f){-3.14159 / 2, 0, ri->frame * 0.03});
+        // RI_euler_rotation_to_quaternion(&screen->transform.rotation, (RI_vector_3f){-3.14159 / 2, 0, ri->frame * 0.03});
 
-        // scene->camera_position = (RI_vector_3f){cos(ri->frame * 0.07) * 10 * sin(ri->frame * 0.2), sin(ri->frame * 0.07) * 10 * sin(ri->frame * 0.2), -300};
-        scene->camera_position = (RI_vector_3f){0, 0, -300};
+        // // scene->camera_position = (RI_vector_3f){cos(ri->frame * 0.07) * 10 * sin(ri->frame * 0.2), sin(ri->frame * 0.07) * 10 * sin(ri->frame * 0.2), -300};
+        // scene->camera_position = (RI_vector_3f){0, 0, -300};
         
-        wall->transform.scale.x = fabs(sin(y_rotation)) * 100 + 70;
+        // wall->transform.scale.x = fabs(sin(y_rotation)) * 100 + 70;
 
         // RI_euler_rotation_to_quaternion(&scene->camera_rotation, (RI_vector_3f){0, y_rotation * .01 + 1.5, 0});
 
         // RI_euler_rotation_to_quaternion(&floor->transform.rotation, (RI_vector_3f){0, y_rotation, 0});
         
-        RI_euler_rotation_to_quaternion(&test_object->transform.rotation, (RI_vector_3f){y_rotation, y_rotation, y_rotation});
+        // RI_euler_rotation_to_quaternion(&test_object->transform.rotation, (RI_vector_3f){y_rotation, y_rotation, y_rotation});
         // RI_euler_rotation_to_quaternion(&test_object->transform.rotation, (RI_vector_3f){0, y_rotation, 0});
 
-        y_rotation += 0.1;
-
-        RI_render(scene, ri->frame_buffer);
-
-        RI_tick();
+        // y_rotation += 0.1;
+        
+        float scale = 0.3;
+
+        for (int i = 0; i < font.glyphs[glyph].number_of_contours; ++i){
+            for (int j = i > 0 ? font.glyphs[1].contour_end_indicies[i - 1] : 0; j < font.glyphs[glyph].contour_end_indicies[i]; ++j){
+                if (!(font.glyphs[glyph].flags[j] & 1)) test_object->material_reference->texture_reference = &ri->error_texture;
+                else test_object->material_reference->texture_reference = test_object_texture;
+                test_object->transform.position = (RI_vector_3f){font.glyphs[glyph].x_coords[j] * scale - 100, font.glyphs[glyph].y_coords[j] * scale - 100, 500};
+            
+                RI_render(scene, ri->frame_buffer, 0);
+                RI_tick();
+            }
+        }
     }
 }

+ 7 - 5
src/library/rasteriver.c

@@ -484,7 +484,7 @@ uint32_t multiply_rgb(uint32_t color, float factor) {
     return (a << 24) | (r << 16) | (g << 8) | b;
 }
 
-int RI_render(RI_scene *scene, RI_texture *target_texture){
+int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
     // do rendering stuff
     if (ri.running){
         double horizontal_fov_factor = target_texture->resolution.x / tanf(0.5 * scene->FOV);
@@ -827,10 +827,12 @@ int RI_render(RI_scene *scene, RI_texture *target_texture){
             ri.z_buffer = RI_realloc(ri.z_buffer, sizeof(double) * target_texture->resolution.x * target_texture->resolution.y);
         }
 
-        for (int pixel_index = 0; pixel_index < target_texture->resolution.x * target_texture->resolution.y; ++pixel_index){
-            target_texture->image_buffer[pixel_index] = 0xFF333333;
-            ri.z_buffer[pixel_index] = 999999999;
-        }
+            for (int pixel_index = 0; pixel_index < target_texture->resolution.x * target_texture->resolution.y; ++pixel_index){
+                if (clear_texture)
+                    target_texture->image_buffer[pixel_index] = 0xFF333333;
+                ri.z_buffer[pixel_index] = 999999999;
+           }
+
 
         for (int face_index = 0; face_index < current_renderable_face_index * 2; ++face_index){
             RI_renderable_face *current_face = &scene->faces_to_render[face_index];

BIN
textures/THIS IS THE WALL.png