From 9afd242121d0cacc1a9973818a3ebf8163d4ada3 Mon Sep 17 00:00:00 2001 From: Martin Opat Date: Sat, 11 Jan 2025 13:05:18 +0100 Subject: [PATCH] Added background colour to consts.h --- src/consts.cu | 3 +++ src/consts.h | 3 +++ src/illumination/Raycaster.cu | 14 +++++++------- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/consts.cu b/src/consts.cu index 8022c53..160720c 100644 --- a/src/consts.cu +++ b/src/consts.cu @@ -30,6 +30,7 @@ __device__ Point3 d_cameraPos; __device__ Vec3 d_cameraDir; __device__ Vec3 d_cameraUp; __device__ Point3 d_lightPos; +__device__ Color3 d_backgroundColor; // Point3 h_cameraPos = Point3::init(300.0f, 200.0f, -700.0f); // Camera for full data set Point3 h_cameraPos = Point3::init(50.0f, -50.0f, -75.0f); // Camera for partially trimmed data set (TODO: Probably upside down atm) @@ -37,6 +38,7 @@ Vec3 center = Vec3::init((float)VOLUME_WIDTH/2.0f, (float)VOLUME_HEIGHT/2.0f, (f Vec3 h_cameraDir = (center - h_cameraPos).normalize(); Vec3 h_cameraUp = Vec3::init(0.0, 0.0, 1.0).normalize(); Point3 h_lightPos = Point3::init(1.5, 2.0, -1.0); +Color3 h_backgroundColor = Color3::init(0.1f, 0.1f, 0.1f); // Copy the above values to the device @@ -52,4 +54,5 @@ void copyConstantsToDevice() { cudaMemcpyToSymbol(d_cameraDir, &h_cameraDir, sizeof(Vec3)); cudaMemcpyToSymbol(d_cameraUp, &h_cameraUp, sizeof(Vec3)); cudaMemcpyToSymbol(d_lightPos, &h_lightPos, sizeof(Point3)); + cudaMemcpyToSymbol(d_backgroundColor, &h_backgroundColor, sizeof(Color3)); } diff --git a/src/consts.h b/src/consts.h index 6164aad..ef8cce1 100644 --- a/src/consts.h +++ b/src/consts.h @@ -35,6 +35,7 @@ const float stepSize = 0.02f; // --------------------------- Illumination Constants --------------------------- +// Shading consts const double ambientStrength = 0.3; const double diffuseStrength = 0.8; const double specularStrength = 0.5; @@ -47,6 +48,8 @@ extern __device__ Vec3 d_cameraDir; extern __device__ Vec3 d_cameraUp; extern __device__ Point3 d_lightPos; +// Background color +extern __device__ Color3 d_backgroundColor; // --------------------------- Transfer Function Constants --------------------------- struct ColorStop { diff --git a/src/illumination/Raycaster.cu b/src/illumination/Raycaster.cu index 317b788..763c4f2 100644 --- a/src/illumination/Raycaster.cu +++ b/src/illumination/Raycaster.cu @@ -72,9 +72,9 @@ __global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer) { if (tNear > tFar) { // No intersection -> Set to brackground color (multiply by SAMPLES_PER_PIXEL because we divide by it later) - accumR = 0.1f * (float)SAMPLES_PER_PIXEL; - accumG = 0.1f * (float)SAMPLES_PER_PIXEL; - accumB = 0.1f * (float)SAMPLES_PER_PIXEL; + accumR = d_backgroundColor.x * (float)SAMPLES_PER_PIXEL; + accumG = d_backgroundColor.y * (float)SAMPLES_PER_PIXEL; + accumB = d_backgroundColor.z * (float)SAMPLES_PER_PIXEL; accumA = 1.0f * (float)SAMPLES_PER_PIXEL; } else { if (tNear < 0.0f) tNear = 0.0f; @@ -119,11 +119,11 @@ __global__ void raycastKernel(float* volumeData, FrameBuffer framebuffer) { accumB += colorB; accumA += alphaAccum; - // Blend with background (for transparency) // TODO: Put background colour in a constant + // Blend with background (for transparency) float leftover = 1.0 - alphaAccum; - accumR = accumR + leftover * 0.1f; - accumG = accumG + leftover * 0.1f; - accumB = accumB + leftover * 0.1f; + accumR = accumR + leftover * d_backgroundColor.x; + accumG = accumG + leftover * d_backgroundColor.y; + accumB = accumB + leftover * d_backgroundColor.z; } }