Skip to content

Commit

Permalink
add clip plane support
Browse files Browse the repository at this point in the history
  • Loading branch information
孙万捷 authored and 孙万捷 committed Jul 15, 2016
1 parent f1d585c commit 2d19a05
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 12 deletions.
2 changes: 1 addition & 1 deletion core/bsdf/lambert.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ __inline__ __device__ float lambert_brdf_f(const glm::vec3& wi, const glm::vec3&
__inline__ __device__ void lambert_brdf_sample_f(const glm::vec3& wo, const glm::vec3& normal, glm::vec3* wi, float* pdf, curandState& rng)
{
*wi = cosine_weightd_sample_hemisphere(rng, normal);
*pdf = glm::dot(*wi, normal) / float(M_PI);
*pdf = fabsf(glm::dot(*wi, normal)) / float(M_PI);
}

#endif //SUNVOLUMERENDER_LAMBERT_H
6 changes: 3 additions & 3 deletions core/bsdf/microfacet.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ __inline__ __device__ float microfacet_brdf_f(const glm::vec3& wi, const glm::ve

//else
auto wh = glm::normalize(wi + wo);
auto fresnelTerm = schlick_fresnel(1.f, ior, fmaxf(0.f, glm::dot(wh, wo)));
auto fresnelTerm = schlick_fresnel(1.f, ior, fabsf(glm::dot(wh, wo)));
auto geometryTerm = geometry_cook_torrance(wi, wo, normal, wh);
#ifdef DISTRIBUTION_BECKMANN
auto D = beckmann_distribution(normal, wh, alpha);
Expand Down Expand Up @@ -104,9 +104,9 @@ __inline__ __device__ void microfacet_brdf_sample_f(const glm::vec3& wo, const g
*wi = glm::reflect(-wo, wh);

#ifdef DISTRIBUTION_BECKMANN
*pdf = beckmann_distribution(normal, wh, alpha) / (4.f * glm::dot(wo, wh));
*pdf = beckmann_distribution(normal, wh, alpha) / (4.f * fabsf(glm::dot(wo, wh)));
#else
*pdf = GGX_distribution(normal, wh, alpha) / (4.f * glm::dot(wo, wh));
*pdf = GGX_distribution(normal, wh, alpha) / (4.f * fabsf(glm::dot(wo, wh)));
#endif
}

Expand Down
16 changes: 15 additions & 1 deletion core/cuda_volume.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ class cudaVolume
this->tex = tex;
}

__host__ __device__ void SetClipPlane(const glm::vec2& x_clip, const glm::vec2& y_clip, const glm::vec2& z_clip)
{
this->x_clip = x_clip;
this->y_clip = y_clip;
this->z_clip = z_clip;
}

__host__ __device__ void SetXClipPlane(const glm::vec2& clip) {x_clip = clip;}
__host__ __device__ void SetYClipPlane(const glm::vec2& clip) {y_clip = clip;}
__host__ __device__ void SetZClipPlane(const glm::vec2& clip) {z_clip = clip;}

__device__ float operator ()(const glm::vec3& pointInWorld) const
{
return GetIntensity(pointInWorld);
Expand All @@ -35,7 +46,7 @@ class cudaVolume

__device__ bool Intersect(const cudaRay& ray, float* tNear, float* tFar) const
{
return bbox.Intersect(ray, tNear, tFar);
return bbox.Intersect(ray, tNear, tFar, x_clip, y_clip, z_clip);
}

__device__ glm::vec3 Gradient_CentralDiff(const glm::vec3& pointInWorld) const
Expand Down Expand Up @@ -92,6 +103,9 @@ class cudaVolume
cudaTextureObject_t tex;
glm::vec3 spacing;
glm::vec3 invSpacing;
glm::vec2 x_clip;
glm::vec2 y_clip;
glm::vec2 z_clip;
};

struct VolumeSample
Expand Down
11 changes: 8 additions & 3 deletions core/geometry/cuda_bbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ class cudaBBox
invSize = 1.f / (vmax - vmin);
}

