<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Ray Tracing with Voxels in C++ Series &#8211; Part 3	</title>
	<atom:link href="https://jacco.ompf2.com/2024/05/08/ray-tracing-with-voxels-in-c-series-part-3/feed/" rel="self" type="application/rss+xml" />
	<link>https://jacco.ompf2.com/2024/05/08/ray-tracing-with-voxels-in-c-series-part-3/</link>
	<description>Links, Code &#38; Articles</description>
	<lastBuildDate>Wed, 29 May 2024 09:10:35 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>
		By: Lynn van Birgelen		</title>
		<link>https://jacco.ompf2.com/2024/05/08/ray-tracing-with-voxels-in-c-series-part-3/#comment-3532</link>

		<dc:creator><![CDATA[Lynn van Birgelen]]></dc:creator>
		<pubDate>Wed, 08 May 2024 16:23:32 +0000</pubDate>
		<guid isPermaLink="false">https://jacco.ompf2.com/?p=1803#comment-3532</guid>

					<description><![CDATA[There’s a slight optimisation possible for the accumulator (which avoids resetting the buffer).
Instead of storing the sum of the results of each frame, we instead store the final result of the previous frame. We still keep track off the amount of frames that the camera has been stationary for.
Then our code should look something like this, pretty straightforward:

&lt;code&gt;// We do this only once per frame, before tracing
float accumulatorWeight = spp / (spp + 1 );
float newSampleWeight = 1.0f - accumulatorWeight;
spp++;&lt;/code&gt;

&lt;code&gt;// Tracing
for (int y = 0; y &#060; SCRHEIGHT; y++) for (int x = 0; x &#060; SCRWIDTH; x++)
{
    Ray r = camera.GetPrimaryRay( x + RandomFloat(), y + RandomFloat() );
    float3 newSample = Trace( r );
    float3&#038; accumulatedPixel = accumulator[x + y * SCRWIDTH];
    // Find the average result (which will be our final pixel for this frame), could also use lerp()
    float3 finalPixel = newSample * newSampleWeight + accumulatedPixel * accumulationWeight;
    // Store in the accumulator buffer
    accumulatedPixel = finalPixel;
    // Set the pixel on the screen
    screen-&#062;pixels[x + y * SCRWIDTH] = RGBF32_to_RGB8( finalPixel );
}
&lt;/code&gt;

Now when we reset the accumulation buffer, we only need to reset the ‘spp’ variable. Note that we reset it to 0 instead of 1, because it should now match the amount of already accumulated frames (which is 0 after resetting):

&lt;code&gt;// Reset accumulation when moving
if (camera.HandleInput( deltaTime )) { spp = 0; }&lt;/code&gt;

Now why does this work? Well, since we’re calculating the accumulators weight at the start of every frame by doing spp/(spp+1), this will become 0/(0+1) = 0 on the first frame after resetting.
This means that only the new sample will be taken into account when calculating ‘finalPixel’, since it’s weight is 1-0 = 1.
This value is then set in the accumulation buffer, and ‘spp’ is incremented. The next frame the accumulation weight will be 1/(1+1) = 0.5f, which makes sense since, after adding a new sample that frame, there will have been 2 total samples (so the accumulation buffer contributes for 50% to the final colour).
I hope that is somewhat clear. Sorry if I made any mistakes with the code, I’m typing this on my phone. :P]]></description>
			<content:encoded><![CDATA[<p>There’s a slight optimisation possible for the accumulator (which avoids resetting the buffer).<br />
Instead of storing the sum of the results of each frame, we instead store the final result of the previous frame. We still keep track off the amount of frames that the camera has been stationary for.<br />
Then our code should look something like this, pretty straightforward:</p>
<p><code>// We do this only once per frame, before tracing<br />
float accumulatorWeight = spp / (spp + 1 );<br />
float newSampleWeight = 1.0f - accumulatorWeight;<br />
spp++;</code></p>
<p><code>// Tracing<br />
for (int y = 0; y &lt; SCRHEIGHT; y++) for (int x = 0; x &lt; SCRWIDTH; x++)<br />
{<br />
    Ray r = camera.GetPrimaryRay( x + RandomFloat(), y + RandomFloat() );<br />
    float3 newSample = Trace( r );<br />
    float3&amp; accumulatedPixel = accumulator[x + y * SCRWIDTH];<br />
    // Find the average result (which will be our final pixel for this frame), could also use lerp()<br />
    float3 finalPixel = newSample * newSampleWeight + accumulatedPixel * accumulationWeight;<br />
    // Store in the accumulator buffer<br />
    accumulatedPixel = finalPixel;<br />
    // Set the pixel on the screen<br />
    screen-&gt;pixels[x + y * SCRWIDTH] = RGBF32_to_RGB8( finalPixel );<br />
}<br />
</code></p>
<p>Now when we reset the accumulation buffer, we only need to reset the ‘spp’ variable. Note that we reset it to 0 instead of 1, because it should now match the amount of already accumulated frames (which is 0 after resetting):</p>
<p><code>// Reset accumulation when moving<br />
if (camera.HandleInput( deltaTime )) { spp = 0; }</code></p>
<p>Now why does this work? Well, since we’re calculating the accumulators weight at the start of every frame by doing spp/(spp+1), this will become 0/(0+1) = 0 on the first frame after resetting.<br />
This means that only the new sample will be taken into account when calculating ‘finalPixel’, since it’s weight is 1-0 = 1.<br />
This value is then set in the accumulation buffer, and ‘spp’ is incremented. The next frame the accumulation weight will be 1/(1+1) = 0.5f, which makes sense since, after adding a new sample that frame, there will have been 2 total samples (so the accumulation buffer contributes for 50% to the final colour).<br />
I hope that is somewhat clear. Sorry if I made any mistakes with the code, I’m typing this on my phone. 😛</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
