-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcuda_camera.h
More file actions
75 lines (62 loc) · 2.17 KB
/
cuda_camera.h
File metadata and controls
75 lines (62 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Created by 孙万捷 on 16/2/6.
//
#ifndef SUNPATHTRACER_CAMERA_H
#define SUNPATHTRACER_CAMERA_H
#include <cuda_runtime.h>
#include <curand_kernel.h>
#include "helper_math.h"
#include "cuda_ray.h"
class cudaCamera
{
public:
__host__ __device__ cudaCamera(){}
__host__ __device__ cudaCamera(const float3& _pos, const float3& _u, const float3& _v, const float3& _w, float fov, unsigned int _imageW,
unsigned int _imageH)
{
Setup(_pos, _u, _v, _w, fov, _imageW, _imageH);
}
__host__ __device__ cudaCamera(const float3& _pos, const float3& target, const float3& up, float fov, unsigned int _imageW, unsigned int _imageH)
{
Setup(_pos, target, up, fov, _imageW, _imageH);
}
__host__ __device__ void Setup(const float3& _pos, const float3& _u, const float3& _v, const float3& _w, float fov, unsigned int _imageW,
unsigned int _imageH)
{
pos = _pos;
u = _u;
v = _v;
w = _w;
imageW = _imageW;
imageH = _imageH;
aspectRatio = (float)imageW / (float)imageH;
tanFovOverTwo = tanf(fov * 0.5f * M_PI / 180.f);
}
__host__ __device__ void Setup(const float3& _pos, const float3& target, const float3& up, float fov, unsigned int _imageW, unsigned int _imageH)
{
pos = _pos;
w = normalize(pos - target);
u = cross(up, w);
v = cross(w, u);
imageW = _imageW;
imageH = _imageH;
aspectRatio = (float)imageW / (float)imageH;
tanFovOverTwo = tanf(fov * 0.5f * M_PI / 180.f);
}
__device__ void GenerateRay(unsigned int x, unsigned int y, curandState* rng, cudaRay* ray) const
{
float nx = 2.f * (((float)x + curand_uniform(rng)) / imageW) - 1.f;
float ny = 2.f * (((float)y + curand_uniform(rng)) / imageH) - 1.f;
nx = nx * aspectRatio * tanFovOverTwo;
ny = ny * tanFovOverTwo;
ray->orig = pos;
ray->dir = normalize(nx * u + ny * v - w);
}
public:
unsigned int imageW, imageH;
float aspectRatio;
float tanFovOverTwo;
float3 pos;
float3 u, v, w;
};
#endif //SUNPATHTRACER_CAMERA_H