__device__ bool Intersect(const cudaRay& ray, float* tNear, float* tFar) const
__device__ bool Intersect(const cudaRay& ray, float* tNear, float* tFar, const glm::vec2& x_clip, const glm::vec2& y_clip, const glm::vec2& z_clip) const
{
auto invDir = 1.f / ray.dir;
auto tbot = invDir * (vmin - ray.orig);
auto ttop = invDir * (vmax - ray.orig);

// compute visible box range after clip
auto clip_vmin = vmin * glm::vec3(-x_clip[0], -y_clip[0], -z_clip[0]);
auto clip_vmax = vmax * glm::vec3(x_clip[1], y_clip[1], z_clip[1]);

auto tbot = invDir * (clip_vmin - ray.orig);
auto ttop = invDir * (clip_vmax - ray.orig);

auto tmin = glm::min(tbot, ttop);
auto tmax = glm::max(tbot, ttop);
Expand Down
1 change: 1 addition & 0 deletions gui/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void Canvas::LoadVolume(std::string filename)
{
volumeReader.Read(filename);
volumeReader.CreateDeviceVolume(&deviceVolume);
deviceVolume.SetClipPlane(glm::vec2(-1.f, 1.f), glm::vec2(-1.f, 1.f), glm::vec2(-1.f, 1.f));
setup_volume(deviceVolume);

ZoomToExtent();
Expand Down
22 changes: 22 additions & 0 deletions gui/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@ class Canvas : public QGLWidget
UpdateCamera();
}

// clip plane
void SetXClipPlane(double min, double max)
{
deviceVolume.SetXClipPlane(glm::vec2(float(min), float(max)));
setup_volume(deviceVolume);
ReStartRender();
}

void SetYClipPlane(double min, double max)
{
deviceVolume.SetYClipPlane(glm::vec2(float(min), float(max)));
setup_volume(deviceVolume);
ReStartRender();
}

void SetZClipPlane(double min, double max)
{
deviceVolume.SetZClipPlane(glm::vec2(float(min), float(max)));
setup_volume(deviceVolume);
ReStartRender();
}

protected:
//opengl
void initializeGL();
Expand Down
23 changes: 23 additions & 0 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ MainWindow::MainWindow(QWidget *parent) :
ConfigureLight();
ConfigureCamera();
ConfigureRenderMode();
ConfigureClipPlane();

// initialize transferfunction on device
canvas->SetTransferFunction(this->tf->GetCompositeTFTextureObject(), 0.5f);
Expand Down Expand Up @@ -133,6 +134,13 @@ void MainWindow::ConfigureCamera()
connect(ui->SliderWidget_apeture, SIGNAL(valueChanged(double)), this, SLOT(onCameraApetureChanged(double)));
}

void MainWindow::ConfigureClipPlane()
{
connect(ui->DoubleRangeSlider_XClip, SIGNAL(positionsChanged(double, double)), this, SLOT(onXClipChanged(double, double)));
connect(ui->DoubleRangeSlider_YClip, SIGNAL(positionsChanged(double, double)), this, SLOT(onYClipChanged(double, double)));
connect(ui->DoubleRangeSlider_ZClip, SIGNAL(positionsChanged(double, double)), this, SLOT(onZClipChanged(double, double)));
}

void MainWindow::onFileOpen()
{
QString fileName = "";
Expand Down Expand Up @@ -442,3 +450,18 @@ void MainWindow::onRenderModeChanged()
break;
}
}

void MainWindow::onXClipChanged(double min, double max)
{
canvas->SetXClipPlane(min, max);
}

void MainWindow::onYClipChanged(double min, double max)
{
canvas->SetYClipPlane(min, max);
}

void MainWindow::onZClipChanged(double min, double max)
{
canvas->SetZClipPlane(min, max);
}
5 changes: 5 additions & 0 deletions gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MainWindow : public QMainWindow
void ConfigureLight();
void ConfigureCamera();
void ConfigureRenderMode();
void ConfigureClipPlane();

private slots:
void onTransferFunctionChanged();
Expand Down Expand Up @@ -68,6 +69,10 @@ private slots:

void onRenderModeChanged();

void onXClipChanged(double min, double max);
void onYClipChanged(double min, double max);
void onZClipChanged(double min, double max);


