diff --git a/tiny_bvh_fenster.cpp b/tiny_bvh_fenster.cpp index 8433ca1..449c58a 100644 --- a/tiny_bvh_fenster.cpp +++ b/tiny_bvh_fenster.cpp @@ -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 @@ -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; @@ -106,6 +113,12 @@ 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; @@ -113,10 +126,6 @@ void Init() 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 ) @@ -170,8 +179,13 @@ 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++) @@ -179,18 +193,29 @@ void Tick( float delta_time_s, fenster& f, uint32_t* buf ) 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() @@ -204,5 +229,7 @@ void Shutdown() // delete allocated buffers tinybvh::free64( rays ); +#ifdef COLOR_DEPTH tinybvh::free64( depths ); +#endif } \ No newline at end of file