Merge branch 'finding-sany2' into gui

This commit is contained in:
Djairo Hougee 2025-01-20 16:44:24 +01:00
commit 1361cf9417
6 changed files with 78 additions and 39 deletions

View File

@ -2,41 +2,33 @@
#include "cuda_error.h" #include "cuda_error.h"
// ----------------------- Colour mapping ----------------------- // ----------------------- Colour mapping -----------------------
// __device__ __constant__ ColorStop d_stopsPythonLike[5]; __device__ __constant__ ColorStop d_stopsPythonLike[5];
__device__ __constant__ ColorStop d_stopsPythonLike[11]; // __device__ __constant__ ColorStop d_stopsPythonLike[11];
__device__ __constant__ ColorStop d_stopsGrayscale[2]; __device__ __constant__ ColorStop d_stopsGrayscale[2];
__device__ __constant__ ColorStop d_stopsBluePurleRed[3]; __device__ __constant__ ColorStop d_stopsBluePurleRed[3];
// const ColorStop h_stopsPythonLike[] = { const ColorStop h_stopsPythonLike[] = {
// { 0.0f, Color3::init(0.2298057f, 0.29871797f, 0.75368315f) }, // Dark Blue { 0.0f, Color3::init(0.2298057f, 0.29871797f, 0.75368315f) }, // Dark Blue
// { 0.25f, Color3::init(0.23437708f, 0.30554173f, 0.75967953f) }, // Mid Blue { 0.25f, Color3::init(0.23437708f, 0.30554173f, 0.75967953f) }, // Mid Blue
// { 0.5f, Color3::init(0.27582712f, 0.36671692f, 0.81255294f) }, // White { 0.5f, Color3::init(0.27582712f, 0.36671692f, 0.81255294f) }, // White
// { 0.75f, Color3::init(0.79606387f, 0.84869321f, 0.93347147f) }, // Light Orange { 0.75f, Color3::init(0.79606387f, 0.84869321f, 0.93347147f) }, // Light Orange
// { 1.0f, Color3::init(0.70567316f, 0.01555616f, 0.15023281f) } // Red { 1.0f, Color3::init(0.70567316f, 0.01555616f, 0.15023281f) } // Red
// }; };
// const ColorStop h_stopsPythonLike[] = {
// { 0.0f, Color3::init(0.2298057f, 0.29871797f, 0.75368315f) }, // Dark Blue
// { 0.85f, Color3::init(0.23437708f, 0.30554173f, 0.75967953f) }, // Mid Blue
// { 0.90f, Color3::init(0.27582712f, 0.36671692f, 0.81255294f) }, // White
// { 0.95f, Color3::init(0.79606387f, 0.84869321f, 0.93347147f) }, // Light Orange
// { 1.0f, Color3::init(0.70567316f, 0.01555616f, 0.15023281f) } // Red
// };
// Python "jet" colour scheme // Python "jet" colour scheme
const ColorStop h_stopsPythonLike[] = { // const ColorStop h_stopsPythonLike[] = {
{ 0.00f, Color3::init(0.00000000f, 0.00000000f, 0.50000000f) }, // { 0.00f, Color3::init(0.00000000f, 0.00000000f, 0.50000000f) },
{ 0.82f, Color3::init(0.00000000f, 0.00000000f, 0.94563280f) }, // { 0.82f, Color3::init(0.00000000f, 0.00000000f, 0.94563280f) },
{ 0.84f, Color3::init(0.00000000f, 0.30000000f, 1.00000000f) }, // { 0.84f, Color3::init(0.00000000f, 0.30000000f, 1.00000000f) },
{ 0.86f, Color3::init(0.00000000f, 0.69215686f, 1.00000000f) }, // { 0.86f, Color3::init(0.00000000f, 0.69215686f, 1.00000000f) },
{ 0.88f, Color3::init(0.16129032f, 1.00000000f, 0.80645161f) }, // { 0.88f, Color3::init(0.16129032f, 1.00000000f, 0.80645161f) },
{ 0.90f, Color3::init(0.49019608f, 1.00000000f, 0.47754586f) }, // { 0.90f, Color3::init(0.49019608f, 1.00000000f, 0.47754586f) },
{ 0.92f, Color3::init(0.80645161f, 1.00000000f, 0.16129032f) }, // { 0.92f, Color3::init(0.80645161f, 1.00000000f, 0.16129032f) },
{ 0.94f, Color3::init(1.00000000f, 0.77051561f, 0.00000000f) }, // { 0.94f, Color3::init(1.00000000f, 0.77051561f, 0.00000000f) },
{ 0.96f, Color3::init(1.00000000f, 0.40740741f, 0.00000000f) }, // { 0.96f, Color3::init(1.00000000f, 0.40740741f, 0.00000000f) },
{ 0.98f, Color3::init(0.94563280f, 0.02977487f, 0.00000000f) }, // { 0.98f, Color3::init(0.94563280f, 0.02977487f, 0.00000000f) },
{ 1.00f, Color3::init(0.50000000f, 0.00000000f, 0.00000000f) }, // { 1.00f, Color3::init(0.50000000f, 0.00000000f, 0.00000000f) },
}; // };
const ColorStop h_stopsGrayscale[] = { const ColorStop h_stopsGrayscale[] = {
{ 0.0f, Color3::init(0.0f, 0.0f, 0.0f) }, // No colour { 0.0f, Color3::init(0.0f, 0.0f, 0.0f) }, // No colour
@ -56,6 +48,8 @@ __device__ Vec3 d_cameraDir;
__device__ Point3 d_lightPos; __device__ Point3 d_lightPos;
__device__ Color3 d_backgroundColor; __device__ Color3 d_backgroundColor;
__device__ Vec3 d_cameraUp; __device__ Vec3 d_cameraUp;
__device__ double d_specularStrength;
__device__ int d_shininess;
Vec3 h_cameraUp = Vec3::init(0.0, 1.0, 0.0).normalize(); Vec3 h_cameraUp = Vec3::init(0.0, 1.0, 0.0).normalize();
@ -84,6 +78,8 @@ __device__ int d_tfComboSelectedColor;
__device__ float d_opacityConst; __device__ float d_opacityConst;
__device__ bool d_showSilhouettes; __device__ bool d_showSilhouettes;
__device__ float d_silhouettesThreshold; __device__ float d_silhouettesThreshold;
__device__ float d_levoyFocus;
__device__ float d_levoyWidth;
// ----------------------- Raycasting ----------------------- // ----------------------- Raycasting -----------------------
__device__ int d_samplesPerPixel; __device__ int d_samplesPerPixel;

View File

@ -41,8 +41,8 @@ const float stepSize = 0.02f;
// Shading consts // Shading consts
const double ambientStrength = 0.3; const double ambientStrength = 0.3;
const double diffuseStrength = 0.8; const double diffuseStrength = 0.8;
const double specularStrength = 0.5; extern __device__ double d_specularStrength; // = 0.5;
const int shininess = 32; extern __device__ int d_shininess; // = 32;
const float fov = 60.0f * (M_PI / 180.0f); const float fov = 60.0f * (M_PI / 180.0f);
// Camera and Light // Camera and Light
@ -78,11 +78,14 @@ extern __device__ int d_samplesPerPixel;
// Silhouettes // Silhouettes
extern __device__ bool d_showSilhouettes; extern __device__ bool d_showSilhouettes;
extern __device__ float d_silhouettesThreshold; extern __device__ float d_silhouettesThreshold;
// controlling levoy opacity function
extern __device__ float d_levoyFocus;
extern __device__ float d_levoyWidth;
const int lenStopsPythonLike = 11; const int lenStopsPythonLike = 5;
const int lenStopsGrayscale = 2; const int lenStopsGrayscale = 2;
const int lenStopsBluePurpleRed = 3; const int lenStopsBluePurpleRed = 3;
extern __constant__ ColorStop d_stopsPythonLike[11]; extern __constant__ ColorStop d_stopsPythonLike[5];
extern __constant__ ColorStop d_stopsGrayscale[2]; extern __constant__ ColorStop d_stopsGrayscale[2];
extern __constant__ ColorStop d_stopsBluePurleRed[3]; extern __constant__ ColorStop d_stopsBluePurleRed[3];

View File

@ -98,18 +98,25 @@ void Widget::tick(double fps) {
ImGui::Begin("Transfer Function Controls"); ImGui::Begin("Transfer Function Controls");
ImGui::DragFloat("Specular Strength", &this->specularStrength, 0.01f, 0.0f, 1.0f, "%.2f");
ImGui::DragInt("Shininess", &this->shininess, 1, 1, 64, "%d", ImGuiSliderFlags_AlwaysClamp);
ImGui::DragInt("Grad. exp. (log [1e-10, 1])", &this->opacityK, 1, 0, 100, "%d%%", ImGuiSliderFlags_AlwaysClamp); ImGui::DragInt("Grad. exp. (log [1e-10, 1])", &this->opacityK, 1, 0, 100, "%d%%", ImGuiSliderFlags_AlwaysClamp);
ImGui::DragFloat("Sig. shift", &this->sigmoidShift, 0.01f, 0.0f, 1.0f, "%.2f"); ImGui::DragFloat("Sig. shift", &this->sigmoidShift, 0.01f, 0.0f, 1.0f, "%.2f");
ImGui::InputFloat("Sig. sxp", &this->sigmoidExp, 10.0f, 100.0f, "%.0f"); ImGui::InputFloat("Sig. sxp", &this->sigmoidExp, 10.0f, 100.0f, "%.0f");
ImGui::DragFloat("Alpha accum. limit", &this->alphaAcumLimit, 0.01f, 0.0f, 1.0f, "%.2f"); ImGui::DragFloat("Alpha accum. limit", &this->alphaAcumLimit, 0.01f, 0.0f, 1.0f, "%.2f");
ImGui::DragInt("Opacity const. (log [1e-5, 1])", &this->opacityConst, 1, 0, 100, "%d%%", ImGuiSliderFlags_AlwaysClamp); ImGui::DragInt("Opacity const. (log [1e-5, 1])", &this->opacityConst, 1, 0, 100, "%d%%", ImGuiSliderFlags_AlwaysClamp);
ImGui::DragFloat("Levoy Width", &this->levoyWidth, 0.01f, 0.0f, 100.0f, "%.2f");
// ImGui::DragFloat("Levoy Focus", &this->levoyFocus, 0.01f, 250.0f, 350.0f, "%.2f");
ImGui::DragFloat("Levoy Focus", &this->levoyFocus, 0.01f, 0.0f, 1.0f, "%.2f");
// the items[] contains the entries for the combobox. The selected index is stored as an int on this->tfComboSelected // the items[] contains the entries for the combobox. The selected index is stored as an int on this->tfComboSelected
// the default entry is set in the constructor, so if you want that to be a specific entry just change it // the default entry is set in the constructor, so if you want that to be a specific entry just change it
// whatever value is selected here is available on the gpu as d_tfComboSelected. // whatever value is selected here is available on the gpu as d_tfComboSelected.
const char* items[] = {"Opacity - gradient", "Opacity - sigmoid", "Opacity - constant", "..."}; const char* items[] = {"Opacity - gradient", "Opacity - sigmoid", "Opacity - constant", "Opacity - levoy"};
if (ImGui::BeginCombo("Transfer function", items[this->tfComboSelected])) { if (ImGui::BeginCombo("Transfer function", items[this->tfComboSelected])) {
// std::cout << "hello???\n";
for (int n = 0; n < IM_ARRAYSIZE(items); n++) { for (int n = 0; n < IM_ARRAYSIZE(items); n++) {
// std::cout << "letsssssa a asdfa???\n";
const bool is_selected = (this->tfComboSelected == n); const bool is_selected = (this->tfComboSelected == n);
if (ImGui::Selectable(items[n], is_selected)) if (ImGui::Selectable(items[n], is_selected))
this->tfComboSelected = n; this->tfComboSelected = n;
@ -228,11 +235,17 @@ void Widget::copyToDevice() {
cudaMemcpyToSymbol(&d_tfComboSelected, &this->tfComboSelected, sizeof(int)); cudaMemcpyToSymbol(&d_tfComboSelected, &this->tfComboSelected, sizeof(int));
cudaMemcpyToSymbol(&d_showSilhouettes, &this->showSilhouettes, sizeof(bool)); cudaMemcpyToSymbol(&d_showSilhouettes, &this->showSilhouettes, sizeof(bool));
cudaMemcpyToSymbol(&d_silhouettesThreshold, &this->silhouettesThreshold, sizeof(float)); cudaMemcpyToSymbol(&d_silhouettesThreshold, &this->silhouettesThreshold, sizeof(float));
cudaMemcpyToSymbol(&d_levoyFocus, &this->levoyFocus, sizeof(float));
cudaMemcpyToSymbol(&d_levoyWidth, &this->levoyWidth, sizeof(float));
double specularStrengthDouble = this->specularStrength;
cudaMemcpyToSymbol(&d_specularStrength, &specularStrengthDouble, sizeof(double));
cudaMemcpyToSymbol(&d_shininess, &this->shininess, sizeof(int));
this->opacityConstReal = std::pow(10.0f, (-5 + 0.05 * this->opacityConst)); this->opacityConstReal = std::pow(10.0f, (-5 + 0.05 * this->opacityConst));
cudaMemcpyToSymbol(&d_opacityConst, &this->opacityConstReal, sizeof(float)); cudaMemcpyToSymbol(&d_opacityConst, &this->opacityConstReal, sizeof(float));
cudaMemcpyToSymbol(&d_tfComboSelectedColor, &this->tfComboSelectedColor, sizeof(int)); cudaMemcpyToSymbol(&d_tfComboSelectedColor, &this->tfComboSelectedColor, sizeof(int));
} }
Widget::~Widget() { Widget::~Widget() {

View File

@ -46,6 +46,10 @@ private:
float opacityConstReal; float opacityConstReal;
bool showSilhouettes; bool showSilhouettes;
float silhouettesThreshold; float silhouettesThreshold;
float levoyFocus;
float levoyWidth;
float specularStrength;
int shininess;
// miscellaneous // miscellaneous
Point3 lightPos; Point3 lightPos;

View File

@ -6,8 +6,8 @@ __device__ Vec3 phongShading(const Vec3& normal, const Vec3& lightDir, const Vec
Vec3 diffuse = baseColor * (diffuseStrength * diff); Vec3 diffuse = baseColor * (diffuseStrength * diff);
Vec3 reflectDir = (normal * (2.0 * normal.dot(lightDir)) - lightDir).normalize(); Vec3 reflectDir = (normal * (2.0 * normal.dot(lightDir)) - lightDir).normalize();
double spec = pow(fmax(viewDir.dot(reflectDir), 0.0), shininess); double spec = pow(fmax(viewDir.dot(reflectDir), 0.0), d_shininess);
Vec3 specular = Vec3::init(1.0, 1.0, 1.0) * (specularStrength * spec); Vec3 specular = Vec3::init(1.0, 1.0, 1.0) * (d_specularStrength * spec);
return ambient + diffuse + specular; return ambient + diffuse + specular;
}; };

View File

@ -3,10 +3,29 @@
#include <stdio.h> #include <stdio.h>
// [Levoy 1988]
__device__ float levoyOpacity(const Vec3 &grad, float val) {
float r = d_levoyWidth; // width
float fv = d_levoyFocus; // chosen value
float epsilon = 1E-8;
float gradMag = grad.length();
if ((gradMag < epsilon) && ((val - epsilon) <= fv) && (fv <= (val + epsilon))) return 1.0f;
if (gradMag < epsilon) return 0.0f;
float lowBound = val - r*gradMag;
float upperBound = val + r*gradMag;
// float lowBound = fv - r;
// float upperBound = fv + r;
if (!((lowBound <= fv) && (fv <= upperBound))) return 0.0f;
// if ((lowBound <= gradMag) && (gradMag <= upperBound)) return 1.0f;
// return 0.0f;
float alpha = d_opacityConst*(1 - (1/r)*fabs((fv-val)/gradMag));
return alpha;
}
__device__ float opacityFromGradient(const Vec3 &grad, const Vec3& rayDir) { __device__ float opacityFromGradient(const Vec3 &grad, const Vec3& rayDir) {
float gradMag = grad.length(); float gradMag = grad.length();
// float gradMag = 1-fabs(grad.normalize().dot(rayDir));
// float gradMag = grad.length()*(1-fabs(grad.normalize().dot(rayDir))); // Alternative, but not particularly better // float gradMag = grad.length()*(1-fabs(grad.normalize().dot(rayDir))); // Alternative, but not particularly better
float alpha = 1.0f - expf(-d_opacityK * gradMag); float alpha = 1.0f - expf(-d_opacityK * gradMag);
return alpha; return alpha;
@ -83,6 +102,10 @@ __device__ float4 transferFunction(float density, const Vec3& grad, const Point3
case 2: case 2:
alpha = d_opacityConst; alpha = d_opacityConst;
break; break;
case 3:
alpha = levoyOpacity(grad, normDensity);
break;
default: default:
alpha = 1.0f; // This should not be reached anyway alpha = 1.0f; // This should not be reached anyway
@ -111,8 +134,8 @@ __device__ float4 transferFunction(float density, const Vec3& grad, const Point3
result.x = 0.0f; result.x = 0.0f;
result.y = 0.0f; result.y = 0.0f;
result.z = 0.0f; result.z = 0.0f;
// result.w = alpha; result.w = alpha;
result.w = d_opacityConst; // result.w = d_opacityConst;
} }
return result; return result;