private:
Ui::MainWindow *ui;
Expand Down
137 changes: 136 additions & 1 deletion gui/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-139</y>
<y>0</y>
<width>363</width>
<height>689</height>
</rect>
Expand Down Expand Up @@ -714,6 +714,136 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="icon">
<iconset resource="../resources/images.qrc">
<normaloff>:/icons/cut_scissor_scissors_32px_4093_easyicon.net.png</normaloff>:/icons/cut_scissor_scissors_32px_4093_easyicon.net.png</iconset>
</attribute>
<attribute name="title">
<string>Clip Plane</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_11">
<item>
<widget class="QScrollArea" name="scrollArea_4">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents_4">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>378</width>
<height>550</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_12">
<item>
<widget class="ctkCollapsibleGroupBox" name="CollapsibleGroupBox_7">
<property name="title">
<string>Clip range</string>
</property>
<layout class="QGridLayout" name="gridLayout_6">
<item row="1" column="1">
<widget class="ctkDoubleRangeSlider" name="DoubleRangeSlider_YClip">
<property name="minimum">
<double>-1.000000000000000</double>
</property>
<property name="maximum">
<double>1.000000000000000</double>
</property>
<property name="singleStep">
<double>0.001000000000000</double>
</property>
<property name="minimumValue">
<double>-1.000000000000000</double>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_17">
<property name="text">
<string>X</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="ctkDoubleRangeSlider" name="DoubleRangeSlider_XClip">
<property name="minimum">
<double>-1.000000000000000</double>
</property>
<property name="maximum">
<double>1.000000000000000</double>
</property>
<property name="singleStep">
<double>0.001000000000000</double>
</property>
<property name="minimumValue">
<double>-1.000000000000000</double>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="ctkDoubleRangeSlider" name="DoubleRangeSlider_ZClip">
<property name="minimum">
<double>-1.000000000000000</double>
</property>
<property name="maximum">
<double>1.000000000000000</double>
</property>
<property name="singleStep">
<double>0.001000000000000</double>
</property>
<property name="minimumValue">
<double>-1.000000000000000</double>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_18">
<property name="text">
<string>Y</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_19">
<property name="text">
<string>Z</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -750,6 +880,11 @@
<extends>QPushButton</extends>
<header>ctkColorPickerButton.h</header>
</customwidget>
<customwidget>
<class>ctkDoubleRangeSlider</class>
<extends>QWidget</extends>
<header>ctkDoubleRangeSlider.h</header>
</customwidget>
<customwidget>
<class>ctkPathLineEdit</class>
<extends>QWidget</extends>
Expand Down
11 changes: 8 additions & 3 deletions pathtracer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ __inline__ __device__ glm::vec3 sample_bsdf(const VolumeSample& vs, glm::vec3* w
if(st == SHANDING_TYPE_ISOTROPIC)
{
hg_phase_sample_f(PHASE_FUNC_G, vs.wo, wi, pdf, rng);
return glm::vec3(vs.color_opacity);
return glm::vec3(vs.color_opacity) * hg_phase_f(vs.wo, *wi);
}
else if(st == SHANDING_TYPE_BRDF)
{
Expand Down Expand Up @@ -230,7 +230,7 @@ __global__ void kernel_pathtracer(const RenderParams renderParams, uint32_t hash

if(t < 0.f)
{
L += T * envLight.GetEnvRadiance(ray.dir);
//L += T * envLight.GetEnvRadiance(ray.dir);
break;
}

Expand All @@ -256,7 +256,12 @@ __global__ void kernel_pathtracer(const RenderParams renderParams, uint32_t hash
auto f = sample_bsdf(vs, &wi, &pdf, rng, st);
float cosTerm = fabsf(glm::dot(glm::normalize(vs.gradient), wi));
if(fmaxf(f.x, fmaxf(f.y, f.z)) > 0.f && pdf > 0.f)
T *= f * cosTerm / pdf;
{
if(st == SHANDING_TYPE_ISOTROPIC)
T *= f / pdf;
else
T *= f * cosTerm / pdf;
}

ray.orig = vs.ptInWorld;
ray.dir = wi;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions resources/images.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="icons">
<file>cut_scissor_scissors_32px_4093_easyicon.net.png</file>
<file>Color_picker_64px_1100527_easyicon.net.png</file>
<file>light_bulb_64px_1076410_easyicon.net.png</file>
<file>photo_camera_77.241379310345px_1201192_easyicon.net.png</file>
Expand Down

0 comments on commit 2d19a05

Please sign in to comment.