Skip to content

Commit

Permalink
Setting up new anim demo project.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbikker committed Jan 10, 2025
1 parent 5346e17 commit b26f1c9
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 8 deletions.
82 changes: 81 additions & 1 deletion tiny_bvh_anim.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
// TODO
#define FENSTER_APP_IMPLEMENTATION
#define SCRWIDTH 800
#define SCRHEIGHT 600
#include "external/fenster.h" // https://github.com/zserge/fenster

#define TINYBVH_IMPLEMENTATION
#include "tiny_bvh.h"
#include <fstream>

using namespace tinybvh;

BVH bvh;
int frameIdx = 0, verts = 0;
bvhvec4* triangles = 0;

// setup view pyramid for a pinhole camera
static bvhvec3 eye( -15.24f, 21.5f, 2.54f ), p1, p2, p3;
static bvhvec3 view = normalize( bvhvec3( 0.826f, -0.438f, -0.356f ) );

void Init()
{
// load raw vertex data for Crytek's Sponza
std::fstream s{ "./testdata/cryteksponza.bin", s.binary | s.in };
s.read( (char*)&verts, 4 );
printf( "Loading triangle data (%i tris).\n", verts );
verts *= 3, triangles = (bvhvec4*)malloc64( verts * 16 );
s.read( (char*)triangles, verts * 16 );
bvh.Build( triangles, verts / 3 );
}

bool UpdateCamera( float delta_time_s, fenster& f )
{
bvhvec3 right = normalize( cross( bvhvec3( 0, 1, 0 ), view ) ), up = 0.8f * cross( view, right );
float moved = 0, spd = 10.0f * delta_time_s;
if (f.keys['A'] || f.keys['D']) eye += right * (f.keys['D'] ? spd : -spd), moved = 1;
if (f.keys['W'] || f.keys['S']) eye += view * (f.keys['W'] ? spd : -spd), moved = 1;
if (f.keys['R'] || f.keys['F']) eye += up * 2.0f * (f.keys['R'] ? spd : -spd), moved = 1;
if (f.keys[20]) view = normalize( view + right * -0.1f * spd ), moved = 1;
if (f.keys[19]) view = normalize( view + right * 0.1f * spd ), moved = 1;
if (f.keys[17]) view = normalize( view + up * -0.1f * spd ), moved = 1;
if (f.keys[18]) view = normalize( view + up * 0.1f * spd ), moved = 1;
// recalculate right, up
right = normalize( cross( bvhvec3( 0, 1, 0 ), view ) ), up = 0.8f * cross( view, right );
bvhvec3 C = eye + 1.2f * view;
p1 = C - right + up, p2 = C + right + up, p3 = C - right - up;
return moved > 0;
}

void Tick( float delta_time_s, fenster& f, uint32_t* buf )
{
// handle user input and update camera
bool moved = UpdateCamera( delta_time_s, f ) || frameIdx++ == 0;

// clear the screen with a debug-friendly color
for (int i = 0; i < SCRWIDTH * SCRHEIGHT; i++) buf[i] = 0xaaaaff;

// trace rays
const bvhvec3 L = normalize( bvhvec3( 1, 2, 3 ) );
for (int 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++)
{
float u = (float)(tx * 4 + x) / SCRWIDTH, v = (float)(ty * 4 + y) / SCRHEIGHT;
bvhvec3 D = normalize( p1 + u * (p2 - p1) + v * (p3 - p1) - eye );
Ray ray( eye, D, 1e30f );
bvh.Intersect( ray );
if (ray.hit.t < 10000)
{
int pixel_x = tx * 4 + x, pixel_y = ty * 4 + y, primIdx = ray.hit.prim;
bvhvec3 v0 = triangles[primIdx * 3];
bvhvec3 v1 = triangles[primIdx * 3 + 1];
bvhvec3 v2 = triangles[primIdx * 3 + 2];
bvhvec3 N = normalize( cross( v1 - v0, v2 - v0 ) );
int c = (int)(255.9f * fabs( dot( N, L ) ));
buf[pixel_x + pixel_y * SCRWIDTH] = c + (c << 8) + (c << 16);
}
}
}
}

void Shutdown() { /* nothing here. */ }
10 changes: 10 additions & 0 deletions tiny_bvh_test.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tiny_bvh_gpu", "vcproj\tiny
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tiny_bvh_custom", "vcproj\tiny_bvh_custom.vcxproj", "{05E87951-A43C-49D9-BC3B-5F6387285D2E}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tiny_bvh_anim", "vcproj\tiny_bvh_anim.vcxproj", "{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -81,6 +83,14 @@ Global
{05E87951-A43C-49D9-BC3B-5F6387285D2E}.Release|x64.Build.0 = Release|x64
{05E87951-A43C-49D9-BC3B-5F6387285D2E}.Release|x86.ActiveCfg = Release|Win32
{05E87951-A43C-49D9-BC3B-5F6387285D2E}.Release|x86.Build.0 = Release|Win32
{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}.Debug|x64.ActiveCfg = Debug|x64
{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}.Debug|x64.Build.0 = Debug|x64
{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}.Debug|x86.ActiveCfg = Debug|Win32
{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}.Debug|x86.Build.0 = Debug|Win32
{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}.Release|x64.ActiveCfg = Release|x64
{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}.Release|x64.Build.0 = Release|x64
{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}.Release|x86.ActiveCfg = Release|Win32
{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 3 additions & 7 deletions vcproj/tiny_bvh_anim.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{2DC14EA1-D6E9-45F1-A6CC-6E0A0BAD8A9B}</ProjectGuid>
<ProjectGuid>{66E868CC-64CC-4A18-B2E3-9BD0BA5FEB14}</ProjectGuid>
<RootNamespace>tinybvhanim</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
Expand Down Expand Up @@ -116,7 +116,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>
Expand All @@ -135,15 +135,11 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions); _CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
<Optimization>Full</Optimization>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<EnableEnhancedInstructionSet>AdvancedVectorExtensions</EnableEnhancedInstructionSet>
<FloatingPointModel>Fast</FloatingPointModel>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
9 changes: 9 additions & 0 deletions vcproj/tiny_bvh_anim.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="..\tiny_bvh_anim.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\tiny_bvh.h" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions vcproj/tiny_bvh_anim.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>

0 comments on commit b26f1c9

Please sign in to comment.