Browse Source

z buffer now reallocs to match resolution of target texture (if tex is bigger)

IverMartinson 5 months ago
parent
commit
e89c624f17
2 changed files with 6 additions and 0 deletions
  1. 1 0
      src/headers/rasteriver.h
  2. 5 0
      src/library/rasteriver.c

+ 1 - 0
src/headers/rasteriver.h

@@ -11,6 +11,7 @@ typedef struct {
     // rendering (non SDL)
     RI_texture *frame_buffer;
     double *z_buffer;
+    RI_vector_2 z_buffer_resolution;
  
     int window_width;
     int window_height;

+ 5 - 0
src/library/rasteriver.c

@@ -789,6 +789,10 @@ int RI_render(RI_scene *scene, RI_texture *target_texture){
             }
         }
 
+        if (ri.z_buffer_resolution.x * ri.z_buffer_resolution.y < target_texture->resolution.x * target_texture->resolution.y){
+            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;
@@ -933,6 +937,7 @@ int sdl_init(int RI_window_width, int RI_window_height, char *RI_window_title){
     ri.frame_buffer->resolution = (RI_vector_2){ri.window_width, ri.window_height};
     
     ri.z_buffer = RI_malloc(sizeof(double) * ri.window_width * ri.window_height);
+    ri.z_buffer_resolution = (RI_vector_2){ri.window_width, ri.window_height};
 
     SDL_Init(SDL_INIT_VIDEO);