Browse Source

added deltatime example to example scene

Iver 4 months ago
parent
commit
5f68fe7caa

+ 3 - 1
.vscode/settings.json

@@ -10,7 +10,9 @@
         "utility": "c",
         "compare": "c",
         "rasteriver.h": "c",
-        "custom_types.h": "c"
+        "custom_types.h": "c",
+        "time.h": "c",
+        "sourparse.h": "c"
     },
     "OpenCL.explorer.localizedProperties": false,
     "C_Cpp.default.compilerPath": "/bin/gcc"

+ 1 - 1
Makefile

@@ -1,7 +1,7 @@
 COMPILER=gcc
 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
+FLAGS_LIB=-D CL_TARGET_OPENCL_VERSION=120 -fPIC -shared -lc -lSDL2 -lm -lOpenCL
 
 main.bin: clean rasteriver.so
 	cp compiled_libs/librasteriver.so build/librasteriver.so

BIN
build/librasteriver.so


BIN
build/main.bin


BIN
compiled_libs/librasteriver.so


+ 3 - 1
readme.md

@@ -61,4 +61,6 @@ To run the binary, it needs to be in the same folder as librasteriver.so (if you
 - [ ] Documentation
 - [x] text rasterizer for UI [NOTE: its for any texture in general]
 - [ ] texture masking; greyscale texture is used to filter out parts of another image. All white = keep the pixel, all black = make it transparent
-- [x] add request empty texture
+- [x] add request empty texture
+- [ ] shaders. User makes a function for a pixel on a tri (passes a function as a pointer) and returns 1/0 whether to keep or ignore the pixel
+- [ ] add batch/blit object copying

+ 1 - 2
src/headers/custom_types.h

@@ -74,13 +74,12 @@ typedef struct {
     uint64_t flags;
     RI_vector_2f uv_loop_multiplier;
     RI_vector_2f texture_render_size;
+    int (*shader_function_pointer) (int pixel_x, int pixel_y, RI_vector_3f position, RI_vector_3f normal, RI_vector_2f uv, uint32_t color);
 } RI_material;
 
 typedef struct { // An entity that has an mesh, transform, materials, etc
     RI_mesh *mesh_reference;
     RI_material *material_reference;
-    RI_vector_3f *transformed_vertex_positions;
-    RI_vector_3f *transformed_normals;
     RI_transform transform;
 } RI_actor;
 

+ 3 - 1
src/headers/functions.h

@@ -14,7 +14,9 @@ int RI_stop(int result); // Stop RasterIver safely and free memory
 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();
+// Tick RasterIver.
+// Copies the window's texture (ri.frame_buffer.image_buffer) onto the screen, calls SDL_RenderPresent, handles SDL events, increases ri.frame by 1, and optionally clears the window's texture
+void RI_tick(int clear_window_texture_after_rendering); 
 void RI_render_text(SP_font *font, RI_texture *target_texture, RI_vector_2f position, uint32_t color, int bezier_resolution, float size, char *text);
 void RI_draw_line(RI_texture *target_texture, RI_vector_2 point_a, RI_vector_2 point_b, uint32_t color);
 RI_vector_2f v2_to_2f(RI_vector_2 v);

+ 11 - 0
src/headers/rasteriver.h

@@ -7,6 +7,7 @@
 #include "math.h"
 #include "sourparse.h"
 #include <SDL2/SDL.h>
+#include <CL/cl.h>
 
 typedef struct {
     // rendering (non SDL)
@@ -53,6 +54,16 @@ typedef struct {
     int allocation_search_limit;
     int current_allocation_index;
     int allocation_table_length;
+
+    // OpenCL
+    cl_platform_id cl_platform;
+    cl_uint cl_number_of_platforms;
+    
+    cl_device_id cl_device;
+    cl_uint cl_number_of_devices;
+
+    cl_context cl_context;
+    cl_command_queue cl_command_queue;
 } RasterIver;
 
 RasterIver* RI_get_ri();

+ 41 - 10
src/launch_program/main.c

@@ -1,4 +1,10 @@
 #include "../headers/rasteriver.h"
+#include <time.h>
+
+int shader_function(int pixel_x, int pixel_y, RI_vector_3f position, RI_vector_3f normal, RI_vector_2f uv, uint32_t color){
+    if (uv.x > 0.5) return 1;
+    else return 0;
+}
 
 int main(){
     SP_font *comic_sans = SP_load_font("fonts/ComicSans-Regular.ttf");
@@ -13,8 +19,6 @@ int main(){
 
     RI_init(700, 700, "This is RasterIver 2.0!!");
 
-    int running = 1;
-
     // data for loading files
     char *filenames[] = {"objects/unit_plane.obj"};
 
@@ -34,14 +38,18 @@ 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;
 
     // actors
     RI_actor* text_plane = &actors[0];
     text_plane->material_reference = text_plane_material;
     text_plane->mesh_reference = text_plane_mesh;
-    text_plane->transform.scale = (RI_vector_3f){50, 50, 50};
-    text_plane->transform.position = (RI_vector_3f){100, 50, 400};
+    text_plane->transform.scale = (RI_vector_3f){100, 100, 100};
+    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});
+
+    RI_vector_4f rotation_delta;
 
     RI_add_actors_to_scene(1, actors, scene);
 
@@ -61,18 +69,41 @@ int main(){
     RI_render_text(cal_sans, text_plane_material->texture_reference, (RI_vector_2f){0, 200}, 0xFFFFFFFF, 2, 80, "Wow!!");
     RI_render_text(QwitcherGrypen, text_plane_material->texture_reference, (RI_vector_2f){0, 300}, 0xFFFFFFFF, 2, 80, "WOW!!!!");    
 
-    while (running){
-        RI_euler_rotation_to_quaternion(&text_plane->transform.rotation, (RI_vector_3f){-1.5, ri->frame / 10.0, 0});
+    long int start, end;
+    char *fps_string = (char *)malloc(64 * sizeof(char));
+    double fps = 0;
+
+    double delta_time = 0;
+    double delta_min = 0.00001;
+    double delta_max = 0.1;
+
+    fps_string[0] = '\0';
+
+    while (ri->running){
+        start = clock();
+
+        RI_euler_rotation_to_quaternion(&rotation_delta, (RI_vector_3f){2.0 * delta_time, 2.0 * delta_time, 2.0 * delta_time});
+
+        quaternion_multiply(&text_plane->transform.rotation, rotation_delta);
 
         RI_render(scene, ri->frame_buffer, 1);
+
+        snprintf(fps_string, 64, "%.2f", fps);
+        RI_render_text(cal_sans, ri->frame_buffer, (RI_vector_2f){0, 670}, 0xFFFFFFFF, 2, 20, fps_string);
         
-        RI_render_text(comic_sans, ri->frame_buffer, (RI_vector_2f){00, 250}, 0xFFFFFFFF, 2, 80, "I love Comic Sans");
-        RI_render_text(cal_sans, ri->frame_buffer, (RI_vector_2f){00, 350}, 0xFFFFFFFF, 2, 80, "I love Cal Sans");
-        RI_render_text(QwitcherGrypen, ri->frame_buffer, (RI_vector_2f){00, 450}, 0xFFFFFFFF, 2, 80, "I love Qwitcher Grypen");
+        snprintf(fps_string, 64, "%.6f", delta_time);
+        RI_render_text(cal_sans, ri->frame_buffer, (RI_vector_2f){0, 640}, 0xFFFFFFFF, 2, 20, fps_string);
 
-        RI_tick();
+        RI_tick(1);
+
+        end = clock();
+
+        delta_time = fmin(fmax(((double)(end - start) / (double)(CLOCKS_PER_SEC)), delta_min), delta_max);
+        fps = 1.0 / delta_time;
     }
 
+    RI_stop(0);
+
     SP_free_font(comic_sans);
     SP_free_font(cal_sans);
     SP_free_font(QwitcherGrypen);

+ 30 - 9
src/library/rasteriver.c

@@ -1,5 +1,5 @@
 #include <stdio.h>
-// #include <CL/cl.h>
+#include <CL/cl.h>
 #include <SDL2/SDL.h>
 #include "../headers/rasteriver.h"
 #define STB_IMAGE_IMPLEMENTATION
@@ -1091,10 +1091,6 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
                         continue;
                     }
                     
-                    if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_WRITE)){
-                        ri.z_buffer[y * target_texture->resolution.x + x] = interpolated_z;
-                    }
-                    
                     double alpha = 1;
 
                     if (!(scene->flags & RI_SCENE_DONT_USE_AA) || !(mat->flags & RI_MATERIAL_DONT_USE_AA)){
@@ -1160,6 +1156,10 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
                         else pixel_color = 0xFF777777;
                     }
 
+                    if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_WRITE)){
+                        ri.z_buffer[y * target_texture->resolution.x + x] = interpolated_z;
+                    }
+
                     if (x >= 0 && y >= 0 && x < target_texture->resolution.x && y < target_texture->resolution.y){
                         target_texture->image_buffer[y * target_texture->resolution.x + x] = pixel_color;
                     }
