Browse Source

added scene flags and scene debug modes

IverMartinson 5 months ago
parent
commit
4fa22232dd
6 changed files with 87 additions and 9 deletions
  1. BIN
      build/librasteriver.so
  2. BIN
      build/main.bin
  3. 4 1
      readme.md
  4. 15 0
      src/headers/custom_types.h
  5. 3 0
      src/launch_program/main.c
  6. 65 8
      src/library/rasteriver.c

BIN
build/librasteriver.so


BIN
build/main.bin


+ 4 - 1
readme.md

@@ -56,4 +56,7 @@ To run the binary, it needs to be in the same folder as librasteriver.so (if you
 - [ ] bump maps
 - [ ] bump maps
 - [ ] normal maps
 - [ ] normal maps
 - [x] memory tracker
 - [x] memory tracker
-- [x] tileable textures
+- [x] tileable textures
+- [ ] UI elements (just images? text? custom text rasterizer? buttons?)
+- [ ] Documentation
+- [ ] text rasterizer for UI

+ 15 - 0
src/headers/custom_types.h

@@ -62,6 +62,7 @@ typedef enum {
     RI_MATERIAL_DOUBLE_SIDED = ((uint64_t)1 << 10), // ignore backface culling
     RI_MATERIAL_DOUBLE_SIDED = ((uint64_t)1 << 10), // ignore backface culling
     RI_MATERIAL_USE_UV_LOOP_MULTIPLIER = ((uint64_t)1 << 11), // use multiplier that tells how many times to loop the texture (scaling also scales texture)
     RI_MATERIAL_USE_UV_LOOP_MULTIPLIER = ((uint64_t)1 << 11), // use multiplier that tells how many times to loop the texture (scaling also scales texture)
     RI_MATERIAL_USE_UV_RENDER_RESOLUTION = ((uint64_t)1 << 12), // use custom resolution that textures always appear as (scaling doesn't change texture)
     RI_MATERIAL_USE_UV_RENDER_RESOLUTION = ((uint64_t)1 << 12), // use custom resolution that textures always appear as (scaling doesn't change texture)
+    RI_MATERIAL_DONT_USE_AA = ((uint64_t)1 << 13), // disable anti-aliasing
 } RI_material_flags;
 } RI_material_flags;
 
 
 typedef struct {
 typedef struct {
@@ -97,8 +98,20 @@ typedef struct {
     int min_screen_x, max_screen_x, min_screen_y, max_screen_y;
     int min_screen_x, max_screen_x, min_screen_y, max_screen_y;
     int should_render;
     int should_render;
     RI_actor* parent_actor;
     RI_actor* parent_actor;
+    int shrunk;
+    int split;
 } RI_renderable_face;
 } RI_renderable_face;
 
 
+typedef enum {
+    RI_SCENE_UNLIT = ((uint64_t)1 << 0), // should calculate lighting
+    RI_SCENE_WIREFRAME = ((uint64_t)1 << 1), // render as wireframe
+    RI_SCENE_DOUBLE_SIDED = ((uint64_t)1 << 2), // ignore backface culling
+    RI_SCENE_DONT_USE_AA = ((uint64_t)1 << 3), // disable anti-aliasing
+    RI_SCENE_DEBUG_CULLS = ((uint64_t)1 << 4), // unchanged tris in grey, shrunk tris in blue, split triangles in green (old tri) and red (new tri)
+    RI_SCENE_DEBUG_AABB = ((uint64_t)1 << 5), // AABB of polygons
+    RI_SCENE_DEBUG_OVERDRAW = ((uint64_t)1 << 6), // overdraw
+} RI_scene_flags;
+
 typedef struct {
 typedef struct {
     RI_actor **actors;
     RI_actor **actors;
     int actor_count;
     int actor_count;
@@ -109,6 +122,8 @@ typedef struct {
     RI_vector_4f camera_rotation;
     RI_vector_4f camera_rotation;
     RI_renderable_face *faces_to_render;
     RI_renderable_face *faces_to_render;
     int face_count;
     int face_count;
+    int antialiasing_subsample_resolution;
+    uint64_t flags;
 } RI_scene;
 } RI_scene;
 
 
 // ----- Memory Manager -----
 // ----- Memory Manager -----

+ 3 - 0
src/launch_program/main.c

@@ -94,6 +94,9 @@ int main(){
 
 
     double y_rotation = 0;
     double y_rotation = 0;
 
 
+    scene->antialiasing_subsample_resolution = 8;
+    scene->flags = RI_SCENE_DONT_USE_AA;
+
     while (running){
     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){sin(ri->frame * 0.1) * 50 - 100, sin(ri->frame * 0.2 + 0.4) * 50, sin(ri->frame * 0.1) * 10 + 200};
 
 

+ 65 - 8
src/library/rasteriver.c

@@ -186,7 +186,8 @@ RI_scene* RI_request_scenes(int RI_number_of_requested_scenes){
         new_scene.actor_count = 0;
         new_scene.actor_count = 0;
         new_scene.actors = NULL;
         new_scene.actors = NULL;
         new_scene.faces_to_render = NULL;
         new_scene.faces_to_render = NULL;
-        
+        new_scene.antialiasing_subsample_resolution = 4;
+
         ri.scenes[i + previous_scene_count] = new_scene;
         ri.scenes[i + previous_scene_count] = new_scene;
     }
     }
 
 
@@ -470,6 +471,19 @@ double mod(double a, double b){
     return ret;
     return ret;
 }
 }
 
 
+uint32_t multiply_rgb(uint32_t color, float factor) {
+    uint8_t a = (color >> 24) & 0xFF;
+    uint8_t r = (color >> 16) & 0xFF;
+    uint8_t g = (color >> 8)  & 0xFF;
+    uint8_t b =  color        & 0xFF;
+
+    r = (uint8_t)fminf(fmaxf(r * factor, 0.0f), 255.0f);
+    g = (uint8_t)fminf(fmaxf(g * factor, 0.0f), 255.0f);
+    b = (uint8_t)fminf(fmaxf(b * factor, 0.0f), 255.0f);
+
+    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){
     // do rendering stuff
     // do rendering stuff
     if (ri.running){
     if (ri.running){
@@ -519,6 +533,8 @@ int RI_render(RI_scene *scene, RI_texture *target_texture){
                 RI_renderable_face *cur_r_face = &scene->faces_to_render[current_renderable_face_index];
                 RI_renderable_face *cur_r_face = &scene->faces_to_render[current_renderable_face_index];
 
 
                 cur_r_face->parent_actor = current_actor;
                 cur_r_face->parent_actor = current_actor;
+                cur_r_face->shrunk = 0;
+                cur_r_face->split = 0;
 
 
                 cur_r_face->material_reference = current_actor->material_reference;
                 cur_r_face->material_reference = current_actor->material_reference;
 
 
@@ -635,6 +651,8 @@ int RI_render(RI_scene *scene, RI_texture *target_texture){
                         vector_2f_lerp(*unclipped_uv, *uv_a, uv_a, fraction_a_to_unclip);
                         vector_2f_lerp(*unclipped_uv, *uv_a, uv_a, fraction_a_to_unclip);
                         vector_2f_lerp(*unclipped_uv, *uv_b, uv_b, fraction_b_to_unclip);
                         vector_2f_lerp(*unclipped_uv, *uv_b, uv_b, fraction_b_to_unclip);
 
 
+                        cur_r_face->shrunk = 1;     
+
                         break;}
                         break;}
 
 
                     case 1: // split polygon
                     case 1: // split polygon
@@ -642,6 +660,8 @@ int RI_render(RI_scene *scene, RI_texture *target_texture){
                         RI_vector_3f clipped_normal, normal_a, normal_b;
                         RI_vector_3f clipped_normal, normal_a, normal_b;
                         RI_vector_2f clipped_uv, uv_a, uv_b;
                         RI_vector_2f clipped_uv, uv_a, uv_b;
 
 
+                cur_r_face->split = 1;
+
                         if (is_0_clipped){ 
                         if (is_0_clipped){ 
                             clipped_point = cur_r_face->position_0;
                             clipped_point = cur_r_face->position_0;
                             point_a = cur_r_face->position_1;
                             point_a = cur_r_face->position_1;
@@ -857,26 +877,55 @@ int RI_render(RI_scene *scene, RI_texture *target_texture){
                     w1 = ((pos_2->y - pos_0->y) * (pixel_x_index - pos_0->x) + (pos_0->x - pos_2->x) * (pixel_y_index - pos_0->y)) / denominator; 
                     w1 = ((pos_2->y - pos_0->y) * (pixel_x_index - pos_0->x) + (pos_0->x - pos_2->x) * (pixel_y_index - pos_0->y)) / denominator; 
                     w2 = 1.0 - w0 - w1; 
                     w2 = 1.0 - w0 - w1; 
                 
                 
-                    if (!(mat->flags & RI_MATERIAL_DOUBLE_SIDED) && denominator > 0){
+                    if (!(mat->flags & RI_MATERIAL_DOUBLE_SIDED || scene->flags & RI_SCENE_DOUBLE_SIDED) && denominator > 0){
                         continue;
                         continue;
                     }
                     }
 
 
                     double w_over_z = (w0 / pos_0->z + w1 / pos_1->z + w2 / pos_2->z); 
                     double w_over_z = (w0 / pos_0->z + w1 / pos_1->z + w2 / pos_2->z); 
                     double interpolated_z = 1.0 / w_over_z;
                     double interpolated_z = 1.0 / w_over_z;
-                
-                    if (!(w0 >= 0 && w1 >= 0 && w2 >= 0) || (mat->flags & RI_MATERIAL_WIREFRAME && (w0 >= mat->wireframe_width && w1 >= mat->wireframe_width && w2 >= mat->wireframe_width))){
+
+                    if (scene->flags & RI_SCENE_DEBUG_AABB) {
+                        target_texture->image_buffer[y * target_texture->resolution.x + x] += 0x0F0F0707;
+                        continue;
+                    }
+
+                    if (!(w0 >= 0 && w1 >= 0 && w2 >= 0) || ((mat->flags & RI_MATERIAL_WIREFRAME || scene->flags & RI_SCENE_WIREFRAME) && (w0 >= mat->wireframe_width && w1 >= mat->wireframe_width && w2 >= mat->wireframe_width))){    
                         continue;
                         continue;
                     }
                     }
                     
                     
-                    if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_TEST) && interpolated_z >= ri.z_buffer[y * target_texture->resolution.x + x]){
+                    if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_TEST) && interpolated_z >= ri.z_buffer[y * target_texture->resolution.x + x]){                        
                         continue;
                         continue;
-                    }   
-                
+                    }
+
+                    if (scene->flags & RI_SCENE_DEBUG_OVERDRAW) {
+                        target_texture->image_buffer[y * target_texture->resolution.x + x] += 0x0F070F07;
+                        
+                        continue;
+                    }
+                    
                     if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_WRITE)){
                     if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_WRITE)){
                         ri.z_buffer[y * target_texture->resolution.x + x] = interpolated_z;
                         ri.z_buffer[y * target_texture->resolution.x + x] = interpolated_z;
                     }
                     }
                     
                     
