2012-10-15 28 views
-4

我有一條從(x0,y0,z0)開始並以屏幕像素結束的光線。此外,我有一個A×B像素的屏幕。生成一個向量

如何從(i,j)像素的起點到終點生成光線?

我知道公式,但我無法設法在C++中實現它。感謝您的幫助

+3

什麼公式和你在哪裏失敗了呢?你嘗試了什麼? –

+0

如果您只需要光線跟蹤渲染,請使用現有的光線跟蹤庫(如povray)。如果你爲了好玩而自己做實現,請更具體地說明你的問題。你的意見是什麼?輸出?解釋輸出是如何計算的,以及你遇到什麼問題? – Antoine

回答

4

您沒有足夠的信息。

你需要知道:

  1. 的觀點(即什麼時候會在照相機看)
  2. 來看
  3. 「向上」和「右」矢量定義的字段相機相對於世界座標的方向。

下面是從我自己的光線追蹤一些相關代碼:

camera::camera(const point3& _eye, const point3& _center) : 
    eye(_eye), center(_center) 
{ 
    up.set(0, 1, 0); 
    recalc(); 

    fov(30); 
    m_aspect = 4.0/3; 
} 

camera::camera(const point3& _eye, const point3& _center, const vector3& _up) : 
    eye(_eye), center(_center), up(_up) 
{ 
    recalc(); 

    fov(30); 
    m_aspect = 4.0/3; 
} 

void camera::recalc() 
{ 
    // renormalise the up vector 
    up.normalise(); 

    // calculate unit view direction vector 
    view = vector3(eye, center); 
    view.normalise(); 

    // and the right hand view vector 
    right.cross(view, up); 
    right.normalise(); 

    // and re-base the up vector (may not be normalised) 
    up.cross(right, view); 
} 

void camera::fov(double fovy) 
{ 
    m_fovy = math::deg2rad(fovy)/2.0; 
    m_tanf = tan(m_fovy); 
} 

void camera::aspect(double aspect) 
{ 
    m_aspect = aspect; 
} 

void camera::aspect(int x, int y) 
{ 
    m_aspect = (double)x/y; 
} 

ray camera::cast_ray(double x, double y) const 
{ 
    vector3 dir(view); 
    dir.add_scaled(right, m_tanf * m_aspect * x); 
    dir.add_scaled(up, m_tanf * y); 
    dir.normalise(); 

    return ray(eye, dir, 0, 1.0); 
} 
+0

現在您已經發布了一些示例代碼,您需要向我們展示來自您的raytracer的示例圖像=) – paddy

+0

@paddy https://dl.dropbox.com/u/4713475/raytrace/balls-07.png – Alnitak

+0

尼斯一。這是面板光源嗎?全球照明? – paddy