這裏是我寫的代碼生成自由度。
void generateDOFfromEye(Image& img, const Camera& camera, Scene scene, float focusPoint)
{
float pixelWidth = 1.0f/(float) img.width;
float pixelHeight = 1.0f/(float) img.height;
for (int y = 0; y < img.height; ++y)
{
for (int x = 0; x < img.width; ++x)
{
Color output(0,0,0,0);
img(x, y) = Color(0,0,0,0);
//Center of the current pixel
float px = (x * pixelWidth) - 0.5;
float py = (y * pixelHeight) - 0.5;
Ray cameraSpaceRay = Ray(Vector(0,0,0,1), Vector(px, py, 1.0f, 0.0f));
Ray ray = camera.Transform() * cameraSpaceRay;
int depth = 0;
int focaldistance = 2502;
Color blend(0,0,0,0);
//Stratified Sampling i.e. Random sampling (with 16 samples) inside each pixel to add DOF
for(int i = 0; i < 16; i++)
{
//random values between [-1,1]
float rw = (static_cast<float>(rand() % RAND_MAX)/RAND_MAX) * 2.0f - 1.0f;
float rh = (static_cast<float>(rand() % RAND_MAX)/RAND_MAX) * 2.0f - 1.0f;
// Since eye position is (0,0,0,1) I generate samples around that point with a 3x3 aperture size window.
float dx = ((rw) * 3 * pixelWidth) - 0.5;
float dy = ((rh) * 3 * pixelHeight) - 0.5;
//Now here I compute point P in the scene where I want to focus my scene
Vector P = Vector(0,0,0,1) + focusPoint * ray.Direction();
Vector dir = P - Vector(dx, dy, 0.0f, 1.0f);
ray = Ray(Vector(dx,dy,0.0f,1.0f), dir);
ray = camera.Transform() * ray;
//Calling the phong shader to render the scene
blend += phongShader(scene, ray, depth, output);
}
blend /= 16.0f;
img(x, y) += blend;
}
}
}
現在我在代碼中看不到任何錯誤。但我得到的結果是隻爲focuspoint > 500
價值模糊圖像,如下圖所示:
如果你能告訴什麼是錯的這個代碼,那麼這將是非常有益的:) 謝謝!
這與在我的相機(或眼睛)位置周圍取少量採樣點並不相似。然後從所有這些位置渲染我的場景。通過在場景中的某個地方定義一個焦點,從每個採樣點發送一個光線到圖像平面中每個像素的焦點。 – sinner 2012-04-04 14:03:53
嗯,是的,這實際上是相同的 - 當然,您可以在每個像素的基礎上累積顏色,而不是累積整個場景的渲染。 – IneQuation 2012-04-04 14:10:28
我的意思是: 是不是採取相似: - 我的相機(或眼睛)位置周圍的樣本點很少。 - 然後通過從圖像平面中的每個像素向每個採樣點發送一個光線到焦點,從所有這些位置渲染場景。 對不起,但如果你可以解釋眼睛的位置,圖像平面,焦平面和場景,它會讓我很容易理解。 – sinner 2012-04-04 14:13:44