Skip to content

Commit

Permalink
Ensure the arrays are allocated before checking the camera position i…
Browse files Browse the repository at this point in the history
…s loadable (early return causes unallocated memory access)

Added defines to select which "shading style" to use
Only allocate the depth array when needed
  • Loading branch information
TheSFReader committed Jan 26, 2025
1 parent 2ab8b00 commit e82fe70
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions tiny_bvh_fenster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#define SCRHEIGHT 600
#include "external/fenster.h" // https://github.com/zserge/fenster

//#define COLOR_PRIM // compute color as hashed triangle Index
//#define COLOR_DEPTH // compute color as depth of intersection

#define LOADSCENE

#define TINYBVH_IMPLEMENTATION
Expand All @@ -14,7 +17,11 @@ using namespace tinybvh;
BVH bvh;
int frameIdx = 0;
Ray* rays = 0;
#ifdef COLOR_DEPTH
int* depths = 0;
#endif



#ifdef LOADSCENE
bvhvec4* vertices = 0;
Expand Down Expand Up @@ -106,17 +113,19 @@ void Init()
else
bvh.BuildHQ( vertices, verts / 3 );

// allocate buffers
rays = (Ray*)tinybvh::malloc64(SCRWIDTH * SCRHEIGHT * 16 * sizeof(Ray));
#ifdef COLOR_DEPTH
depths = (int*)tinybvh::malloc64(SCRWIDTH * SCRHEIGHT * sizeof(int));
#endif

// load camera position / direction from file
std::fstream t = std::fstream{ "camera.bin", t.binary | t.in };
if (!t.is_open()) return;
t.seekp( 0 );
t.read( (char*)&eye, sizeof( eye ) );
t.read( (char*)&view, sizeof( view ) );
t.close();

// allocate buffers
rays = (Ray*)tinybvh::malloc64( SCRWIDTH * SCRHEIGHT * 16 * sizeof( Ray ) );
depths = (int*)tinybvh::malloc64( SCRWIDTH * SCRHEIGHT * sizeof( int ) );
}

bool UpdateCamera( float delta_time_s, fenster& f )
Expand Down Expand Up @@ -170,27 +179,43 @@ void Tick( float delta_time_s, fenster& f, uint32_t* buf )
}

// trace primary rays
for (int i = 0; i < N; i++) depths[i] = bvh.Intersect( rays[i] );

for (int i = 0; i < N; i++) {
#ifdef COLOR_DEPTH
depths[i] = bvh.Intersect(rays[i]);
#else
bvh.Intersect(rays[i]);
#endif
}
// visualize result
const bvhvec3 L = tinybvh_normalize( bvhvec3( 1, 2, 3 ) );
for (int i = 0, ty = 0; ty < SCRHEIGHT / 4; ty++) for (int tx = 0; tx < SCRWIDTH / 4; tx++)
{
for (int y = 0; y < 4; y++) for (int x = 0; x < 4; x++, i++) if (rays[i].hit.t < 10000)
{
int pixel_x = tx * 4 + x, pixel_y = ty * 4 + y, primIdx = rays[i].hit.prim;


#ifdef COLOR_DEPTH
buf[pixel_x + pixel_y * SCRWIDTH] = depths[i] << 17; // render depth as red
#elif defined COLOR_PRIM
buf[pixel_x + pixel_y * SCRWIDTH] = (primIdx * 0xdeece66d + 0xb) & 0xFFFFFF; // color is hashed primitive index
#else
int v0idx = primIdx * 3, v1idx = v0idx + 1, v2idx = v0idx + 2;
if (inds) v0idx = indices[v0idx], v1idx = indices[v1idx], v2idx = indices[v2idx];
bvhvec3 v0 = vertices[v0idx];
bvhvec3 v1 = vertices[v1idx];
bvhvec3 v2 = vertices[v2idx];
bvhvec3 N = tinybvh_normalize( tinybvh_cross( v1 - v0, v2 - v0 ) );
int c = (int)(255.9f * fabs( tinybvh_dot( N, L ) ));
bvhvec3 N = tinybvh_normalize(tinybvh_cross(v1 - v0, v2 - v0));
int c = (int)(255.9f * fabs(tinybvh_dot(N, L)));
buf[pixel_x + pixel_y * SCRWIDTH] = c + (c << 8) + (c << 16);
// buf[pixel_x + pixel_y * SCRWIDTH] = (primIdx * 0xdeece66d + 0xb) & 0xFFFFFF; // color is hashed primitive index
// buf[pixel_x + pixel_y * SCRWIDTH] = depths[i] << 17; // render depth as red
#endif
}
}

// print frame time / rate in window title
char title[50];
sprintf(title, "tiny_bvh %.2f s %.2f Hz", delta_time_s, 1.0f / delta_time_s);
fenster_update_title(&f, title);
}

void Shutdown()
Expand All @@ -204,5 +229,7 @@ void Shutdown()

// delete allocated buffers
tinybvh::free64( rays );
#ifdef COLOR_DEPTH
tinybvh::free64( depths );
#endif
}

0 comments on commit e82fe70

Please sign in to comment.