Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Physical camera implementation #62

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement physical camera
  • Loading branch information
yozhijk committed Jul 14, 2016
commit 336390d1a05de86962c1a8b517df1860af9daf1f
13 changes: 7 additions & 6 deletions App/CL/camera.cl
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ typedef struct _Camera

// Near and far Z
float2 zcap;
// Vertical field of view in radians
float fovy;
// Focal lenght
float focal_length;
// Camera aspect ratio
float aspect;
float focal_length;
float focus_distance;
float aperture;
} Camera;

Expand Down Expand Up @@ -124,7 +124,7 @@ __kernel void PerspectiveCamera_GeneratePaths(
float2 csample = hsample * camera->dim;

// Calculate direction to image plane
myray->d.xyz = normalize(camera->zcap.x * camera->forward + csample.x * camera->right + csample.y * camera->up);
myray->d.xyz = normalize(camera->focal_length * camera->forward + csample.x * camera->right + csample.y * camera->up);
// Origin == camera position + nearz * d
myray->o.xyz = camera->p + camera->zcap.x * myray->d.xyz;
// Max T value = zfar - znear since we moved origin to znear
Expand Down Expand Up @@ -184,6 +184,7 @@ __kernel void PerspectiveCameraDof_GeneratePaths(
Rng rng;
InitRng(randseed + globalid.x * 157 + 10433 * globalid.y, &rng);

//
#ifdef SOBOL
__global SobolSampler* sampler = samplers + globalid.y * imgwidth + globalid.x;

Expand Down Expand Up @@ -221,11 +222,11 @@ __kernel void PerspectiveCameraDof_GeneratePaths(


float2 lsample = camera->aperture * Sample_MapToDisk(sample1);
float2 fpsample = csample * camera->focal_length / camera->zcap.x;
float2 fpsample = csample * camera->focus_distance / camera->focal_length;
float2 cdir = fpsample - lsample;

float3 o = camera->p + lsample.x * camera->right + lsample.y * camera->up;
float3 d = normalize(camera->forward * camera->focal_length + camera->right * cdir.x + camera->up * cdir.y);
float3 d = normalize(camera->forward * camera->focus_distance + camera->right * cdir.x + camera->up * cdir.y);


// Calculate direction to image plane
Expand Down
8 changes: 4 additions & 4 deletions App/CL/payload.cl
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ typedef enum
kVolumeLightV = 12,
kMaterial = 13,
#ifdef MULTISCATTER
kVolumeIndirectU = 24,
kVolumeIndirectV = 25,
kNumPerBounce = 26,
kVolumeIndirectU = 14,
kVolumeIndirectV = 15,
kNumPerBounce = 16,
#else
kNumPerBounce = 24
kNumPerBounce = 14
#endif
} SampleDim;

Expand Down
9 changes: 9 additions & 0 deletions App/CLW/clwscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

namespace Baikal
{
enum class CameraType
{
kDefault,
kPhysical,
kSpherical,
kFisheye
};

struct ClwScene
{
CLWBuffer<FireRays::float3> vertices;
Expand All @@ -28,6 +36,7 @@ namespace Baikal
int numemissive;
int envmapidx;
float envmapmul;
CameraType camera_type;

std::vector<FireRays::Shape*> isect_shapes;
};
Expand Down
4 changes: 3 additions & 1 deletion App/PT/ptrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ namespace Baikal
void PtRenderer::GeneratePrimaryRays(ClwScene const& scene)
{
// Fetch kernel
CLWKernel genkernel = m_render_data->program.GetKernel("PerspectiveCameraDof_GeneratePaths");
std::string kernel_name = (scene.camera_type == CameraType::kDefault) ? "PerspectiveCamera_GeneratePaths" : "PerspectiveCameraDof_GeneratePaths";

CLWKernel genkernel = m_render_data->program.GetKernel(kernel_name);

// Set kernel parameters
genkernel.SetArg(0,scene.camera);
Expand Down
46 changes: 23 additions & 23 deletions App/Scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,25 +603,25 @@ Scene* Scene::LoadFromObj(std::string const& filename, std::string const& basepa
}


auto iter = textures.find("rm.jpg");
if (iter != textures.end())
{
specular.kxmapidx = iter->second;
}
else
{
Texture texture;

// Load texture
LoadTexture(basepath + "/rm.jpg", texture, scene->texturedata_);

// Add texture desc
specular.nsmapidx= (int)scene->textures_.size();
scene->textures_.push_back(texture);

// Save in the map
textures["rm.jpg"] = specular.nsmapidx;
}
//auto iter = textures.find("rm.jpg");
//if (iter != textures.end())
//{
// specular.kxmapidx = iter->second;
//}
//else
//{
// Texture texture;

// // Load texture
// LoadTexture(basepath + "/rm.jpg", texture, scene->texturedata_);

// // Add texture desc
// specular.nsmapidx= (int)scene->textures_.size();
// scene->textures_.push_back(texture);

// // Save in the map
// textures["rm.jpg"] = specular.nsmapidx;
//}

scene->materials_.push_back(specular);
scene->material_names_.push_back(objmaterials[i].name);
Expand Down Expand Up @@ -729,7 +729,7 @@ Scene* Scene::LoadFromObj(std::string const& filename, std::string const& basepa
else if (objmaterials[i].name == "light" || objmaterials[i].name == "Emit" || objmaterials[i].name == "Light3" || objmaterials[i].name == "dayLight_portal")
{
Material emissive;
emissive.kx = 12.f * float3(0.8f, 0.8f, 0.8f);
emissive.kx = 5.f * float3(0.8f, 0.8f, 0.8f);
emissive.type = kEmissive;

if (!objmaterials[i].diffuse_texname.empty())
Expand Down Expand Up @@ -761,10 +761,10 @@ Scene* Scene::LoadFromObj(std::string const& filename, std::string const& basepa
matmap[i] = scene->materials_.size() - 1;
continue;
}
else if (objmaterials[i].name == "HeadLightAngelEye")
else if (objmaterials[i].name == "HeadLightAngelEye1")
{
Material emissive;
emissive.kx = 5.f * float3(0.53f, 0.7f, 0.95f);
emissive.kx = 50.f * float3(0.53f, 0.7f, 0.95f);
emissive.type = kEmissive;

if (!objmaterials[i].diffuse_texname.empty())
Expand Down Expand Up @@ -796,7 +796,7 @@ Scene* Scene::LoadFromObj(std::string const& filename, std::string const& basepa
matmap[i] = scene->materials_.size() - 1;
continue;
}
else if (objmaterials[i].name == "HeadLightGlass")
else if (objmaterials[i].name == "HeadLightGlass1")
{
Material emissive;
emissive.kx = 50.f * float3(0.64f, 0.723f, 0.8f);
Expand Down
3 changes: 3 additions & 0 deletions App/Scene/scene_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ namespace Baikal

void SceneTracker::UpdateCamera(Scene const& scene, ClwScene& out) const
{
// Update camere type
out.camera_type = scene.camera_->GetAperture() > 0.f ? CameraType::kPhysical : CameraType::kDefault;

// Update camera data
m_context.WriteBuffer(0, out.camera, scene.camera_.get(), 1);
}
Expand Down
48 changes: 34 additions & 14 deletions App/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,18 @@ int g_samplecount = 0;
float g_ao_radius = 1.f;
float g_envmapmul = 1.f;
float g_cspeed = 100.25f;
float3 g_camera_pos = float3(0, 1, 2);
float3 g_camera_at = float3(0, 1, 0);

float3 g_camera_pos = float3(0.f, 1.f, 4.f);
float3 g_camera_at = float3(0.f, 1.f, 0.f);
float3 g_camera_up = float3(0.f, 1.f, 0.f);

float2 g_camera_sensor_size = float2(0.036f, 0.024f); // default full frame sensor 36x24 mm
float2 g_camera_zcap = float2(0.0f, 100000.f);
float g_camera_focal_length = 0.035f; // 35mm lens
float g_camera_focus_distance = 0.f;
float g_camera_aperture = 0.f;


bool g_recording_enabled = false;
int g_frame_count = 0;
bool g_benchmark = false;
Expand Down Expand Up @@ -136,12 +146,6 @@ int g_primary = -1;
std::unique_ptr<Baikal::Scene> g_scene;


#define CAMERA_POSITION float3(0, 0.3f, 0.f)
#define CAMERA_AT float3(1.f, 0.3f, 0.f)
#define CAMERA_UP float3(0, 1, 0)
#define CAMERA_ZCAP float2 (0.00001f, 100000.f)
#define CAMERA_FOVY ((float)M_PI_4 * 1.5f)

static bool g_is_left_pressed = false;
static bool g_is_right_pressed = false;
static bool g_is_fwd_pressed = false;
Expand Down Expand Up @@ -322,13 +326,20 @@ void InitData()
g_scene->camera_.reset(new PerspectiveCamera(
g_camera_pos
, g_camera_at
, CAMERA_UP
, CAMERA_ZCAP
, CAMERA_FOVY
, (float)g_window_width / g_window_height
));
, g_camera_up));

g_scene->SetEnvironment("../Resources/Textures/studio015.hdr", "", g_envmapmul);
g_scene->camera_->SetSensorSize(g_camera_sensor_size);
g_scene->camera_->SetDepthRange(g_camera_zcap);
g_scene->camera_->SetFocalLength(g_camera_focal_length);
g_scene->camera_->SetFocusDistance(g_camera_focus_distance);
g_scene->camera_->SetAperture(g_camera_aperture);

std::cout << "Camera type: " << (g_scene->camera_->GetAperture() > 0.f ? "Physical" : "Pinhole") << "\n";
std::cout << "Lens focal length: " << g_scene->camera_->GetFocalLength() * 1000.f << "mm\n";
std::cout << "Lens focus distance: " << g_scene->camera_->GetFocusDistance() << "m\n";
std::cout << "F-Stop: " << 1.f / (g_scene->camera_->GetAperture() * 10.f) << "\n";

//g_scene->SetEnvironment("../Resources/Textures/studio015.hdr", "", g_envmapmul);

#pragma omp parallel for
for (int i = 0; i < g_cfgs.size(); ++i)
Expand Down Expand Up @@ -754,6 +765,15 @@ int main(int argc, char * argv[])
char* numsamples = GetCmdOption(argv, argv + argc, "-ns");
g_num_samples = numsamples ? atoi(numsamples) : g_num_samples;

char* camera_aperture = GetCmdOption(argv, argv + argc, "-a");
g_camera_aperture = camera_aperture ? atof(camera_aperture) : g_camera_aperture;

char* camera_dist = GetCmdOption(argv, argv + argc, "-fd");
g_camera_focus_distance = camera_dist ? atof(camera_dist) : g_camera_focus_distance;

char* camera_focal_length = GetCmdOption(argv, argv + argc, "-fl");
g_camera_focal_length = camera_focal_length ? atof(camera_focal_length) : g_camera_focal_length;

char* interop = GetCmdOption(argv, argv + argc, "-interop");
g_interop = interop ? (atoi(interop) > 0) : g_interop;

Expand Down
Loading