-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrenderer.cpp
61 lines (57 loc) · 2.05 KB
/
renderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "template.h"
// -----------------------------------------------------------
// Calculate light transport via a ray
// -----------------------------------------------------------
float3 Renderer::Trace( Ray& ray, int, int, int /* we'll use these later */ )
{
scene.FindNearest( ray );
if (ray.voxel == 0) return float3( 0 ); // or a fancy sky color
float3 N = ray.GetNormal();
float3 I = ray.IntersectionPoint();
float3 albedo = ray.GetAlbedo();
return (N + 1) * 0.5f;
}
// -----------------------------------------------------------
// Application initialization - Executed once, at app start
// -----------------------------------------------------------
void Renderer::Init()
{
}
// -----------------------------------------------------------
// Main application tick function - Executed every frame
// -----------------------------------------------------------
void Renderer::Tick( float deltaTime )
{
// high-resolution timer, see template.h
Timer t;
// pixel loop: lines are executed as OpenMP parallel tasks (disabled in DEBUG)
#pragma omp parallel for schedule(dynamic)
for (int y = 0; y < SCRHEIGHT; y++)
{
// trace a primary ray for each pixel on the line
for (int x = 0; x < SCRWIDTH; x++)
{
Ray r = camera.GetPrimaryRay( (float)x, (float)y );
float3 pixel = Trace( r );
screen->pixels[x + y * SCRWIDTH] = RGBF32_to_RGB8( pixel );
}
}
// performance report - running average - ms, MRays/s
static float avg = 10, alpha = 1;
avg = (1 - alpha) * avg + alpha * t.elapsed() * 1000;
if (alpha > 0.05f) alpha *= 0.5f;
float fps = 1000.0f / avg, rps = (SCRWIDTH * SCRHEIGHT) / avg;
printf( "%5.2fms (%.1ffps) - %.1fMrays/s\n", avg, fps, rps / 1000 );
// handle user input
camera.HandleInput( deltaTime );
}
// -----------------------------------------------------------
// Update user interface (imgui)
// -----------------------------------------------------------
void Renderer::UI()
{
// ray query on mouse
Ray r = camera.GetPrimaryRay( (float)mousePos.x, (float)mousePos.y );
scene.FindNearest( r );
ImGui::Text( "voxel: %i", r.voxel );
}