@@ -1175,7 +1175,7 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
     return 0;
 }
 
-void RI_tick(){
+void RI_tick(int clear_window_texture_after_rendering){
     SDL_UpdateTexture(ri.texture, NULL, ri.frame_buffer->image_buffer, ri.window_width * sizeof(uint32_t));
 
     SDL_RenderClear(ri.renderer);
@@ -1191,10 +1191,33 @@ void RI_tick(){
         }
     }
 
+    if (clear_window_texture_after_rendering){
+        RI_clear_texture(ri.frame_buffer);
+    }
+
     ++ri.frame;
 }
 
 int opencl_init(){
+    cl_int cl_result;
+
+    cl_result = clGetPlatformIDs(1, &ri.cl_platform, &ri.cl_number_of_platforms);
+
+    if (cl_result != CL_SUCCESS || ri.cl_number_of_platforms == 0) {
+        debug("[OpenCL Init] Error! No OpenCL platforms");
+        RI_stop(1);
+    } 
+    
+    cl_result = clGetDeviceIDs(ri.cl_platform, CL_DEVICE_TYPE_GPU, 1, &ri.cl_device, &ri.cl_number_of_devices);
+
+    if (cl_result != CL_SUCCESS || ri.cl_number_of_devices == 0) {
+        debug("[OpenCL Init] Error! No OpenCL devices");
+        RI_stop(1);
+    }
+    
+    ri.cl_context = clCreateContext(NULL, 1, &ri.cl_device, NULL, NULL, &cl_result);
+    ri.cl_command_queue = clCreateCommandQueue(ri.cl_context, ri.cl_device, 0, &cl_result);
+
     return 0;
 }
 
@@ -1294,8 +1317,6 @@ int RI_stop(int result){
         RI_free(ri.allocation_table);
     }
 
-    exit(result);
-
     return 0;
 }
 
@@ -1333,7 +1354,7 @@ int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title){
         ri.current_allocation_index++;
     }
 
-    opencl_init();
+    // opencl_init();
 
     sdl_init(RI_window_width, RI_window_height, RI_window_title);