Skip to content

Commit

Permalink
add automatic shading type selection
Browse files Browse the repository at this point in the history
  • Loading branch information
孙万捷 authored and 孙万捷 committed Aug 1, 2016
1 parent fd20e37 commit 23b3b1a
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 14 deletions.
16 changes: 9 additions & 7 deletions core/VolumeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ void VolumeReader::Read(std::string filename)
histogram.resize(histDims[0]);
memcpy(&histogram[0], hist->GetOutput()->GetScalarPointer(), sizeof(uint32_t) * histDims[0]);

//auto imageGradientMagnitude = vtkSmartPointer<vtkImageGradientMagnitude>::New();
//imageGradientMagnitude->SetDimensionality(3);
//imageGradientMagnitude->SetInput(imageCast->GetOutput());
//imageGradientMagnitude->Update();
//auto magData = imageGradientMagnitude->GetOutput();
//auto magRange = magData->GetScalarRange();
//maxMagnitude = magRange[2];
auto imageGradientMagnitude = vtkSmartPointer<vtkImageGradientMagnitude>::New();
imageGradientMagnitude->SetDimensionality(3);
imageGradientMagnitude->SetInput(imageCast->GetOutput());
imageGradientMagnitude->Update();
auto magData = imageGradientMagnitude->GetOutput();
auto magRange = magData->GetScalarRange();
maxMagnitude = magRange[1];
std::cout<<maxMagnitude<<std::endl;

//auto ptr = reinterpret_cast<unsigned short*>(data) + 60 * 512 * 512;
//unsigned char* tmp = new unsigned char[512 * 512 * 3];
Expand Down Expand Up @@ -180,6 +181,7 @@ void VolumeReader::CreateDeviceVolume(cudaVolume* volume)
cudaBBox bbox = cudaBBox(vmin, vmax);

volume->Set(bbox, spacing, volumeTex);
volume->SetInvMaxMagnitude(1.f / maxMagnitude);
}

glm::vec3 VolumeReader::GetVolumeSize()
Expand Down
1 change: 1 addition & 0 deletions core/VolumeReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class VolumeReader
glm::vec3 spacing = glm::vec3(glm::uninitialize);
glm::ivec3 dim = glm::ivec3(glm::uninitialize);
char* volumeData = nullptr;
float maxMagnitude = 0.f;

cudaArray* volumeArray = nullptr;
cudaTextureObject_t volumeTex = 0;
Expand Down
10 changes: 10 additions & 0 deletions core/cuda_volume.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ class cudaVolume
return 1.f / bbox.invSize;
}

__host__ __device__ void SetInvMaxMagnitude(float invMag) {invMaxMagnitude = invMag;}

__host__ __device__ float GetInvMaxMagnitude() const {return invMaxMagnitude;}

__host__ __device__ void SetGradientFactor(float g) {gradientFactor = g;}

__host__ __device__ float GetGradientFactor() const {return gradientFactor;}

private:
__device__ glm::vec3 GetNormalizedTexCoord(const glm::vec3& pointInWorld) const
{
Expand Down Expand Up @@ -104,6 +112,8 @@ class cudaVolume
cudaBBox bbox;
cudaTextureObject_t tex;
float densityScale;
float invMaxMagnitude;
float gradientFactor;
glm::vec3 spacing;
glm::vec3 invSpacing;
glm::vec2 x_clip;
Expand Down
2 changes: 2 additions & 0 deletions gui/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Canvas::Canvas(const QGLFormat &format, QWidget *parent) : QGLWidget(format, par
// render params
renderParams.SetupHDRBuffer(WIDTH, HEIGHT);
renderParams.traceDepth = 1;

deviceVolume.SetGradientFactor(0.5f);
}

Canvas::~Canvas()
Expand Down
7 changes: 7 additions & 0 deletions gui/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class Canvas : public QGLWidget
ReStartRender();
}

void SetGradientFactor(double g)
{
deviceVolume.SetGradientFactor(g);
setup_volume(deviceVolume);
ReStartRender();
}

void SetScatterTimes(double val)
{
renderParams.traceDepth = val;
Expand Down
6 changes: 6 additions & 0 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void MainWindow::ConfigureTransferFunction()
connect(tf, SIGNAL(Changed()), this, SLOT(onTransferFunctionChanged()));

connect(ui->SliderWidget_DensityScale, SIGNAL(valueChanged(double)), this, SLOT(onDensityScaleChanged(double)));
connect(ui->SliderWidget_GradientFactor, SIGNAL(valueChanged(double)), this, SLOT(onGradientFactorChanged(double)));
}

void MainWindow::onTransferFunctionChanged()
Expand Down Expand Up @@ -472,3 +473,8 @@ void MainWindow::onDensityScaleChanged(double s)
{
canvas->SetDensityScale(s);
}

void MainWindow::onGradientFactorChanged(double g)
{
canvas->SetGradientFactor(g);
}
1 change: 1 addition & 0 deletions gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private slots:
void onTransferFunctionChanged();
void onDensityScaleChanged(double s);
void onScatterTimesChanged(double val);
void onGradientFactorChanged(double g);

void onEnvLightUOffsetChanged(double u);
void onEnvLightVOffsetChanged(double v);
Expand Down
28 changes: 26 additions & 2 deletions gui/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-144</y>
<y>-180</y>
<width>363</width>
<height>730</height>
<height>766</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
Expand Down Expand Up @@ -203,6 +203,30 @@
</item>
</layout>
</item>
<item>
<layout class="QGridLayout" name="gridLayout_7">
<item row="1" column="1">
<widget class="ctkSliderWidget" name="SliderWidget_GradientFactor">
<property name="singleStep">
<double>0.010000000000000</double>
</property>
<property name="maximum">
<double>1.000000000000000</double>
</property>
<property name="value">
<double>0.500000000000000</double>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_21">
<property name="text">
<string>Gradient factor</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="ctkVTKScalarsToColorsWidget" name="opacityTransferFunc">
<property name="minimumSize">
Expand Down
13 changes: 8 additions & 5 deletions pathtracer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,13 @@ __global__ void kernel_pathtracer(const RenderParams renderParams, uint32_t hash
glm::vec3 wi;
float pdf = 0.f;
ShadingType st;
if(vs.gradientMagnitude < 1e-3)
st = SHANDING_TYPE_ISOTROPIC;
else

auto gradientFactor = volume.GetGradientFactor();
auto Pbrdf = vs.color_opacity.a * (1.f - expf(-25.f * gradientFactor * gradientFactor * gradientFactor * vs.gradientMagnitude * 65535.f * volume.GetInvMaxMagnitude()));
if(curand_uniform(&rng) < Pbrdf)
st = SHANDING_TYPE_BRDF;
else
st = SHANDING_TYPE_ISOTROPIC;

L += T * estimate_direct_light(vs, rng, st);

Expand All @@ -258,9 +261,9 @@ __global__ void kernel_pathtracer(const RenderParams renderParams, uint32_t hash
if(fmaxf(f.x, fmaxf(f.y, f.z)) > 0.f && pdf > 0.f)
{
if(st == SHANDING_TYPE_ISOTROPIC)
T *= f / pdf;
T *= f / (pdf * (1.f - Pbrdf));
else
T *= f * cosTerm / pdf;
T *= f * cosTerm / (pdf * Pbrdf);
}

ray.orig = vs.ptInWorld;
Expand Down

0 comments on commit 23b3b1a

Please sign in to comment.