Skip to content

Commit

Permalink
add recasting
Browse files Browse the repository at this point in the history
  • Loading branch information
孙万捷 authored and 孙万捷 committed Jul 6, 2016
1 parent 09ddba8 commit 60474fc
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 19 deletions.
5 changes: 5 additions & 0 deletions core/VolumeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,9 @@ float VolumeReader::GetBoundingSphereRadius()
{
auto size = GetVolumeSize();
return glm::length(size) * 0.5f;
}

float VolumeReader::GetElementBoundingSphereRadius() const
{
return glm::length(spacing) * 0.5f;
}
1 change: 1 addition & 0 deletions core/VolumeReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class VolumeReader
void CreateDeviceVolume(cudaVolume* volume);
glm::vec3 GetVolumeSize();
float GetBoundingSphereRadius();
float GetElementBoundingSphereRadius() const;

private:
void ClearHost();
Expand Down
2 changes: 0 additions & 2 deletions core/bsdf/microfacet.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ __inline__ __device__ glm::vec3 sample_GGX(const glm::vec3& normal, float alpha,
{
cudaONB onb(normal);
float phi = 2.f * float(M_PI) * curand_uniform(&rng);
float sinPhi = sinf(phi);
float cosPhi = cosf(phi);

float u = curand_uniform(&rng);
float tanTheta = alpha * sqrtf(u) / sqrtf(1.f - u);
Expand Down
12 changes: 12 additions & 0 deletions core/cuda_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ class cudaCamera
ray->orig = pos + apetureSample.x * u + apetureSample.y * v;
ray->dir = glm::normalize((nx - apetureSample.x) * u + (ny - apetureSample.y) * v - focalLength * w);
}

__device__ void GenerateRay(unsigned int x, unsigned int y, cudaRay* ray) const
{
float nx = 2.f * ((x + 0.5f) / (imageW - 1.f)) - 1.f;
float ny = 2.f * ((y + 0.5f) / (imageH - 1.f)) - 1.f;

nx = nx * aspectRatio * tanFovxOverTwo;
ny = ny * tanFovxOverTwo;

ray->orig = pos;
ray->dir = normalize(nx * u + ny * v - w);
}
#endif

public:
Expand Down
31 changes: 28 additions & 3 deletions gui/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Canvas::Canvas(const QGLFormat &format, QWidget *parent) : QGLWidget(format, par
{
// lights
lights.SetEnvironmentLight("LA_Downtown_Helipad_GoldenHour_Env.hdr");
lights.SetEnvironmentLightIntensity(1.f);
lights.SetEnvironmentLightIntensity(0.f);
setup_env_lights(lights.environmentLight);

// render params
Expand All @@ -33,7 +33,6 @@ void Canvas::LoadVolume(std::string filename)
UpdateCamera();

ready = true;
StartTimer();
}

void Canvas::initializeGL()
Expand Down Expand Up @@ -66,7 +65,31 @@ void Canvas::paintGL()
checkCudaErrors(cudaGraphicsMapResources(1, &resource, 0));
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void**)&img, &size, resource));

rendering(img, renderParams);
if(renderMode == RENDER_MODE_RAYCASTING)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glBegin(GL_QUADS);
glColor4f(0.4745f, 0.9294f, 0.8901f, 1.f);
glVertex2f(1.f, 1.f);

glColor4f(0.4745f, 0.9294f, 0.8901f, 1.f);
glVertex2f(-1.f, 1.f);

glColor4f(0.9490f, 0.9647f, 0.9803f, 1.f);
glVertex2f(-1.f, -1.f);

glColor4f(0.9490f, 0.9647f, 0.9803f, 1.f);
glVertex2f(1.f, -1.f);
glEnd();

render_raycasting(img, volumeReader.GetElementBoundingSphereRadius());
}
else
{
render_pathtracer(img, renderParams);
}
checkCudaErrors(cudaDeviceSynchronize());

checkCudaErrors(cudaGraphicsUnmapResources(1, &resource, 0));
Expand All @@ -75,6 +98,8 @@ void Canvas::paintGL()
glDrawPixels(WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

glDisable(GL_BLEND);

renderParams.frameNo++;
}

Expand Down
29 changes: 27 additions & 2 deletions gui/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@

#include "common.h"
#include "utils/helper_cuda.h"
#include "core/pathtracer.h"
#include "pathtracer.h"
#include "raycasting.h"
#include "core/cuda_transfer_function.h"
#include "core/cuda_volume.h"
#include "core/render_parameters.h"

enum RenderMode {RENDER_MODE_PATHTRACER, RENDER_MODE_RAYCASTING};