-                    uint32_t pixel_color = 0xFF000000;
+                    double alpha = 1;
+
+                    if (!(scene->flags & RI_SCENE_DONT_USE_AA) || !(mat->flags & RI_MATERIAL_DONT_USE_AA)){
+                        float total_inside = 0;
+                        
+                        for (float sub_y = 1.0 / (-scene->antialiasing_subsample_resolution / 2.0) - 0.5; sub_y < 1.0 / (scene->antialiasing_subsample_resolution / 2.0) - 0.5; sub_y += 1.0 / (scene->antialiasing_subsample_resolution / 2.0)){
+                            for (float sub_x = 1.0 / (-scene->antialiasing_subsample_resolution / 2.0) - 0.5; sub_x < 1.0 / (scene->antialiasing_subsample_resolution / 2.0) - 0.5; sub_x += 1.0 / (scene->antialiasing_subsample_resolution / 2.0)){
+                                w0 = ((pos_1->y - pos_2->y) * (pixel_x_index + sub_x - pos_2->x) + (pos_2->x - pos_1->x) * (pixel_y_index + sub_y - pos_2->y)) / denominator;
+                                w1 = ((pos_2->y - pos_0->y) * (pixel_x_index + sub_x - pos_0->x) + (pos_0->x - pos_2->x) * (pixel_y_index + sub_y - pos_0->y)) / denominator; 
+                                w2 = 1.0 - w0 - w1;
+                                
+                                if(!(w0 >= 0 && w1 >= 0 && w2 >= 0)) total_inside++;
+                            }
+                        }
+                        
+                        alpha = 1.0 - total_inside / (scene->antialiasing_subsample_resolution * scene->antialiasing_subsample_resolution);
+                    }
+                    
+                    uint32_t pixel_color = 0x00000000;
                     
                     
                     if (mat->flags & RI_MATERIAL_HAS_TEXTURE && uv_0 && uv_1 && uv_2){                
                     if (mat->flags & RI_MATERIAL_HAS_TEXTURE && uv_0 && uv_1 && uv_2){                
                         double ux = (w0 * (uv_0->x / pos_0->z) + w1 * (uv_1->x / pos_1->z) + w2 * (uv_2->x / pos_2->z)) / w_over_z;
                         double ux = (w0 * (uv_0->x / pos_0->z) + w1 * (uv_1->x / pos_1->z) + w2 * (uv_2->x / pos_2->z)) / w_over_z;
@@ -915,6 +964,14 @@ int RI_render(RI_scene *scene, RI_texture *target_texture){
                     // x = target_texture->resolution.x - 1 - x;
                     // x = target_texture->resolution.x - 1 - x;
                     // y = target_texture->resolution.y - 1 - y;
                     // y = target_texture->resolution.y - 1 - y;
                 
                 
+
+                    if (scene->flags & RI_SCENE_DEBUG_CULLS){ // show unchanged tris in grey, shrunk tris in blue, split triangles in green (old tri) and red (new tri)
+                        if (current_face->shrunk) pixel_color = 0xFF7777FF;
+                        else if (current_face->split) pixel_color = 0xFF77FF77;
+                        else if (face_index >= current_renderable_face_index) pixel_color = 0xFFFF7777;
+                        else pixel_color = 0xFF777777;
+                    }
+
                     if (x >= 0 && y >= 0 && x < target_texture->resolution.x && y < target_texture->resolution.y){
                     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;
                         target_texture->image_buffer[y * target_texture->resolution.x + x] = pixel_color;
                     }
                     }