fixed missing last tile, and changed bvh node to binary
authorJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 21 Jun 2021 20:07:12 +0000 (23:07 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 21 Jun 2021 20:07:12 +0000 (23:07 +0300)
src/geom.c
src/geom.h
src/level.c
src/rt.c

index 3751286..ef15156 100644 (file)
@@ -7,13 +7,8 @@ void free_bvh_tree(struct bvhnode *tree)
 
        free(tree->faces);
 
-       node = tree->sub;
-       while(node) {
-               tmp = node;
-               node = node->next;
-               free_bvh_tree(tmp);
-       }
-
+       free_bvh_tree(tree->left);
+       free_bvh_tree(tree->right);
        free(tree);
 }
 
index 50784a4..7f71094 100644 (file)
@@ -26,9 +26,7 @@ struct bvhnode {
        struct triangle *faces;
        int num_faces;
 
-       struct bvhnode *next;
-       struct bvhnode *sub;
-       int num_sub;
+       struct bvhnode *left, *right;
 };
 
 struct rayhit {
index fcdc405..21a82f9 100644 (file)
@@ -119,6 +119,8 @@ static void draw_level_rec(struct bvhnode *bn)
        struct material *curmtl;
        float color[4] = {0, 0, 0, 1};
 
+       if(!bn) return;
+
        if(bn->faces) {
                tri = bn->faces;
                curmtl = tri->mtl;
@@ -145,11 +147,8 @@ static void draw_level_rec(struct bvhnode *bn)
                glEnd();
        }
 
-       bn = bn->sub;
-       while(bn) {
-               draw_level_rec(bn);
-               bn = bn->next;
-       }
+       draw_level_rec(bn->left);
+       draw_level_rec(bn->right);
 }
 
 void draw_level(struct level *lvl)
index 18b7f17..020c51f 100644 (file)
--- a/src/rt.c
+++ b/src/rt.c
@@ -33,8 +33,8 @@ int fbsize(int width, int height)
        if(!(fbptr = malloc(width * height * sizeof *fb.pixels))) {
                return -1;
        }
-       xtiles = width / TILESZ;
-       ytiles = height / TILESZ;
+       xtiles = (width + TILESZ - 1) / TILESZ;
+       ytiles = (height + TILESZ - 1) / TILESZ;
        if(!(tileptr = malloc(xtiles * ytiles * sizeof *tiles))) {
                free(fbptr);
                return -1;