class Canvas : public QGLWidget
{
Q_OBJECT
Expand All @@ -39,17 +42,37 @@ class Canvas : public QGLWidget
setup_transferfunction(transferFunction);
ReStartRender();
};
void ReStartRender() {renderParams.frameNo = 0;}
void LoadVolume(std::string filename);
void StartTimer() {timerId = this->startTimer(0);}
void KillTimer() {this->killTimer(timerId);}

void ReStartRender()
{
updateGL();
renderParams.frameNo = 0;
}

void SetScatterTimes(double val)
{
renderParams.traceDepth = val;
ReStartRender();
}

void SetRenderMode(RenderMode mode)
{
if(mode == RENDER_MODE_PATHTRACER)
{
StartTimer();
}
else if(mode == RENDER_MODE_RAYCASTING)
{
KillTimer();
}

renderMode = mode;
ReStartRender();
}

// lights
void SetEnvLightBackground(const glm::vec3& color)
{
Expand Down Expand Up @@ -164,6 +187,8 @@ class Canvas : public QGLWidget
cudaCamera camera;
cudaVolume deviceVolume;
cudaTransferFunction transferFunction;

RenderMode renderMode = RENDER_MODE_RAYCASTING;
};


Expand Down
27 changes: 27 additions & 0 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ MainWindow::MainWindow(QWidget *parent) :
ConfigureActions();
ConfigureLight();
ConfigureCamera();
ConfigureRenderMode();

// initialize transferfunction on device
canvas->SetTransferFunction(this->tf->GetCompositeTFTextureObject(), 0.5f);
Expand All @@ -31,6 +32,16 @@ MainWindow::~MainWindow()
exit(0);
}

void MainWindow::ConfigureRenderMode()
{
renderModeGroup = new QButtonGroup(this);
renderModeGroup->addButton(ui->radioButton_Pathtracer, 0);
renderModeGroup->addButton(ui->radioButton_Raycasting, 1);

connect(ui->radioButton_Pathtracer, SIGNAL(clicked()), this, SLOT(onRenderModeChanged()));
connect(ui->radioButton_Raycasting, SIGNAL(clicked()), this, SLOT(onRenderModeChanged()));
}

void MainWindow::ConfigureTransferFunction()
{
vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunc = vtkSmartPointer<vtkPiecewiseFunction>::New();
Expand Down Expand Up @@ -415,3 +426,19 @@ void MainWindow::onScatterTimesChanged(double val)
{
canvas->SetScatterTimes(val);
}

void MainWindow::onRenderModeChanged()
{
auto id = renderModeGroup->checkedId();
switch(id)
{
case 0:
canvas->SetRenderMode(RENDER_MODE_PATHTRACER);
break;
case 1:
canvas->SetRenderMode(RENDER_MODE_RAYCASTING);
break;
default:
break;
}
}
5 changes: 5 additions & 0 deletions gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QMainWindow>
#include <QLabel>
#include <QButtonGroup>

#include <vtkVolumeProperty.h>

Expand Down Expand Up @@ -36,6 +37,7 @@ class MainWindow : public QMainWindow
void ConfigureActions();
void ConfigureLight();
void ConfigureCamera();
void ConfigureRenderMode();

private slots:
void onTransferFunctionChanged();
Expand Down Expand Up @@ -64,12 +66,15 @@ private slots:

void onFileOpen();

void onRenderModeChanged();


private:
Ui::MainWindow *ui;

TransferFunction* tf;
Canvas* canvas;
QButtonGroup* renderModeGroup;
};

#endif // MAINWINDOW_H
34 changes: 30 additions & 4 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>-78</y>
<y>0</y>
<width>363</width>
<height>628</height>
<height>694</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
Expand Down Expand Up @@ -147,6 +147,32 @@
</layout>
</widget>
</item>
<item>
<widget class="ctkCollapsibleGroupBox" name="CollapsibleGroupBox_6">
<property name="title">
<string>Render Mode</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QRadioButton" name="radioButton_Pathtracer">
<property name="text">
<string>Path tracing</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_Raycasting">
<property name="text">
<string>Ray casting</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="ctkCollapsibleGroupBox" name="CollapsibleGroupBox">
<property name="title">
Expand Down Expand Up @@ -240,7 +266,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<y>-139</y>
<width>363</width>
<height>689</height>
</rect>
Expand Down Expand Up @@ -529,7 +555,7 @@
<double>0.010000000000000</double>
</property>
<property name="value">
<double>1.000000000000000</double>
<double>0.000000000000000</double>
</property>
</widget>
</item>
Expand Down
Loading

0 comments on commit 60474fc

Please sign in to comment.