-4

(x0, y0, z0) から始まり、画面上のピクセルで終わる光線があります。さらに、A x B ピクセルの 1 つの画面があります。

(i,j) ピクセルの始点から終点までの光線を生成するにはどうすればよいですか?

式は知っていますが、 c++ で実装できませんでした。手伝ってくれてありがとう

4

1 に答える 1

5

情報が不十分です。

次のことを知っておく必要があります。

  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);
}
于 2012-10-15T09:40:31.037 に答える