Skip to content

Commit

Permalink
Cleaned renderer; preparing for time measurements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbikker committed Nov 2, 2024
1 parent f4a23b6 commit 0a253e5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 43 deletions.
14 changes: 14 additions & 0 deletions external/fenster.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <stdint.h>
#include <stdlib.h>
#include <chrono>

struct fenster {
const char *title;
Expand All @@ -43,6 +44,19 @@ struct fenster {
#endif
};

struct Timer
{
Timer() { reset(); }
float elapsed() const
{
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - start);
return (float)time_span.count();
}
void reset() { start = std::chrono::high_resolution_clock::now(); }
std::chrono::high_resolution_clock::time_point start;
};

#ifndef FENSTER_API
#define FENSTER_API extern
#endif
Expand Down
78 changes: 35 additions & 43 deletions tiny_bvh_fenster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,81 @@

#define TINYBVH_IMPLEMENTATION
#include "tiny_bvh.h"
#ifdef _MSC_VER
#include "stdlib.h" // for rand
#include "stdio.h" // for printf
#else
#include <cstdlib>
#include <cstdio>
#endif

using namespace tinybvh;

#define SPHERE_COUNT 259

struct TriVertex {
TriVertex() = default;
TriVertex( bvhvec3 v ) : x( v.x ), y( v.y ), z( v.z ), dummy( 0 ) {}
float x, y, z, dummy;
};
TriVertex triangles[SPHERE_COUNT * 6 * 2 * 49 * 3]{};
bvhvec4 triangles[259 /* level 3 */ * 6 * 2 * 49 * 3]{};
int verts = 0, spheres = 0;
BVH bvh;

void create_sphere( float x, float y, float z, float s )
void sphere_flake( float x, float y, float z, float s, int d = 0 )
{
// procedural tesselated sphere flake object
#define P(F,a,b,c) p[i+F*64]={(float)a ,(float)b,(float)c}
bvhvec3 p[384], pos( x, y, z ), ofs( 3.5 );
for (int i = 0, u = 0; u < 8; u++) for (int v = 0; v < 8; v++, i++)
P( 0, u, v, 0 ), P( 1, u, 0, v ), P( 2, 0, u, v ),
P( 3, u, v, 7 ), P( 4, u, 7, v ), P( 5, 7, u, v );
for (int i = 0; i < 384; i++) p[i] = normalize( p[i] - ofs ) * s + pos;
for ( int i = 0, side = 0; side < 6; side++, i += 8 )
for (int i = 0, side = 0; side < 6; side++, i += 8)
for (int u = 0; u < 7; u++, i++) for (int v = 0; v < 7; v++, i++)
triangles[verts++] = p[i], triangles[verts++] = p[i + 8],
triangles[verts++] = p[i + 1], triangles[verts++] = p[i + 1],
triangles[verts++] = p[i + 9], triangles[verts++] = p[i + 8];
}

void sphere_flake( float x, float y, float z, float s, int d = 0 )
{
spheres++;
create_sphere( x, y, z, s * 0.5f );
if (d < 3) sphere_flake( x + s * 0.75f, y, z, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x - s * 0.75f, y, z, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x, y + s * 0.75f, z, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x, x - s * 0.75f, z, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x, y, z + s * 0.75f, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x, y, z - s * 0.75f, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x + s * 1.55f, y, z, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x - s * 1.5f, y, z, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x, y + s * 1.5f, z, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x, x - s * 1.5f, z, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x, y, z + s * 1.5f, s * 0.5f, d + 1 );
if (d < 3) sphere_flake( x, y, z - s * 1.5f, s * 0.5f, d + 1 );
}

void Init()
{
// generate a sphere flake scene
sphere_flake( 0, 0, 0, 3 );
sphere_flake( 0, 0, 0, 1.5f );

// build a BVH over the scene
bvh.Build( (bvhvec4*)triangles, verts / 3 );
}

void Tick( uint32_t* buf )
{
// trace primary rays
// setup camera
bvhvec3 eye( -3.5f, -1.5f, -6.5f ), view = normalize( bvhvec3( 3, 1.5f, 5 ) );
bvhvec3 right = normalize( cross( bvhvec3( 0, 1, 0 ), view ) );
bvhvec3 up = 0.8f * cross( view, right ), C = eye + 2 * view;
bvhvec3 p1 = C - right + up, p2 = C + right + up, p3 = C - right - up;
static int s, x, y = -1;
if (y < SCRHEIGHT - 1) for( y++, x = 0; x < SCRWIDTH; x++ )
// store primary rays in a buffer
int N = 0;
Ray* rays = new Ray[SCRWIDTH * SCRHEIGHT * 16];
for (int y = 0; y < SCRHEIGHT; y++) for (int x = 0; x < SCRWIDTH; x++)
{
for (int s = 0; s < 16; s++)
{
float u = (float)(x * 4 + (s & 3)) / (SCRWIDTH * 4);
float v = (float)(y * 4 + (s >> 2)) / (SCRHEIGHT * 4);
bvhvec3 P = p1 + u * (p2 - p1) + v * (p3 - p1);
rays[N++] = Ray( eye, normalize( P - eye ) );
}
}
// trace rays
for (int i = 0; i < N; i++) bvh.Intersect( rays[i] );
// visualize result
for (int i = 0, y = 0; y < SCRHEIGHT; y++) for (int x = 0; x < SCRWIDTH; x++)
{
float u = (float)x / SCRWIDTH;
float v = (float)y / SCRHEIGHT;
bvhvec3 P = p1 + u * (p2 - p1) + v * (p3 - p1);
Ray ray( eye, normalize( P - eye ) );
bvh.Intersect( ray );
int c = 0;
if (ray.hit.t < 1000)
float avg = 0;
for (int s = 0; s < 16; s++, i++) if (rays[i].hit.t < 1000)
{
int primIdx = ray.hit.prim;
int primIdx = rays[i].hit.prim;
bvhvec3 v0 = *(bvhvec3*)&triangles[primIdx * 3 + 0];
bvhvec3 v1 = *(bvhvec3*)&triangles[primIdx * 3 + 1];
bvhvec3 v2 = *(bvhvec3*)&triangles[primIdx * 3 + 2];
bvhvec3 N = normalize( cross( v1 - v0, v2 - v0 ) );
c = (int)( 200.0f * fabs( dot( N, normalize( bvhvec3( 1, 2, 3 ) ) ) ) ) + 55;
avg += fabs( dot( N, normalize( bvhvec3( 1, 2, 3 ) ) ) );
}
buf[x + y * SCRWIDTH] = c + (c << 8) + (c << 16);
int c = (int)(15.9f * avg);
buf[x + y * SCRWIDTH] = c + (c << 8) + (c << 16);
}
else y = 0;
}

0 comments on commit 0a253e5

Please sign in to comment.